From 3d43d0ac4b05b303383422cf6c7da81dc7991a8a Mon Sep 17 00:00:00 2001 From: Jigisha Patil Date: Fri, 19 Jan 2024 15:49:54 -0800 Subject: [PATCH] Add E2E for LT deletion --- .../launchtemplate/launchtemplate.go | 33 ++++++------- .../integration/launch_template_test.go | 47 +++++++++++++++++++ 2 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 test/suites/integration/launch_template_test.go diff --git a/pkg/providers/launchtemplate/launchtemplate.go b/pkg/providers/launchtemplate/launchtemplate.go index 7f727d4288f8..032d9c5c8f87 100644 --- a/pkg/providers/launchtemplate/launchtemplate.go +++ b/pkg/providers/launchtemplate/launchtemplate.go @@ -24,8 +24,6 @@ import ( "sync" "time" - "go.uber.org/multierr" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2/ec2iface" @@ -395,27 +393,26 @@ func (p *Provider) getInstanceProfile(nodeClass *v1beta1.EC2NodeClass) (string, func (p *Provider) DeleteLaunchTemplates(ctx context.Context, nodeClass *v1beta1.EC2NodeClass) error { clusterName := options.FromContext(ctx).ClusterName - ctx = logging.WithLogger(ctx, logging.FromContext(ctx).With("tag-key", karpenterManagedTagKey, "tag-value", clusterName)) - var deleteLTerr error - describeErr := p.ec2api.DescribeLaunchTemplatesPagesWithContext(ctx, &ec2.DescribeLaunchTemplatesInput{ - Filters: []*ec2.Filter{{Name: aws.String(fmt.Sprintf("tag:%s", karpenterManagedTagKey)), Values: []*string{aws.String(clusterName)}}, - {Name: aws.String(fmt.Sprintf("tag:%s", v1beta1.LabelNodeClass)), Values: []*string{aws.String(nodeClass.Name)}}}, + var ltNames []*string + if err := p.ec2api.DescribeLaunchTemplatesPagesWithContext(ctx, &ec2.DescribeLaunchTemplatesInput{ + Filters: []*ec2.Filter{ + {Name: aws.String(fmt.Sprintf("tag:%s", karpenterManagedTagKey)), Values: []*string{aws.String(clusterName)}}, + {Name: aws.String(fmt.Sprintf("tag:%s", v1beta1.LabelNodeClass)), Values: []*string{aws.String(nodeClass.Name)}}, + }, }, func(output *ec2.DescribeLaunchTemplatesOutput, _ bool) bool { for _, lt := range output.LaunchTemplates { - if _, err := p.ec2api.DeleteLaunchTemplateWithContext(ctx, &ec2.DeleteLaunchTemplateInput{LaunchTemplateName: lt.LaunchTemplateName}); err != nil { - logging.FromContext(ctx).With("launch-template", lt.LaunchTemplateName).Errorf("failed to delete launch template, %v", err) - deleteLTerr = multierr.Append(deleteLTerr, err) - return false - } + ltNames = append(ltNames, lt.LaunchTemplateName) } - logging.FromContext(ctx).Debugf("deleted %v launch templates", len(output.LaunchTemplates)) return true - }) - if describeErr != nil { - return fmt.Errorf("fetching launch templates, %w", describeErr) + }); err != nil { + return fmt.Errorf("fetching launch templates, %w", err) } - if deleteLTerr != nil { - return fmt.Errorf("deleting launch templates, %w", deleteLTerr) + + for _, name := range ltNames { + if _, err := p.ec2api.DeleteLaunchTemplateWithContext(ctx, &ec2.DeleteLaunchTemplateInput{LaunchTemplateName: name}); err != nil { + return fmt.Errorf("deleting launch templates, %w", err) + } } + logging.FromContext(ctx).With("launchTemplates", utils.PrettySlice(aws.StringValueSlice(ltNames), 5)).Debugf("deleted %v launch templates", len(ltNames)) return nil } diff --git a/test/suites/integration/launch_template_test.go b/test/suites/integration/launch_template_test.go new file mode 100644 index 000000000000..7bb8797967e0 --- /dev/null +++ b/test/suites/integration/launch_template_test.go @@ -0,0 +1,47 @@ +/* +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package integration_test + +import ( + "fmt" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ec2" + coretest "sigs.k8s.io/karpenter/pkg/test" + + "github.com/aws/karpenter-provider-aws/pkg/apis/v1beta1" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("Launch Template Deletion", func() { + FIt("should remove the generated Launch Templates when deleting the NodeClass", func() { + pod := coretest.Pod() + env.ExpectCreated(nodePool, nodeClass, pod) + env.EventuallyExpectHealthy(pod) + env.ExpectCreatedNodeCount("==", 1) + + env.ExpectDeleted(nodePool, nodeClass) + Eventually(func(g Gomega) { + output, _ := env.EC2API.DescribeLaunchTemplatesWithContext(env.Context, &ec2.DescribeLaunchTemplatesInput{ + Filters: []*ec2.Filter{ + {Name: aws.String(fmt.Sprintf("tag:%s", v1beta1.LabelNodeClass)), Values: []*string{aws.String(nodeClass.Name)}}, + }, + }) + g.Expect(len(output.LaunchTemplates)).To(HaveLen(0)) + }).Should(Succeed()) + }) +})