@@ -25,10 +25,15 @@ import (
2525 "os"
2626 "path/filepath"
2727
28+ "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
29+ "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/msi/armmsi"
2830 . "github.com/onsi/ginkgo/v2"
2931 . "github.com/onsi/gomega"
3032 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"
3135 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
36+ clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
3237 capi_e2e "sigs.k8s.io/cluster-api/test/e2e"
3338 "sigs.k8s.io/cluster-api/test/framework"
3439 "sigs.k8s.io/cluster-api/test/framework/clusterctl"
@@ -141,6 +146,40 @@ func SelfHostedSpec(ctx context.Context, inputGetter func() SelfHostedSpecInput)
141146 Namespace : namespace .Name ,
142147 })
143148
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+
144183 Log ("Waiting for the cluster to be reconciled after moving to self hosted" )
145184 selfHostedCluster = framework .DiscoveryAndWaitForCluster (ctx , framework.DiscoveryAndWaitForClusterInput {
146185 Getter : selfHostedClusterProxy .GetClient (),
@@ -193,6 +232,31 @@ func SelfHostedSpec(ctx context.Context, inputGetter func() SelfHostedSpecInput)
193232 Namespace : selfHostedNamespace .Name ,
194233 })
195234
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+
196260 Log ("Waiting for the cluster to be reconciled after moving back to booststrap" )
197261 clusterResources .Cluster = framework .DiscoveryAndWaitForCluster (ctx , framework.DiscoveryAndWaitForClusterInput {
198262 Getter : input .BootstrapClusterProxy .GetClient (),
0 commit comments