sigs.k8s.io/cluster-api-provider-azure@v1.14.3/test/e2e/aks_node_labels.go (about) 1 //go:build e2e 2 // +build e2e 3 4 /* 5 Copyright 2023 The Kubernetes Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package e2e 21 22 import ( 23 "context" 24 "sync" 25 26 "github.com/Azure/azure-sdk-for-go/sdk/azidentity" 27 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4" 28 . "github.com/onsi/ginkgo/v2" 29 . "github.com/onsi/gomega" 30 "k8s.io/apimachinery/pkg/types" 31 "k8s.io/utils/ptr" 32 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 33 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 34 expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" 35 "sigs.k8s.io/controller-runtime/pkg/client" 36 ) 37 38 type AKSNodeLabelsSpecInput struct { 39 Cluster *clusterv1.Cluster 40 MachinePools []*expv1.MachinePool 41 WaitForUpdate []interface{} 42 } 43 44 func AKSNodeLabelsSpec(ctx context.Context, inputGetter func() AKSNodeLabelsSpecInput) { 45 input := inputGetter() 46 47 cred, err := azidentity.NewDefaultAzureCredential(nil) 48 Expect(err).NotTo(HaveOccurred()) 49 50 agentpoolsClient, err := armcontainerservice.NewAgentPoolsClient(getSubscriptionID(Default), cred, nil) 51 Expect(err).NotTo(HaveOccurred()) 52 53 mgmtClient := bootstrapClusterProxy.GetClient() 54 Expect(mgmtClient).NotTo(BeNil()) 55 56 infraControlPlane := &infrav1.AzureManagedControlPlane{} 57 err = mgmtClient.Get(ctx, client.ObjectKey{ 58 Namespace: input.Cluster.Spec.ControlPlaneRef.Namespace, 59 Name: input.Cluster.Spec.ControlPlaneRef.Name, 60 }, infraControlPlane) 61 Expect(err).NotTo(HaveOccurred()) 62 63 var wg sync.WaitGroup 64 65 for _, mp := range input.MachinePools { 66 wg.Add(1) 67 go func(mp *expv1.MachinePool) { 68 defer GinkgoRecover() 69 defer wg.Done() 70 71 ammp := &infrav1.AzureManagedMachinePool{} 72 Expect(mgmtClient.Get(ctx, types.NamespacedName{ 73 Namespace: mp.Spec.Template.Spec.InfrastructureRef.Namespace, 74 Name: mp.Spec.Template.Spec.InfrastructureRef.Name, 75 }, ammp)).To(Succeed()) 76 77 var expectedLabels map[string]string 78 checkLabels := func(g Gomega) { 79 resp, err := agentpoolsClient.Get(ctx, infraControlPlane.Spec.ResourceGroupName, infraControlPlane.Name, *ammp.Spec.Name, nil) 80 g.Expect(err).NotTo(HaveOccurred()) 81 g.Expect(resp.Properties.ProvisioningState).To(Equal(ptr.To("Succeeded"))) 82 83 agentpool := resp.AgentPool 84 var actualLabels map[string]string 85 if agentpool.Properties.NodeLabels != nil { 86 actualLabels = make(map[string]string) 87 for k, v := range agentpool.Properties.NodeLabels { 88 actualLabels[k] = ptr.Deref(v, "") 89 } 90 } 91 if expectedLabels == nil { 92 g.Expect(actualLabels).To(BeNil()) 93 } else { 94 g.Expect(actualLabels).To(Equal(expectedLabels)) 95 } 96 } 97 98 Byf("Deleting all node labels for machine pool %s", mp.Name) 99 expectedLabels = nil 100 var initialLabels map[string]string 101 Eventually(func(g Gomega) { 102 g.Expect(mgmtClient.Get(ctx, client.ObjectKeyFromObject(ammp), ammp)).To(Succeed()) 103 initialLabels = ammp.Spec.NodeLabels 104 ammp.Spec.NodeLabels = expectedLabels 105 g.Expect(mgmtClient.Update(ctx, ammp)).To(Succeed()) 106 }, inputGetter().WaitForUpdate...).Should(Succeed()) 107 Eventually(checkLabels, input.WaitForUpdate...).Should(Succeed()) 108 109 Byf("Creating node labels for machine pool %s", mp.Name) 110 expectedLabels = map[string]string{ 111 "test": "label", 112 "another": "value", 113 } 114 Eventually(func(g Gomega) { 115 g.Expect(mgmtClient.Get(ctx, client.ObjectKeyFromObject(ammp), ammp)).To(Succeed()) 116 ammp.Spec.NodeLabels = expectedLabels 117 g.Expect(mgmtClient.Update(ctx, ammp)).To(Succeed()) 118 }, input.WaitForUpdate...).Should(Succeed()) 119 Eventually(checkLabels, input.WaitForUpdate...).Should(Succeed()) 120 121 Byf("Updating node labels for machine pool %s", mp.Name) 122 expectedLabels["test"] = "updated" 123 delete(expectedLabels, "another") 124 expectedLabels["new"] = "value" 125 Eventually(func(g Gomega) { 126 g.Expect(mgmtClient.Get(ctx, client.ObjectKeyFromObject(ammp), ammp)).To(Succeed()) 127 ammp.Spec.NodeLabels = expectedLabels 128 g.Expect(mgmtClient.Update(ctx, ammp)).To(Succeed()) 129 }, input.WaitForUpdate...).Should(Succeed()) 130 Eventually(checkLabels, input.WaitForUpdate...).Should(Succeed()) 131 132 Byf("Restoring initial node labels for machine pool %s", mp.Name) 133 expectedLabels = initialLabels 134 Eventually(func(g Gomega) { 135 g.Expect(mgmtClient.Get(ctx, client.ObjectKeyFromObject(ammp), ammp)).To(Succeed()) 136 ammp.Spec.NodeLabels = expectedLabels 137 g.Expect(mgmtClient.Update(ctx, ammp)).To(Succeed()) 138 }, input.WaitForUpdate...).Should(Succeed()) 139 Eventually(checkLabels, input.WaitForUpdate...).Should(Succeed()) 140 }(mp) 141 } 142 143 wg.Wait() 144 }