@@ -25,10 +25,15 @@ import (
25
25
"os"
26
26
"path/filepath"
27
27
28
+ "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
29
+ "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/msi/armmsi"
28
30
. "github.com/onsi/ginkgo/v2"
29
31
. "github.com/onsi/gomega"
30
32
corev1 "k8s.io/api/core/v1"
33
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34
+ infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
31
35
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
36
+ clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
32
37
capi_e2e "sigs.k8s.io/cluster-api/test/e2e"
33
38
"sigs.k8s.io/cluster-api/test/framework"
34
39
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
@@ -141,6 +146,40 @@ func SelfHostedSpec(ctx context.Context, inputGetter func() SelfHostedSpecInput)
141
146
Namespace : namespace .Name ,
142
147
})
143
148
149
+ // The workload cluster is not set up for workload identity. Use UserAssignedMSI there instead.
150
+ err := selfHostedClusterProxy .GetClient ().Delete (ctx , & infrav1.AzureClusterIdentity {
151
+ ObjectMeta : metav1.ObjectMeta {
152
+ Namespace : cluster .Namespace ,
153
+ Name : e2eConfig .GetVariable (ClusterIdentityName ),
154
+ },
155
+ })
156
+ Expect (err ).NotTo (HaveOccurred ())
157
+ cred , err := azidentity .NewDefaultAzureCredential (nil )
158
+ Expect (err ).NotTo (HaveOccurred ())
159
+ identityClient , err := armmsi .NewUserAssignedIdentitiesClient (getSubscriptionID (Default ), cred , nil )
160
+ Expect (err ).NotTo (HaveOccurred ())
161
+ identityRG := e2eConfig .GetVariable (AzureIdentityResourceGroup )
162
+ identityName := e2eConfig .GetVariable (AzureUserIdentity )
163
+ identity , err := identityClient .Get (ctx , identityRG , identityName , nil )
164
+ Expect (err ).NotTo (HaveOccurred ())
165
+ err = selfHostedClusterProxy .GetClient ().Create (ctx , & infrav1.AzureClusterIdentity {
166
+ ObjectMeta : metav1.ObjectMeta {
167
+ Namespace : cluster .Namespace ,
168
+ Name : e2eConfig .GetVariable (ClusterIdentityName ),
169
+ Labels : map [string ]string {
170
+ clusterctlv1 .ClusterctlMoveHierarchyLabel : "true" ,
171
+ },
172
+ },
173
+ Spec : infrav1.AzureClusterIdentitySpec {
174
+ AllowedNamespaces : & infrav1.AllowedNamespaces {},
175
+ ClientID : * identity .Properties .ClientID ,
176
+ ResourceID : * identity .ID ,
177
+ TenantID : e2eConfig .GetVariable (AzureTenantID ),
178
+ Type : infrav1 .UserAssignedMSI ,
179
+ },
180
+ })
181
+ Expect (err ).NotTo (HaveOccurred ())
182
+
144
183
Log ("Waiting for the cluster to be reconciled after moving to self hosted" )
145
184
selfHostedCluster = framework .DiscoveryAndWaitForCluster (ctx , framework.DiscoveryAndWaitForClusterInput {
146
185
Getter : selfHostedClusterProxy .GetClient (),
@@ -193,6 +232,31 @@ func SelfHostedSpec(ctx context.Context, inputGetter func() SelfHostedSpecInput)
193
232
Namespace : selfHostedNamespace .Name ,
194
233
})
195
234
235
+ // Restore the workload identity AzureClusterIdentity
236
+ err := input .BootstrapClusterProxy .GetClient ().Delete (ctx , & infrav1.AzureClusterIdentity {
237
+ ObjectMeta : metav1.ObjectMeta {
238
+ Namespace : namespace .Name ,
239
+ Name : e2eConfig .GetVariable (ClusterIdentityName ),
240
+ },
241
+ })
242
+ Expect (err ).NotTo (HaveOccurred ())
243
+ err = input .BootstrapClusterProxy .GetClient ().Create (ctx , & infrav1.AzureClusterIdentity {
244
+ ObjectMeta : metav1.ObjectMeta {
245
+ Namespace : namespace .Name ,
246
+ Name : e2eConfig .GetVariable (ClusterIdentityName ),
247
+ Labels : map [string ]string {
248
+ clusterctlv1 .ClusterctlMoveHierarchyLabel : "true" ,
249
+ },
250
+ },
251
+ Spec : infrav1.AzureClusterIdentitySpec {
252
+ AllowedNamespaces : & infrav1.AllowedNamespaces {},
253
+ ClientID : e2eConfig .GetVariable (AzureClientIDUserAssignedIdentity ),
254
+ TenantID : e2eConfig .GetVariable (AzureTenantID ),
255
+ Type : infrav1 .WorkloadIdentity ,
256
+ },
257
+ })
258
+ Expect (err ).NotTo (HaveOccurred ())
259
+
196
260
Log ("Waiting for the cluster to be reconciled after moving back to booststrap" )
197
261
clusterResources .Cluster = framework .DiscoveryAndWaitForCluster (ctx , framework.DiscoveryAndWaitForClusterInput {
198
262
Getter : input .BootstrapClusterProxy .GetClient (),
0 commit comments