Skip to content

Commit 62e37b9

Browse files
committed
refactor: add global feature.Gate variable
This is a readonly variable that can be used anywhere in the codebase to determine if a specific feature gate was set.
1 parent 35fd169 commit 62e37b9

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

cmd/main.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1414
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
1515
cliflag "k8s.io/component-base/cli/flag"
16-
"k8s.io/component-base/featuregate"
1716
"k8s.io/component-base/logs"
1817
logsv1 "k8s.io/component-base/logs/api/v1"
1918
"k8s.io/component-base/version/verflag"
@@ -33,7 +32,7 @@ import (
3332
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers"
3433
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/server"
3534
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/controllers/namespacesync"
36-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/features"
35+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/feature"
3736
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws"
3837
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker"
3938
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic"
@@ -132,10 +131,8 @@ func main() {
132131
pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
133132
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
134133

135-
// Add feature gate flags.
136-
featureGate := featuregate.NewFeatureGate()
137-
utilruntime.Must(featureGate.Add(features.DefaultFeatureGates()))
138-
featureGate.AddFlag(pflag.CommandLine)
134+
// Add feature gate flag.
135+
feature.MutableGates.AddFlag(pflag.CommandLine)
139136

140137
pflag.Parse()
141138

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// Copyright 2024 Nutanix. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package features
4+
package feature
55

6-
import "k8s.io/component-base/featuregate"
6+
import (
7+
"k8s.io/component-base/featuregate"
8+
)
79

8-
// DefaultFeatureGates returns all known feature gates.
10+
// defaultFeatureGates returns all known feature gates.
911
// To add a new feature, define a key for it above and add it here. The features will be
1012
// available throughout the codebase.
11-
func DefaultFeatureGates() map[featuregate.Feature]featuregate.FeatureSpec {
13+
func defaultFeatureGates() map[featuregate.Feature]featuregate.FeatureSpec {
1214
return map[featuregate.Feature]featuregate.FeatureSpec{}
1315
}

pkg/feature/gates.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package feature
2+
3+
import (
4+
"k8s.io/apimachinery/pkg/util/runtime"
5+
"k8s.io/component-base/featuregate"
6+
)
7+
8+
func init() {
9+
runtime.Must(MutableGates.Add(defaultFeatureGates()))
10+
}
11+
12+
var (
13+
// MutableGates is a mutable version of DefaultFeatureGate.
14+
// Only top-level commands/options setup should make use of this.
15+
// Tests that need to modify feature gates for the duration of their test should use:
16+
// featuregatetesting "k8s.io/component-base/featuregate/testing"
17+
// featuregatetesting.SetFeatureGateDuringTest(
18+
// t,
19+
// features.Gates,
20+
// features.<FeatureName>,
21+
// <value>,
22+
// )()
23+
MutableGates featuregate.MutableFeatureGate = featuregate.NewFeatureGate()
24+
25+
// Gates is a shared global FeatureGate.
26+
Gates featuregate.FeatureGate = MutableGates
27+
)

0 commit comments

Comments
 (0)