-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature flag system to support gradual rollouts (#151)
Introduce a lightweight feature flag implementation to enable safer and more controlled feature releases. This system will allow us to: - Reduce risk by gradually rolling out new features to subsets of users - Quickly disable problematc features without requiring a new release - Manage feature lifecycles more effectively across different environments The implementation uses a simple map-based approach for for efficiently introducing and graduating new features in ACK. Usage example: ```go func someLogic() { ... if cfg.FeatureGates.IsEnabled("FeatureName") { } else { } } ``` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
- Loading branch information
Showing
4 changed files
with
375 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
// Package featuregate provides a simple mechanism for managing feature gates | ||
// in ACK controllers. It allows for default gates to be defined and | ||
// optionally overridden. | ||
package featuregate | ||
|
||
// defaultACKFeatureGates is a map of feature names to Feature structs | ||
// representing the default feature gates for ACK controllers. | ||
var defaultACKFeatureGates = FeatureGates{ | ||
// Set feature gates here | ||
// "feature1": {Stage: Alpha, Enabled: false}, | ||
} | ||
|
||
// FeatureStage represents the development stage of a feature. | ||
type FeatureStage string | ||
|
||
const ( | ||
// Alpha represents a feature in early testing, potentially unstable. | ||
// Alpha features may be removed or changed at any time and are disabled | ||
// by default. | ||
Alpha FeatureStage = "alpha" | ||
|
||
// Beta represents a feature in advanced testing, more stable than alpha. | ||
// Beta features are enabled by default. | ||
Beta FeatureStage = "beta" | ||
|
||
// GA represents a feature that is generally available and stable. | ||
GA FeatureStage = "ga" | ||
) | ||
|
||
// Feature represents a single feature gate with its properties. | ||
type Feature struct { | ||
// Stage indicates the current development stage of the feature. | ||
Stage FeatureStage | ||
|
||
// Enabled determines if the feature is enabled. | ||
Enabled bool | ||
} | ||
|
||
// FeatureGates is a map representing a set of feature gates. | ||
type FeatureGates map[string]Feature | ||
|
||
// IsEnabled checks if a feature with the given name is enabled. | ||
// It returns true if the feature exists and is enabled, false | ||
// otherwise. | ||
func (fg FeatureGates) IsEnabled(name string) bool { | ||
feature, ok := fg[name] | ||
return ok && feature.Enabled | ||
} | ||
|
||
// GetFeature retrieves a feature by its name. | ||
// It returns the Feature struct and a boolean indicating whether the | ||
// feature was found. | ||
func (fg FeatureGates) GetFeature(name string) (Feature, bool) { | ||
feature, ok := fg[name] | ||
return feature, ok | ||
} | ||
|
||
// GetFeatureNames returns a slice of feature names in the FeatureGates | ||
// instance. | ||
func (fg FeatureGates) GetFeatureNames() []string { | ||
names := make([]string, 0, len(fg)) | ||
for name := range fg { | ||
names = append(names, name) | ||
} | ||
return names | ||
} | ||
|
||
// GetDefaultFeatureGates returns a new FeatureGates instance initialized with the default feature set. | ||
// This function should be used when no overrides are needed. | ||
func GetDefaultFeatureGates() FeatureGates { | ||
gates := make(FeatureGates) | ||
for name, feature := range defaultACKFeatureGates { | ||
gates[name] = feature | ||
} | ||
return gates | ||
} | ||
|
||
// GetFeatureGatesWithOverrides returns a new FeatureGates instance with the default features, | ||
// but with the provided overrides applied. This allows for runtime configuration of feature gates. | ||
func GetFeatureGatesWithOverrides(featureGateOverrides map[string]bool) FeatureGates { | ||
gates := GetDefaultFeatureGates() | ||
for name, enabled := range featureGateOverrides { | ||
if feature, ok := gates[name]; ok { | ||
feature.Enabled = enabled | ||
gates[name] = feature | ||
} | ||
} | ||
return gates | ||
} |
Oops, something went wrong.