Skip to content

Commit

Permalink
Add E2E for LT deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
jigisha620 committed Jan 22, 2024
1 parent c61a008 commit 3d43d0a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 18 deletions.
33 changes: 15 additions & 18 deletions pkg/providers/launchtemplate/launchtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
47 changes: 47 additions & 0 deletions test/suites/integration/launch_template_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
})

0 comments on commit 3d43d0a

Please sign in to comment.