k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/features/client_adapter.go (about) 1 /* 2 Copyright 2024 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 features 18 19 import ( 20 "fmt" 21 22 clientfeatures "k8s.io/client-go/features" 23 "k8s.io/component-base/featuregate" 24 ) 25 26 // clientAdapter adapts a k8s.io/component-base/featuregate.MutableFeatureGate to client-go's 27 // feature Gate and Registry interfaces. The component-base types Feature, FeatureSpec, and 28 // prerelease, and the component-base prerelease constants, are duplicated by parallel types and 29 // constants in client-go. The parallel types exist to allow the feature gate mechanism to be used 30 // for client-go features without introducing a circular dependency between component-base and 31 // client-go. 32 type clientAdapter struct { 33 mfg featuregate.MutableFeatureGate 34 } 35 36 var _ clientfeatures.Gates = &clientAdapter{} 37 38 func (a *clientAdapter) Enabled(name clientfeatures.Feature) bool { 39 return a.mfg.Enabled(featuregate.Feature(name)) 40 } 41 42 var _ clientfeatures.Registry = &clientAdapter{} 43 44 func (a *clientAdapter) Add(in map[clientfeatures.Feature]clientfeatures.FeatureSpec) error { 45 out := map[featuregate.Feature]featuregate.FeatureSpec{} 46 for name, spec := range in { 47 converted := featuregate.FeatureSpec{ 48 Default: spec.Default, 49 LockToDefault: spec.LockToDefault, 50 } 51 switch spec.PreRelease { 52 case clientfeatures.Alpha: 53 converted.PreRelease = featuregate.Alpha 54 case clientfeatures.Beta: 55 converted.PreRelease = featuregate.Beta 56 case clientfeatures.GA: 57 converted.PreRelease = featuregate.GA 58 case clientfeatures.Deprecated: 59 converted.PreRelease = featuregate.Deprecated 60 default: 61 // The default case implies programmer error. The same set of prerelease 62 // constants must exist in both component-base and client-go, and each one 63 // must have a case here. 64 panic(fmt.Sprintf("unrecognized prerelease %q of feature %q", spec.PreRelease, name)) 65 } 66 out[featuregate.Feature(name)] = converted 67 } 68 return a.mfg.Add(out) 69 }