sigs.k8s.io/cluster-api-provider-aws@v1.5.5/test/e2e/suites/managed/helpers.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  
    26  	"github.com/aws/aws-sdk-go/aws"
    27  	"github.com/aws/aws-sdk-go/aws/client"
    28  	"github.com/aws/aws-sdk-go/service/eks"
    29  	"github.com/aws/aws-sdk-go/service/iam"
    30  	. "github.com/onsi/gomega"
    31  	corev1 "k8s.io/api/core/v1"
    32  	apimachinerytypes "k8s.io/apimachinery/pkg/types"
    33  	crclient "sigs.k8s.io/controller-runtime/pkg/client"
    34  
    35  	infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
    36  	"sigs.k8s.io/cluster-api/test/framework/clusterctl"
    37  )
    38  
    39  // EKS related constants.
    40  const (
    41  	EKSManagedPoolFlavor               = "eks-managedmachinepool"
    42  	EKSControlPlaneOnlyFlavor          = "eks-control-plane-only"
    43  	EKSControlPlaneOnlyWithAddonFlavor = "eks-control-plane-only-withaddon"
    44  	EKSMachineDeployOnlyFlavor         = "eks-machine-deployment-only"
    45  	EKSManagedPoolOnlyFlavor           = "eks-managed-machinepool-only"
    46  )
    47  
    48  type DefaultConfigClusterFn func(clusterName, namespace string) clusterctl.ConfigClusterInput
    49  
    50  func getEKSClusterName(namespace, clusterName string) string {
    51  	return fmt.Sprintf("%s_%s-control-plane", namespace, clusterName)
    52  }
    53  
    54  func getEKSNodegroupName(namespace, clusterName string) string {
    55  	return fmt.Sprintf("%s_%s-pool-0", namespace, clusterName)
    56  }
    57  
    58  func getControlPlaneName(clusterName string) string {
    59  	return fmt.Sprintf("%s-control-plane", clusterName)
    60  }
    61  
    62  func verifyClusterActiveAndOwned(eksClusterName, clusterName string, sess client.ConfigProvider) {
    63  	cluster, err := getEKSCluster(eksClusterName, sess)
    64  	Expect(err).NotTo(HaveOccurred())
    65  
    66  	tagName := infrav1.ClusterTagKey(clusterName)
    67  	tagValue, ok := cluster.Tags[tagName]
    68  	Expect(ok).To(BeTrue(), "expecting the cluster owned tag to exist")
    69  	Expect(*tagValue).To(BeEquivalentTo(string(infrav1.ResourceLifecycleOwned)))
    70  
    71  	Expect(*cluster.Status).To(BeEquivalentTo(eks.ClusterStatusActive))
    72  }
    73  
    74  func getEKSCluster(eksClusterName string, sess client.ConfigProvider) (*eks.Cluster, error) {
    75  	eksClient := eks.New(sess)
    76  	input := &eks.DescribeClusterInput{
    77  		Name: aws.String(eksClusterName),
    78  	}
    79  	result, err := eksClient.DescribeCluster(input)
    80  
    81  	return result.Cluster, err
    82  }
    83  
    84  func getEKSClusterAddon(eksClusterName, addonName string, sess client.ConfigProvider) (*eks.Addon, error) {
    85  	eksClient := eks.New(sess)
    86  
    87  	describeInput := &eks.DescribeAddonInput{
    88  		AddonName:   &addonName,
    89  		ClusterName: &eksClusterName,
    90  	}
    91  	describeOutput, err := eksClient.DescribeAddon(describeInput)
    92  	if err != nil {
    93  		return nil, fmt.Errorf("describing eks addon %s: %w", addonName, err)
    94  	}
    95  
    96  	return describeOutput.Addon, nil
    97  }
    98  
    99  func verifySecretExists(ctx context.Context, secretName, namespace string, k8sclient crclient.Client) {
   100  	secret := &corev1.Secret{}
   101  	err := k8sclient.Get(ctx, apimachinerytypes.NamespacedName{Name: secretName, Namespace: namespace}, secret)
   102  
   103  	Expect(err).ShouldNot(HaveOccurred())
   104  }
   105  
   106  func verifyConfigMapExists(ctx context.Context, name, namespace string, k8sclient crclient.Client) {
   107  	cm := &corev1.ConfigMap{}
   108  	err := k8sclient.Get(ctx, apimachinerytypes.NamespacedName{Name: name, Namespace: namespace}, cm)
   109  
   110  	Expect(err).ShouldNot(HaveOccurred())
   111  }
   112  
   113  func VerifyRoleExistsAndOwned(roleName string, clusterName string, checkOwned bool, sess client.ConfigProvider) {
   114  	iamClient := iam.New(sess)
   115  	input := &iam.GetRoleInput{
   116  		RoleName: aws.String(roleName),
   117  	}
   118  
   119  	output, err := iamClient.GetRole(input)
   120  	Expect(err).ShouldNot(HaveOccurred())
   121  
   122  	if checkOwned {
   123  		found := false
   124  		expectedTagName := infrav1.ClusterAWSCloudProviderTagKey(clusterName)
   125  		for _, tag := range output.Role.Tags {
   126  			if *tag.Key == expectedTagName && *tag.Value == string(infrav1.ResourceLifecycleOwned) {
   127  				found = true
   128  				break
   129  			}
   130  		}
   131  		Expect(found).To(BeTrue(), "expecting the cluster owned tag to exist")
   132  	}
   133  }
   134  
   135  func verifyManagedNodeGroup(eksClusterName, nodeGroupName string, checkOwned bool, sess client.ConfigProvider) {
   136  	eksClient := eks.New(sess)
   137  	input := &eks.DescribeNodegroupInput{
   138  		ClusterName:   aws.String(eksClusterName),
   139  		NodegroupName: aws.String(nodeGroupName),
   140  	}
   141  	result, err := eksClient.DescribeNodegroup(input)
   142  	Expect(err).NotTo(HaveOccurred())
   143  	Expect(*result.Nodegroup.Status).To(BeEquivalentTo(eks.NodegroupStatusActive))
   144  
   145  	if checkOwned {
   146  		tagName := infrav1.ClusterAWSCloudProviderTagKey(eksClusterName)
   147  		tagValue, ok := result.Nodegroup.Tags[tagName]
   148  		Expect(ok).To(BeTrue(), "expecting the cluster owned tag to exist")
   149  		Expect(*tagValue).To(BeEquivalentTo(string(infrav1.ResourceLifecycleOwned)))
   150  	}
   151  }