Skip to content

Commit 1fe566d

Browse files
Implement feature gating from flags
This change reads in feature gates from `-feature-gates` and passes them to the machine controller.
1 parent 0c7c376 commit 1fe566d

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

Diff for: cmd/manager/main.go

+27-4
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@ import (
1717
"flag"
1818
"fmt"
1919
"os"
20+
"strings"
2021
"time"
2122

2223
configv1 "github.com/openshift/api/config/v1"
24+
apifeatures "github.com/openshift/api/features"
2325
machinev1 "github.com/openshift/api/machine/v1"
2426
machinev1beta1 "github.com/openshift/api/machine/v1beta1"
27+
"github.com/openshift/library-go/pkg/features"
2528
"github.com/openshift/machine-api-operator/pkg/controller/machine"
2629
"github.com/openshift/machine-api-operator/pkg/metrics"
2730
machineactuator "github.com/openshift/machine-api-provider-aws/pkg/actuators/machine"
2831
machinesetcontroller "github.com/openshift/machine-api-provider-aws/pkg/actuators/machineset"
2932
awsclient "github.com/openshift/machine-api-provider-aws/pkg/client"
3033
"github.com/openshift/machine-api-provider-aws/pkg/version"
3134
corev1 "k8s.io/api/core/v1"
35+
"k8s.io/apiserver/pkg/util/feature"
3236
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
33-
k8sflag "k8s.io/component-base/cli/flag"
37+
"k8s.io/component-base/featuregate"
3438
"k8s.io/klog/v2"
3539
"k8s.io/klog/v2/klogr"
3640
ctrl "sigs.k8s.io/controller-runtime"
@@ -93,8 +97,15 @@ func main() {
9397
"The address for health checking.",
9498
)
9599

96-
featureGateArgs := map[string]bool{}
97-
flag.Var(k8sflag.NewMapStringBool(&featureGateArgs), "feature-gates", "A set of key=value pairs that describe feature gates for alpha/experimen")
100+
// Sets up feature gates
101+
defaultMutableGate := feature.DefaultMutableFeatureGate
102+
gateOpts, err := features.NewFeatureGateOptions(defaultMutableGate, apifeatures.SelfManaged, apifeatures.FeatureGateMachineAPIMigration)
103+
if err != nil {
104+
klog.Fatalf("Error setting up feature gates: %v", err)
105+
}
106+
107+
// Add the --feature-gates flag
108+
gateOpts.AddFlagsToGoFlagSet(nil)
98109

99110
klog.InitFlags(nil)
100111
flag.Set("logtostderr", "true")
@@ -137,6 +148,18 @@ func main() {
137148
klog.Infof("Watching machine-api objects only in namespace %q for reconciliation.", *watchNamespace)
138149
}
139150

151+
// Sets feature gates from flags
152+
klog.Infof("Initializing feature gates: %s", strings.Join(defaultMutableGate.KnownFeatures(), ", "))
153+
warnings, err := gateOpts.ApplyTo(defaultMutableGate)
154+
if err != nil {
155+
klog.Fatalf("Error setting feature gates from flags: %v", err)
156+
}
157+
if len(warnings) > 0 {
158+
klog.Infof("Warnings setting feature gates from flags: %v", warnings)
159+
}
160+
161+
klog.Infof("FeatureGateMachineAPIMigration initialised: %t", defaultMutableGate.Enabled(featuregate.Feature(apifeatures.FeatureGateMachineAPIMigration)))
162+
140163
mgr, err := manager.New(cfg, opts)
141164
if err != nil {
142165
klog.Fatalf("Error creating manager: %v", err)
@@ -175,7 +198,7 @@ func main() {
175198
RegionCache: describeRegionsCache,
176199
})
177200

178-
if err := machine.AddWithActuator(mgr, machineActuator); err != nil {
201+
if err := machine.AddWithActuator(mgr, machineActuator, defaultMutableGate); err != nil {
179202
klog.Fatalf("Error adding actuator: %v", err)
180203
}
181204

Diff for: pkg/actuators/machine/controller_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
configv1 "github.com/openshift/api/config/v1"
1313
machinev1beta1 "github.com/openshift/api/machine/v1beta1"
1414
machinecontroller "github.com/openshift/machine-api-operator/pkg/controller/machine"
15+
testutils "github.com/openshift/machine-api-operator/pkg/util/testing"
1516
awsclient "github.com/openshift/machine-api-provider-aws/pkg/client"
1617
mockaws "github.com/openshift/machine-api-provider-aws/pkg/client/mock"
1718
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -91,8 +92,10 @@ func TestMachineControllerWithDelayedExistSuccess(t *testing.T) {
9192
AwsClientBuilder: awsClientBuilder,
9293
}
9394
actuator := NewActuator(params)
95+
gate, err := testutils.NewDefaultMutableFeatureGate()
96+
g.Expect(err).ToNot(HaveOccurred())
9497

95-
g.Expect(machinecontroller.AddWithActuator(mgr, actuator)).To(Succeed())
98+
g.Expect(machinecontroller.AddWithActuator(mgr, actuator, gate)).To(Succeed())
9699
}
97100

98101
var machine *machinev1beta1.Machine

0 commit comments

Comments
 (0)