@@ -18,6 +18,7 @@ package featuregates
1818
1919import (
2020 "strings"
21+ "sync"
2122
2223 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2324 "k8s.io/apimachinery/pkg/util/version"
@@ -38,10 +39,6 @@ const (
3839 IMEXDaemonsWithDNSNames featuregate.Feature = "IMEXDaemonsWithDNSNames"
3940)
4041
41- // FeatureGates is a singleton representing the set of all feature gates and their values.
42- // It contains both project-specific feature gates and standard Kubernetes logging feature gates.
43- var FeatureGates featuregate.MutableVersionedFeatureGate
44-
4542// defaultFeatureGates contains the default settings for all project-specific feature gates.
4643// These will be registered with the standard Kubernetes feature gate system.
4744var defaultFeatureGates = map [featuregate.Feature ]featuregate.VersionedSpecs {
@@ -68,9 +65,19 @@ var defaultFeatureGates = map[featuregate.Feature]featuregate.VersionedSpecs{
6865 },
6966}
7067
71- // init instantiates and sets the singleton 'FeatureGates' variable with newFeatureGates().
72- func init () {
73- FeatureGates = newFeatureGates (parseProjectVersion ())
68+ var (
69+ featureGatesOnce sync.Once
70+ featureGates featuregate.MutableVersionedFeatureGate
71+ )
72+
73+ // GetFeatureGates instantiates and returns the package-level singleton representing
74+ // the set of all feature gates and their values.
75+ // It contains both project-specific feature gates and standard Kubernetes logging feature gates.
76+ func GetFeatureGates () featuregate.MutableVersionedFeatureGate {
77+ featureGatesOnce .Do (func () {
78+ featureGates = newFeatureGates (parseProjectVersion ())
79+ })
80+ return featureGates
7481}
7582
7683// parseProjectVersion parses the project version string and returns major.minor version.
@@ -106,21 +113,21 @@ func newFeatureGates(version *version.Version) featuregate.MutableVersionedFeatu
106113// Enabled returns true if the specified feature gate is enabled in the global FeatureGates singleton.
107114// This is a convenience function that uses the global feature gate registry.
108115func Enabled (feature featuregate.Feature ) bool {
109- return FeatureGates .Enabled (feature )
116+ return GetFeatureGates () .Enabled (feature )
110117}
111118
112119// KnownFeatures returns a list of known feature gates with their descriptions.
113120func KnownFeatures () []string {
114- return FeatureGates .KnownFeatures ()
121+ return GetFeatureGates () .KnownFeatures ()
115122}
116123
117124// ToMap returns all known feature gates as a map[string]bool suitable for
118125// template rendering (e.g., {"FeatureA": true, "FeatureB": false}).
119126// Returns an empty map if no feature gates are configured.
120127func ToMap () map [string ]bool {
121128 result := make (map [string ]bool )
122- for feature := range FeatureGates .GetAll () {
123- result [string (feature )] = FeatureGates .Enabled (feature )
129+ for feature := range GetFeatureGates () .GetAll () {
130+ result [string (feature )] = GetFeatureGates () .Enabled (feature )
124131 }
125132 return result
126133}
0 commit comments