Skip to content

Commit

Permalink
[CleanAMI] Ensure at least 1 AMI exists for each test image architect…
Browse files Browse the repository at this point in the history
…ure (#1452)
  • Loading branch information
lisguo authored Dec 3, 2024
1 parent 748073f commit b8531ce
Showing 1 changed file with 59 additions and 25 deletions.
84 changes: 59 additions & 25 deletions tool/clean/clean_ami/clean_ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"fmt"
"log"
"sort"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -24,6 +23,36 @@ import (
"github.com/aws/amazon-cloudwatch-agent/tool/clean"
)

// Image Prefixes are taken from checking the Image Builder Pipelines in us-west-2
var imagePrefixes = []string{
"cloudwatch-agent-integration-test-aarch64-al2023",
"cloudwatch-agent-integration-test-al2",
"cloudwatch-agent-integration-test-alma-linux-8",
"cloudwatch-agent-integration-test-alma-linux-9",
"cloudwatch-agent-integration-test-arm64-al2",
"cloudwatch-agent-integration-test-debian-11-arm64",
"cloudwatch-agent-integration-test-debian-12-arm64",
"cloudwatch-agent-integration-test-nvidia-gpu-al2",
"cloudwatch-agent-integration-test-ol7",
"cloudwatch-agent-integration-test-ol8",
"cloudwatch-agent-integration-test-ol9",
"cloudwatch-agent-integration-test-rocky-linux-8",
"cloudwatch-agent-integration-test-rocky-linux-9",
"cloudwatch-agent-integration-test-sles-15",
"cloudwatch-agent-integration-test-ubuntu-23",
"cloudwatch-agent-integration-test-ubuntu-24",
"cloudwatch-agent-integration-test-ubuntu",
"cloudwatch-agent-integration-test-ubuntu-LTS-22",
"cloudwatch-agent-integration-test-win-10",
"cloudwatch-agent-integration-test-win-11",
"cloudwatch-agent-integration-test-win-2016",
"cloudwatch-agent-integration-test-win-2019",
"cloudwatch-agent-integration-test-win-2022",
"cloudwatch-agent-integration-test-x86-al2023",
"cloudwatch-agent-integration-test-mac",
"cloudwatch-agent-integration-test-nvidia-gpu",
}

func main() {
err := cleanAMIs()
if err != nil {
Expand Down Expand Up @@ -137,38 +166,43 @@ func cleanAMIs() error {
}
ec2client := ec2.NewFromConfig(defaultConfig)

// Get list of ami
nameFilter := types.Filter{Name: aws.String("name"), Values: []string{
"cloudwatch-agent-integration-test*",
}}

//get instances to delete
describeImagesInput := ec2.DescribeImagesInput{Filters: []types.Filter{nameFilter}}
describeImagesOutput, err := ec2client.DescribeImages(ctx, &describeImagesInput)
if err != nil {
return err
}

var errList []error
// stores a list of AMIs per each macos version/architecture
macosImageAmiMap := make(map[string][]types.Image)

for _, image := range describeImagesOutput.Images {
if image.Name != nil && strings.HasPrefix(*image.Name, "cloudwatch-agent-integration-test-mac") {
// mac image - add it to the map and do nothing else for now
macosImageAmiMap[*image.Name] = append(macosImageAmiMap[*image.Name], image)
} else {
// non mac image - clean it if it's older than 60 days
cleanNonMacAMIs(ctx, ec2client, image, expirationDate, &errList)
// Cleanup for each AMI image type
var errList []error
for _, filter := range imagePrefixes {
nameFilter := types.Filter{Name: aws.String("name"), Values: []string{
fmt.Sprintf("%s*", filter),
}}

//get instances to delete
describeImagesInput := ec2.DescribeImagesInput{Filters: []types.Filter{nameFilter}}
describeImagesOutput, err := ec2client.DescribeImages(ctx, &describeImagesInput)
if err != nil {
log.Printf("Image filter %s returned an error, skipping :%v", filter, err.Error())
continue
}

log.Printf("%s: %d images found", filter, len(describeImagesOutput.Images))
if len(describeImagesOutput.Images) <= 1 {
log.Printf("1 or less image found for filter %s, skipping", filter)
continue
}

for _, image := range describeImagesOutput.Images {
if image.Name != nil && filter == "cloudwatch-agent-integration-test-mac" {
// mac image - add it to the map and do nothing else for now
macosImageAmiMap[*image.Name] = append(macosImageAmiMap[*image.Name], image)
} else {
// non mac image - clean it if it's older than 60 days
cleanNonMacAMIs(ctx, ec2client, image, expirationDate, &errList)
}
}
}

// handle the mac AMIs
cleanMacAMIs(ctx, ec2client, macosImageAmiMap, expirationDate, &errList)

if len(errList) != 0 {
return fmt.Errorf("%v", errList)
}

return nil
}

0 comments on commit b8531ce

Please sign in to comment.