sigs.k8s.io/cluster-api-provider-aws@v1.5.5/test/e2e/suites/gc_unmanaged/gc_unmanaged_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_unmanaged //nolint:stylecheck
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  	"os"
    26  	"path/filepath"
    27  
    28  	"github.com/gofrs/flock"
    29  	"github.com/onsi/ginkgo"
    30  	"github.com/onsi/ginkgo/config"
    31  	. "github.com/onsi/gomega"
    32  	"k8s.io/utils/pointer"
    33  
    34  	"sigs.k8s.io/cluster-api-provider-aws/test/e2e/shared"
    35  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    36  	controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
    37  	"sigs.k8s.io/cluster-api/test/framework"
    38  	"sigs.k8s.io/cluster-api/test/framework/clusterctl"
    39  	"sigs.k8s.io/cluster-api/util"
    40  )
    41  
    42  var _ = ginkgo.Context("[unmanaged] [gc]", func() {
    43  	var (
    44  		ctx               context.Context
    45  		result            *clusterctl.ApplyClusterTemplateAndWaitResult
    46  		requiredResources *shared.TestResource
    47  	)
    48  
    49  	ginkgo.It("[unmanaged] [gc] should cleanup a cluster that has ELB/NLB load balancers", func() {
    50  		ginkgo.By("should have a valid test configuration")
    51  		specName := "unmanaged-gc-cluster"
    52  
    53  		ctx = context.TODO()
    54  		result = &clusterctl.ApplyClusterTemplateAndWaitResult{}
    55  
    56  		Expect(e2eCtx.Environment.BootstrapClusterProxy).ToNot(BeNil(), "Invalid argument. BootstrapClusterProxy can't be nil")
    57  		Expect(e2eCtx.E2EConfig).ToNot(BeNil(), "Invalid argument. e2eConfig can't be nil when calling %s spec", specName)
    58  		Expect(e2eCtx.E2EConfig.Variables).To(HaveKey(shared.KubernetesVersion))
    59  
    60  		requiredResources = &shared.TestResource{EC2Normal: 2 * e2eCtx.Settings.InstanceVCPU, IGW: 1, NGW: 1, VPC: 1, ClassicLB: 1, EIP: 1}
    61  		requiredResources.WriteRequestedResources(e2eCtx, specName)
    62  		Expect(shared.AcquireResources(requiredResources, config.GinkgoConfig.ParallelNode, flock.New(shared.ResourceQuotaFilePath))).To(Succeed())
    63  		defer shared.ReleaseResources(requiredResources, config.GinkgoConfig.ParallelNode, flock.New(shared.ResourceQuotaFilePath))
    64  		namespace := shared.SetupNamespace(ctx, specName, e2eCtx)
    65  		defer shared.DumpSpecResourcesAndCleanup(ctx, "", namespace, e2eCtx)
    66  		ginkgo.By("Creating cluster with single control plane")
    67  		clusterName := fmt.Sprintf("%s-%s", specName, util.RandomString(6))
    68  
    69  		configCluster := defaultConfigCluster(clusterName, namespace.Name)
    70  		configCluster.KubernetesVersion = e2eCtx.E2EConfig.GetVariable(shared.PreCSIKubernetesVer)
    71  		configCluster.WorkerMachineCount = pointer.Int64Ptr(1)
    72  		createCluster(ctx, configCluster, result)
    73  
    74  		shared.Byf("getting cluster with name %s", clusterName)
    75  		cluster := framework.GetClusterByName(ctx, framework.GetClusterByNameInput{
    76  			Getter:    e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
    77  			Namespace: namespace.Name,
    78  			Name:      clusterName,
    79  		})
    80  		Expect(cluster).NotTo(BeNil(), "couldn't find cluster")
    81  
    82  		workloadClusterProxy := e2eCtx.Environment.BootstrapClusterProxy.GetWorkloadCluster(ctx, cluster.Namespace, cluster.Name)
    83  		workloadYamlPath := e2eCtx.E2EConfig.GetVariable(shared.GcWorkloadPath)
    84  		shared.Byf("Installing sample workload with load balancer services: %s", workloadYamlPath)
    85  		workloadYaml, err := os.ReadFile(workloadYamlPath) //nolint:gosec
    86  		Expect(err).ShouldNot(HaveOccurred())
    87  		Expect(workloadClusterProxy.Apply(ctx, workloadYaml)).ShouldNot(HaveOccurred())
    88  
    89  		shared.Byf("Waiting for the Deployment to be available")
    90  		shared.WaitForDeploymentsAvailable(ctx, shared.WaitForDeploymentsAvailableInput{
    91  			Getter:    workloadClusterProxy.GetClient(),
    92  			Name:      "podinfo",
    93  			Namespace: "default",
    94  		}, e2eCtx.E2EConfig.GetIntervals("", "wait-deployment-ready")...)
    95  
    96  		shared.Byf("Checking we have the load balancers in AWS")
    97  		shared.WaitForLoadBalancerToExistForService(shared.WaitForLoadBalancerToExistForServiceInput{
    98  			AWSSession:       e2eCtx.BootstrapUserAWSSession,
    99  			ServiceName:      "podinfo-nlb",
   100  			ServiceNamespace: "default",
   101  			ClusterName:      clusterName,
   102  			Type:             shared.LoadBalancerTypeNLB,
   103  		}, e2eCtx.E2EConfig.GetIntervals("", "wait-loadbalancer-ready")...)
   104  		shared.WaitForLoadBalancerToExistForService(shared.WaitForLoadBalancerToExistForServiceInput{
   105  			AWSSession:       e2eCtx.BootstrapUserAWSSession,
   106  			ServiceName:      "podinfo-elb",
   107  			ServiceNamespace: "default",
   108  			ClusterName:      clusterName,
   109  			Type:             shared.LoadBalancerTypeELB,
   110  		}, e2eCtx.E2EConfig.GetIntervals("", "wait-loadbalancer-ready")...)
   111  
   112  		shared.Byf("Deleting workload/tenant cluster %s", clusterName)
   113  		framework.DeleteCluster(ctx, framework.DeleteClusterInput{
   114  			Deleter: e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
   115  			Cluster: cluster,
   116  		})
   117  		framework.WaitForClusterDeleted(ctx, framework.WaitForClusterDeletedInput{
   118  			Getter:  e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
   119  			Cluster: cluster,
   120  		}, e2eCtx.E2EConfig.GetIntervals("", "wait-delete-cluster")...)
   121  
   122  		shared.Byf("Getting counts of service load balancers")
   123  		arns, err := shared.GetLoadBalancerARNs(shared.GetLoadBalancerARNsInput{
   124  			AWSSession:       e2eCtx.BootstrapUserAWSSession,
   125  			ServiceName:      "podinfo-nlb",
   126  			ServiceNamespace: "default",
   127  			ClusterName:      clusterName,
   128  			Type:             shared.LoadBalancerTypeNLB,
   129  		})
   130  		Expect(err).NotTo(HaveOccurred())
   131  		Expect(arns).To(HaveLen(0), "there are %d service load balancers (nlb) still", len(arns))
   132  		arns, err = shared.GetLoadBalancerARNs(shared.GetLoadBalancerARNsInput{
   133  			AWSSession:       e2eCtx.BootstrapUserAWSSession,
   134  			ServiceName:      "podinfo-elb",
   135  			ServiceNamespace: "default",
   136  			ClusterName:      clusterName,
   137  			Type:             shared.LoadBalancerTypeELB,
   138  		})
   139  		Expect(err).NotTo(HaveOccurred())
   140  		Expect(arns).To(HaveLen(0), "there are %d service load balancers (elb) still", len(arns))
   141  	})
   142  })
   143  
   144  // TODO (richardcase): remove this when we merge these tests with the main eks e2e tests.
   145  func defaultConfigCluster(clusterName, namespace string) clusterctl.ConfigClusterInput {
   146  	return clusterctl.ConfigClusterInput{
   147  		LogFolder:                filepath.Join(e2eCtx.Settings.ArtifactFolder, "clusters", e2eCtx.Environment.BootstrapClusterProxy.GetName()),
   148  		ClusterctlConfigPath:     e2eCtx.Environment.ClusterctlConfigPath,
   149  		KubeconfigPath:           e2eCtx.Environment.BootstrapClusterProxy.GetKubeconfigPath(),
   150  		InfrastructureProvider:   clusterctl.DefaultInfrastructureProvider,
   151  		Flavor:                   clusterctl.DefaultFlavor,
   152  		Namespace:                namespace,
   153  		ClusterName:              clusterName,
   154  		KubernetesVersion:        e2eCtx.E2EConfig.GetVariable(shared.KubernetesVersion),
   155  		ControlPlaneMachineCount: pointer.Int64Ptr(1),
   156  		WorkerMachineCount:       pointer.Int64Ptr(0),
   157  	}
   158  }
   159  
   160  // TODO (richardcase): remove this when we merge these tests with the main eks e2e tests.
   161  func createCluster(ctx context.Context, configCluster clusterctl.ConfigClusterInput, result *clusterctl.ApplyClusterTemplateAndWaitResult) (*clusterv1.Cluster, []*clusterv1.MachineDeployment, *controlplanev1.KubeadmControlPlane) {
   162  	clusterctl.ApplyClusterTemplateAndWait(ctx, clusterctl.ApplyClusterTemplateAndWaitInput{
   163  		ClusterProxy:                 e2eCtx.Environment.BootstrapClusterProxy,
   164  		ConfigCluster:                configCluster,
   165  		WaitForClusterIntervals:      e2eCtx.E2EConfig.GetIntervals("", "wait-cluster"),
   166  		WaitForControlPlaneIntervals: e2eCtx.E2EConfig.GetIntervals("", "wait-control-plane"),
   167  		WaitForMachineDeployments:    e2eCtx.E2EConfig.GetIntervals("", "wait-worker-nodes"),
   168  	}, result)
   169  
   170  	return result.Cluster, result.MachineDeployments, result.ControlPlane
   171  }