Skip to content

Commit 7de11f1

Browse files
authored
refactor: add global feature.Gates variable (#1167)
**What problem does this PR solve?**: Refactoring the feature gate code to make it easier to use. Added a new `feature.Gate` readonly variable that can be used anywhere in the codebase to determine if a specific feature gate was set. This is based on CAPI's implementation https://github.com/kubernetes-sigs/cluster-api/tree/main/feature **Which issue(s) this PR fixes**: Fixes # **How Has This Been Tested?**: <!-- Please describe the tests that you ran to verify your changes. Provide output from the tests and any manual steps needed to replicate the tests. --> **Special notes for your reviewer**: <!-- Use this to provide any additional information to the reviewers. This may include: - Best way to review the PR. - Where the author wants the most review attention on. - etc. -->
1 parent 5c37aa2 commit 7de11f1

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2025 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package feature
5+
6+
import (
7+
"k8s.io/apimachinery/pkg/util/runtime"
8+
"k8s.io/component-base/featuregate"
9+
)
10+
11+
//nolint:gochecknoinits // Code is copied from upstream.
12+
func init() {
13+
runtime.Must(MutableGates.Add(defaultFeatureGates()))
14+
}
15+
16+
var (
17+
// MutableGates is a mutable version of DefaultFeatureGate.
18+
// Only top-level commands/options setup should make use of this.
19+
// Tests that need to modify feature gates for the duration of their test should use:
20+
// featuregatetesting "k8s.io/component-base/featuregate/testing"
21+
// featuregatetesting.SetFeatureGateDuringTest(
22+
// t,
23+
// features.Gates,
24+
// features.<FeatureName>,
25+
// <value>,
26+
// )()
27+
MutableGates featuregate.MutableFeatureGate = featuregate.NewFeatureGate()
28+
29+
// Gates is a shared global FeatureGate.
30+
Gates featuregate.FeatureGate = MutableGates
31+
)

0 commit comments

Comments
 (0)