From 5fdeaa8e42c75964cbbc8c0036573eef1dcf64ed Mon Sep 17 00:00:00 2001 From: Sean Sullivan Date: Wed, 10 May 2023 19:13:54 +0000 Subject: [PATCH] update for feedback --- .../resource/fallback_query_param_verifier.go | 2 +- .../pkg/resource/query_param_verifier_v3.go | 31 ++++++++----------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/staging/src/k8s.io/cli-runtime/pkg/resource/fallback_query_param_verifier.go b/staging/src/k8s.io/cli-runtime/pkg/resource/fallback_query_param_verifier.go index 0ed088e8338..05418801e8f 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/resource/fallback_query_param_verifier.go +++ b/staging/src/k8s.io/cli-runtime/pkg/resource/fallback_query_param_verifier.go @@ -49,7 +49,7 @@ func NewFallbackQueryParamVerifier(primary Verifier, secondary Verifier) Verifie func (f *fallbackQueryParamVerifier) HasSupport(gvk schema.GroupVersionKind) error { err := f.primary.HasSupport(gvk) // If an error was returned from the primary OpenAPI endpoint, - // we fallback to check the secondary OpenAPI endpoint for an + // we fallback to check the secondary OpenAPI endpoint for // any error *except* "paramUnsupportedError". if err != nil && !IsParamUnsupportedError(err) { klog.V(7).Infof("openapi v3 error...falling back to legacy: %s", err) diff --git a/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier_v3.go b/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier_v3.go index 6df53beb0c4..502c58cf3d3 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier_v3.go +++ b/staging/src/k8s.io/cli-runtime/pkg/resource/query_param_verifier_v3.go @@ -64,15 +64,7 @@ func (v *queryParamVerifierV3) HasSupport(gvk schema.GroupVersionKind) error { } gvSpec, err := v.root.GVSpec(gvk.GroupVersion()) if err == nil { - // Even if there is no error returned fetching OpenAPI V3 document, - // check if the returned document is valid before checking support. - if gvSpec == nil || gvSpec.Paths == nil { - return fmt.Errorf("Invalid OpenAPI V3 document") - } - if supports := supportsQueryParamV3(gvSpec, gvk, v.queryParam); supports { - return nil - } - return NewParamUnsupportedError(gvk, v.queryParam) + return supportsQueryParamV3(gvSpec, gvk, v.queryParam) } if _, isErr := err.(*openapi3.GroupVersionNotFoundError); !isErr { return err @@ -85,9 +77,7 @@ func (v *queryParamVerifierV3) HasSupport(gvk schema.GroupVersionKind) error { // If error retrieving Namespace spec, propagate error. return err } - if supports := supportsQueryParamV3(namespaceSpec, namespaceGVK, v.queryParam); supports { - return nil - } + return supportsQueryParamV3(namespaceSpec, namespaceGVK, v.queryParam) } return NewParamUnsupportedError(gvk, v.queryParam) } @@ -110,9 +100,14 @@ func hasGVKExtensionV3(extensions spec.Extensions, gvk schema.GroupVersionKind) // supportsQueryParam is a method that let's us look in the OpenAPI if the // specific group-version-kind supports the specific query parameter for -// the PATCH end-point. Returns true if the query param is supported by the -// spec for the passed GVK; false otherwise. -func supportsQueryParamV3(doc *spec3.OpenAPI, gvk schema.GroupVersionKind, queryParam VerifiableQueryParam) bool { +// the PATCH end-point. Returns nil if the passed GVK supports the passed +// query parameter; otherwise, a "paramUnsupportedError" is returned (except +// when an invalid document error is returned when an invalid OpenAPI V3 +// is passed in). +func supportsQueryParamV3(doc *spec3.OpenAPI, gvk schema.GroupVersionKind, queryParam VerifiableQueryParam) error { + if doc == nil || doc.Paths == nil { + return fmt.Errorf("Invalid OpenAPI V3 document") + } for _, path := range doc.Paths.Paths { // If operation is not PATCH, then continue. op := path.PathProps.Patch @@ -127,10 +122,10 @@ func supportsQueryParamV3(doc *spec3.OpenAPI, gvk schema.GroupVersionKind, query // for the PATCH operation. for _, param := range op.OperationProps.Parameters { if param.ParameterProps.Name == string(queryParam) { - return true + return nil } } - return false + return NewParamUnsupportedError(gvk, queryParam) } - return false + return NewParamUnsupportedError(gvk, queryParam) }