From 4fc0eaed17986e1a81455245371d370b416dde87 Mon Sep 17 00:00:00 2001 From: Jason Deal Date: Mon, 16 Oct 2023 17:37:50 -0700 Subject: [PATCH] gh comments --- hack/code/prices_gen.go | 12 ++-- hack/code/vpc_limits_gen.go | 4 +- hack/docs/instancetypes_gen_docs.go | 4 +- pkg/apis/settings/settings.go | 5 +- pkg/operator/options/options.go | 48 ++++++------- pkg/operator/options/options_validation.go | 17 +---- pkg/providers/instancetype/nodeclass_test.go | 2 +- test/pkg/environment/aws/environment.go | 13 ++++ test/suites/chaos/suite_test.go | 10 +-- test/suites/consolidation/suite_test.go | 14 ++-- test/suites/drift/suite_test.go | 25 ++++--- test/suites/expiration/expiration_test.go | 5 +- test/suites/integration/ami_test.go | 71 +++++++++---------- test/suites/integration/aws_metadata_test.go | 8 +-- .../backwards_compatability_test.go | 7 +- .../integration/block_device_mappings_test.go | 8 +-- test/suites/integration/cni_test.go | 15 ++-- test/suites/integration/daemonset_test.go | 7 +- test/suites/integration/emptiness_test.go | 8 +-- .../integration/extended_resources_test.go | 25 +++---- test/suites/integration/hash_test.go | 12 ++-- .../integration/instance_profile_test.go | 4 +- .../suites/integration/kubelet_config_test.go | 18 +++-- test/suites/integration/scheduling_test.go | 5 +- .../suites/integration/security_group_test.go | 15 ++-- test/suites/integration/storage_test.go | 11 ++- test/suites/integration/subnet_test.go | 27 ++++--- test/suites/integration/tags_test.go | 19 +++-- test/suites/integration/termination_test.go | 6 +- test/suites/integration/webhook_test.go | 67 +++++++++-------- test/suites/interruption/suite_test.go | 19 +++-- test/suites/ipv6/suite_test.go | 9 ++- .../suites/machine/garbage_collection_test.go | 16 ++--- test/suites/machine/link_test.go | 36 +++++----- test/suites/machine/machine_test.go | 9 +-- test/suites/scale/deprovisioning_test.go | 5 +- test/suites/scale/provisioning_test.go | 5 +- test/suites/utilization/suite_test.go | 6 +- 38 files changed, 285 insertions(+), 312 deletions(-) diff --git a/hack/code/prices_gen.go b/hack/code/prices_gen.go index 73bd67f0fb07..413ceeb3cd6c 100644 --- a/hack/code/prices_gen.go +++ b/hack/code/prices_gen.go @@ -34,7 +34,7 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/reconcile" - awsoptions "github.com/aws/karpenter/pkg/operator/options" + "github.com/aws/karpenter/pkg/operator/options" "github.com/aws/karpenter/pkg/providers/pricing" "github.com/aws/karpenter/pkg/test" ) @@ -65,13 +65,13 @@ func getPartitionSuffix(partition string) string { } } -type Options struct { +type CLIOptions struct { partition string output string } -func NewOptions() *Options { - o := &Options{} +func NewCLIOptions() *CLIOptions { + o := &CLIOptions{} flag.StringVar(&o.partition, "partition", "aws", "The partition to generate prices for. Valid options are \"aws\", \"aws-us-gov\", and \"aws-cn\".") flag.StringVar(&o.output, "output", "pkg/providers/pricing/zz_generated.pricing_aws.go", "The destination for the generated go file.") flag.Parse() @@ -82,7 +82,7 @@ func NewOptions() *Options { } func main() { - opts := NewOptions() + opts := NewCLIOptions() f, err := os.Create("pricing.heapprofile") if err != nil { log.Fatal("could not create memory profile: ", err) @@ -93,7 +93,7 @@ func main() { os.Setenv("AWS_SDK_LOAD_CONFIG", "true") os.Setenv("AWS_REGION", region) ctx := context.Background() - ctx = awsoptions.ToContext(ctx, test.Options()) + ctx = options.ToContext(ctx, test.Options()) sess := session.Must(session.NewSession()) ec2 := ec22.New(sess) src := &bytes.Buffer{} diff --git a/hack/code/vpc_limits_gen.go b/hack/code/vpc_limits_gen.go index 773777fc567c..5baaa0460050 100644 --- a/hack/code/vpc_limits_gen.go +++ b/hack/code/vpc_limits_gen.go @@ -26,13 +26,13 @@ import ( "time" ) -type options struct { +type Options struct { sourceOutput string urlInput string } func main() { - opts := options{} + opts := Options{} flag.StringVar(&opts.urlInput, "url", "https://raw.githubusercontent.com/aws/amazon-vpc-resource-controller-k8s/master/pkg/aws/vpc/limits.go", "url of the raw vpc/limits.go file in the github.com/aws/amazon-vpc-resource-controller-k8s repo") flag.StringVar(&opts.sourceOutput, "output", "pkg/providers/instancetype/zz_generated.vpclimits.go", "output location for the generated go source file") diff --git a/hack/docs/instancetypes_gen_docs.go b/hack/docs/instancetypes_gen_docs.go index 3c924b328682..8b0265e551ea 100644 --- a/hack/docs/instancetypes_gen_docs.go +++ b/hack/docs/instancetypes_gen_docs.go @@ -42,7 +42,7 @@ import ( nodepoolutil "github.com/aws/karpenter-core/pkg/utils/nodepool" awscloudprovider "github.com/aws/karpenter/pkg/cloudprovider" "github.com/aws/karpenter/pkg/operator" - awsoptions "github.com/aws/karpenter/pkg/operator/options" + "github.com/aws/karpenter/pkg/operator/options" "github.com/aws/karpenter/pkg/test" "github.com/aws/karpenter-core/pkg/cloudprovider" @@ -78,7 +78,7 @@ func main() { lo.Must0(os.Setenv("AWS_REGION", "us-east-1")) ctx := coreoptions.ToContext(context.Background(), coretest.Options()) - ctx = awsoptions.ToContext(ctx, test.Options(test.OptionsFields{ + ctx = options.ToContext(ctx, test.Options(test.OptionsFields{ ClusterName: lo.ToPtr("docs-gen"), ClusterEndpoint: lo.ToPtr("https://docs-gen.aws"), IsolatedVPC: lo.ToPtr(true), // disable pricing lookup diff --git a/pkg/apis/settings/settings.go b/pkg/apis/settings/settings.go index 184e4770f8ce..feb3617f35bd 100644 --- a/pkg/apis/settings/settings.go +++ b/pkg/apis/settings/settings.go @@ -70,6 +70,9 @@ func (*Settings) ConfigMap() string { // Inject creates a Settings from the supplied ConfigMap func (*Settings) Inject(ctx context.Context, cm *v1.ConfigMap) (context.Context, error) { s := defaultSettings.DeepCopy() + if cm == nil { + return ToContext(ctx, s), nil + } if err := configmap.Parse(cm.Data, configmap.AsString("aws.assumeRoleARN", &s.AssumeRoleARN), @@ -105,7 +108,7 @@ func ToContext(ctx context.Context, s *Settings) context.Context { func FromContext(ctx context.Context) *Settings { data := ctx.Value(ContextKey) if data == nil { - return nil + panic("settings not in context") } return data.(*Settings) } diff --git a/pkg/operator/options/options.go b/pkg/operator/options/options.go index 86caa969fa5f..aab2bbf12ae5 100644 --- a/pkg/operator/options/options.go +++ b/pkg/operator/options/options.go @@ -99,32 +99,21 @@ func (o *Options) MergeSettings(ctx context.Context) { if s == nil { return } - if !o.setFlags["assume-role-arn"] { - o.AssumeRoleARN = s.AssumeRoleARN - } - if !o.setFlags["assume-role-duration"] { - o.AssumeRoleDuration = s.AssumeRoleDuration - } - if !o.setFlags["cluster-ca-bundle"] { - o.ClusterCABundle = s.ClusterCABundle - } - if !o.setFlags["cluster-name"] { - o.ClusterName = s.ClusterName - } - if !o.setFlags["cluster-endpoint"] { - o.ClusterEndpoint = s.ClusterEndpoint - } - if !o.setFlags["isolated-vpc"] { - o.IsolatedVPC = s.IsolatedVPC - } - if !o.setFlags["vm-memory-overhead-percent"] { - o.VMMemoryOverheadPercent = s.VMMemoryOverheadPercent - } - if !o.setFlags["interruption-queue-name"] { - o.InterruptionQueueName = s.InterruptionQueueName - } - if !o.setFlags["reserved-enis"] { - o.ReservedENIs = s.ReservedENIs + + mergeField(&o.AssumeRoleARN, s.AssumeRoleARN, o.setFlags["assume-role-arn"]) + mergeField(&o.AssumeRoleDuration, s.AssumeRoleDuration, o.setFlags["assume-role-duration"]) + mergeField(&o.ClusterCABundle, s.ClusterCABundle, o.setFlags["cluster-ca-bundle"]) + mergeField(&o.ClusterName, s.ClusterName, o.setFlags["cluster-name"]) + mergeField(&o.ClusterEndpoint, s.ClusterEndpoint, o.setFlags["cluster-endpoint"]) + mergeField(&o.IsolatedVPC, s.IsolatedVPC, o.setFlags["isolated-vpc"]) + mergeField(&o.VMMemoryOverheadPercent, s.VMMemoryOverheadPercent, o.setFlags["vm-memory-overhead-percent"]) + mergeField(&o.InterruptionQueueName, s.InterruptionQueueName, o.setFlags["interruption-queue-name"]) + mergeField(&o.ReservedENIs, s.ReservedENIs, o.setFlags["reserved-enis"]) + + // Temporarily revalidate ClusterName, since default value is invalid + o.setFlags["cluster-name"] = true + if err := o.validateClusterName(); err != nil { + panic(fmt.Errorf("validating cluster name, %w", err)) } } @@ -139,3 +128,10 @@ func FromContext(ctx context.Context) *Options { } return retval.(*Options) } + +// Note: Separated out to help with cyclomatic complexity check +func mergeField[T any](dest *T, src T, isDestSet bool) { + if !isDestSet { + *dest = src + } +} diff --git a/pkg/operator/options/options_validation.go b/pkg/operator/options/options_validation.go index 480a1fb0bea8..ea3045b0a480 100644 --- a/pkg/operator/options/options_validation.go +++ b/pkg/operator/options/options_validation.go @@ -32,12 +32,8 @@ func (o Options) Validate() (errs *apis.FieldError) { } func (o Options) validateAssumeRoleDuration() (errs *apis.FieldError) { - // TODO: Remove with karpenter-global-settings - if !o.setFlags["aws-assume-role-arn"] { - return nil - } if o.AssumeRoleDuration < time.Minute*15 { - return errs.Also(apis.ErrInvalidValue("assumeRoleDuration cannot be less than 15 Minutes", "assumeRoleDuration")) + return errs.Also(apis.ErrInvalidValue("assume-role-duration cannot be less than 15 Minutes", "assume-role-duration")) } return nil } @@ -48,29 +44,22 @@ func (o Options) validateClusterName() (errs *apis.FieldError) { return nil } if o.ClusterName == "" { - return errs.Also(apis.ErrMissingField("clusterName is required", "clusterName")) + return errs.Also(apis.ErrMissingField("cluster-name is required", "cluster-name")) } return nil } func (o Options) validateEndpoint() (errs *apis.FieldError) { - if o.ClusterEndpoint == "" { - return nil - } endpoint, err := url.Parse(o.ClusterEndpoint) // url.Parse() will accept a lot of input without error; make // sure it's a real URL if err != nil || !endpoint.IsAbs() || endpoint.Hostname() == "" { - return errs.Also(apis.ErrInvalidValue(fmt.Sprintf("%q not a valid clusterEndpoint URL", o.ClusterEndpoint), "clusterEndpoint")) + return errs.Also(apis.ErrInvalidValue(fmt.Sprintf("%q not a valid cluster-endpoint URL", o.ClusterEndpoint), "cluster-endpoint")) } return nil } func (o Options) validateVMMemoryOverheadPercent() (errs *apis.FieldError) { - // TODO: Remove with karpenter-global-settings - if !o.setFlags["aws-vm-memory-overhead-percent"] { - return nil - } if o.VMMemoryOverheadPercent < 0 { return errs.Also(apis.ErrInvalidValue("cannot be negative", "vmMemoryOverheadPercent")) } diff --git a/pkg/providers/instancetype/nodeclass_test.go b/pkg/providers/instancetype/nodeclass_test.go index 00215fbb7d7b..16cb06e0f502 100644 --- a/pkg/providers/instancetype/nodeclass_test.go +++ b/pkg/providers/instancetype/nodeclass_test.go @@ -647,7 +647,7 @@ var _ = Describe("NodeClass/InstanceTypes", func() { var info *ec2.InstanceTypeInfo BeforeEach(func() { ctx = options.ToContext(ctx, test.Options(test.OptionsFields{ - ClusterName: lo.ToPtr("CLUSTER_NAME"), + ClusterName: lo.ToPtr("karpenter-cluster"), })) var ok bool diff --git a/test/pkg/environment/aws/environment.go b/test/pkg/environment/aws/environment.go index 7961472461b2..3c4e746d1621 100644 --- a/test/pkg/environment/aws/environment.go +++ b/test/pkg/environment/aws/environment.go @@ -15,6 +15,7 @@ limitations under the License. package aws import ( + "os" "testing" "github.com/aws/aws-sdk-go/aws" @@ -90,3 +91,15 @@ func GetTimeStreamAPI(session *session.Session) timestreamwriteiface.TimestreamW } return &NoOpTimeStreamAPI{} } + +func GetClusterName() string { + return lo.Must(os.LookupEnv("CLUSTER_NAME")) +} + +func GetClusterEndpoint() string { + return lo.Must(os.LookupEnv("CLUSTER_ENDPOINT")) +} + +func GetInterruptionQueueName() string { + return lo.Must(os.LookupEnv("INTERRUPTION_QUEUE_NAME")) +} diff --git a/test/suites/chaos/suite_test.go b/test/suites/chaos/suite_test.go index 471151070fbd..acec83edcdb1 100644 --- a/test/suites/chaos/suite_test.go +++ b/test/suites/chaos/suite_test.go @@ -17,7 +17,6 @@ package chaos_test import ( "context" "fmt" - "os" "sync/atomic" "testing" "time" @@ -42,6 +41,7 @@ import ( "github.com/aws/karpenter/pkg/apis/v1alpha1" awstest "github.com/aws/karpenter/pkg/test" "github.com/aws/karpenter/test/pkg/debug" + "github.com/aws/karpenter/test/pkg/environment/aws" "github.com/aws/karpenter/test/pkg/environment/common" ) @@ -69,8 +69,8 @@ var _ = Describe("Chaos", func() { defer cancel() provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ Requirements: []v1.NodeSelectorRequirement{ @@ -114,8 +114,8 @@ var _ = Describe("Chaos", func() { defer cancel() provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ Requirements: []v1.NodeSelectorRequirement{ diff --git a/test/suites/consolidation/suite_test.go b/test/suites/consolidation/suite_test.go index cfcec68c2f96..5dd06983cf15 100644 --- a/test/suites/consolidation/suite_test.go +++ b/test/suites/consolidation/suite_test.go @@ -16,13 +16,11 @@ package consolidation_test import ( "fmt" - "os" "strings" "testing" "time" "github.com/aws/aws-sdk-go/aws" - "github.com/samber/lo" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -61,8 +59,8 @@ var _ = AfterEach(func() { env.AfterEach() }) var _ = Describe("Consolidation", func() { It("should consolidate nodes (delete)", Label(debug.NoWatch), Label(debug.NoEvents), func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ Requirements: []v1.NodeSelectorRequirement{ @@ -127,8 +125,8 @@ var _ = Describe("Consolidation", func() { }) It("should consolidate on-demand nodes (replace)", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ Requirements: []v1.NodeSelectorRequirement{ @@ -243,8 +241,8 @@ var _ = Describe("Consolidation", func() { }) It("should consolidate on-demand nodes to spot (replace)", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ Requirements: []v1.NodeSelectorRequirement{ diff --git a/test/suites/drift/suite_test.go b/test/suites/drift/suite_test.go index 4605710f4762..d76e249710e2 100644 --- a/test/suites/drift/suite_test.go +++ b/test/suites/drift/suite_test.go @@ -16,7 +16,6 @@ package drift_test import ( "fmt" - "os" "sort" "strings" "testing" @@ -73,8 +72,8 @@ var _ = Describe("Drift", Label("AWS"), func() { BeforeEach(func() { customAMI = env.GetCustomAMI("/aws/service/eks/optimized-ami/%s/amazon-linux-2/recommended/image_id", 1) nodeTemplate = awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner = test.Provisioner(test.ProvisionerOptions{ Requirements: []v1.NodeSelectorRequirement{{Key: v1alpha5.LabelCapacityType, Operator: v1.NodeSelectorOpIn, Values: []string{v1alpha5.CapacityTypeOnDemand}}}, @@ -101,7 +100,7 @@ var _ = Describe("Drift", Label("AWS"), func() { oldCustomAMI := *parameter.Parameter.Value nodeTemplate.Spec.AMIFamily = &v1alpha1.AMIFamilyCustom nodeTemplate.Spec.AMISelector = map[string]string{"aws-ids": oldCustomAMI} - nodeTemplate.Spec.UserData = awssdk.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", lo.Must(os.LookupEnv("CLUSTER_NAME")))) + nodeTemplate.Spec.UserData = awssdk.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", aws.GetClusterName())) env.ExpectCreated(pod, nodeTemplate, provisioner) env.EventuallyExpectHealthy(pod) @@ -134,7 +133,7 @@ var _ = Describe("Drift", Label("AWS"), func() { oldCustomAMI := *parameter.Parameter.Value nodeTemplate.Spec.AMIFamily = &v1alpha1.AMIFamilyCustom nodeTemplate.Spec.AMISelector = map[string]string{"aws-ids": oldCustomAMI} - nodeTemplate.Spec.UserData = awssdk.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", lo.Must(os.LookupEnv("CLUSTER_NAME")))) + nodeTemplate.Spec.UserData = awssdk.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", aws.GetClusterName())) env.ExpectCreated(pod, nodeTemplate, provisioner) env.EventuallyExpectHealthy(pod) @@ -151,7 +150,7 @@ var _ = Describe("Drift", Label("AWS"), func() { }) It("should deprovision nodes that have drifted due to securitygroup", func() { By("getting the cluster vpc id") - output, err := env.EKSAPI.DescribeCluster(&eks.DescribeClusterInput{Name: awssdk.String(lo.Must(os.LookupEnv("CLUSTER_NAME")))}) + output, err := env.EKSAPI.DescribeCluster(&eks.DescribeClusterInput{Name: awssdk.String(aws.GetClusterName())}) Expect(err).To(BeNil()) By("creating new security group") @@ -165,11 +164,11 @@ var _ = Describe("Drift", Label("AWS"), func() { Tags: []*ec2.Tag{ { Key: awssdk.String("karpenter.sh/discovery"), - Value: awssdk.String(lo.Must(os.LookupEnv("CLUSTER_NAME"))), + Value: awssdk.String(aws.GetClusterName()), }, { Key: awssdk.String(test.DiscoveryLabel), - Value: awssdk.String(lo.Must(os.LookupEnv("CLUSTER_NAME"))), + Value: awssdk.String(aws.GetClusterName()), }, { Key: awssdk.String("creation-date"), @@ -185,7 +184,7 @@ var _ = Describe("Drift", Label("AWS"), func() { var securitygroups []aws.SecurityGroup var testSecurityGroup aws.SecurityGroup Eventually(func(g Gomega) { - securitygroups = env.GetSecurityGroups(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) + securitygroups = env.GetSecurityGroups(map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}) testSecurityGroup, _ = lo.Find(securitygroups, func(sg aws.SecurityGroup) bool { return awssdk.StringValue(sg.GroupName) == "security-group-drift" }) @@ -222,7 +221,7 @@ var _ = Describe("Drift", Label("AWS"), func() { env.EventuallyExpectNotFound(pod, machine, node) }) It("should deprovision nodes that have drifted due to subnets", func() { - subnets := env.GetSubnetNameAndIds(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) + subnets := env.GetSubnetNameAndIds(map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}) Expect(len(subnets)).To(BeNumerically(">", 1)) nodeTemplate.Spec.SubnetSelector = map[string]string{"aws-ids": subnets[0].ID} @@ -300,7 +299,7 @@ var _ = Describe("Drift", Label("AWS"), func() { ) DescribeTable("AWSNodeTemplate Drift", func(fieldName string, nodeTemplateSpec v1alpha1.AWSNodeTemplateSpec) { if fieldName == "InstanceProfile" { - nodeTemplateSpec.AWS.InstanceProfile = awssdk.String(fmt.Sprintf("KarpenterNodeInstanceProfile-Drift-%s", lo.Must(os.LookupEnv("CLUSTER_NAME")))) + nodeTemplateSpec.AWS.InstanceProfile = awssdk.String(fmt.Sprintf("KarpenterNodeInstanceProfile-Drift-%s", aws.GetClusterName())) ExpectInstanceProfileCreated(nodeTemplateSpec.AWS.InstanceProfile) } @@ -476,7 +475,7 @@ func ExpectInstanceProfileCreated(instanceProfileName *string) { Tags: []*iam.Tag{ { Key: awssdk.String(test.DiscoveryLabel), - Value: awssdk.String(lo.Must(os.LookupEnv("CLUSTER_NAME"))), + Value: awssdk.String(aws.GetClusterName()), }, }, } @@ -485,7 +484,7 @@ func ExpectInstanceProfileCreated(instanceProfileName *string) { Expect(ignoreAlreadyExists(err)).ToNot(HaveOccurred()) addInstanceProfile := &iam.AddRoleToInstanceProfileInput{ InstanceProfileName: instanceProfileName, - RoleName: awssdk.String(fmt.Sprintf("KarpenterNodeRole-%s", lo.Must(os.LookupEnv("CLUSTER_NAME")))), + RoleName: awssdk.String(fmt.Sprintf("KarpenterNodeRole-%s", aws.GetClusterName())), } _, err = env.IAMAPI.AddRoleToInstanceProfile(addInstanceProfile) Expect(ignoreAlreadyContainsRole(err)).ToNot(HaveOccurred()) diff --git a/test/suites/expiration/expiration_test.go b/test/suites/expiration/expiration_test.go index e1a38c21cf5d..48892bedb518 100644 --- a/test/suites/expiration/expiration_test.go +++ b/test/suites/expiration/expiration_test.go @@ -15,7 +15,6 @@ limitations under the License. package expiration_test import ( - "os" "testing" "time" @@ -67,8 +66,8 @@ var _ = Describe("Expiration", func() { var provisioner *v1alpha5.Provisioner BeforeEach(func() { nodeTemplate = awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner = test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: nodeTemplate.Name}, diff --git a/test/suites/integration/ami_test.go b/test/suites/integration/ami_test.go index 85150fb167ab..61821e9afb8e 100644 --- a/test/suites/integration/ami_test.go +++ b/test/suites/integration/ami_test.go @@ -26,7 +26,6 @@ import ( "github.com/aws/aws-sdk-go/service/ssm" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/samber/lo" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" @@ -48,8 +47,8 @@ var _ = Describe("AMI", func() { It("should use the AMI defined by the AMI Selector", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyAL2, }, AMISelector: map[string]string{"aws-ids": customAMI}, @@ -71,12 +70,12 @@ var _ = Describe("AMI", func() { Expect(err).To(BeNil()) oldCustomAMI := *parameter.Parameter.Value provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyCustom, }, AMISelector: map[string]string{"aws-ids": fmt.Sprintf("%s,%s", customAMI, oldCustomAMI)}, - UserData: aws.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", lo.Must(os.LookupEnv("CLUSTER_NAME")))), + UserData: aws.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", awsenv.GetClusterName())), }) provisioner := test.Provisioner(test.ProvisionerOptions{ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}}) pod := test.Pod() @@ -95,12 +94,12 @@ var _ = Describe("AMI", func() { Expect(output.Images).To(HaveLen(1)) provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyCustom, }, AMISelector: map[string]string{"aws::name": *output.Images[0].Name, "aws::owners": "fakeOwnerValue"}, - UserData: aws.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", lo.Must(os.LookupEnv("CLUSTER_NAME")))), + UserData: aws.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", awsenv.GetClusterName())), }) provisioner := test.Provisioner(test.ProvisionerOptions{ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}}) @@ -118,12 +117,12 @@ var _ = Describe("AMI", func() { Expect(output.Images).To(HaveLen(1)) provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyCustom, }, AMISelector: map[string]string{"aws::name": *output.Images[0].Name}, - UserData: aws.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", lo.Must(os.LookupEnv("CLUSTER_NAME")))), + UserData: aws.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", awsenv.GetClusterName())), }) provisioner := test.Provisioner(test.ProvisionerOptions{ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}}) @@ -137,12 +136,12 @@ var _ = Describe("AMI", func() { }) It("should support ami selector aws::ids", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyCustom, }, AMISelector: map[string]string{"aws::ids": customAMI}, - UserData: aws.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", lo.Must(os.LookupEnv("CLUSTER_NAME")))), + UserData: aws.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", awsenv.GetClusterName())), }) provisioner := test.Provisioner(test.ProvisionerOptions{ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}}) pod := test.Pod() @@ -157,8 +156,8 @@ var _ = Describe("AMI", func() { Context("AMIFamily", func() { It("should provision a node using the AL2 family", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, @@ -170,8 +169,8 @@ var _ = Describe("AMI", func() { }) It("should provision a node using the Bottlerocket family", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyBottlerocket, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ @@ -184,8 +183,8 @@ var _ = Describe("AMI", func() { }) It("should provision a node using the Ubuntu family", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyUbuntu, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ @@ -212,12 +211,12 @@ var _ = Describe("AMI", func() { }) It("should support Custom AMIFamily with AMI Selectors", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyCustom, }, AMISelector: map[string]string{"aws-ids": customAMI}, - UserData: aws.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", lo.Must(os.LookupEnv("CLUSTER_NAME")))), + UserData: aws.String(fmt.Sprintf("#!/bin/bash\n/etc/eks/bootstrap.sh '%s'", awsenv.GetClusterName())), }) provisioner := test.Provisioner(test.ProvisionerOptions{ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}}) pod := test.Pod() @@ -231,8 +230,8 @@ var _ = Describe("AMI", func() { It("should have the AWSNodeTemplateStatus for AMIs using wildcard", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, }, AMISelector: map[string]string{"aws::name": "*"}, }) @@ -244,8 +243,8 @@ var _ = Describe("AMI", func() { It("should have the AWSNodeTemplateStatus for AMIs using tags", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, }, AMISelector: map[string]string{"aws-ids": customAMI}, }) @@ -263,8 +262,8 @@ var _ = Describe("AMI", func() { content, err := os.ReadFile("testdata/al2_userdata_input.sh") Expect(err).ToNot(HaveOccurred()) provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyAL2, }, UserData: aws.String(string(content)), @@ -292,8 +291,8 @@ var _ = Describe("AMI", func() { content, err := os.ReadFile("testdata/al2_no_mime_userdata_input.sh") Expect(err).ToNot(HaveOccurred()) provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyAL2, }, UserData: aws.String(string(content)), @@ -321,8 +320,8 @@ var _ = Describe("AMI", func() { content, err := os.ReadFile("testdata/br_userdata_input.sh") Expect(err).ToNot(HaveOccurred()) provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyBottlerocket, }, UserData: aws.String(string(content)), @@ -353,8 +352,8 @@ var _ = Describe("AMI", func() { content, err := os.ReadFile("testdata/windows_userdata_input.ps1") Expect(err).ToNot(HaveOccurred()) provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyWindows2022, }, UserData: aws.String(string(content)), diff --git a/test/suites/integration/aws_metadata_test.go b/test/suites/integration/aws_metadata_test.go index 25b9689f966f..62e6821a9f18 100644 --- a/test/suites/integration/aws_metadata_test.go +++ b/test/suites/integration/aws_metadata_test.go @@ -15,17 +15,15 @@ limitations under the License. package integration_test import ( - "os" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/samber/lo" "github.com/aws/karpenter-core/pkg/apis/v1alpha5" "github.com/aws/karpenter-core/pkg/test" "github.com/aws/karpenter/pkg/apis/v1alpha1" + awsenv "github.com/aws/karpenter/test/pkg/environment/aws" awstest "github.com/aws/karpenter/pkg/test" ) @@ -34,8 +32,8 @@ var _ = Describe("MetadataOptions", func() { It("should use specified metadata options", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, LaunchTemplate: v1alpha1.LaunchTemplate{ MetadataOptions: &v1alpha1.MetadataOptions{ HTTPEndpoint: aws.String("enabled"), diff --git a/test/suites/integration/backwards_compatability_test.go b/test/suites/integration/backwards_compatability_test.go index 02bbd512def7..1c6bd89fd7e3 100644 --- a/test/suites/integration/backwards_compatability_test.go +++ b/test/suites/integration/backwards_compatability_test.go @@ -15,8 +15,6 @@ limitations under the License. package integration_test import ( - "os" - "github.com/aws/aws-sdk-go/service/ec2" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -24,6 +22,7 @@ import ( "github.com/aws/karpenter-core/pkg/test" "github.com/aws/karpenter/pkg/apis/v1alpha1" + "github.com/aws/karpenter/test/pkg/environment/aws" ) var _ = Describe("BackwardsCompatability", func() { @@ -31,8 +30,8 @@ var _ = Describe("BackwardsCompatability", func() { provisioner := test.Provisioner( test.ProvisionerOptions{ Provider: &v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, Tags: map[string]string{ "custom-tag": "custom-value", "custom-tag2": "custom-value2", diff --git a/test/suites/integration/block_device_mappings_test.go b/test/suites/integration/block_device_mappings_test.go index 8d1596ec5b79..0366e52ffa31 100644 --- a/test/suites/integration/block_device_mappings_test.go +++ b/test/suites/integration/block_device_mappings_test.go @@ -15,17 +15,15 @@ limitations under the License. package integration_test import ( - "os" - "github.com/aws/aws-sdk-go/aws" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/samber/lo" "github.com/aws/karpenter-core/pkg/apis/v1alpha5" "github.com/aws/karpenter-core/pkg/test" "github.com/aws/karpenter-core/pkg/utils/resources" "github.com/aws/karpenter/pkg/apis/v1alpha1" + awsenv "github.com/aws/karpenter/test/pkg/environment/aws" awstest "github.com/aws/karpenter/pkg/test" ) @@ -34,8 +32,8 @@ var _ = Describe("BlockDeviceMappings", func() { It("should use specified block device mappings", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, LaunchTemplate: v1alpha1.LaunchTemplate{ BlockDeviceMappings: []*v1alpha1.BlockDeviceMapping{ { diff --git a/test/suites/integration/cni_test.go b/test/suites/integration/cni_test.go index 12b4c24afd2c..cd486a3c2920 100644 --- a/test/suites/integration/cni_test.go +++ b/test/suites/integration/cni_test.go @@ -15,20 +15,19 @@ limitations under the License. package integration_test import ( - "os" "strconv" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/samber/lo" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" "github.com/aws/karpenter-core/pkg/apis/v1alpha5" "github.com/aws/karpenter-core/pkg/test" "github.com/aws/karpenter/pkg/apis/v1alpha1" + awsenv "github.com/aws/karpenter/test/pkg/environment/aws" awstest "github.com/aws/karpenter/pkg/test" ) @@ -40,8 +39,8 @@ var _ = Describe("CNITests", func() { }) provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyAL2, }, }) @@ -59,8 +58,8 @@ var _ = Describe("CNITests", func() { It("should set eni-limited maxPods when AWSENILimited when AWS_ENI_LIMITED_POD_DENSITY is true", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyAL2, }, }) @@ -80,8 +79,8 @@ var _ = Describe("CNITests", func() { }) provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyAL2, }, }) diff --git a/test/suites/integration/daemonset_test.go b/test/suites/integration/daemonset_test.go index 0be728094234..10fd5a2eb5f0 100644 --- a/test/suites/integration/daemonset_test.go +++ b/test/suites/integration/daemonset_test.go @@ -15,8 +15,6 @@ limitations under the License. package integration_test import ( - "os" - appsv1 "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" schedulingv1 "k8s.io/api/scheduling/v1" @@ -30,6 +28,7 @@ import ( "github.com/aws/karpenter-core/pkg/test" "github.com/aws/karpenter/pkg/apis/v1alpha1" awstest "github.com/aws/karpenter/pkg/test" + "github.com/aws/karpenter/test/pkg/environment/aws" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -46,8 +45,8 @@ var _ = Describe("DaemonSet", func() { BeforeEach(func() { provider = awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner = test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, diff --git a/test/suites/integration/emptiness_test.go b/test/suites/integration/emptiness_test.go index e5dde1537546..036878d6c5a3 100644 --- a/test/suites/integration/emptiness_test.go +++ b/test/suites/integration/emptiness_test.go @@ -15,8 +15,6 @@ limitations under the License. package integration_test import ( - "os" - "k8s.io/apimachinery/pkg/labels" "knative.dev/pkg/ptr" @@ -24,19 +22,19 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/samber/lo" "github.com/aws/karpenter-core/pkg/apis/v1alpha5" "github.com/aws/karpenter-core/pkg/test" "github.com/aws/karpenter/pkg/apis/v1alpha1" awstest "github.com/aws/karpenter/pkg/test" + "github.com/aws/karpenter/test/pkg/environment/aws" ) var _ = Describe("Emptiness", func() { It("should terminate an empty node", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, diff --git a/test/suites/integration/extended_resources_test.go b/test/suites/integration/extended_resources_test.go index 1299f218c6f1..4aa1df40825a 100644 --- a/test/suites/integration/extended_resources_test.go +++ b/test/suites/integration/extended_resources_test.go @@ -31,6 +31,7 @@ import ( "github.com/aws/karpenter-core/pkg/apis/v1alpha5" "github.com/aws/karpenter-core/pkg/test" "github.com/aws/karpenter/pkg/apis/v1alpha1" + "github.com/aws/karpenter/test/pkg/environment/aws" awstest "github.com/aws/karpenter/pkg/test" ) @@ -40,8 +41,8 @@ var _ = Describe("Extended Resources", func() { ExpectNvidiaDevicePluginCreated() provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, @@ -79,8 +80,8 @@ var _ = Describe("Extended Resources", func() { // For Bottlerocket, we are testing that resources are initialized without needing a device plugin provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ AMIFamily: &v1alpha1.AMIFamilyBottlerocket, - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, @@ -123,8 +124,8 @@ var _ = Describe("Extended Resources", func() { "aws.enablePodENI": "true", }) provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, @@ -177,8 +178,8 @@ var _ = Describe("Extended Resources", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ AMIFamily: &v1alpha1.AMIFamilyCustom, - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, AMISelector: map[string]string{ "aws-ids": customAMI, @@ -194,8 +195,8 @@ var _ = Describe("Extended Resources", func() { }, }, }) - provider.Spec.UserData = lo.ToPtr(fmt.Sprintf(string(rawContent), lo.Must(os.LookupEnv("CLUSTER_NAME")), - lo.Must(os.LookupEnv("CLUSTER_ENDPOINT")), env.ExpectCABundle(), provisioner.Name)) + provider.Spec.UserData = lo.ToPtr(fmt.Sprintf(string(rawContent), aws.GetClusterName(), + aws.GetClusterEndpoint(), env.ExpectCABundle(), provisioner.Name)) numPods := 1 dep := test.Deployment(test.DeploymentOptions{ @@ -230,8 +231,8 @@ var _ = Describe("Extended Resources", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, AMISelector: map[string]string{"aws-ids": "ami-0fae925f94979981f"}, }) diff --git a/test/suites/integration/hash_test.go b/test/suites/integration/hash_test.go index d6a786c70b5a..3ec48a454e7b 100644 --- a/test/suites/integration/hash_test.go +++ b/test/suites/integration/hash_test.go @@ -15,16 +15,14 @@ limitations under the License. package integration_test import ( - "os" - . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/samber/lo" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/aws/karpenter-core/pkg/apis/v1alpha5" "github.com/aws/karpenter-core/pkg/test" "github.com/aws/karpenter/pkg/apis/v1alpha1" + "github.com/aws/karpenter/test/pkg/environment/aws" awstest "github.com/aws/karpenter/pkg/test" ) @@ -33,8 +31,8 @@ var _ = Describe("CRD Hash", func() { It("should have Provisioner hash", func() { nodeTemplate := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, }) provisioner := test.Provisioner(test.ProvisionerOptions{ @@ -56,8 +54,8 @@ var _ = Describe("CRD Hash", func() { It("should have AWSNodeTemplate hash", func() { nodeTemplate := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, }) env.ExpectCreated(nodeTemplate) diff --git a/test/suites/integration/instance_profile_test.go b/test/suites/integration/instance_profile_test.go index 6d020dc3a818..e0b3016636fc 100644 --- a/test/suites/integration/instance_profile_test.go +++ b/test/suites/integration/instance_profile_test.go @@ -16,7 +16,6 @@ package integration_test import ( "fmt" - "os" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/iam" @@ -30,6 +29,7 @@ import ( awserrors "github.com/aws/karpenter/pkg/errors" "github.com/aws/karpenter/pkg/providers/instanceprofile" "github.com/aws/karpenter/pkg/test" + awsenv "github.com/aws/karpenter/test/pkg/environment/aws" ) var _ = Describe("InstanceProfile Generation", func() { @@ -49,7 +49,7 @@ var _ = Describe("InstanceProfile Generation", func() { Tags: map[string]string{"*": "*"}, }, }, - Role: fmt.Sprintf("KarpenterNodeRole-%s", lo.Must(os.LookupEnv("CLUSTER_NAME"))), + Role: fmt.Sprintf("KarpenterNodeRole-%s", awsenv.GetClusterName()), }, }) }) diff --git a/test/suites/integration/kubelet_config_test.go b/test/suites/integration/kubelet_config_test.go index cec679d420e2..672285d114c9 100644 --- a/test/suites/integration/kubelet_config_test.go +++ b/test/suites/integration/kubelet_config_test.go @@ -16,11 +16,9 @@ package integration_test import ( "math" - "os" "time" . "github.com/onsi/ginkgo/v2" - "github.com/samber/lo" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -42,8 +40,8 @@ var _ = Describe("KubeletConfiguration Overrides", func() { BeforeEach(func() { nodeTemplate = awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) // MaxPods needs to account for the daemonsets that will run on the nodes @@ -177,8 +175,8 @@ var _ = Describe("KubeletConfiguration Overrides", func() { }) It("should schedule pods onto separate nodes when maxPods is set", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) // MaxPods needs to account for the daemonsets that will run on the nodes @@ -220,8 +218,8 @@ var _ = Describe("KubeletConfiguration Overrides", func() { }) It("should schedule pods onto separate nodes when podsPerCore is set", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) // PodsPerCore needs to account for the daemonsets that will run on the nodes // This will have 4 pods available on each node (2 taken by daemonset pods) @@ -274,8 +272,8 @@ var _ = Describe("KubeletConfiguration Overrides", func() { }) It("should ignore podsPerCore value when Bottlerocket is used", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, AMIFamily: &v1alpha1.AMIFamilyBottlerocket, }}) // All pods should schedule to a single node since we are ignoring podsPerCore value diff --git a/test/suites/integration/scheduling_test.go b/test/suites/integration/scheduling_test.go index d35064a77bd6..751e2dc3fd42 100644 --- a/test/suites/integration/scheduling_test.go +++ b/test/suites/integration/scheduling_test.go @@ -16,7 +16,6 @@ package integration_test import ( "fmt" - "os" "time" . "github.com/onsi/ginkgo/v2" @@ -44,8 +43,8 @@ var _ = Describe("Scheduling", Ordered, ContinueOnFailure, func() { BeforeEach(func() { provider = awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner = test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, diff --git a/test/suites/integration/security_group_test.go b/test/suites/integration/security_group_test.go index 36489e49880b..67129e98f9d6 100644 --- a/test/suites/integration/security_group_test.go +++ b/test/suites/integration/security_group_test.go @@ -16,7 +16,6 @@ package integration_test import ( "fmt" - "os" "strings" "time" @@ -37,14 +36,14 @@ import ( var _ = Describe("SecurityGroups", func() { It("should use the security-group-id selector", func() { - securityGroups := env.GetSecurityGroups(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) + securityGroups := env.GetSecurityGroups(map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}) Expect(len(securityGroups)).To(BeNumerically(">", 1)) ids := strings.Join([]string{*securityGroups[0].GroupId, *securityGroups[1].GroupId}, ",") provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ SecurityGroupSelector: map[string]string{"aws-ids": ids}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, }) provisioner := test.Provisioner(test.ProvisionerOptions{ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}}) @@ -58,7 +57,7 @@ var _ = Describe("SecurityGroups", func() { }) It("should use the security group selector with multiple tag values", func() { - securityGroups := env.GetSecurityGroups(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) + securityGroups := env.GetSecurityGroups(map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}) Expect(len(securityGroups)).To(BeNumerically(">", 1)) first := securityGroups[0] last := securityGroups[len(securityGroups)-1] @@ -69,7 +68,7 @@ var _ = Describe("SecurityGroups", func() { lo.FromPtr(lo.FindOrElse(first.Tags, &ec2.Tag{}, func(tag *ec2.Tag) bool { return lo.FromPtr(tag.Key) == "Name" }).Value), lo.FromPtr(lo.FindOrElse(last.Tags, &ec2.Tag{}, func(tag *ec2.Tag) bool { return lo.FromPtr(tag.Key) == "Name" }).Value), )}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, }) provisioner := test.Provisioner(test.ProvisionerOptions{ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}}) @@ -85,8 +84,8 @@ var _ = Describe("SecurityGroups", func() { It("should update the AWSNodeTemplateStatus for security groups", func() { nodeTemplate := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, }) @@ -96,7 +95,7 @@ var _ = Describe("SecurityGroups", func() { }) func EventuallyExpectSecurityGroups(env *aws.Environment, nodeTemplate *v1alpha1.AWSNodeTemplate) { - securityGroups := env.GetSecurityGroups(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) + securityGroups := env.GetSecurityGroups(map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}) Expect(securityGroups).ToNot(HaveLen(0)) ids := sets.New(lo.Map(securityGroups, func(s aws.SecurityGroup, _ int) string { diff --git a/test/suites/integration/storage_test.go b/test/suites/integration/storage_test.go index 5947e3f4b0f0..198f9773168a 100644 --- a/test/suites/integration/storage_test.go +++ b/test/suites/integration/storage_test.go @@ -16,10 +16,8 @@ package integration_test import ( "fmt" - "os" "github.com/aws/aws-sdk-go/aws" - "github.com/samber/lo" appsv1 "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" storagev1 "k8s.io/api/storage/v1" @@ -31,6 +29,7 @@ import ( "github.com/aws/karpenter-core/pkg/apis/v1alpha5" "github.com/aws/karpenter-core/pkg/test" "github.com/aws/karpenter/pkg/apis/v1alpha1" + awsenv "github.com/aws/karpenter/test/pkg/environment/aws" awstest "github.com/aws/karpenter/pkg/test" @@ -54,8 +53,8 @@ var _ = Describe("Dynamic PVC", func() { } provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}}) @@ -92,8 +91,8 @@ var _ = Describe("Dynamic PVC", func() { var _ = Describe("Static PVC", func() { It("should run a pod with a static persistent volume", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": awsenv.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}}) diff --git a/test/suites/integration/subnet_test.go b/test/suites/integration/subnet_test.go index 0b88c73caa93..51753bf4d8f6 100644 --- a/test/suites/integration/subnet_test.go +++ b/test/suites/integration/subnet_test.go @@ -16,7 +16,6 @@ package integration_test import ( "fmt" - "os" "time" "github.com/aws/aws-sdk-go/service/ec2" @@ -37,14 +36,14 @@ import ( var _ = Describe("Subnets", func() { It("should use the subnet-id selector", func() { - subnets := env.GetSubnets(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) + subnets := env.GetSubnets(map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}) Expect(len(subnets)).ToNot(Equal(0)) shuffledAZs := lo.Shuffle(lo.Keys(subnets)) firstSubnet := subnets[shuffledAZs[0]][0] provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, SubnetSelector: map[string]string{"aws-ids": firstSubnet}, }, }) @@ -58,7 +57,7 @@ var _ = Describe("Subnets", func() { env.ExpectInstance(pod.Spec.NodeName).To(HaveField("SubnetId", HaveValue(Equal(firstSubnet)))) }) It("should use resource based naming as node names", func() { - subnets := env.GetSubnets(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) + subnets := env.GetSubnets(map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}) Expect(len(subnets)).ToNot(Equal(0)) allSubnets := lo.Flatten(lo.Values(subnets)) @@ -70,8 +69,8 @@ var _ = Describe("Subnets", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, }) provisioner := test.Provisioner(test.ProvisionerOptions{ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}}) @@ -85,14 +84,14 @@ var _ = Describe("Subnets", func() { }) It("should use the subnet tag selector with multiple tag values", func() { // Get all the subnets for the cluster - subnets := env.GetSubnetNameAndIds(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) + subnets := env.GetSubnetNameAndIds(map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}) Expect(len(subnets)).To(BeNumerically(">", 1)) firstSubnet := subnets[0] lastSubnet := subnets[len(subnets)-1] provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, SubnetSelector: map[string]string{"Name": fmt.Sprintf("%s,%s", firstSubnet.Name, lastSubnet.Name)}, }, }) @@ -107,14 +106,14 @@ var _ = Describe("Subnets", func() { }) It("should use a subnet within the AZ requested", func() { - subnets := env.GetSubnets(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) + subnets := env.GetSubnets(map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}) Expect(len(subnets)).ToNot(Equal(0)) shuffledAZs := lo.Shuffle(lo.Keys(subnets)) provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, }) provisioner := test.Provisioner(test.ProvisionerOptions{ @@ -141,8 +140,8 @@ var _ = Describe("Subnets", func() { It("should have the AWSNodeTemplateStatus for subnets", func() { nodeTemplate := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, }) @@ -198,7 +197,7 @@ type SubnetInfo struct { } func EventuallyExpectSubnets(env *aws.Environment, nodeTemplate *v1alpha1.AWSNodeTemplate) { - subnets := env.GetSubnets(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) + subnets := env.GetSubnets(map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}) Expect(subnets).ToNot(HaveLen(0)) ids := sets.New(lo.Flatten(lo.Values(subnets))...) diff --git a/test/suites/integration/tags_test.go b/test/suites/integration/tags_test.go index bd8402fbd9de..63bb7d5e3471 100644 --- a/test/suites/integration/tags_test.go +++ b/test/suites/integration/tags_test.go @@ -15,8 +15,6 @@ limitations under the License. package integration_test import ( - "os" - "github.com/aws/aws-sdk-go/service/ec2" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -35,6 +33,7 @@ import ( "github.com/aws/karpenter/pkg/apis/v1beta1" "github.com/aws/karpenter/pkg/providers/instance" "github.com/aws/karpenter/pkg/test" + "github.com/aws/karpenter/test/pkg/environment/aws" ) var _ = Describe("Tags", func() { @@ -42,8 +41,8 @@ var _ = Describe("Tags", func() { It("should tag all associated resources", func() { provider := test.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, Tags: map[string]string{"TestTag": "TestVal"}, }, }) @@ -63,8 +62,8 @@ var _ = Describe("Tags", func() { It("should tag all associated resources with global tags", func() { provider := test.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, }) @@ -95,10 +94,10 @@ var _ = Describe("Tags", func() { BeforeEach(func() { nodeClass = test.EC2NodeClass(v1beta1.EC2NodeClass{Spec: v1beta1.EC2NodeClassSpec{ SecurityGroupSelectorTerms: []v1beta1.SecurityGroupSelectorTerm{{ - Tags: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + Tags: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}, SubnetSelectorTerms: []v1beta1.SubnetSelectorTerm{{ - Tags: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + Tags: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}, }}) @@ -170,8 +169,8 @@ var _ = Describe("Tags", func() { It("shouldn't tag nodes provisioned by v1alpha5 provisioner", func() { nodeTemplate := test.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := coretest.Provisioner(coretest.ProvisionerOptions{ diff --git a/test/suites/integration/termination_test.go b/test/suites/integration/termination_test.go index 3a5ead7a0584..e6c2f373f14c 100644 --- a/test/suites/integration/termination_test.go +++ b/test/suites/integration/termination_test.go @@ -15,7 +15,6 @@ limitations under the License. package integration_test import ( - "os" "time" . "github.com/onsi/ginkgo/v2" @@ -26,13 +25,14 @@ import ( "github.com/aws/karpenter-core/pkg/test" "github.com/aws/karpenter/pkg/apis/v1alpha1" awstest "github.com/aws/karpenter/pkg/test" + "github.com/aws/karpenter/test/pkg/environment/aws" ) var _ = Describe("Termination", func() { It("should terminate the node and the instance on deletion", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, diff --git a/test/suites/integration/webhook_test.go b/test/suites/integration/webhook_test.go index 9112e8606263..b0a1c71ee09c 100644 --- a/test/suites/integration/webhook_test.go +++ b/test/suites/integration/webhook_test.go @@ -16,19 +16,18 @@ package integration_test import ( "fmt" - "os" v1 "k8s.io/api/core/v1" "knative.dev/pkg/ptr" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/samber/lo" "github.com/aws/karpenter-core/pkg/apis/v1alpha5" "github.com/aws/karpenter-core/pkg/test" "github.com/aws/karpenter/pkg/apis/v1alpha1" awstest "github.com/aws/karpenter/pkg/test" + "github.com/aws/karpenter/test/pkg/environment/aws" ) var _ = Describe("Webhooks", func() { @@ -134,8 +133,8 @@ var _ = Describe("Webhooks", func() { It("should error when provider and providerRef are combined", func() { Expect(env.Client.Create(env, test.Provisioner(test.ProvisionerOptions{ Provider: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, ProviderRef: &v1alpha5.MachineTemplateRef{Name: "test"}, }))).ToNot(Succeed()) @@ -267,15 +266,15 @@ var _ = Describe("Webhooks", func() { It("should error when amiSelector is not defined for amiFamily Custom", func() { Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ AMIFamily: &v1alpha1.AMIFamilyCustom, - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}))).ToNot(Succeed()) }) It("should fail if both userdata and launchTemplate are set", func() { Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ LaunchTemplate: v1alpha1.LaunchTemplate{LaunchTemplateName: ptr.String("lt")}, - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, UserData: ptr.String("data"), }))).ToNot(Succeed()) @@ -283,47 +282,47 @@ var _ = Describe("Webhooks", func() { It("should fail if both amiSelector and launchTemplate are set", func() { Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ LaunchTemplate: v1alpha1.LaunchTemplate{LaunchTemplateName: ptr.String("lt")}, - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, AMISelector: map[string]string{"foo": "bar"}, }))).ToNot(Succeed()) }) It("should fail for poorly formatted aws-ids", func() { Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }, AMISelector: map[string]string{"aws-ids": "must-start-with-ami"}, }))).ToNot(Succeed()) }) It("should succeed when tags don't contain restricted keys", func() { Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, Tags: map[string]string{"karpenter.sh/custom-key": "custom-value", "kubernetes.io/role/key": "custom-value"}, }, }))).To(Succeed()) }) It("should error when tags contains a restricted key", func() { Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, Tags: map[string]string{"karpenter.sh/provisioner-name": "custom-value"}, }, }))).ToNot(Succeed()) Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - Tags: map[string]string{"karpenter.sh/managed-by": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + Tags: map[string]string{"karpenter.sh/managed-by": aws.GetClusterName()}, }, }))).ToNot(Succeed()) Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - Tags: map[string]string{fmt.Sprintf("kubernetes.io/cluster/%s", lo.Must(os.LookupEnv("CLUSTER_NAME"))): "owned"}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + Tags: map[string]string{fmt.Sprintf("kubernetes.io/cluster/%s", aws.GetClusterName()): "owned"}, }, }))).ToNot(Succeed()) }) @@ -331,22 +330,22 @@ var _ = Describe("Webhooks", func() { Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ SecurityGroupSelector: map[string]string{ - "karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME")), + "karpenter.sh/discovery": aws.GetClusterName(), "aws-ids": "sg-12345", }, SubnetSelector: map[string]string{ - "karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME")), + "karpenter.sh/discovery": aws.GetClusterName(), }, }, }))).ToNot(Succeed()) Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ SecurityGroupSelector: map[string]string{ - "karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME")), + "karpenter.sh/discovery": aws.GetClusterName(), "aws-ids": "sg-12345", }, SubnetSelector: map[string]string{ - "karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME")), + "karpenter.sh/discovery": aws.GetClusterName(), }, }, }))).ToNot(Succeed()) @@ -355,10 +354,10 @@ var _ = Describe("Webhooks", func() { Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ SecurityGroupSelector: map[string]string{ - "karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME")), + "karpenter.sh/discovery": aws.GetClusterName(), }, SubnetSelector: map[string]string{ - "karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME")), + "karpenter.sh/discovery": aws.GetClusterName(), "aws-ids": "subnet-12345", }, }, @@ -366,10 +365,10 @@ var _ = Describe("Webhooks", func() { Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ SecurityGroupSelector: map[string]string{ - "karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME")), + "karpenter.sh/discovery": aws.GetClusterName(), }, SubnetSelector: map[string]string{ - "karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME")), + "karpenter.sh/discovery": aws.GetClusterName(), "aws-ids": "subnet-12345", }, }, @@ -379,10 +378,10 @@ var _ = Describe("Webhooks", func() { Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ SecurityGroupSelector: map[string]string{ - "karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME")), + "karpenter.sh/discovery": aws.GetClusterName(), }, SubnetSelector: map[string]string{ - "karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME")), + "karpenter.sh/discovery": aws.GetClusterName(), }, }, AMISelector: map[string]string{ @@ -393,10 +392,10 @@ var _ = Describe("Webhooks", func() { Expect(env.Client.Create(env, awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ SecurityGroupSelector: map[string]string{ - "karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME")), + "karpenter.sh/discovery": aws.GetClusterName(), }, SubnetSelector: map[string]string{ - "karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME")), + "karpenter.sh/discovery": aws.GetClusterName(), }, }, AMISelector: map[string]string{ diff --git a/test/suites/interruption/suite_test.go b/test/suites/interruption/suite_test.go index db852f467cd0..12fc8cc6b3ec 100644 --- a/test/suites/interruption/suite_test.go +++ b/test/suites/interruption/suite_test.go @@ -16,7 +16,6 @@ package interruption_test import ( "fmt" - "os" "testing" "time" @@ -56,7 +55,7 @@ func TestInterruption(t *testing.T) { var _ = BeforeEach(func() { env.Context = options.ToContext(env.Context, awstest.Options(awstest.OptionsFields{ - InterruptionQueueName: lo.ToPtr(lo.Must(os.LookupEnv("INTERRUPTION_QUEUE_NAME"))), + InterruptionQueueName: lo.ToPtr(aws.GetInterruptionQueueName()), })) env.BeforeEach() env.ExpectQueueExists() @@ -68,8 +67,8 @@ var _ = Describe("Interruption", Label("AWS"), func() { It("should terminate the spot instance and spin-up a new node on spot interruption warning", func() { By("Creating a single healthy node with a healthy deployment") provider = awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ Requirements: []v1.NodeSelectorRequirement{ @@ -116,8 +115,8 @@ var _ = Describe("Interruption", Label("AWS"), func() { It("should terminate the node at the API server when the EC2 instance is stopped", func() { By("Creating a single healthy node with a healthy deployment") provider = awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ Requirements: []v1.NodeSelectorRequirement{ @@ -156,8 +155,8 @@ var _ = Describe("Interruption", Label("AWS"), func() { It("should terminate the node at the API server when the EC2 instance is terminated", func() { By("Creating a single healthy node with a healthy deployment") provider = awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ Requirements: []v1.NodeSelectorRequirement{ @@ -196,8 +195,8 @@ var _ = Describe("Interruption", Label("AWS"), func() { It("should terminate the node when receiving a scheduled change health event", func() { By("Creating a single healthy node with a healthy deployment") provider = awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ Requirements: []v1.NodeSelectorRequirement{ diff --git a/test/suites/ipv6/suite_test.go b/test/suites/ipv6/suite_test.go index 6758485e65a7..1d84001b91f3 100644 --- a/test/suites/ipv6/suite_test.go +++ b/test/suites/ipv6/suite_test.go @@ -16,7 +16,6 @@ package ipv6_test import ( "net" - "os" "testing" . "github.com/onsi/ginkgo/v2" @@ -52,8 +51,8 @@ var _ = Describe("IPv6", func() { It("should provision an IPv6 node by discovering kube-dns IPv6", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, Requirements: []v1.NodeSelectorRequirement{ { @@ -82,8 +81,8 @@ var _ = Describe("IPv6", func() { clusterDNSAddr := env.ExpectIPv6ClusterDNS() provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{ AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, Requirements: []v1.NodeSelectorRequirement{ { diff --git a/test/suites/machine/garbage_collection_test.go b/test/suites/machine/garbage_collection_test.go index fd808cb70713..f00d0397315b 100644 --- a/test/suites/machine/garbage_collection_test.go +++ b/test/suites/machine/garbage_collection_test.go @@ -43,8 +43,8 @@ var _ = Describe("NodeClaimGarbageCollection", func() { BeforeEach(func() { provisioner = test.Provisioner() - securityGroups := env.GetSecurityGroups(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) - subnets := env.GetSubnetNameAndIds(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) + securityGroups := env.GetSecurityGroups(map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}) + subnets := env.GetSubnetNameAndIds(map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}) Expect(securityGroups).ToNot(HaveLen(0)) Expect(subnets).ToNot(HaveLen(0)) @@ -75,7 +75,7 @@ var _ = Describe("NodeClaimGarbageCollection", func() { ResourceType: aws.String(ec2.ResourceTypeInstance), Tags: []*ec2.Tag{ { - Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", lo.Must(os.LookupEnv("CLUSTER_NAME")))), + Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", environmentaws.GetClusterName())), Value: aws.String("owned"), }, { @@ -93,8 +93,8 @@ var _ = Describe("NodeClaimGarbageCollection", func() { // Update the userData for the instance input with the correct provisionerName rawContent, err := os.ReadFile("testdata/al2_userdata_input.sh") Expect(err).ToNot(HaveOccurred()) - instanceInput.UserData = lo.ToPtr(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(string(rawContent), lo.Must(os.LookupEnv("CLUSTER_NAME")), - lo.Must(os.LookupEnv("CLUSTER_ENDPOINT")), env.ExpectCABundle(), provisioner.Name)))) + instanceInput.UserData = lo.ToPtr(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(string(rawContent), environmentaws.GetClusterName(), + environmentaws.GetClusterEndpoint(), env.ExpectCABundle(), provisioner.Name)))) // Create an instance manually to mock Karpenter launching an instance out := env.ExpectRunInstances(instanceInput) @@ -120,7 +120,7 @@ var _ = Describe("NodeClaimGarbageCollection", func() { Tags: []*ec2.Tag{ { Key: aws.String(v1alpha5.MachineManagedByAnnotationKey), - Value: aws.String(lo.Must(os.LookupEnv("CLUSTER_NAME"))), + Value: aws.String(environmentaws.GetClusterName()), }, }, }) @@ -139,8 +139,8 @@ var _ = Describe("NodeClaimGarbageCollection", func() { }) provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, diff --git a/test/suites/machine/link_test.go b/test/suites/machine/link_test.go index 5fbf13a9d56f..9390ad22a1fc 100644 --- a/test/suites/machine/link_test.go +++ b/test/suites/machine/link_test.go @@ -42,8 +42,8 @@ var _ = Describe("MachineLink", func() { var instanceInput *ec2.RunInstancesInput BeforeEach(func() { - securityGroups := env.GetSecurityGroups(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) - subnets := env.GetSubnetNameAndIds(map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}) + securityGroups := env.GetSecurityGroups(map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}) + subnets := env.GetSubnetNameAndIds(map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}) Expect(securityGroups).ToNot(HaveLen(0)) Expect(subnets).ToNot(HaveLen(0)) @@ -74,7 +74,7 @@ var _ = Describe("MachineLink", func() { ResourceType: aws.String(ec2.ResourceTypeInstance), Tags: []*ec2.Tag{ { - Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", lo.Must(os.LookupEnv("CLUSTER_NAME")))), + Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", environmentaws.GetClusterName())), Value: aws.String("owned"), }, }, @@ -87,8 +87,8 @@ var _ = Describe("MachineLink", func() { It("should succeed to link a Machine for an existing instance launched by Karpenter", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ AMIFamily: &v1alpha1.AMIFamilyAL2, - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, @@ -102,8 +102,8 @@ var _ = Describe("MachineLink", func() { Key: aws.String(v1alpha5.ProvisionerNameLabelKey), Value: aws.String(provisioner.Name), }) - instanceInput.UserData = lo.ToPtr(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(string(rawContent), lo.Must(os.LookupEnv("CLUSTER_NAME")), - lo.Must(os.LookupEnv("CLUSTER_ENDPOINT")), env.ExpectCABundle(), provisioner.Name)))) + instanceInput.UserData = lo.ToPtr(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(string(rawContent), environmentaws.GetClusterName(), + environmentaws.GetClusterEndpoint(), env.ExpectCABundle(), provisioner.Name)))) // Create an instance manually to mock Karpenter launching an instance out := env.ExpectRunInstances(instanceInput) @@ -141,15 +141,15 @@ var _ = Describe("MachineLink", func() { return aws.StringValue(t.Key) == v1alpha5.MachineManagedByAnnotationKey }) g.Expect(ok).To(BeTrue()) - g.Expect(aws.StringValue(tag.Value)).To(Equal(lo.Must(os.LookupEnv("CLUSTER_NAME")))) + g.Expect(aws.StringValue(tag.Value)).To(Equal(environmentaws.GetClusterName())) }, time.Minute, time.Second).Should(Succeed()) }) It("should succeed to link a Machine for an existing instance launched by Karpenter with provider", func() { provisioner := test.Provisioner(test.ProvisionerOptions{ Provider: &v1alpha1.AWS{ AMIFamily: &v1alpha1.AMIFamilyAL2, - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, }, }) env.ExpectCreated(provisioner) @@ -161,8 +161,8 @@ var _ = Describe("MachineLink", func() { Key: aws.String(v1alpha5.ProvisionerNameLabelKey), Value: aws.String(provisioner.Name), }) - instanceInput.UserData = lo.ToPtr(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(string(rawContent), lo.Must(os.LookupEnv("CLUSTER_NAME")), - lo.Must(os.LookupEnv("CLUSTER_ENDPOINT")), env.ExpectCABundle(), provisioner.Name)))) + instanceInput.UserData = lo.ToPtr(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(string(rawContent), environmentaws.GetClusterName(), + environmentaws.GetClusterEndpoint(), env.ExpectCABundle(), provisioner.Name)))) // Create an instance manually to mock Karpenter launching an instance out := env.ExpectRunInstances(instanceInput) @@ -200,14 +200,14 @@ var _ = Describe("MachineLink", func() { return aws.StringValue(t.Key) == v1alpha5.MachineManagedByAnnotationKey }) g.Expect(ok).To(BeTrue()) - g.Expect(aws.StringValue(tag.Value)).To(Equal(lo.Must(os.LookupEnv("CLUSTER_NAME")))) + g.Expect(aws.StringValue(tag.Value)).To(Equal(environmentaws.GetClusterName())) }, time.Minute, time.Second).Should(Succeed()) }) It("should succeed to link a Machine for an existing instance re-owned by Karpenter", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ AMIFamily: &v1alpha1.AMIFamilyAL2, - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": environmentaws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, @@ -219,8 +219,8 @@ var _ = Describe("MachineLink", func() { Expect(err).ToNot(HaveOccurred()) // No tag specifications since we're mocking an instance not launched by Karpenter - instanceInput.UserData = lo.ToPtr(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(string(rawContent), lo.Must(os.LookupEnv("CLUSTER_NAME")), - lo.Must(os.LookupEnv("CLUSTER_ENDPOINT")), env.ExpectCABundle(), provisioner.Name)))) + instanceInput.UserData = lo.ToPtr(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(string(rawContent), environmentaws.GetClusterName(), + environmentaws.GetClusterEndpoint(), env.ExpectCABundle(), provisioner.Name)))) // Create an instance manually to mock Karpenter launching an instance out := env.ExpectRunInstances(instanceInput) @@ -263,7 +263,7 @@ var _ = Describe("MachineLink", func() { return aws.StringValue(t.Key) == v1alpha5.MachineManagedByAnnotationKey }) g.Expect(ok).To(BeTrue()) - g.Expect(aws.StringValue(tag.Value)).To(Equal(lo.Must(os.LookupEnv("CLUSTER_NAME")))) + g.Expect(aws.StringValue(tag.Value)).To(Equal(environmentaws.GetClusterName())) tag, ok = lo.Find(instance.Tags, func(t *ec2.Tag) bool { return aws.StringValue(t.Key) == v1alpha5.ProvisionerNameLabelKey }) diff --git a/test/suites/machine/machine_test.go b/test/suites/machine/machine_test.go index c570e4bf11b9..fca7d4ce1120 100644 --- a/test/suites/machine/machine_test.go +++ b/test/suites/machine/machine_test.go @@ -33,14 +33,15 @@ import ( "github.com/aws/karpenter-core/pkg/utils/resources" "github.com/aws/karpenter/pkg/apis/v1alpha1" awstest "github.com/aws/karpenter/pkg/test" + "github.com/aws/karpenter/test/pkg/environment/aws" ) var _ = Describe("StandaloneMachine", func() { var nodeTemplate *v1alpha1.AWSNodeTemplate BeforeEach(func() { nodeTemplate = awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) }) It("should create a standard machine within the 'c' instance family", func() { @@ -272,8 +273,8 @@ var _ = Describe("StandaloneMachine", func() { // Create userData that adds custom labels through the --kubelet-extra-args nodeTemplate.Spec.AMIFamily = &v1alpha1.AMIFamilyCustom nodeTemplate.Spec.AMISelector = map[string]string{"aws-ids": customAMI} - nodeTemplate.Spec.UserData = lo.ToPtr(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(string(rawContent), lo.Must(os.LookupEnv("CLUSTER_NAME")), - lo.Must(os.LookupEnv("CLUSTER_ENDPOINT")), env.ExpectCABundle())))) + nodeTemplate.Spec.UserData = lo.ToPtr(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(string(rawContent), aws.GetClusterName(), + aws.GetClusterEndpoint(), env.ExpectCABundle())))) machine := test.Machine(v1alpha5.Machine{ Spec: v1alpha5.MachineSpec{ diff --git a/test/suites/scale/deprovisioning_test.go b/test/suites/scale/deprovisioning_test.go index f007f099ee22..e7231ad7f12c 100644 --- a/test/suites/scale/deprovisioning_test.go +++ b/test/suites/scale/deprovisioning_test.go @@ -17,7 +17,6 @@ package scale_test import ( "context" "fmt" - "os" "strconv" "sync" "time" @@ -86,8 +85,8 @@ var _ = Describe("Deprovisioning", Label(debug.NoWatch), Label(debug.NoEvents), "featureGates.driftEnabled": "true", }) nodeTemplate = awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisionerOptions = test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{ diff --git a/test/suites/scale/provisioning_test.go b/test/suites/scale/provisioning_test.go index dba5acefe91e..a0e41d441f52 100644 --- a/test/suites/scale/provisioning_test.go +++ b/test/suites/scale/provisioning_test.go @@ -16,7 +16,6 @@ package scale_test import ( "context" - "os" "strconv" "time" @@ -46,8 +45,8 @@ var _ = Describe("Provisioning", Label(debug.NoWatch), Label(debug.NoEvents), fu BeforeEach(func() { nodeTemplate = awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner = test.Provisioner(test.ProvisionerOptions{ ProviderRef: &v1alpha5.MachineTemplateRef{ diff --git a/test/suites/utilization/suite_test.go b/test/suites/utilization/suite_test.go index b0084f6d181c..77a5d0a21b60 100644 --- a/test/suites/utilization/suite_test.go +++ b/test/suites/utilization/suite_test.go @@ -15,12 +15,10 @@ limitations under the License. package utilization_test import ( - "os" "testing" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/samber/lo" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/labels" @@ -54,8 +52,8 @@ var _ = AfterEach(func() { env.AfterEach() }) var _ = Describe("Utilization", Label(debug.NoWatch), Label(debug.NoEvents), func() { It("should provision one pod per node", func() { provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{AWS: v1alpha1.AWS{ - SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, - SubnetSelector: map[string]string{"karpenter.sh/discovery": lo.Must(os.LookupEnv("CLUSTER_NAME"))}, + SecurityGroupSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, + SubnetSelector: map[string]string{"karpenter.sh/discovery": aws.GetClusterName()}, }}) provisioner := test.Provisioner(test.ProvisionerOptions{ProviderRef: &v1alpha5.MachineTemplateRef{Name: provider.Name}, Requirements: []v1.NodeSelectorRequirement{{ Key: v1.LabelInstanceTypeStable,