|
| 1 | +/* |
| 2 | + * Copyright 2024-2025 KusionStack 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 | + "strings" |
| 22 | + |
| 23 | + "github.com/spf13/pflag" |
| 24 | + "k8s.io/component-base/featuregate" |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + // DefaultMutableFeatureGate is a mutable version of DefaultFeatureGate. |
| 29 | + // Only top-level commands/options setup and the k8s.io/component-base/featuregate/testing package should make use of this. |
| 30 | + // Tests that need to modify feature gates for the duration of their test should use: |
| 31 | + // defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.<FeatureName>, <value>)() |
| 32 | + DefaultMutableFeatureGate featuregate.MutableFeatureGate = NewXSetFeatureGate() |
| 33 | + |
| 34 | + // DefaultFeatureGate is a shared global FeatureGate. |
| 35 | + // Top-level commands/options setup that needs to modify this feature gate should use DefaultMutableFeatureGate. |
| 36 | + DefaultFeatureGate featuregate.FeatureGate = DefaultMutableFeatureGate |
| 37 | +) |
| 38 | + |
| 39 | +var flagName = "xset-feature-gates" |
| 40 | + |
| 41 | +type xsetFeatureGate struct { |
| 42 | + featuregate.MutableFeatureGate |
| 43 | +} |
| 44 | + |
| 45 | +func NewXSetFeatureGate() featuregate.MutableFeatureGate { |
| 46 | + return &xsetFeatureGate{ |
| 47 | + MutableFeatureGate: featuregate.NewFeatureGate(), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (g *xsetFeatureGate) AddFlag(fs *pflag.FlagSet) { |
| 52 | + known := g.KnownFeatures() |
| 53 | + fs.Var(g, flagName, ""+ |
| 54 | + "A set of key=value pairs that describe feature gates for alpha/experimental features. "+ |
| 55 | + "Options are:\n"+strings.Join(known, "\n")) |
| 56 | +} |
| 57 | + |
| 58 | +func (g *xsetFeatureGate) String() string { |
| 59 | + return g.MutableFeatureGate.(fmt.Stringer).String() |
| 60 | +} |
| 61 | + |
| 62 | +func (g *xsetFeatureGate) Type() string { |
| 63 | + return "mapStringBool" |
| 64 | +} |
0 commit comments