-
Notifications
You must be signed in to change notification settings - Fork 1
/
ecr.go
92 lines (81 loc) · 2.41 KB
/
ecr.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
package services
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
)
// ECRClient holds a connection to AWS ECR.
type ECRClient struct {
client *ecr.ECR
region string
}
// NewECRClient initializes an ECRClient.
func NewECRClient(region string) *ECRClient {
return &ECRClient{
client: ecr.New(session.New(), aws.NewConfig().WithRegion(region)),
region: region,
}
}
// ListRepositories will return all ECR repositories as []string.
func (c *ECRClient) ListRepositories() ([]string, error) {
allRepositories, err := c.client.DescribeRepositories(&ecr.DescribeRepositoriesInput{})
if err != nil {
return []string{}, err
}
repositories := make([]string, 0, len(allRepositories.Repositories))
for _, repo := range allRepositories.Repositories {
repositories = append(repositories, *repo.RepositoryName)
}
return repositories, nil
}
// ListImages will return all image identifiers for a given repository.
func (c *ECRClient) ListImages(repository string) ([]*ecr.ImageIdentifier, error) {
var token *string
var imageIDs []*ecr.ImageIdentifier
for {
resp, err := c.client.ListImages(&ecr.ListImagesInput{
RepositoryName: aws.String(repository),
NextToken: token,
})
if err != nil {
return nil, err
}
imageIDs = append(imageIDs, resp.ImageIds...)
if token = resp.NextToken; token == nil {
break
}
}
return imageIDs, nil
}
// PurgeImages will batch delete images by image identitfier in sets of 100.
func (c *ECRClient) PurgeImages(repository string, images []*ecr.ImageIdentifier) error {
// No images found, back-out.
if len(images) <= 0 {
return nil
}
// Purge the images in batches of 100.
i := 0
for i = 0; i < int(len(images)/100); i++ {
err := c.BatchPurge(repository, images[i*100:(i+1)*100])
if err != nil {
return fmt.Errorf("Failed purging images in repository `%v` (%v)", repository, err)
}
}
err := c.BatchPurge(repository, images[i*100:])
if err != nil {
return fmt.Errorf("Failed purging images in repository `%v` (%v)", repository, err)
}
return err
}
// BatchPurge will batch delete images by the image identifiers.
func (c *ECRClient) BatchPurge(repository string, images []*ecr.ImageIdentifier) error {
_, err := c.client.BatchDeleteImage(&ecr.BatchDeleteImageInput{
RepositoryName: aws.String(repository),
ImageIds: images,
})
if err != nil {
return err
}
return nil
}