|
| 1 | +//go:build e2e |
| 2 | +// +build e2e |
| 3 | + |
| 4 | +/* |
| 5 | +Copyright 2025 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 managed |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "fmt" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 28 | + ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types" |
| 29 | + "github.com/onsi/ginkgo/v2" |
| 30 | + . "github.com/onsi/gomega" |
| 31 | + corev1 "k8s.io/api/core/v1" |
| 32 | + |
| 33 | + ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/eks/api/v1beta2" |
| 34 | + "sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/awserrors" |
| 35 | + "sigs.k8s.io/cluster-api-provider-aws/v2/test/e2e/shared" |
| 36 | + "sigs.k8s.io/cluster-api/test/framework" |
| 37 | + "sigs.k8s.io/cluster-api/util" |
| 38 | +) |
| 39 | + |
| 40 | +// EKS scaling config test. |
| 41 | +var _ = ginkgo.Describe("EKS scaling config test", func() { |
| 42 | + var ( |
| 43 | + namespace *corev1.Namespace |
| 44 | + ctx context.Context |
| 45 | + specName = "cluster" |
| 46 | + clusterName string |
| 47 | + ) |
| 48 | + |
| 49 | + ginkgo.It("[managed] [scaling-config] Able to create cluster with scaling tier and update from standard to tier-xl", func() { |
| 50 | + ginkgo.By("should have a valid test configuration") |
| 51 | + Expect(e2eCtx.Environment.BootstrapClusterProxy).ToNot(BeNil(), "Invalid argument. BootstrapClusterProxy can't be nil") |
| 52 | + Expect(e2eCtx.E2EConfig).ToNot(BeNil(), "Invalid argument. e2eConfig can't be nil when calling %s spec", specName) |
| 53 | + |
| 54 | + scalingTier := ekscontrolplanev1.ControlPlaneScalingTierStandard |
| 55 | + shared.SetEnvVar(shared.ScalingTier, string(scalingTier), false) |
| 56 | + |
| 57 | + ctx = context.TODO() |
| 58 | + namespace = shared.SetupSpecNamespace(ctx, specName, e2eCtx) |
| 59 | + clusterName = fmt.Sprintf("%s-%s", specName, util.RandomString(6)) |
| 60 | + eksClusterName := getEKSClusterName(namespace.Name, clusterName) |
| 61 | + |
| 62 | + ginkgo.By("default iam role should exist") |
| 63 | + VerifyRoleExistsAndOwned(ctx, ekscontrolplanev1.DefaultEKSControlPlaneRole, eksClusterName, false, e2eCtx.AWSSession) |
| 64 | + |
| 65 | + getManagedClusterSpec := func() ManagedClusterSpecInput { |
| 66 | + return ManagedClusterSpecInput{ |
| 67 | + E2EConfig: e2eCtx.E2EConfig, |
| 68 | + ConfigClusterFn: defaultConfigCluster, |
| 69 | + BootstrapClusterProxy: e2eCtx.Environment.BootstrapClusterProxy, |
| 70 | + AWSSession: e2eCtx.BootstrapUserAWSSession, |
| 71 | + Namespace: namespace, |
| 72 | + ClusterName: clusterName, |
| 73 | + Flavour: EKSScalingConfigFlavor, |
| 74 | + ControlPlaneMachineCount: 1, // NOTE: this cannot be zero as clusterctl returns an error |
| 75 | + WorkerMachineCount: 0, |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + ginkgo.By("should create an EKS control plane with standard scaling tier") |
| 80 | + ManagedClusterSpec(ctx, getManagedClusterSpec) |
| 81 | + |
| 82 | + ginkgo.By(fmt.Sprintf("getting cluster with name %s", clusterName)) |
| 83 | + cluster := framework.GetClusterByName(ctx, framework.GetClusterByNameInput{ |
| 84 | + Getter: e2eCtx.Environment.BootstrapClusterProxy.GetClient(), |
| 85 | + Namespace: namespace.Name, |
| 86 | + Name: clusterName, |
| 87 | + }) |
| 88 | + Expect(cluster).NotTo(BeNil(), "couldn't find cluster") |
| 89 | + |
| 90 | + WaitForEKSClusterScalingTier(ctx, e2eCtx.BootstrapUserAWSSession, eksClusterName, scalingTier) |
| 91 | + |
| 92 | + changedScalingTier := ekscontrolplanev1.ControlPlaneScalingTierXL |
| 93 | + ginkgo.By(fmt.Sprintf("Changing the scaling tier from %s to %s", scalingTier, changedScalingTier)) |
| 94 | + shared.SetEnvVar(shared.ScalingTier, string(changedScalingTier), false) |
| 95 | + ManagedClusterSpec(ctx, getManagedClusterSpec) |
| 96 | + WaitForEKSClusterScalingTier(ctx, e2eCtx.BootstrapUserAWSSession, eksClusterName, changedScalingTier) |
| 97 | + |
| 98 | + framework.DeleteCluster(ctx, framework.DeleteClusterInput{ |
| 99 | + Deleter: e2eCtx.Environment.BootstrapClusterProxy.GetClient(), |
| 100 | + Cluster: cluster, |
| 101 | + }) |
| 102 | + framework.WaitForClusterDeleted(ctx, framework.WaitForClusterDeletedInput{ |
| 103 | + ClusterProxy: e2eCtx.Environment.BootstrapClusterProxy, |
| 104 | + Cluster: cluster, |
| 105 | + ClusterctlConfigPath: e2eCtx.Environment.ClusterctlConfigPath, |
| 106 | + ArtifactFolder: e2eCtx.Settings.ArtifactFolder, |
| 107 | + }, e2eCtx.E2EConfig.GetIntervals("", "wait-delete-cluster")...) |
| 108 | + }) |
| 109 | +}) |
| 110 | + |
| 111 | +// WaitForEKSClusterScalingTier waits for the EKS cluster to have the expected scaling tier. |
| 112 | +func WaitForEKSClusterScalingTier(ctx context.Context, sess *aws.Config, eksClusterName string, scalingTier ekscontrolplanev1.ControlPlaneScalingTier) { |
| 113 | + ginkgo.By(fmt.Sprintf("Checking EKS control plane scaling tier matches %s", scalingTier)) |
| 114 | + Eventually(func() error { |
| 115 | + cluster, err := getEKSCluster(ctx, eksClusterName, sess) |
| 116 | + if err != nil { |
| 117 | + smithyErr := awserrors.ParseSmithyError(err) |
| 118 | + notFoundErr := &ekstypes.ResourceNotFoundException{} |
| 119 | + if smithyErr.ErrorCode() == notFoundErr.ErrorCode() { |
| 120 | + // Unrecoverable error stop trying and fail early. |
| 121 | + return StopTrying(fmt.Sprintf("unrecoverable error: cluster %q not found: %s", eksClusterName, smithyErr.ErrorMessage())) |
| 122 | + } |
| 123 | + return err // For transient errors, retry |
| 124 | + } |
| 125 | + |
| 126 | + // Check if scaling config exists |
| 127 | + if cluster.ControlPlaneScalingConfig == nil { |
| 128 | + // If no scaling config, the cluster is in standard mode |
| 129 | + if scalingTier == ekscontrolplanev1.ControlPlaneScalingTierStandard { |
| 130 | + return nil |
| 131 | + } |
| 132 | + return fmt.Errorf("scaling config is nil, but expected tier %s", scalingTier) |
| 133 | + } |
| 134 | + |
| 135 | + expectedTier := ekstypes.ProvisionedControlPlaneTier(scalingTier) |
| 136 | + actualTier := cluster.ControlPlaneScalingConfig.Tier |
| 137 | + |
| 138 | + if actualTier != expectedTier { |
| 139 | + // The scaling tier change hasn't been reflected in EKS yet, error and try again. |
| 140 | + return fmt.Errorf("scaling tier mismatch: expected %s, but found %s", expectedTier, actualTier) |
| 141 | + } |
| 142 | + |
| 143 | + // Success in finding the change has been reflected in EKS. |
| 144 | + return nil |
| 145 | + }, 5*time.Minute, 10*time.Second).Should(Succeed(), fmt.Sprintf("eventually failed checking EKS Cluster %q scaling tier is %s", eksClusterName, scalingTier)) |
| 146 | +} |
| 147 | + |
0 commit comments