| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package featuregate |
| 16 |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "maps" |
| 20 | "sort" |
| 21 | "strings" |
| 22 | "sync" |
| 23 | "sync/atomic" |
| 24 | ) |
| 25 |
| 26 | // Feature represents a feature gate name |
| 27 | type Feature string |
| 28 |
| 29 | // FeatureStage represents the maturity level of a feature |
| 30 | type FeatureStage string |
| 31 |
| 32 | const ( |
| 33 | // Alpha means the feature is experimental and disabled by default |
| 34 | Alpha FeatureStage = "ALPHA" |
| 35 | // Beta means the feature is more stable but still might change and is disabled by default |
| 36 | Beta FeatureStage = "BETA" |
| 37 | // GA means the feature is generally available and enabled by default |
| 38 | GA FeatureStage = "" |
| 39 | ) |
| 40 |
| 41 | // FeatureSpec describes a feature and its properties |
| 42 | type FeatureSpec struct { |
| 43 | // Default is the default enablement state for the feature |
| 44 | Default bool |
| 45 | // LockToDefault indicates the feature cannot be changed from its default |
| 46 | LockToDefault bool |
| 47 | // Stage indicates the maturity level of the feature |
| 48 | Stage FeatureStage |
| 49 | } |
| 50 |
| 51 | // Define all available features here |
| 52 | var ( |
| 53 | VirtualNet = Feature("VirtualNet") |
| 54 | ) |
| 55 |
| 56 | // defaultFeatures defines default features with their specifications |
| 57 | var defaultFeatures = map[Feature]FeatureSpec{ |
| 58 | // Actual features |
| 59 | VirtualNet: {Default: false, Stage: Alpha}, |
| 60 | } |
| 61 |
| 62 | // FeatureGate indicates whether a given feature is enabled or not |
| 63 | type FeatureGate interface { |
| 64 | // Enabled returns true if the key is enabled |
| 65 | Enabled(key Feature) bool |
| 66 | // KnownFeatures returns a slice of strings describing the known features |
| 67 | KnownFeatures() []string |
| 68 | } |
| 69 |
| 70 | // MutableFeatureGate allows for dynamic feature gate configuration |
| 71 | type MutableFeatureGate interface { |
| 72 | FeatureGate |
| 73 |
| 74 | // SetFromMap sets feature gate values from a map[string]bool |
| 75 | SetFromMap(m map[string]bool) error |
| 76 | // Add adds features to the feature gate |
| 77 | Add(features map[Feature]FeatureSpec) error |
| 78 | // String returns a string representing the feature gate configuration |
| 79 | String() string |
| 80 | } |
| 81 |
| 82 | // featureGate implements the FeatureGate and MutableFeatureGate interfaces |
| 83 | type featureGate struct { |
| 84 | // lock guards writes to known, enabled, and reads/writes of closed |
| 85 | lock sync.Mutex |
| 86 | // known holds a map[Feature]FeatureSpec |
| 87 | known atomic.Value |
| 88 | // enabled holds a map[Feature]bool |
| 89 | enabled atomic.Value |
| 90 | // closed is set to true once the feature gates are considered immutable |
| 91 | closed bool |
| 92 | } |
| 93 |
| 94 | // NewFeatureGate creates a new feature gate with the default features |
| 95 | func NewFeatureGate() MutableFeatureGate { |
| 96 | known := maps.Clone(defaultFeatures) |
| 97 |
| 98 | f := &featureGate{} |
| 99 | f.known.Store(known) |
| 100 | f.enabled.Store(map[Feature]bool{}) |
| 101 | return f |
| 102 | } |
| 103 |
| 104 | // SetFromMap sets feature gate values from a map[string]bool |
| 105 | func (f *featureGate) SetFromMap(m map[string]bool) error { |
| 106 | f.lock.Lock() |
| 107 | defer f.lock.Unlock() |
| 108 |
| 109 | // Copy existing state |
| 110 | known := maps.Clone(f.known.Load().(map[Feature]FeatureSpec)) |
| 111 | enabled := maps.Clone(f.enabled.Load().(map[Feature]bool)) |
| 112 |
| 113 | // Apply the new settings |
| 114 | for k, v := range m { |
| 115 | k := Feature(k) |
| 116 | featureSpec, ok := known[k] |
| 117 | if !ok { |
| 118 | return fmt.Errorf("unrecognized feature gate: %s", k) |
| 119 | } |
| 120 | if featureSpec.LockToDefault && featureSpec.Default != v { |
| 121 | return fmt.Errorf("cannot set feature gate %v to %v, feature is locked to %v", k, v, featureSpec.Default) |
| 122 | } |
| 123 | enabled[k] = v |
| 124 | } |
| 125 |
| 126 | // Persist the changes |
| 127 | f.known.Store(known) |
| 128 | f.enabled.Store(enabled) |
| 129 | return nil |
| 130 | } |
| 131 |
| 132 | // Add adds features to the feature gate |
| 133 | func (f *featureGate) Add(features map[Feature]FeatureSpec) error { |
| 134 | f.lock.Lock() |
| 135 | defer f.lock.Unlock() |
| 136 |
| 137 | if f.closed { |
| 138 | return fmt.Errorf("cannot add feature gates after the feature gate is closed") |
| 139 | } |
| 140 |
| 141 | // Copy existing state |
| 142 | known := maps.Clone(f.known.Load().(map[Feature]FeatureSpec)) |
| 143 |
| 144 | // Add new features |
| 145 | for name, spec := range features { |
| 146 | if existingSpec, found := known[name]; found { |
| 147 | if existingSpec == spec { |
| 148 | continue |
| 149 | } |
| 150 | return fmt.Errorf("feature gate %q with different spec already exists: %v", name, existingSpec) |
| 151 | } |
| 152 | known[name] = spec |
| 153 | } |
| 154 |
| 155 | // Persist changes |
| 156 | f.known.Store(known) |
| 157 |
| 158 | return nil |
| 159 | } |
| 160 |
| 161 | // String returns a string containing all enabled feature gates, formatted as "key1=value1,key2=value2,..." |
| 162 | func (f *featureGate) String() string { |
| 163 | enabled := f.enabled.Load().(map[Feature]bool) |
| 164 | pairs := make([]string, 0, len(enabled)) |
| 165 | for k, v := range enabled { |
| 166 | pairs = append(pairs, fmt.Sprintf("%s=%t", k, v)) |
| 167 | } |
| 168 | sort.Strings(pairs) |
| 169 | return strings.Join(pairs, ",") |
| 170 | } |
| 171 |
| 172 | // Enabled returns true if the key is enabled |
| 173 | func (f *featureGate) Enabled(key Feature) bool { |
| 174 | if v, ok := f.enabled.Load().(map[Feature]bool)[key]; ok { |
| 175 | return v |
| 176 | } |
| 177 | if v, ok := f.known.Load().(map[Feature]FeatureSpec)[key]; ok { |
| 178 | return v.Default |
| 179 | } |
| 180 | return false |
| 181 | } |
| 182 |
| 183 | // KnownFeatures returns a slice of strings describing the FeatureGate's known features |
| 184 | // GA features are hidden from the list |
| 185 | func (f *featureGate) KnownFeatures() []string { |
| 186 | knownFeatures := f.known.Load().(map[Feature]FeatureSpec) |
| 187 | known := make([]string, 0, len(knownFeatures)) |
| 188 | for k, v := range knownFeatures { |
| 189 | if v.Stage == GA { |
| 190 | continue |
| 191 | } |
| 192 | known = append(known, fmt.Sprintf("%s=true|false (%s - default=%t)", k, v.Stage, v.Default)) |
| 193 | } |
| 194 | sort.Strings(known) |
| 195 | return known |
| 196 | } |
| 197 |
| 198 | // Default feature gates instance |
| 199 | var DefaultFeatureGates = NewFeatureGate() |
| 200 |
| 201 | // Enabled checks if a feature is enabled in the default feature gates |
| 202 | func Enabled(name Feature) bool { |
| 203 | return DefaultFeatureGates.Enabled(name) |
| 204 | } |
| 205 |
| 206 | // SetFromMap sets feature gate values from a map in the default feature gates |
| 207 | func SetFromMap(featureMap map[string]bool) error { |
| 208 | return DefaultFeatureGates.SetFromMap(featureMap) |
| 209 | } |