sigs.k8s.io/cluster-api-provider-aws@v1.5.5/test/e2e/shared/defaults.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 shared
    21  
    22  import (
    23  	"flag"
    24  	"strings"
    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/iam"
    29  	"k8s.io/apimachinery/pkg/runtime"
    30  	cgscheme "k8s.io/client-go/kubernetes/scheme"
    31  
    32  	infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
    33  	"sigs.k8s.io/cluster-api/test/framework"
    34  )
    35  
    36  // Constants.
    37  const (
    38  	DefaultSSHKeyPairName                = "cluster-api-provider-aws-sigs-k8s-io"
    39  	AMIPrefix                            = "capa-ami-ubuntu-18.04-"
    40  	DefaultImageLookupOrg                = "258751437250"
    41  	KubernetesVersion                    = "KUBERNETES_VERSION"
    42  	KubernetesVersionManagement          = "KUBERNETES_VERSION_MANAGEMENT"
    43  	CNIPath                              = "CNI"
    44  	CNIResources                         = "CNI_RESOURCES"
    45  	CNIAddonVersion                      = "VPC_ADDON_VERSION"
    46  	CorednsAddonVersion                  = "COREDNS_ADDON_VERSION"
    47  	GcWorkloadPath                       = "GC_WORKLOAD"
    48  	AwsNodeMachineType                   = "AWS_NODE_MACHINE_TYPE"
    49  	AwsAvailabilityZone1                 = "AWS_AVAILABILITY_ZONE_1"
    50  	AwsAvailabilityZone2                 = "AWS_AVAILABILITY_ZONE_2"
    51  	MultiAzFlavor                        = "multi-az"
    52  	LimitAzFlavor                        = "limit-az"
    53  	SpotInstancesFlavor                  = "spot-instances"
    54  	SSMFlavor                            = "ssm"
    55  	TopologyFlavor                       = "topology"
    56  	UpgradeToMain                        = "upgrade-to-main"
    57  	ExternalCloudProvider                = "external-cloud-provider"
    58  	SimpleMultitenancyFlavor             = "simple-multitenancy"
    59  	NestedMultitenancyFlavor             = "nested-multitenancy"
    60  	NestedMultitenancyClusterClassFlavor = "nested-multitenancy-clusterclass"
    61  	KCPScaleInFlavor                     = "kcp-scale-in"
    62  	IgnitionFlavor                       = "ignition"
    63  	StorageClassOutTreeZoneLabel         = "topology.ebs.csi.aws.com/zone"
    64  	GPUFlavor                            = "gpu"
    65  	InstanceVcpu                         = "AWS_MACHINE_TYPE_VCPU_USAGE"
    66  	PreCSIKubernetesVer                  = "PRE_1_23_KUBERNETES_VERSION"
    67  	PostCSIKubernetesVer                 = "POST_1_23_KUBERNETES_VERSION"
    68  	EFSSupport                           = "efs-support"
    69  )
    70  
    71  var ResourceQuotaFilePath = "/tmp/capa-e2e-resource-usage.lock"
    72  var (
    73  	MultiTenancySimpleRole = MultitenancyRole("Simple")
    74  	MultiTenancyJumpRole   = MultitenancyRole("Jump")
    75  	MultiTenancyNestedRole = MultitenancyRole("Nested")
    76  	MultiTenancyRoles      = []MultitenancyRole{MultiTenancySimpleRole, MultiTenancyJumpRole, MultiTenancyNestedRole}
    77  	roleLookupCache        = make(map[string]string)
    78  )
    79  
    80  type MultitenancyRole string
    81  
    82  func (m MultitenancyRole) EnvVarARN() string {
    83  	return "MULTI_TENANCY_" + strings.ToUpper(string(m)) + "_ROLE_ARN"
    84  }
    85  
    86  func (m MultitenancyRole) EnvVarName() string {
    87  	return "MULTI_TENANCY_" + strings.ToUpper(string(m)) + "_ROLE_NAME"
    88  }
    89  
    90  func (m MultitenancyRole) EnvVarIdentity() string {
    91  	return "MULTI_TENANCY_" + strings.ToUpper(string(m)) + "_IDENTITY_NAME"
    92  }
    93  
    94  func (m MultitenancyRole) IdentityName() string {
    95  	return strings.ToLower(m.RoleName())
    96  }
    97  
    98  func (m MultitenancyRole) RoleName() string {
    99  	return "CAPAMultiTenancy" + string(m)
   100  }
   101  
   102  func (m MultitenancyRole) SetEnvVars(prov client.ConfigProvider) error {
   103  	arn, err := m.RoleARN(prov)
   104  	if err != nil {
   105  		return err
   106  	}
   107  	SetEnvVar(m.EnvVarARN(), arn, false)
   108  	SetEnvVar(m.EnvVarName(), m.RoleName(), false)
   109  	SetEnvVar(m.EnvVarIdentity(), m.IdentityName(), false)
   110  	return nil
   111  }
   112  
   113  func (m MultitenancyRole) RoleARN(prov client.ConfigProvider) (string, error) {
   114  	if roleARN, ok := roleLookupCache[m.RoleName()]; ok {
   115  		return roleARN, nil
   116  	}
   117  	iamSvc := iam.New(prov)
   118  	role, err := iamSvc.GetRole(&iam.GetRoleInput{RoleName: aws.String(m.RoleName())})
   119  	if err != nil {
   120  		return "", err
   121  	}
   122  	roleARN := aws.StringValue(role.Role.Arn)
   123  	roleLookupCache[m.RoleName()] = roleARN
   124  	return roleARN, nil
   125  }
   126  
   127  // Service codes and quotas can be found under: https://us-west-1.console.aws.amazon.com/servicequotas/home/services
   128  func getLimitedResources() map[string]*ServiceQuota {
   129  	serviceQuotas := map[string]*ServiceQuota{}
   130  	serviceQuotas["igw"] = &ServiceQuota{
   131  		ServiceCode:         "vpc",
   132  		QuotaName:           "Internet gateways per Region",
   133  		QuotaCode:           "L-A4707A72",
   134  		DesiredMinimumValue: 20,
   135  	}
   136  
   137  	serviceQuotas["ngw"] = &ServiceQuota{
   138  		ServiceCode:         "vpc",
   139  		QuotaName:           "NAT gateways per Availability Zone",
   140  		QuotaCode:           "L-FE5A380F",
   141  		DesiredMinimumValue: 20,
   142  	}
   143  
   144  	serviceQuotas["vpc"] = &ServiceQuota{
   145  		ServiceCode:         "vpc",
   146  		QuotaName:           "VPCs per Region",
   147  		QuotaCode:           "L-F678F1CE",
   148  		DesiredMinimumValue: 20,
   149  	}
   150  
   151  	serviceQuotas["ec2-normal"] = &ServiceQuota{
   152  		ServiceCode:         "ec2",
   153  		QuotaName:           "Running On-Demand Standard (A, C, D, H, I, M, R, T, Z) instances",
   154  		QuotaCode:           "L-1216C47A",
   155  		DesiredMinimumValue: 128,
   156  	}
   157  
   158  	serviceQuotas["eip"] = &ServiceQuota{
   159  		ServiceCode:         "ec2",
   160  		QuotaName:           "EC2-VPC Elastic IPs",
   161  		QuotaCode:           "L-0263D0A3",
   162  		DesiredMinimumValue: 100,
   163  	}
   164  
   165  	serviceQuotas["classiclb"] = &ServiceQuota{
   166  		ServiceCode:         "elasticloadbalancing",
   167  		QuotaName:           "Classic Load Balancers per Region",
   168  		QuotaCode:           "L-E9E9831D",
   169  		DesiredMinimumValue: 20,
   170  	}
   171  
   172  	serviceQuotas["ec2-GPU"] = &ServiceQuota{
   173  		ServiceCode:         "ec2",
   174  		QuotaName:           "Running On-Demand G and VT instances",
   175  		QuotaCode:           "L-DB2E81BA",
   176  		DesiredMinimumValue: 8,
   177  	}
   178  
   179  	serviceQuotas["volume-GP2"] = &ServiceQuota{
   180  		ServiceCode:         "ebs",
   181  		QuotaName:           "Storage for General Purpose SSD (gp2) volumes, in TiB",
   182  		QuotaCode:           "L-D18FCD1D",
   183  		DesiredMinimumValue: 50,
   184  	}
   185  
   186  	return serviceQuotas
   187  }
   188  
   189  // DefaultScheme returns the default scheme to use for testing.
   190  func DefaultScheme() *runtime.Scheme {
   191  	sc := runtime.NewScheme()
   192  	framework.TryAddDefaultSchemes(sc)
   193  	_ = infrav1.AddToScheme(sc)
   194  	_ = cgscheme.AddToScheme(sc)
   195  	return sc
   196  }
   197  
   198  // CreateDefaultFlags will create the default flags used for the tests and binds them to the e2e context.
   199  func CreateDefaultFlags(ctx *E2EContext) {
   200  	flag.StringVar(&ctx.Settings.ConfigPath, "config-path", "", "path to the e2e config file")
   201  	flag.StringVar(&ctx.Settings.ArtifactFolder, "artifacts-folder", "", "folder where e2e test artifact should be stored")
   202  	flag.BoolVar(&ctx.Settings.UseCIArtifacts, "kubetest.use-ci-artifacts", false, "use the latest build from the main branch of the Kubernetes repository")
   203  	flag.StringVar(&ctx.Settings.KubetestConfigFilePath, "kubetest.config-file", "", "path to the kubetest configuration file")
   204  	flag.IntVar(&ctx.Settings.GinkgoNodes, "kubetest.ginkgo-nodes", 1, "number of ginkgo nodes to use")
   205  	flag.IntVar(&ctx.Settings.GinkgoSlowSpecThreshold, "kubetest.ginkgo-slowSpecThreshold", 120, "time in s before spec is marked as slow")
   206  	flag.BoolVar(&ctx.Settings.UseExistingCluster, "use-existing-cluster", false, "if true, the test uses the current cluster instead of creating a new one (default discovery rules apply)")
   207  	flag.BoolVar(&ctx.Settings.SkipCleanup, "skip-cleanup", false, "if true, the resource cleanup after tests will be skipped")
   208  	flag.BoolVar(&ctx.Settings.SkipCloudFormationDeletion, "skip-cloudformation-deletion", false, "if true, an AWS CloudFormation stack will not be deleted")
   209  	flag.BoolVar(&ctx.Settings.SkipCloudFormationCreation, "skip-cloudformation-creation", false, "if true, an AWS CloudFormation stack will not be created")
   210  	flag.StringVar(&ctx.Settings.DataFolder, "data-folder", "", "path to the data folder")
   211  	flag.StringVar(&ctx.Settings.SourceTemplate, "source-template", "infrastructure-aws/generated/cluster-template.yaml", "path to the data folder")
   212  }