forked from seatgeek/docker-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecr_manager.go
99 lines (77 loc) · 2.04 KB
/
ecr_manager.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
package main
import (
"context"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/cenkalti/backoff"
log "github.com/sirupsen/logrus"
)
type ecrManager struct {
client *ecr.Client // AWS ECR client
repositories map[string]bool // list of repositories in ECR
}
func (e *ecrManager) exists(name string) bool {
if _, ok := e.repositories[name]; ok {
return true
}
return false
}
func (e *ecrManager) ensure(name string) error {
if e.exists(name) {
return nil
}
return e.create(name)
}
func (e *ecrManager) create(name string) error {
_, err := e.client.CreateRepository(context.Background(), &ecr.CreateRepositoryInput{
RepositoryName: &name,
})
if err != nil {
return err
}
e.repositories[name] = true
return nil
}
func (e *ecrManager) Login() (string, error) {
log.Info("Obtaining authorization token from AWS...")
output, err := e.client.GetAuthorizationToken(context.Background(), &ecr.GetAuthorizationTokenInput{})
if err != nil {
log.Errorf("Unable to obtain authorization token: %s", err)
}
var auth string
for _, authData := range output.AuthorizationData {
auth = *authData.AuthorizationToken
}
log.Info("Authorization token obtained successfully from AWS...")
return auth, err
}
func (e *ecrManager) buildCache(nextToken *string) error {
if nextToken == nil {
log.Info("Loading list of ECR repositories")
}
resp, err := e.client.DescribeRepositories(context.Background(), &ecr.DescribeRepositoriesInput{
NextToken: nextToken,
})
if err != nil {
return err
}
if e.repositories == nil {
e.repositories = make(map[string]bool)
}
for _, repo := range resp.Repositories {
e.repositories[*repo.RepositoryName] = true
}
// keep paging as long as there is a token for the next page
if resp.NextToken != nil {
e.buildCache(resp.NextToken)
}
// no next token means we hit the last page
if nextToken == nil {
log.Info("Done loading ECR repositories")
}
return nil
}
func (e *ecrManager) buildCacheBackoff() backoff.Operation {
return func() error {
return e.buildCache(nil)
}
}