Skip to content

Commit

Permalink
osbuild-service-maintenance/aws: implement removal of launch templates
Browse files Browse the repository at this point in the history
Launch templates of instances that are terminated should be removed.
  • Loading branch information
schuellerf committed Dec 4, 2024
1 parent 89e97a8 commit 63f34e1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cmd/osbuild-service-maintenance/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ func AWSCleanup(maxConcurrentRequests int, dryRun bool, accessKeyID, accessKey s
}
}

errLaunchTemplates := searchLTAndCleanup(ctx, a, dryRun)
if errLaunchTemplates != nil {
logrus.Errorf("Error in cleaning up launch templates: %v", errLaunchTemplates)
if err != nil {
err = fmt.Errorf("Multiple errors while processing AWSCleanup: %w and %w.", err, errLaunchTemplates)
}
}

return err
}

Expand Down Expand Up @@ -277,6 +285,41 @@ func checkIfInUse(reservations []ec2types.Reservation) bool {
return inUse
}

func searchLTAndCleanup(ctx context.Context, a *awscloud.AWS, dryRun bool) error {

launchTemplates, err := a.DescribeLaunchTemplatesByPrefix(ctx, "launch-template-for-i-")
if err != nil {
return err
}

for _, lt := range launchTemplates {
if lt.LaunchTemplateName == nil || lt.LaunchTemplateId == nil {
logrus.Errorf(
"Launch template needs to have a LaunchTemplateName (%v) and a LaunchTemplateId (%v).",
lt.LaunchTemplateName,
lt.LaunchTemplateId)
continue
}

reservations, err := a.DescribeInstancesByLaunchTemplateID(*lt.LaunchTemplateId)
if err != nil {
logrus.Errorf("Failed to describe launch template %s: %v", *lt.LaunchTemplateId, err)
continue
}

ltInUse := checkIfInUse(reservations)

if !ltInUse {
logrus.Infof("Deleting launch template: %s (%s)\n", *lt.LaunchTemplateName, *lt.LaunchTemplateId)
if !dryRun {
err := a.DeleteLaunchTemplateById(ctx, lt.LaunchTemplateId)

if err != nil {
logrus.Errorf("Failed to delete launch template %s: %v", *lt.LaunchTemplateId, err)
}
}
} else {
fmt.Printf("Launch template %s has non terminated instances associated with it.\n", *lt.LaunchTemplateId)
}
}
return nil
Expand Down
30 changes: 30 additions & 0 deletions internal/cloud/awscloud/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func (a *AWS) DescribeInstancesBySecurityGroupID(securityGroupID string) ([]ec2t
return a.describeInstancesByKeyValue("instance.group-id", securityGroupID)
}

func (a *AWS) DescribeInstancesByLaunchTemplateID(launchTemplateID string) ([]ec2types.Reservation, error) {
return a.describeInstancesByKeyValue("tag:aws:ec2launchtemplate:id", launchTemplateID)
}

func (a *AWS) DescribeInstancesByInstanceID(instanceID string) ([]ec2types.Reservation, error) {
res, err := a.ec2.DescribeInstances(
context.Background(),
Expand Down Expand Up @@ -132,6 +136,22 @@ func (a *AWS) DescribeSecurityGroupsByPrefix(ctx context.Context, prefix string)
return securityGroups, nil
}

func (a *AWS) DescribeLaunchTemplatesByPrefix(ctx context.Context, prefix string) ([]ec2types.LaunchTemplate, error) {
var launchTemplates []ec2types.LaunchTemplate

ltOutput, err := a.ec2.DescribeLaunchTemplates(ctx, &ec2.DescribeLaunchTemplatesInput{})
if err != nil {
return launchTemplates, fmt.Errorf("failed to describe security groups: %w", err)
}

for _, lt := range ltOutput.LaunchTemplates {
if lt.LaunchTemplateName != nil && strings.HasPrefix(*lt.LaunchTemplateName, prefix) {
launchTemplates = append(launchTemplates, lt)
}
}
return launchTemplates, nil
}

func (a *AWS) DeleteSecurityGroupById(ctx context.Context, sgID *string) error {
_, err := a.ec2.DeleteSecurityGroup(
ctx,
Expand All @@ -141,3 +161,13 @@ func (a *AWS) DeleteSecurityGroupById(ctx context.Context, sgID *string) error {
)
return err
}

func (a *AWS) DeleteLaunchTemplateById(ctx context.Context, ltID *string) error {
_, err := a.ec2.DeleteLaunchTemplate(
ctx,
&ec2.DeleteLaunchTemplateInput{
LaunchTemplateId: ltID,
},
)
return err
}

0 comments on commit 63f34e1

Please sign in to comment.