-
Notifications
You must be signed in to change notification settings - Fork 45
/
ecr_public_manager.go
84 lines (67 loc) · 1.67 KB
/
ecr_public_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
package main
import (
"context"
"github.com/aws/aws-sdk-go-v2/service/ecrpublic"
"github.com/cenkalti/backoff"
log "github.com/sirupsen/logrus"
)
type ecrPublicManager struct {
client *ecrpublic.Client // AWS public ECR client
repositories map[string]bool // list of repositories in public ECR
}
func (e *ecrPublicManager) exists(name string) bool {
if _, ok := e.repositories[name]; ok {
return true
}
return false
}
func (e *ecrPublicManager) ensure(name string) error {
if e.exists(name) {
return nil
}
return e.create(name)
}
func (e *ecrPublicManager) create(name string) error {
_, err := e.client.CreateRepository(context.TODO(), &ecrpublic.CreateRepositoryInput{
RepositoryName: &name,
})
if err != nil {
return err
}
e.repositories[name] = true
return nil
}
func (e *ecrPublicManager) buildCache(nextToken *string) error {
if nextToken == nil {
log.Info("Loading the list of ECR public repositories")
}
resp, err := e.client.DescribeRepositories(context.TODO(), &ecrpublic.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 {
err := e.buildCache(resp.NextToken)
if err != nil {
return err
}
}
// no next token means we hit the last page
if nextToken == nil {
log.Info("Done loading ECR public repositories")
}
return nil
}
func (e *ecrPublicManager) buildCacheBackoff() backoff.Operation {
return func() error {
return e.buildCache(nil)
}
}