@@ -17,29 +17,23 @@ limitations under the License.
17
17
package main
18
18
19
19
import (
20
- "context"
21
20
"flag"
22
- "fmt"
23
21
"os"
22
+ "strings"
24
23
"time"
25
24
26
25
configv1 "github.com/openshift/api/config/v1"
27
26
apifeatures "github.com/openshift/api/features"
28
27
machinev1 "github.com/openshift/api/machine/v1beta1"
29
- configclient "github.com/openshift/client-go/config/clientset/versioned"
30
- configinformers "github.com/openshift/client-go/config/informers/externalversions"
31
- "github.com/openshift/library-go/pkg/operator/configobserver/featuregates"
32
- "github.com/openshift/library-go/pkg/operator/events"
28
+ "github.com/openshift/library-go/pkg/features"
33
29
"github.com/openshift/machine-api-operator/pkg/controller/machine"
34
30
"github.com/openshift/machine-api-operator/pkg/metrics"
35
31
actuator "github.com/openshift/machine-api-provider-azure/pkg/cloud/azure/actuators/machine"
36
32
machinesetcontroller "github.com/openshift/machine-api-provider-azure/pkg/cloud/azure/actuators/machineset"
37
33
"github.com/openshift/machine-api-provider-azure/pkg/cloud/azure/services/resourceskus"
38
34
"github.com/openshift/machine-api-provider-azure/pkg/record"
39
- corev1 "k8s.io/api/core/v1"
40
- "k8s.io/client-go/kubernetes"
41
- "k8s.io/client-go/rest"
42
- k8sflag "k8s.io/component-base/cli/flag"
35
+ "k8s.io/apiserver/pkg/util/feature"
36
+ "k8s.io/component-base/featuregate"
43
37
"k8s.io/klog/v2"
44
38
"k8s.io/klog/v2/klogr"
45
39
ctrl "sigs.k8s.io/controller-runtime"
@@ -100,9 +94,15 @@ func main() {
100
94
1 ,
101
95
"Maximum number of concurrent reconciles per controller instance." ,
102
96
)
97
+ // Sets up feature gates
98
+ defaultMutableGate := feature .DefaultMutableFeatureGate
99
+ gateOpts , err := features .NewFeatureGateOptions (defaultMutableGate , apifeatures .SelfManaged , apifeatures .FeatureGateAzureWorkloadIdentity , apifeatures .FeatureGateMachineAPIMigration )
100
+ if err != nil {
101
+ klog .Fatalf ("Error setting up feature gates: %v" , err )
102
+ }
103
103
104
- featureGateArgs := map [ string ] bool {}
105
- flag . Var ( k8sflag . NewMapStringBool ( & featureGateArgs ), "feature-gates" , "A set of key=value pairs that describe feature gates for alpha/experimen" )
104
+ // Add the --feature-gates flag
105
+ gateOpts . AddFlagsToGoFlagSet ( nil )
106
106
107
107
klog .InitFlags (nil )
108
108
flag .Set ("logtostderr" , "true" )
@@ -135,6 +135,20 @@ func main() {
135
135
klog .Infof ("Watching machine-api objects only in namespace %q for reconciliation." , * watchNamespace )
136
136
}
137
137
138
+ // Sets feature gates from flags
139
+ klog .Infof ("Initializing feature gates: %s" , strings .Join (defaultMutableGate .KnownFeatures (), ", " ))
140
+ warnings , err := gateOpts .ApplyTo (defaultMutableGate )
141
+ if err != nil {
142
+ klog .Fatalf ("Error setting feature gates from flags: %v" , err )
143
+ }
144
+ if len (warnings ) > 0 {
145
+ klog .Infof ("Warnings setting feature gates from flags: %v" , warnings )
146
+ }
147
+
148
+ klog .Infof ("FeatureGateMachineAPIMigration initialised: %t" , defaultMutableGate .Enabled (featuregate .Feature (apifeatures .FeatureGateMachineAPIMigration )))
149
+ klog .Infof ("FeatureGateAzureWorkloadIdentity initialised: %t" , defaultMutableGate .Enabled (featuregate .Feature (apifeatures .FeatureGateAzureWorkloadIdentity )))
150
+ azureWorkloadIdentityEnabled := defaultMutableGate .Enabled (featuregate .Feature (apifeatures .FeatureGateAzureWorkloadIdentity ))
151
+
138
152
// Setup a Manager
139
153
mgr , err := manager .New (cfg , opts )
140
154
if err != nil {
@@ -143,31 +157,8 @@ func main() {
143
157
144
158
// Initialize event recorder.
145
159
record .InitFromRecorder (mgr .GetEventRecorderFor ("azure-controller" ))
146
-
147
160
stopSignalContext := ctrl .SetupSignalHandler ()
148
161
149
- featureGateAccessor , err := createFeatureGateAccessor (
150
- context .Background (),
151
- cfg ,
152
- "machine-api-provider-azure" ,
153
- "openshift-machine-api" ,
154
- "machine-api-controllers" ,
155
- getReleaseVersion (),
156
- "0.0.1-snapshot" ,
157
- syncPeriod ,
158
- stopSignalContext .Done (),
159
- )
160
- if err != nil {
161
- klog .Fatalf ("Failed to create feature gate accessor: %v" , err )
162
- }
163
-
164
- featureGates , err := awaitEnabledFeatureGates (featureGateAccessor , 1 * time .Minute )
165
- if err != nil {
166
- klog .Fatalf ("Failed to get feature gates: %v" , err )
167
- }
168
-
169
- azureWorkloadIdentityEnabled := featureGates .Enabled (apifeatures .FeatureGateAzureWorkloadIdentity )
170
-
171
162
// Initialize machine actuator.
172
163
machineActuator := actuator .NewActuator (actuator.ActuatorParams {
173
164
CoreClient : mgr .GetClient (),
@@ -186,7 +177,7 @@ func main() {
186
177
187
178
if err := machine .AddWithActuatorOpts (mgr , machineActuator , controller.Options {
188
179
MaxConcurrentReconciles : * maxConcurrentReconciles ,
189
- }); err != nil {
180
+ }, defaultMutableGate ); err != nil {
190
181
klog .Fatal (err )
191
182
}
192
183
@@ -215,62 +206,3 @@ func main() {
215
206
klog .Fatalf ("Failed to run manager: %v" , err )
216
207
}
217
208
}
218
-
219
- func createFeatureGateAccessor (ctx context.Context , cfg * rest.Config , operatorName , deploymentNamespace , deploymentName , desiredVersion , missingVersion string , syncPeriod time.Duration , stop <- chan struct {}) (featuregates.FeatureGateAccess , error ) {
220
- ctx , cancelFn := context .WithCancel (ctx )
221
- go func () {
222
- defer cancelFn ()
223
- <- stop
224
- }()
225
-
226
- kubeClient , err := kubernetes .NewForConfig (cfg )
227
- if err != nil {
228
- return nil , fmt .Errorf ("failed to create kube client: %w" , err )
229
- }
230
-
231
- eventRecorder := events .NewKubeRecorder (kubeClient .CoreV1 ().Events (deploymentNamespace ), operatorName , & corev1.ObjectReference {
232
- APIVersion : "apps/v1" ,
233
- Kind : "Deployment" ,
234
- Namespace : deploymentNamespace ,
235
- Name : deploymentName ,
236
- })
237
-
238
- configClient , err := configclient .NewForConfig (cfg )
239
- if err != nil {
240
- return nil , fmt .Errorf ("failed to create config client: %w" , err )
241
- }
242
- configInformers := configinformers .NewSharedInformerFactory (configClient , syncPeriod )
243
-
244
- featureGateAccessor := featuregates .NewFeatureGateAccess (
245
- desiredVersion , missingVersion ,
246
- configInformers .Config ().V1 ().ClusterVersions (), configInformers .Config ().V1 ().FeatureGates (),
247
- eventRecorder ,
248
- )
249
- go featureGateAccessor .Run (ctx )
250
- go configInformers .Start (stop )
251
-
252
- return featureGateAccessor , nil
253
- }
254
-
255
- func awaitEnabledFeatureGates (accessor featuregates.FeatureGateAccess , timeout time.Duration ) (featuregates.FeatureGate , error ) {
256
- select {
257
- case <- accessor .InitialFeatureGatesObserved ():
258
- featureGates , err := accessor .CurrentFeatureGates ()
259
- if err != nil {
260
- return nil , err
261
- } else {
262
- klog .Infof ("FeatureGates initialized: knownFeatureGates=%v" , featureGates .KnownFeatures ())
263
- return featureGates , nil
264
- }
265
- case <- time .After (timeout ):
266
- return nil , fmt .Errorf ("timed out waiting for FeatureGate detection" )
267
- }
268
- }
269
-
270
- func getReleaseVersion () string {
271
- releaseVersion := os .Getenv ("RELEASE_VERSION" )
272
- if len (releaseVersion ) == 0 {
273
- return "0.0.1-snapshot"
274
- }
275
- return releaseVersion
276
- }
0 commit comments