From e78d05daec30a757b3817bbdc78f5ea0c1c7ea17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20G=C3=BC=C3=A7l=C3=BC?= Date: Fri, 3 Mar 2023 13:04:03 +0300 Subject: [PATCH 1/2] Skip hasSupport check for List type Currently, when user applies a List type resource, there is an unnecessary request to customresourcedefinitions endpoint by validation. The reason of this request is that validation can not find List type in OpenAPI schema and behaves this type as CRD. This PR returns unsupported when List type is applied. In terms of functionality, there is no bug or any issue, because eventually `schemaValidation` detects this List and executes `validateList` function. Only issue is unnecessary CRD enpoint request. --- .../k8s.io/cli-runtime/pkg/resource/query_param_verifier.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier.go b/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier.go index bc0769530fe..d933b89d8b1 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier.go +++ b/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier.go @@ -19,7 +19,6 @@ package resource import ( "errors" "fmt" - openapi_v2 "github.com/google/gnostic/openapiv2" yaml "gopkg.in/yaml.v2" "k8s.io/apimachinery/pkg/runtime/schema" @@ -72,6 +71,10 @@ const ( // HasSupport checks if the given gvk supports the query param configured on v func (v *QueryParamVerifier) HasSupport(gvk schema.GroupVersionKind) error { + if (gvk == schema.GroupVersionKind{Version: "v1", Kind: "List"}) { + return NewParamUnsupportedError(gvk, v.queryParam) + } + oapi, err := v.openAPIGetter.OpenAPISchema() if err != nil { return fmt.Errorf("failed to download openapi: %v", err) From 9e7737d7d4c09f90bab7abf8e646f21f1d4eec6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20G=C3=BC=C3=A7l=C3=BC?= Date: Tue, 7 Mar 2023 08:45:26 +0300 Subject: [PATCH 2/2] Unit tests to assure List type not support fieldvalidation --- .../pkg/resource/query_param_verifier.go | 1 + .../pkg/resource/query_param_verifier_test.go | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier.go b/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier.go index d933b89d8b1..3b08025c3ef 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier.go +++ b/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier.go @@ -19,6 +19,7 @@ package resource import ( "errors" "fmt" + openapi_v2 "github.com/google/gnostic/openapiv2" yaml "gopkg.in/yaml.v2" "k8s.io/apimachinery/pkg/runtime/schema" diff --git a/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier_test.go b/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier_test.go index 8b67e1094b3..b1c1bb620ba 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier_test.go +++ b/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier_test.go @@ -69,6 +69,16 @@ func TestSupportsQueryParam(t *testing.T) { supports: false, queryParam: QueryParamFieldValidation, }, + { + gvk: schema.GroupVersionKind{ + Group: "", + Version: "v1", + Kind: "List", + }, + success: false, + supports: false, + queryParam: QueryParamFieldValidation, + }, } for _, test := range tests { @@ -124,6 +134,11 @@ func TestFieldValidationVerifier(t *testing.T) { if err == nil { t.Fatalf("Random doesn't support fieldValidation, yet no error found") } + + err = fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "List"}) + if err == nil { + t.Fatalf("List does not support fieldValidation, yet no error found") + } } type EmptyOpenAPI struct{} @@ -159,4 +174,9 @@ func TestFieldValidationVerifierNoOpenAPI(t *testing.T) { if err == nil { t.Fatalf("MyCRD doesn't support fieldValidation, yet no error found") } + + err = fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "List"}) + if err == nil { + t.Fatalf("List does not support fieldValidation, yet no error found") + } }