forked from seatgeek/docker-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecr_private_manager.go
82 lines (64 loc) · 1.59 KB
/
ecr_private_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
package main
import (
"context"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/cenkalti/backoff"
log "github.com/sirupsen/logrus"
)
type ecrPrivateManager struct {
client *ecr.Client // AWS ECR client
repositories map[string]bool // list of repositories in ECR
}
func (e *ecrPrivateManager) exists(name string) bool {
if _, ok := e.repositories[name]; ok {
return true
}
return false
}
func (e *ecrPrivateManager) ensure(name string) error {
if e.exists(name) {
return nil
}
return e.create(name)
}
func (e *ecrPrivateManager) create(name string) error {
_, err := e.client.CreateRepository(context.TODO(), &ecr.CreateRepositoryInput{
RepositoryName: &name,
})
if err != nil {
return err
}
e.repositories[name] = true
return nil
}
func (e *ecrPrivateManager) buildCache(nextToken *string) error {
if nextToken == nil {
log.Info("Loading list of ECR repositories")
}
resp, err := e.client.DescribeRepositories(context.TODO(), &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 *ecrPrivateManager) buildCacheBackoff() backoff.Operation {
return func() error {
return e.buildCache(nil)
}
}