Skip to content

Commit 6e53ee7

Browse files
Updates vsphere feature gates
This change updates vsphere feature gates to be read in from command line args, via the --feature-gates flag rather than accessing the feature gates directly.
1 parent 3a817c7 commit 6e53ee7

File tree

1 file changed

+31
-49
lines changed

1 file changed

+31
-49
lines changed

cmd/vsphere/main.go

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
package main
22

33
import (
4-
"context"
54
"flag"
65
"fmt"
76
"os"
7+
"strings"
88
"time"
99

1010
configv1 "github.com/openshift/api/config/v1"
1111
apifeatures "github.com/openshift/api/features"
1212
machinev1 "github.com/openshift/api/machine/v1beta1"
13-
configv1client "github.com/openshift/client-go/config/clientset/versioned"
14-
configinformers "github.com/openshift/client-go/config/informers/externalversions"
1513
"github.com/openshift/library-go/pkg/config/leaderelection"
16-
"github.com/openshift/library-go/pkg/operator/configobserver/featuregates"
17-
"github.com/openshift/library-go/pkg/operator/events"
14+
"github.com/openshift/library-go/pkg/features"
1815
capimachine "github.com/openshift/machine-api-operator/pkg/controller/machine"
1916
"github.com/openshift/machine-api-operator/pkg/controller/vsphere"
2017
machine "github.com/openshift/machine-api-operator/pkg/controller/vsphere"
@@ -23,6 +20,9 @@ import (
2320
"github.com/openshift/machine-api-operator/pkg/util"
2421
"github.com/openshift/machine-api-operator/pkg/version"
2522
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/apiserver/pkg/util/feature"
24+
k8sflag "k8s.io/component-base/cli/flag"
25+
"k8s.io/component-base/featuregate"
2626
"k8s.io/klog/v2"
2727
"k8s.io/klog/v2/textlogger"
2828
ipamv1beta1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1beta1"
@@ -35,6 +35,8 @@ import (
3535
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
3636
)
3737

38+
const timeout = 10 * time.Minute
39+
3840
func main() {
3941
var printVersion bool
4042
flag.BoolVar(&printVersion, "version", false, "print version and exit")
@@ -90,6 +92,18 @@ func main() {
9092
":9440",
9193
"The address for health checking.",
9294
)
95+
96+
// Sets up feature gates
97+
defaultMutableGate := feature.DefaultMutableFeatureGate
98+
_, err := features.NewFeatureGateOptions(defaultMutableGate, apifeatures.SelfManaged, apifeatures.FeatureGateVSphereStaticIPs, apifeatures.FeatureGateMachineAPIMigration)
99+
if err != nil {
100+
klog.Fatalf("Error setting up feature gates: %v", err)
101+
}
102+
103+
featureGateArgs := map[string]bool{}
104+
flag.Var(k8sflag.NewMapStringBool(&featureGateArgs), "feature-gates", "A set of key=value pairs that describe feature gates for alpha/experimental features. "+
105+
"Options are:\n"+strings.Join(defaultMutableGate.KnownFeatures(), "\n"))
106+
93107
flag.Parse()
94108

95109
if logToStderr != nil {
@@ -102,7 +116,7 @@ func main() {
102116
}
103117

104118
cfg := config.GetConfigOrDie()
105-
syncPeriod := 10 * time.Minute
119+
syncPeriod := timeout
106120

107121
le := util.GetLeaderElectionConfig(cfg, configv1.LeaderElection{
108122
Disable: !*leaderElect,
@@ -132,6 +146,17 @@ func main() {
132146
klog.Infof("Watching machine-api objects only in namespace %q for reconciliation.", *watchNamespace)
133147
}
134148

149+
// Sets feature gates from flags
150+
klog.Infof("Initializing feature gates: %s", strings.Join(defaultMutableGate.KnownFeatures(), ", "))
151+
err = defaultMutableGate.SetFromMap(featureGateArgs)
152+
if err != nil {
153+
klog.Fatalf("Error setting feature gates from flags: %v", err)
154+
}
155+
klog.Infof("FeatureGateMachineAPIMigration initialised: %t", defaultMutableGate.Enabled(featuregate.Feature(apifeatures.FeatureGateMachineAPIMigration)))
156+
157+
staticIPFeatureGateEnabled := defaultMutableGate.Enabled(featuregate.Feature(apifeatures.FeatureGateVSphereStaticIPs))
158+
klog.Infof("FeatureGateVSphereStaticIPs initialised: %t", staticIPFeatureGateEnabled)
159+
135160
// Setup a Manager
136161
mgr, err := manager.New(cfg, opts)
137162
if err != nil {
@@ -142,41 +167,6 @@ func main() {
142167
// network error or stale cache.
143168
taskIDCache := make(map[string]string)
144169

145-
desiredVersion := getReleaseVersion()
146-
missingVersion := "0.0.1-snapshot"
147-
148-
configClient, err := configv1client.NewForConfig(mgr.GetConfig())
149-
if err != nil {
150-
klog.Fatal(err, "unable to create config client")
151-
os.Exit(1)
152-
}
153-
configInformers := configinformers.NewSharedInformerFactory(configClient, 10*time.Minute)
154-
155-
// By default, this will exit(0) if the featuregates change
156-
featureGateAccessor := featuregates.NewFeatureGateAccess(
157-
desiredVersion, missingVersion,
158-
configInformers.Config().V1().ClusterVersions(),
159-
configInformers.Config().V1().FeatureGates(),
160-
events.NewLoggingEventRecorder("vspherecontroller"),
161-
)
162-
go featureGateAccessor.Run(context.Background())
163-
go configInformers.Start(context.Background().Done())
164-
165-
select {
166-
case <-featureGateAccessor.InitialFeatureGatesObserved():
167-
featureGates, _ := featureGateAccessor.CurrentFeatureGates()
168-
klog.Infof("FeatureGates initialized: %v", featureGates.KnownFeatures())
169-
case <-time.After(1 * time.Minute):
170-
klog.Fatal("timed out waiting for FeatureGate detection")
171-
}
172-
173-
featureGates, err := featureGateAccessor.CurrentFeatureGates()
174-
if err != nil {
175-
klog.Fatalf("unable to retrieve current feature gates: %v", err)
176-
}
177-
// read featuregate read and usage to set a variable to pass to a controller
178-
staticIPFeatureGateEnabled := featureGates.Enabled(apifeatures.FeatureGateVSphereStaticIPs)
179-
180170
// Initialize machine actuator.
181171
machineActuator := machine.NewActuator(machine.ActuatorParams{
182172
Client: mgr.GetClient(),
@@ -228,11 +218,3 @@ func main() {
228218
klog.Fatalf("Failed to run manager: %v", err)
229219
}
230220
}
231-
232-
func getReleaseVersion() string {
233-
releaseVersion := os.Getenv("RELEASE_VERSION")
234-
if len(releaseVersion) == 0 {
235-
return "0.0.1-snapshot"
236-
}
237-
return releaseVersion
238-
}

0 commit comments

Comments
 (0)