|
| 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/services/containerservice/mgmt/2021-05-01/containerservice" |
| 27 | + "github.com/Azure/go-autorest/autorest/azure/auth" |
| 28 | + . "github.com/onsi/ginkgo/v2" |
| 29 | + . "github.com/onsi/gomega" |
| 30 | + "k8s.io/apimachinery/pkg/types" |
| 31 | + "k8s.io/utils/pointer" |
| 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 | + settings, err := auth.GetSettingsFromEnvironment() |
| 48 | + Expect(err).NotTo(HaveOccurred()) |
| 49 | + subscriptionID := settings.GetSubscriptionID() |
| 50 | + auth, err := settings.GetAuthorizer() |
| 51 | + Expect(err).NotTo(HaveOccurred()) |
| 52 | + |
| 53 | + agentpoolsClient := containerservice.NewAgentPoolsClient(subscriptionID) |
| 54 | + agentpoolsClient.Authorizer = auth |
| 55 | + |
| 56 | + mgmtClient := bootstrapClusterProxy.GetClient() |
| 57 | + Expect(mgmtClient).NotTo(BeNil()) |
| 58 | + |
| 59 | + infraControlPlane := &infrav1.AzureManagedControlPlane{} |
| 60 | + err = mgmtClient.Get(ctx, client.ObjectKey{ |
| 61 | + Namespace: input.Cluster.Spec.ControlPlaneRef.Namespace, |
| 62 | + Name: input.Cluster.Spec.ControlPlaneRef.Name, |
| 63 | + }, infraControlPlane) |
| 64 | + Expect(err).NotTo(HaveOccurred()) |
| 65 | + |
| 66 | + var wg sync.WaitGroup |
| 67 | + |
| 68 | + for _, mp := range input.MachinePools { |
| 69 | + wg.Add(1) |
| 70 | + go func(mp *expv1.MachinePool) { |
| 71 | + defer GinkgoRecover() |
| 72 | + defer wg.Done() |
| 73 | + |
| 74 | + ammp := &infrav1.AzureManagedMachinePool{} |
| 75 | + Expect(mgmtClient.Get(ctx, types.NamespacedName{ |
| 76 | + Namespace: mp.Spec.Template.Spec.InfrastructureRef.Namespace, |
| 77 | + Name: mp.Spec.Template.Spec.InfrastructureRef.Name, |
| 78 | + }, ammp)).To(Succeed()) |
| 79 | + |
| 80 | + var expectedLabels map[string]string |
| 81 | + checkLabels := func(g Gomega) { |
| 82 | + agentpool, err := agentpoolsClient.Get(ctx, infraControlPlane.Spec.ResourceGroupName, infraControlPlane.Name, *ammp.Spec.Name) |
| 83 | + g.Expect(err).NotTo(HaveOccurred()) |
| 84 | + |
| 85 | + var actualLabels map[string]string |
| 86 | + if agentpool.NodeLabels != nil { |
| 87 | + actualLabels = make(map[string]string) |
| 88 | + for k, v := range agentpool.NodeLabels { |
| 89 | + actualLabels[k] = pointer.StringDeref(v, "") |
| 90 | + } |
| 91 | + } |
| 92 | + if expectedLabels == nil { |
| 93 | + g.Expect(actualLabels).To(BeNil()) |
| 94 | + } else { |
| 95 | + g.Expect(actualLabels).To(Equal(expectedLabels)) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + Byf("Creating node labels for machine pool %s", mp.Name) |
| 100 | + var initialLabels map[string]string |
| 101 | + expectedLabels = map[string]string{ |
| 102 | + "test": "label", |
| 103 | + "another": "value", |
| 104 | + } |
| 105 | + Eventually(func(g Gomega) { |
| 106 | + g.Expect(mgmtClient.Get(ctx, client.ObjectKeyFromObject(ammp), ammp)).To(Succeed()) |
| 107 | + initialLabels = ammp.Spec.NodeLabels |
| 108 | + ammp.Spec.NodeLabels = expectedLabels |
| 109 | + g.Expect(mgmtClient.Update(ctx, ammp)).To(Succeed()) |
| 110 | + }, input.WaitForUpdate...).Should(Succeed()) |
| 111 | + Eventually(checkLabels, input.WaitForUpdate...).Should(Succeed()) |
| 112 | + |
| 113 | + Byf("Updating node labels for machine pool %s", mp.Name) |
| 114 | + expectedLabels["test"] = "updated" |
| 115 | + delete(expectedLabels, "another") |
| 116 | + expectedLabels["new"] = "value" |
| 117 | + Eventually(func(g Gomega) { |
| 118 | + g.Expect(mgmtClient.Get(ctx, client.ObjectKeyFromObject(ammp), ammp)).To(Succeed()) |
| 119 | + ammp.Spec.NodeLabels = expectedLabels |
| 120 | + g.Expect(mgmtClient.Update(ctx, ammp)).To(Succeed()) |
| 121 | + }, input.WaitForUpdate...).Should(Succeed()) |
| 122 | + Eventually(checkLabels, input.WaitForUpdate...).Should(Succeed()) |
| 123 | + |
| 124 | + if initialLabels != nil { |
| 125 | + Byf("Restoring initial node labels for machine pool %s", mp.Name) |
| 126 | + expectedLabels = initialLabels |
| 127 | + Eventually(func(g Gomega) { |
| 128 | + g.Expect(mgmtClient.Get(ctx, client.ObjectKeyFromObject(ammp), ammp)).To(Succeed()) |
| 129 | + ammp.Spec.NodeLabels = expectedLabels |
| 130 | + g.Expect(mgmtClient.Update(ctx, ammp)).To(Succeed()) |
| 131 | + }, input.WaitForUpdate...).Should(Succeed()) |
| 132 | + Eventually(checkLabels, input.WaitForUpdate...).Should(Succeed()) |
| 133 | + } |
| 134 | + }(mp) |
| 135 | + } |
| 136 | + |
| 137 | + wg.Wait() |
| 138 | +} |
0 commit comments