sigs.k8s.io/cluster-api-provider-aws@v1.5.5/test/e2e/suites/gc_managed/gc_managed_test.go (about)

     1  //go:build e2e
     2  // +build e2e
     3  
     4  /*
     5  Copyright 2022 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 gc_managed //nolint:stylecheck
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  	"os"
    26  	"path/filepath"
    27  
    28  	"github.com/onsi/ginkgo"
    29  	. "github.com/onsi/gomega"
    30  	corev1 "k8s.io/api/core/v1"
    31  	"k8s.io/utils/pointer"
    32  
    33  	ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/controlplane/eks/api/v1beta1"
    34  	"sigs.k8s.io/cluster-api-provider-aws/test/e2e/shared"
    35  	ms "sigs.k8s.io/cluster-api-provider-aws/test/e2e/suites/managed"
    36  	"sigs.k8s.io/cluster-api/test/framework"
    37  	"sigs.k8s.io/cluster-api/test/framework/clusterctl"
    38  	"sigs.k8s.io/cluster-api/util"
    39  )
    40  
    41  // EKS cluster external resource gc tests.
    42  var _ = ginkgo.Describe("[managed] [gc] EKS Cluster external resource GC tests", func() {
    43  	var (
    44  		namespace   *corev1.Namespace
    45  		ctx         context.Context
    46  		specName    = "eks-extresgc"
    47  		clusterName string
    48  	)
    49  
    50  	ginkgo.It("[managed] [gc] should cleanup a cluster that has ELB/NLB load balancers", func() {
    51  		ginkgo.By("should have a valid test configuration")
    52  		Expect(e2eCtx.Environment.BootstrapClusterProxy).ToNot(BeNil(), "Invalid argument. BootstrapClusterProxy can't be nil")
    53  		Expect(e2eCtx.E2EConfig).ToNot(BeNil(), "Invalid argument. e2eConfig can't be nil when calling %s spec", specName)
    54  		Expect(e2eCtx.E2EConfig.Variables).To(HaveKey(shared.KubernetesVersion))
    55  
    56  		ctx = context.TODO()
    57  		namespace = shared.SetupSpecNamespace(ctx, specName, e2eCtx)
    58  		clusterName = fmt.Sprintf("%s-%s", specName, util.RandomString(6))
    59  
    60  		ginkgo.By("default iam role should exist")
    61  		ms.VerifyRoleExistsAndOwned(ekscontrolplanev1.DefaultEKSControlPlaneRole, clusterName, false, e2eCtx.BootstrapUserAWSSession)
    62  
    63  		ginkgo.By("should create an EKS control plane")
    64  		ms.ManagedClusterSpec(ctx, func() ms.ManagedClusterSpecInput {
    65  			return ms.ManagedClusterSpecInput{
    66  				E2EConfig:                e2eCtx.E2EConfig,
    67  				ConfigClusterFn:          defaultConfigCluster,
    68  				BootstrapClusterProxy:    e2eCtx.Environment.BootstrapClusterProxy,
    69  				AWSSession:               e2eCtx.BootstrapUserAWSSession,
    70  				Namespace:                namespace,
    71  				ClusterName:              clusterName,
    72  				Flavour:                  ms.EKSManagedPoolFlavor,
    73  				ControlPlaneMachineCount: 1, // NOTE: this cannot be zero as clusterctl returns an error
    74  				WorkerMachineCount:       1,
    75  			}
    76  		})
    77  
    78  		shared.Byf("getting cluster with name %s", clusterName)
    79  		cluster := framework.GetClusterByName(ctx, framework.GetClusterByNameInput{
    80  			Getter:    e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
    81  			Namespace: namespace.Name,
    82  			Name:      clusterName,
    83  		})
    84  		Expect(cluster).NotTo(BeNil(), "couldn't find cluster")
    85  
    86  		ginkgo.By("getting AWSManagedControlPlane")
    87  		cp := ms.GetControlPlaneByName(ctx, ms.GetControlPlaneByNameInput{
    88  			Getter:    e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
    89  			Namespace: cluster.Spec.InfrastructureRef.Namespace,
    90  			Name:      cluster.Spec.ControlPlaneRef.Name,
    91  		})
    92  
    93  		shared.Byf("Waiting for the machine pool to be running")
    94  		mp := framework.DiscoveryAndWaitForMachinePools(ctx, framework.DiscoveryAndWaitForMachinePoolsInput{
    95  			Lister:  e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
    96  			Getter:  e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
    97  			Cluster: cluster,
    98  		}, e2eCtx.E2EConfig.GetIntervals("", "wait-worker-nodes")...)
    99  		Expect(len(mp)).To(Equal(1))
   100  
   101  		workloadClusterProxy := e2eCtx.Environment.BootstrapClusterProxy.GetWorkloadCluster(ctx, cluster.Namespace, cluster.Name)
   102  		workloadYamlPath := e2eCtx.E2EConfig.GetVariable(shared.GcWorkloadPath)
   103  		shared.Byf("Installing sample workload with load balancer services: %s", workloadYamlPath)
   104  		workloadYaml, err := os.ReadFile(workloadYamlPath) //nolint:gosec
   105  		Expect(err).ShouldNot(HaveOccurred())
   106  		Expect(workloadClusterProxy.Apply(ctx, workloadYaml)).ShouldNot(HaveOccurred())
   107  
   108  		shared.Byf("Waiting for the Deployment to be available")
   109  		shared.WaitForDeploymentsAvailable(ctx, shared.WaitForDeploymentsAvailableInput{
   110  			Getter:    workloadClusterProxy.GetClient(),
   111  			Name:      "podinfo",
   112  			Namespace: "default",
   113  		}, e2eCtx.E2EConfig.GetIntervals("", "wait-deployment-ready")...)
   114  
   115  		shared.Byf("Checking we have the load balancers in AWS")
   116  		shared.WaitForLoadBalancerToExistForService(shared.WaitForLoadBalancerToExistForServiceInput{
   117  			AWSSession:       e2eCtx.BootstrapUserAWSSession,
   118  			ServiceName:      "podinfo-nlb",
   119  			ServiceNamespace: "default",
   120  			ClusterName:      cp.Spec.EKSClusterName,
   121  			Type:             shared.LoadBalancerTypeNLB,
   122  		}, e2eCtx.E2EConfig.GetIntervals("", "wait-loadbalancer-ready")...)
   123  		shared.WaitForLoadBalancerToExistForService(shared.WaitForLoadBalancerToExistForServiceInput{
   124  			AWSSession:       e2eCtx.BootstrapUserAWSSession,
   125  			ServiceName:      "podinfo-elb",
   126  			ServiceNamespace: "default",
   127  			ClusterName:      cp.Spec.EKSClusterName,
   128  			Type:             shared.LoadBalancerTypeELB,
   129  		}, e2eCtx.E2EConfig.GetIntervals("", "wait-loadbalancer-ready")...)
   130  
   131  		shared.Byf("Deleting workload/tenant cluster %s", clusterName)
   132  		framework.DeleteCluster(ctx, framework.DeleteClusterInput{
   133  			Deleter: e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
   134  			Cluster: cluster,
   135  		})
   136  		framework.WaitForClusterDeleted(ctx, framework.WaitForClusterDeletedInput{
   137  			Getter:  e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
   138  			Cluster: cluster,
   139  		}, e2eCtx.E2EConfig.GetIntervals("", "wait-delete-cluster")...)
   140  
   141  		shared.Byf("Getting counts of service load balancers")
   142  		arns, err := shared.GetLoadBalancerARNs(shared.GetLoadBalancerARNsInput{
   143  			AWSSession:       e2eCtx.BootstrapUserAWSSession,
   144  			ServiceName:      "podinfo-nlb",
   145  			ServiceNamespace: "default",
   146  			ClusterName:      cp.Spec.EKSClusterName,
   147  			Type:             shared.LoadBalancerTypeNLB,
   148  		})
   149  		Expect(err).NotTo(HaveOccurred())
   150  		Expect(arns).To(HaveLen(0), "there are %d service load balancers (nlb) still", len(arns))
   151  		arns, err = shared.GetLoadBalancerARNs(shared.GetLoadBalancerARNsInput{
   152  			AWSSession:       e2eCtx.BootstrapUserAWSSession,
   153  			ServiceName:      "podinfo-elb",
   154  			ServiceNamespace: "default",
   155  			ClusterName:      cp.Spec.EKSClusterName,
   156  			Type:             shared.LoadBalancerTypeELB,
   157  		})
   158  		Expect(err).NotTo(HaveOccurred())
   159  		Expect(arns).To(HaveLen(0), "there are %d service load balancers (elb) still", len(arns))
   160  	})
   161  })
   162  
   163  // TODO (richardcase): remove this when we merge these tests with the main eks e2e tests.
   164  func defaultConfigCluster(clusterName, namespace string) clusterctl.ConfigClusterInput {
   165  	return clusterctl.ConfigClusterInput{
   166  		LogFolder:                filepath.Join(e2eCtx.Settings.ArtifactFolder, "clusters", e2eCtx.Environment.BootstrapClusterProxy.GetName()),
   167  		ClusterctlConfigPath:     e2eCtx.Environment.ClusterctlConfigPath,
   168  		KubeconfigPath:           e2eCtx.Environment.BootstrapClusterProxy.GetKubeconfigPath(),
   169  		InfrastructureProvider:   "aws",
   170  		Flavor:                   ms.EKSManagedPoolFlavor,
   171  		Namespace:                namespace,
   172  		ClusterName:              clusterName,
   173  		KubernetesVersion:        e2eCtx.E2EConfig.GetVariable(shared.KubernetesVersion),
   174  		ControlPlaneMachineCount: pointer.Int64Ptr(1),
   175  		WorkerMachineCount:       pointer.Int64Ptr(0),
   176  	}
   177  }