|
| 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 | + "fmt" |
| 25 | + "sync" |
| 26 | + |
| 27 | + "github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2021-05-01/containerservice" |
| 28 | + "github.com/Azure/go-autorest/autorest/azure/auth" |
| 29 | + . "github.com/onsi/ginkgo/v2" |
| 30 | + . "github.com/onsi/gomega" |
| 31 | + corev1 "k8s.io/api/core/v1" |
| 32 | + "k8s.io/apimachinery/pkg/types" |
| 33 | + infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" |
| 34 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 35 | + expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 37 | +) |
| 38 | + |
| 39 | +type AKSNodeTaintsSpecInput struct { |
| 40 | + Cluster *clusterv1.Cluster |
| 41 | + MachinePools []*expv1.MachinePool |
| 42 | + WaitForUpdate []interface{} |
| 43 | +} |
| 44 | + |
| 45 | +func AKSNodeTaintsSpec(ctx context.Context, inputGetter func() AKSNodeTaintsSpecInput) { |
| 46 | + input := inputGetter() |
| 47 | + |
| 48 | + settings, err := auth.GetSettingsFromEnvironment() |
| 49 | + Expect(err).NotTo(HaveOccurred()) |
| 50 | + subscriptionID := settings.GetSubscriptionID() |
| 51 | + auth, err := settings.GetAuthorizer() |
| 52 | + Expect(err).NotTo(HaveOccurred()) |
| 53 | + |
| 54 | + agentpoolsClient := containerservice.NewAgentPoolsClient(subscriptionID) |
| 55 | + agentpoolsClient.Authorizer = auth |
| 56 | + |
| 57 | + mgmtClient := bootstrapClusterProxy.GetClient() |
| 58 | + Expect(mgmtClient).NotTo(BeNil()) |
| 59 | + |
| 60 | + infraControlPlane := &infrav1.AzureManagedControlPlane{} |
| 61 | + err = mgmtClient.Get(ctx, client.ObjectKey{ |
| 62 | + Namespace: input.Cluster.Spec.ControlPlaneRef.Namespace, |
| 63 | + Name: input.Cluster.Spec.ControlPlaneRef.Name, |
| 64 | + }, infraControlPlane) |
| 65 | + Expect(err).NotTo(HaveOccurred()) |
| 66 | + |
| 67 | + var wg sync.WaitGroup |
| 68 | + |
| 69 | + for _, mp := range input.MachinePools { |
| 70 | + wg.Add(1) |
| 71 | + go func(mp *expv1.MachinePool) { |
| 72 | + defer GinkgoRecover() |
| 73 | + defer wg.Done() |
| 74 | + |
| 75 | + ammp := &infrav1.AzureManagedMachinePool{} |
| 76 | + Expect(mgmtClient.Get(ctx, types.NamespacedName{ |
| 77 | + Namespace: mp.Spec.Template.Spec.InfrastructureRef.Namespace, |
| 78 | + Name: mp.Spec.Template.Spec.InfrastructureRef.Name, |
| 79 | + }, ammp)).To(Succeed()) |
| 80 | + initialTaints := ammp.Spec.Taints |
| 81 | + |
| 82 | + var expectedTaints infrav1.Taints |
| 83 | + checkTaints := func(g Gomega) { |
| 84 | + var expectedTaintStrs *[]string |
| 85 | + if expectedTaints != nil { |
| 86 | + expectedTaintStrs = new([]string) |
| 87 | + *expectedTaintStrs = make([]string, 0, len(expectedTaints)) |
| 88 | + for _, taint := range expectedTaints { |
| 89 | + *expectedTaintStrs = append(*expectedTaintStrs, fmt.Sprintf("%s=%s:%s", taint.Key, taint.Value, taint.Effect)) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + agentpool, err := agentpoolsClient.Get(ctx, infraControlPlane.Spec.ResourceGroupName, infraControlPlane.Name, *ammp.Spec.Name) |
| 94 | + g.Expect(err).NotTo(HaveOccurred()) |
| 95 | + actualTaintStrs := agentpool.NodeTaints |
| 96 | + if expectedTaintStrs == nil { |
| 97 | + g.Expect(actualTaintStrs).To(BeNil()) |
| 98 | + } else { |
| 99 | + g.Expect(actualTaintStrs).To(Equal(expectedTaintStrs)) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + Byf("Creating taints for machine pool %s", mp.Name) |
| 104 | + expectedTaints = infrav1.Taints{ |
| 105 | + { |
| 106 | + Effect: infrav1.TaintEffect(corev1.TaintEffectPreferNoSchedule), |
| 107 | + Key: "capz-e2e-1", |
| 108 | + Value: "test1", |
| 109 | + }, |
| 110 | + { |
| 111 | + Effect: infrav1.TaintEffect(corev1.TaintEffectPreferNoSchedule), |
| 112 | + Key: "capz-e2e-2", |
| 113 | + Value: "test2", |
| 114 | + }, |
| 115 | + } |
| 116 | + Eventually(func(g Gomega) { |
| 117 | + g.Expect(mgmtClient.Get(ctx, client.ObjectKeyFromObject(ammp), ammp)).To(Succeed()) |
| 118 | + ammp.Spec.Taints = expectedTaints |
| 119 | + g.Expect(mgmtClient.Update(ctx, ammp)).To(Succeed()) |
| 120 | + Eventually(checkTaints, input.WaitForUpdate...).Should(Succeed()) |
| 121 | + }, input.WaitForUpdate...).Should(Succeed()) |
| 122 | + |
| 123 | + Byf("Updating taints for machine pool %s", mp.Name) |
| 124 | + expectedTaints = expectedTaints[:1] |
| 125 | + Eventually(func(g Gomega) { |
| 126 | + g.Expect(mgmtClient.Get(ctx, client.ObjectKeyFromObject(ammp), ammp)).To(Succeed()) |
| 127 | + ammp.Spec.Taints = expectedTaints |
| 128 | + g.Expect(mgmtClient.Update(ctx, ammp)).To(Succeed()) |
| 129 | + }, input.WaitForUpdate...).Should(Succeed()) |
| 130 | + Eventually(checkTaints, input.WaitForUpdate...).Should(Succeed()) |
| 131 | + |
| 132 | + if initialTaints != nil { |
| 133 | + Byf("Restoring initial taints for machine pool %s", mp.Name) |
| 134 | + expectedTaints = initialTaints |
| 135 | + Eventually(func(g Gomega) { |
| 136 | + g.Expect(mgmtClient.Get(ctx, client.ObjectKeyFromObject(ammp), ammp)).To(Succeed()) |
| 137 | + ammp.Spec.Taints = expectedTaints |
| 138 | + g.Expect(mgmtClient.Update(ctx, ammp)).To(Succeed()) |
| 139 | + }, input.WaitForUpdate...).Should(Succeed()) |
| 140 | + Eventually(checkTaints, input.WaitForUpdate...).Should(Succeed()) |
| 141 | + } |
| 142 | + }(mp) |
| 143 | + } |
| 144 | + |
| 145 | + wg.Wait() |
| 146 | +} |
0 commit comments