- .github
- applyconfigurations
- discovery
- dynamic
- examples
- features
- gentype
- informers
- kubernetes
- kubernetes_test
- listers
- metadata
- openapi
- openapi3
- pkg
- plugin
-
rest
-
fake
-
testdata
-
watch
- OWNERS
- client.go
- client_test.go
- config.go
- config_test.go
- connection_test.go
- exec.go
- exec_test.go
- plugin.go
- plugin_test.go
- request.go
- request_test.go
- request_watchlist_test.go
- transport.go
- transport_test.go
- url_utils.go
- url_utils_test.go
- urlbackoff.go
- urlbackoff_test.go
- warnings.go
- with_retry.go
- with_retry_test.go
- zz_generated.deepcopy.go
-
fake
- restmapper
- scale
- testing
- third_party
- tools
- transport
- util
- CHANGELOG.md
- CONTRIBUTING.md
- INSTALL.md
- LICENSE
- OWNERS
- README.md
- SECURITY_CONTACTS
- code-of-conduct.md
- doc.go
- go.mod
- go.sum
Explanation
This code defines a feature gate implementation that uses environment variables to control feature states.
Key Components:
envVarFeatureGates
struct: This struct holds the logic for managing feature gates, including:known
: A map storing all registered features and their default values and lock settings.enabledViaEnvVar
: An atomic value containing a map of features and their states obtained from environment variables.enabledViaSetMethod
: A map of features and their states explicitly set using theSet
method.readEnvVarsOnce
: A sync.Once used to ensure environment variable reading happens only once.readEnvVars
: An atomic boolean indicating if environment variables have been read.callSiteName
: The name of the file where theenvVarFeatureGates
instance is created, used for debugging.
Functionality:
- Initialization:
newEnvVarFeatureGates
: Creates a newenvVarFeatureGates
instance. It initializes theknown
map from the providedfeatures
argument, and sets up theenabledViaEnvVar
andenabledViaSetMethod
maps.getEnabledMapFromEnvVar
: This function is executed only once, usingsync.Once
. It reads environment variables of the formKUBE_FEATURE_<feature_name>
and populates theenabledViaEnvVar
map. If an environment variable cannot be parsed as a boolean or conflicts with a locked feature, an error is logged but ignored.
- Feature State Retrieval:
Enabled
: This method checks if a given feature is enabled. It first looks for the feature inenabledViaSetMethod
. If found, the value fromenabledViaSetMethod
is returned. Otherwise, it retrieves the value fromenabledViaEnvVar
. If the feature is not found in either map, the default value fromknown
is used.
- Feature State Modification:
Set
: Allows setting a feature’s state explicitly. It validates the feature name and ensures that locked features cannot be set to a value different from their default.
Key Points:
- Feature Lock: The
LockToDefault
field inFeatureSpec
prevents changing a feature’s state from its default value. - Environment Variable Preference: The
Set
method overrides values set by environment variables, giving explicit control to users. - Error Handling: Errors during environment variable parsing or setting locked features are logged but ignored to prevent fatal failures.
Use Cases:
This code is designed to manage feature flags in applications that require flexible control over functionality.
- Development and Testing: Feature gates enable toggling features during development and testing without code changes.
- Production Rollout: Gradual rollouts of new features can be done by enabling them for a subset of users through environment variables.
- Feature Toggles: This allows for quick disabling or enabling of features in production, providing flexibility and control over functionality.
Overall:
The envVarFeatureGates
implementation provides a robust and flexible way to manage feature flags in Go applications, leveraging environment variables and explicit control for feature toggling.
Graph
The graph shows the usage of functions within the codebase.
Select a code symbol to view it's graph