Skip to content

Commit d3a1d28

Browse files
Merge pull request #55 from dlom/CCO-321
CCO-346: Add support for Azure workload identity tokens
2 parents 64f7f85 + ebf43c9 commit d3a1d28

File tree

1,078 files changed

+200932
-1077
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,078 files changed

+200932
-1077
lines changed

cmd/manager/main.go

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,27 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"context"
2021
"flag"
22+
"fmt"
2123
"os"
2224
"time"
2325

2426
configv1 "github.com/openshift/api/config/v1"
2527
machinev1 "github.com/openshift/api/machine/v1beta1"
28+
configclient "github.com/openshift/client-go/config/clientset/versioned"
29+
configinformers "github.com/openshift/client-go/config/informers/externalversions"
30+
"github.com/openshift/library-go/pkg/operator/configobserver/featuregates"
31+
"github.com/openshift/library-go/pkg/operator/events"
2632
"github.com/openshift/machine-api-operator/pkg/controller/machine"
2733
"github.com/openshift/machine-api-operator/pkg/metrics"
2834
actuator "github.com/openshift/machine-api-provider-azure/pkg/cloud/azure/actuators/machine"
2935
machinesetcontroller "github.com/openshift/machine-api-provider-azure/pkg/cloud/azure/actuators/machineset"
3036
"github.com/openshift/machine-api-provider-azure/pkg/cloud/azure/services/resourceskus"
3137
"github.com/openshift/machine-api-provider-azure/pkg/record"
38+
corev1 "k8s.io/api/core/v1"
39+
"k8s.io/client-go/kubernetes"
40+
"k8s.io/client-go/rest"
3241
"k8s.io/klog/v2"
3342
"k8s.io/klog/v2/klogr"
3443
ctrl "sigs.k8s.io/controller-runtime"
@@ -116,11 +125,36 @@ func main() {
116125
// Initialize event recorder.
117126
record.InitFromRecorder(mgr.GetEventRecorderFor("azure-controller"))
118127

128+
stopSignalContext := ctrl.SetupSignalHandler()
129+
130+
featureGateAccessor, err := createFeatureGateAccessor(
131+
context.Background(),
132+
cfg,
133+
"machine-api-provider-azure",
134+
"openshift-machine-api",
135+
"machine-api-controllers",
136+
getReleaseVersion(),
137+
"0.0.1-snapshot",
138+
syncPeriod,
139+
stopSignalContext.Done(),
140+
)
141+
if err != nil {
142+
klog.Fatalf("Failed to create feature gate accessor: %w", err)
143+
}
144+
145+
featureGates, err := awaitEnabledFeatureGates(featureGateAccessor, 1*time.Minute)
146+
if err != nil {
147+
klog.Fatalf("Failed to get feature gates: %w", err)
148+
}
149+
150+
azureWorkloadIdentityEnabled := featureGates.Enabled(configv1.FeatureGateAzureWorkloadIdentity)
151+
119152
// Initialize machine actuator.
120153
machineActuator := actuator.NewActuator(actuator.ActuatorParams{
121-
CoreClient: mgr.GetClient(),
122-
ReconcilerBuilder: actuator.NewReconciler,
123-
EventRecorder: mgr.GetEventRecorderFor("azure-controller"),
154+
CoreClient: mgr.GetClient(),
155+
ReconcilerBuilder: actuator.NewReconciler,
156+
EventRecorder: mgr.GetEventRecorderFor("azure-controller"),
157+
AzureWorkloadIdentityEnabled: azureWorkloadIdentityEnabled,
124158
})
125159

126160
if err := machinev1.AddToScheme(mgr.GetScheme()); err != nil {
@@ -141,6 +175,8 @@ func main() {
141175
Client: mgr.GetClient(),
142176
Log: ctrl.Log.WithName("controllers").WithName("MachineSet"),
143177
ResourceSkusServiceBuilder: resourceskus.NewService,
178+
179+
AzureWorkloadIdentityEnabled: azureWorkloadIdentityEnabled,
144180
}).SetupWithManager(mgr, controller.Options{}); err != nil {
145181
setupLog.Error(err, "unable to create controller", "controller", "MachineSet")
146182
os.Exit(1)
@@ -154,7 +190,66 @@ func main() {
154190
klog.Fatal(err)
155191
}
156192

157-
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
193+
if err := mgr.Start(stopSignalContext); err != nil {
158194
klog.Fatalf("Failed to run manager: %v", err)
159195
}
160196
}
197+
198+
func createFeatureGateAccessor(ctx context.Context, cfg *rest.Config, operatorName, deploymentNamespace, deploymentName, desiredVersion, missingVersion string, syncPeriod time.Duration, stop <-chan struct{}) (featuregates.FeatureGateAccess, error) {
199+
ctx, cancelFn := context.WithCancel(ctx)
200+
go func() {
201+
defer cancelFn()
202+
<-stop
203+
}()
204+
205+
kubeClient, err := kubernetes.NewForConfig(cfg)
206+
if err != nil {
207+
return nil, fmt.Errorf("failed to create kube client: %w", err)
208+
}
209+
210+
eventRecorder := events.NewKubeRecorder(kubeClient.CoreV1().Events(deploymentNamespace), operatorName, &corev1.ObjectReference{
211+
APIVersion: "apps/v1",
212+
Kind: "Deployment",
213+
Namespace: deploymentNamespace,
214+
Name: deploymentName,
215+
})
216+
217+
configClient, err := configclient.NewForConfig(cfg)
218+
if err != nil {
219+
return nil, fmt.Errorf("failed to create config client: %w", err)
220+
}
221+
configInformers := configinformers.NewSharedInformerFactory(configClient, syncPeriod)
222+
223+
featureGateAccessor := featuregates.NewFeatureGateAccess(
224+
desiredVersion, missingVersion,
225+
configInformers.Config().V1().ClusterVersions(), configInformers.Config().V1().FeatureGates(),
226+
eventRecorder,
227+
)
228+
go featureGateAccessor.Run(ctx)
229+
go configInformers.Start(stop)
230+
231+
return featureGateAccessor, nil
232+
}
233+
234+
func awaitEnabledFeatureGates(accessor featuregates.FeatureGateAccess, timeout time.Duration) (featuregates.FeatureGate, error) {
235+
select {
236+
case <-accessor.InitialFeatureGatesObserved():
237+
featureGates, err := accessor.CurrentFeatureGates()
238+
if err != nil {
239+
return nil, err
240+
} else {
241+
klog.Infof("FeatureGates initialized: knownFeatureGates=%v", featureGates.KnownFeatures())
242+
return featureGates, nil
243+
}
244+
case <-time.After(timeout):
245+
return nil, fmt.Errorf("timed out waiting for FeatureGate detection")
246+
}
247+
}
248+
249+
func getReleaseVersion() string {
250+
releaseVersion := os.Getenv("RELEASE_VERSION")
251+
if len(releaseVersion) == 0 {
252+
return "0.0.1-snapshot"
253+
}
254+
return releaseVersion
255+
}

go.mod

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/openshift/machine-api-operator v0.2.1-0.20230531233206-931f6f67c1c7
2020
github.com/pkg/errors v0.9.1
2121
github.com/spf13/cobra v1.7.0
22-
golang.org/x/crypto v0.6.0
22+
golang.org/x/crypto v0.7.0
2323
golang.org/x/net v0.10.0 // indirect
2424

2525
// kube 1.27
@@ -35,22 +35,25 @@ require (
3535
)
3636

3737
require (
38-
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1
39-
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.1
38+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.0
39+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0
4040
github.com/jongio/azidext/go/azidext v0.4.0
41+
github.com/openshift/client-go v0.0.0-20230503144108-75015d2347cb
42+
github.com/openshift/library-go v0.0.0-20230508110756-9b7abe2c9cbf
4143
)
4244

4345
require (
44-
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 // indirect
46+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
4547
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
4648
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
4749
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
4850
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
4951
github.com/Azure/go-autorest/logger v0.2.1 // indirect
5052
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
51-
github.com/AzureAD/microsoft-authentication-library-for-go v0.8.1 // indirect
53+
github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0 // indirect
5254
github.com/MakeNowJust/heredoc v1.0.0 // indirect
5355
github.com/beorn7/perks v1.0.1 // indirect
56+
github.com/blang/semver/v4 v4.0.0 // indirect
5457
github.com/cespare/xxhash/v2 v2.2.0 // indirect
5558
github.com/chai2010/gettext-go v1.0.2 // indirect
5659
github.com/davecgh/go-spew v1.1.1 // indirect
@@ -68,7 +71,7 @@ require (
6871
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
6972
github.com/gobuffalo/flect v1.0.2 // indirect
7073
github.com/gogo/protobuf v1.3.2 // indirect
71-
github.com/golang-jwt/jwt/v4 v4.4.3 // indirect
74+
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
7275
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
7376
github.com/golang/protobuf v1.5.3 // indirect
7477
github.com/google/btree v1.0.1 // indirect
@@ -95,14 +98,13 @@ require (
9598
github.com/modern-go/reflect2 v1.0.2 // indirect
9699
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
97100
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
98-
github.com/openshift/client-go v0.0.0-20230503144108-75015d2347cb // indirect
99-
github.com/openshift/library-go v0.0.0-20230508110756-9b7abe2c9cbf // indirect
100101
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
101102
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
102103
github.com/prometheus/client_golang v1.15.1 // indirect
103104
github.com/prometheus/client_model v0.4.0 // indirect
104105
github.com/prometheus/common v0.42.0 // indirect
105106
github.com/prometheus/procfs v0.9.0 // indirect
107+
github.com/robfig/cron v1.2.0 // indirect
106108
github.com/russross/blackfriday/v2 v2.1.0 // indirect
107109
github.com/spf13/afero v1.9.3 // indirect
108110
github.com/spf13/pflag v1.0.5 // indirect
@@ -125,11 +127,14 @@ require (
125127
gopkg.in/yaml.v2 v2.4.0 // indirect
126128
gopkg.in/yaml.v3 v3.0.1 // indirect
127129
k8s.io/apiextensions-apiserver v0.27.2 // indirect
130+
k8s.io/apiserver v0.27.2 // indirect
128131
k8s.io/cli-runtime v0.27.1 // indirect
129132
k8s.io/component-base v0.27.2 // indirect
133+
k8s.io/kube-aggregator v0.27.1 // indirect
130134
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
131135
k8s.io/kubectl v0.27.1 // indirect
132136
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
137+
sigs.k8s.io/kube-storage-version-migrator v0.0.4 // indirect
133138
sigs.k8s.io/kustomize/api v0.13.4 // indirect
134139
sigs.k8s.io/kustomize/kyaml v0.14.2 // indirect
135140
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect

0 commit comments

Comments
 (0)