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

     1  //go:build e2e
     2  // +build e2e
     3  
     4  /*
     5  Copyright 2020 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/aws/client"
    28  	"github.com/onsi/ginkgo"
    29  	. "github.com/onsi/gomega"
    30  	corev1 "k8s.io/api/core/v1"
    31  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    32  	"k8s.io/utils/pointer"
    33  
    34  	ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/controlplane/eks/api/v1beta1"
    35  	"sigs.k8s.io/cluster-api-provider-aws/test/e2e/shared"
    36  	"sigs.k8s.io/cluster-api/test/framework"
    37  	"sigs.k8s.io/cluster-api/test/framework/clusterctl"
    38  )
    39  
    40  // ManagedClusterSpecInput is the input for ManagedClusterSpec.
    41  type ManagedClusterSpecInput struct {
    42  	E2EConfig                *clusterctl.E2EConfig
    43  	ConfigClusterFn          DefaultConfigClusterFn
    44  	BootstrapClusterProxy    framework.ClusterProxy
    45  	AWSSession               client.ConfigProvider
    46  	Namespace                *corev1.Namespace
    47  	ClusterName              string
    48  	Flavour                  string
    49  	ControlPlaneMachineCount int64
    50  	WorkerMachineCount       int64
    51  	KubernetesVersion        string
    52  	CluserSpecificRoles      bool
    53  }
    54  
    55  // ManagedClusterSpec implements a test for creating a managed cluster using CAPA.
    56  func ManagedClusterSpec(ctx context.Context, inputGetter func() ManagedClusterSpecInput) {
    57  	input := inputGetter()
    58  	Expect(input.E2EConfig).ToNot(BeNil(), "Invalid argument. input.E2EConfig can't be nil")
    59  	Expect(input.ConfigClusterFn).ToNot(BeNil(), "Invalid argument. input.ConfigClusterFn can't be nil")
    60  	Expect(input.BootstrapClusterProxy).ToNot(BeNil(), "Invalid argument. input.BootstrapClusterProxy can't be nil")
    61  	Expect(input.AWSSession).ToNot(BeNil(), "Invalid argument. input.AWSSession can't be nil")
    62  	Expect(input.Namespace).NotTo(BeNil(), "Invalid argument. input.Namespace can't be nil")
    63  	Expect(input.ClusterName).ShouldNot(HaveLen(0), "Invalid argument. input.ClusterName can't be empty")
    64  
    65  	shared.Byf("creating an applying the %s template", input.Flavour)
    66  	configCluster := input.ConfigClusterFn(input.ClusterName, input.Namespace.Name)
    67  	configCluster.Flavor = input.Flavour
    68  	configCluster.ControlPlaneMachineCount = pointer.Int64Ptr(input.ControlPlaneMachineCount)
    69  	configCluster.WorkerMachineCount = pointer.Int64Ptr(input.WorkerMachineCount)
    70  	if input.KubernetesVersion != "" {
    71  		configCluster.KubernetesVersion = input.KubernetesVersion
    72  	}
    73  	err := shared.ApplyTemplate(ctx, configCluster, input.BootstrapClusterProxy)
    74  	Expect(err).ShouldNot(HaveOccurred())
    75  
    76  	shared.Byf("Waiting for the cluster to be provisioned")
    77  	cluster := framework.DiscoveryAndWaitForCluster(ctx, framework.DiscoveryAndWaitForClusterInput{
    78  		Getter:    input.BootstrapClusterProxy.GetClient(),
    79  		Namespace: configCluster.Namespace,
    80  		Name:      configCluster.ClusterName,
    81  	}, input.E2EConfig.GetIntervals("", "wait-cluster")...)
    82  	Expect(cluster).NotTo(BeNil())
    83  
    84  	shared.Byf("Checking EKS cluster is active")
    85  	eksClusterName := getEKSClusterName(input.Namespace.Name, input.ClusterName)
    86  	verifyClusterActiveAndOwned(eksClusterName, input.ClusterName, input.AWSSession)
    87  
    88  	if input.CluserSpecificRoles {
    89  		ginkgo.By("Checking that the cluster specific IAM role exists")
    90  		VerifyRoleExistsAndOwned(fmt.Sprintf("%s-iam-service-role", input.ClusterName), input.ClusterName, true, input.AWSSession)
    91  	} else {
    92  		ginkgo.By("Checking that the cluster default IAM role exists")
    93  		VerifyRoleExistsAndOwned(ekscontrolplanev1.DefaultEKSControlPlaneRole, input.ClusterName, false, input.AWSSession)
    94  	}
    95  
    96  	shared.Byf("Checking kubeconfig secrets exist")
    97  	bootstrapClient := input.BootstrapClusterProxy.GetClient()
    98  	verifySecretExists(ctx, fmt.Sprintf("%s-kubeconfig", input.ClusterName), input.Namespace.Name, bootstrapClient)
    99  	verifySecretExists(ctx, fmt.Sprintf("%s-user-kubeconfig", input.ClusterName), input.Namespace.Name, bootstrapClient)
   100  
   101  	time.Sleep(2 * time.Minute) //TODO: replace with an eventually on the aws-iam-auth check
   102  
   103  	shared.Byf("Checking that aws-iam-authenticator config map exists")
   104  	workloadClusterProxy := input.BootstrapClusterProxy.GetWorkloadCluster(ctx, input.Namespace.Name, input.ClusterName)
   105  	workloadClient := workloadClusterProxy.GetClient()
   106  	verifyConfigMapExists(ctx, "aws-auth", metav1.NamespaceSystem, workloadClient)
   107  }
   108  
   109  // DeleteClusterSpecInput is the input to DeleteClusterSpec.
   110  type DeleteClusterSpecInput struct {
   111  	E2EConfig             *clusterctl.E2EConfig
   112  	BootstrapClusterProxy framework.ClusterProxy
   113  	Namespace             *corev1.Namespace
   114  	ClusterName           string
   115  }