1 /* 2 Copyright 2023 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package resource 18 19 import ( 20 "k8s.io/apimachinery/pkg/runtime/schema" 21 "k8s.io/klog/v2" 22 ) 23 24 // fallbackQueryParamVerifier encapsulates the primary Verifier that 25 // is invoked, and the secondary/fallback Verifier. 26 type fallbackQueryParamVerifier struct { 27 primary Verifier 28 secondary Verifier 29 } 30 31 var _ Verifier = &fallbackQueryParamVerifier{} 32 33 // NewFallbackQueryParamVerifier returns a new Verifier which will invoke the 34 // initial/primary Verifier. If the primary Verifier is "NotFound", then the 35 // secondary Verifier is invoked as a fallback. 36 func NewFallbackQueryParamVerifier(primary Verifier, secondary Verifier) Verifier { 37 return &fallbackQueryParamVerifier{ 38 primary: primary, 39 secondary: secondary, 40 } 41 } 42 43 // HasSupport returns an error if the passed GVK does not support the 44 // query param (fieldValidation), as determined by the primary and 45 // secondary OpenAPI endpoints. The primary endoint is checked first, 46 // but if there is an error retrieving the OpenAPI V3 document, the 47 // secondary attempts to determine support. If the GVK supports the query param, 48 // nil is returned. 49 func (f *fallbackQueryParamVerifier) HasSupport(gvk schema.GroupVersionKind) error { 50 err := f.primary.HasSupport(gvk) 51 // If an error was returned from the primary OpenAPI endpoint, 52 // we fallback to check the secondary OpenAPI endpoint for 53 // any error *except* "paramUnsupportedError". 54 if err != nil && !IsParamUnsupportedError(err) { 55 klog.V(7).Infof("openapi v3 error...falling back to legacy: %s", err) 56 err = f.secondary.HasSupport(gvk) 57 } 58 return err 59 } 60