-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
138 lines (113 loc) · 4.31 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
"gopkg.in/Clever/kayvee-go.v6/logger"
"github.com/Clever/prune-images/config"
)
var (
kv = logger.New("prune-images")
)
func pruneRepos() error {
kv.InfoD("pruning-repos", logger.M{"dry-run": config.DryRun, "minImages": config.MinImagesInRepo})
regions := strings.Split(os.Getenv("REGIONS"), ",")
if len(regions) == 0 {
return fmt.Errorf("env variable REGIONS not set")
}
// Prune repositories per region
for _, region := range config.Regions {
kv.DebugD("ecr-prune-region", logger.M{"region": region})
awsConfig := aws.NewConfig().WithMaxRetries(10)
awsConfig.Region = ®ion
ecrClient := ecr.New(session.New(), awsConfig)
var pruneRepoErr error
// Func to fetch and prune each repository thru pagination
pruneRepos := func(output *ecr.DescribeRepositoriesOutput, lastPage bool) bool {
for _, repo := range output.Repositories {
if err := pruneRepo(ecrClient, repo); err != nil {
pruneRepoErr = fmt.Errorf("failed to prune repo %v: %v", *repo.RepositoryName, err.Error())
return false
}
}
return !lastPage
}
// Get and prune repositories in region
err := ecrClient.DescribeRepositoriesPages(&ecr.DescribeRepositoriesInput{}, pruneRepos)
if err != nil {
return fmt.Errorf("failed to get repositories in region %v: %v", region, err.Error())
}
if pruneRepoErr != nil {
return pruneRepoErr
}
}
return nil
}
func pruneRepo(ecrClient *ecr.ECR, repo *ecr.Repository) error {
kv.DebugD("ecr-get-repo-images", logger.M{"repo": *repo.RepositoryName, "registry": *repo.RegistryId})
images := []*ecr.ImageDetail{}
weekAgo := time.Now().Add(-1 * 7 * 25 * time.Hour)
// Get all images in repo thru pagination
err := ecrClient.DescribeImagesPages(&ecr.DescribeImagesInput{
RegistryId: repo.RegistryId,
RepositoryName: repo.RepositoryName,
}, func(output *ecr.DescribeImagesOutput, lastPage bool) bool {
images = append(images, output.ImageDetails...)
return !lastPage
})
if err != nil {
kv.WarnD("failed-to-get-repo-images", logger.M{"error": err.Error(), "repo": *repo.RepositoryName, "registry": *repo.RegistryId})
return fmt.Errorf("failed-to-get-repo-images: %v", err.Error())
}
// If the image limit is not reached, skip
if len(images) <= config.MinImagesInRepo {
kv.InfoD("ecr-skip-prune-repo", logger.M{"repo": *repo.RepositoryName, "registry": *repo.RegistryId, "image_count": len(images)})
return nil
}
kv.InfoD("ecr-prune-repo", logger.M{"repo": *repo.RepositoryName, "registry": *repo.RegistryId, "image_count": len(images)})
// Sort the images from most recent to least recent
sort.Slice(images, func(i, j int) bool {
return images[i].ImagePushedAt.Unix() > images[j].ImagePushedAt.Unix()
})
imagesToDelete := []*ecr.ImageIdentifier{}
for i := config.MinImagesInRepo; i < len(images); i++ {
image := images[i]
// Only remove images added more than 1 week ago.
if image.ImagePushedAt.Before(weekAgo) {
kv.DebugD("image-to-delete", logger.M{"repo": *repo.RepositoryName, "registry": *repo.RegistryId, "image": *image.ImageDigest})
imagesToDelete = append(imagesToDelete, &ecr.ImageIdentifier{
ImageDigest: image.ImageDigest,
})
}
}
if !config.DryRun {
// Delete images in batches of 100, the maximum amount for BatchDeleteImage.
// https://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_BatchDeleteImage.html
maxItems := 100
for len(imagesToDelete) > 0 {
batchToDelete := make([]*ecr.ImageIdentifier, len(imagesToDelete))
copy(batchToDelete, imagesToDelete)
if len(imagesToDelete) >= maxItems {
batchToDelete = batchToDelete[:maxItems]
imagesToDelete = imagesToDelete[maxItems:]
} else {
imagesToDelete = []*ecr.ImageIdentifier{}
}
batchDeleteOutput, err := ecrClient.BatchDeleteImage(&ecr.BatchDeleteImageInput{
ImageIds: batchToDelete,
RegistryId: repo.RegistryId,
RepositoryName: repo.RepositoryName,
})
if err != nil {
return fmt.Errorf("failed to delete ECR repository images %s: %s", *repo.RepositoryName, err.Error())
}
kv.InfoD("erc-batch-delete-images", logger.M{"count": len(batchDeleteOutput.ImageIds), "repo": *repo.RepositoryName, "registry": *repo.RegistryId})
}
}
return nil
}