-
Notifications
You must be signed in to change notification settings - Fork 45
/
main.go
221 lines (184 loc) · 5.58 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/aws/aws-sdk-go-v2/service/ecrpublic"
"github.com/cenkalti/backoff"
docker "github.com/fsouza/go-dockerclient"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
const (
ecrPublicRegistryPrefix = "public.ecr.aws"
ecrPublicRegion = "us-east-1"
)
var (
config Config
isPrivateECR bool
)
// ecrManager is an interface which defines the methods ECR private or public managers should implement.
type ecrManager interface {
exists(name string) bool
ensure(name string) error
create(name string) error
buildCache(nextToken *string) error
buildCacheBackoff() backoff.Operation
}
// Config is the result of the parsed yaml file
type Config struct {
Cleanup bool `yaml:"cleanup"`
Workers int `yaml:"workers"`
Repositories []Repository `yaml:"repositories,flow"`
Target TargetConfig `yaml:"target"`
}
// TargetConfig contains info on where to mirror repositories to
type TargetConfig struct {
Registry string `yaml:"registry"`
Prefix string `yaml:"prefix"`
}
// Repository is a single docker hub repository to mirror
type Repository struct {
PrivateRegistry string `yaml:"private_registry"`
Name string `yaml:"name"`
MatchTags []string `yaml:"match_tag"`
DropTags []string `yaml:"ignore_tag"`
MaxTags int `yaml:"max_tags"`
MaxTagAge *Duration `yaml:"max_tag_age"`
RemoteTagSource string `yaml:"remote_tags_source"`
RemoteTagConfig map[string]string `yaml:"remote_tags_config"`
TargetPrefix *string `yaml:"target_prefix"`
Host string `yaml:"host"`
}
func createDockerClient() (*docker.Client, error) {
client, err := docker.NewClientFromEnv()
return client, err
}
func main() {
// log level
if rawLevel := os.Getenv("LOG_LEVEL"); rawLevel != "" {
logLevel, err := log.ParseLevel(rawLevel)
if err != nil {
log.Fatal(err)
}
log.SetLevel(logLevel)
}
// mirror file to read
configFile := "config.yaml"
if f := os.Getenv("CONFIG_FILE"); f != "" {
configFile = f
}
content, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatal(fmt.Sprintf("Could not read config file: %s", err))
}
if err := yaml.Unmarshal(content, &config); err != nil {
log.Fatal(fmt.Sprintf("Could not parse config file: %s", err))
}
if config.Target.Registry == "" {
log.Fatal("Missing `target -> registry` yaml config")
}
isPrivateECR = !strings.HasPrefix(config.Target.Registry, ecrPublicRegistryPrefix)
if config.Workers == 0 {
config.Workers = runtime.NumCPU()
}
// number of workers
if w := os.Getenv("NUM_WORKERS"); w != "" {
p, err := strconv.Atoi(w)
if err != nil {
log.Fatal(fmt.Sprintf("Could not parse NUM_WORKERS env: %s", err))
}
config.Workers = p
}
// init Docker client
log.Info("Creating Docker client")
var client DockerClient
client, err = createDockerClient()
if err != nil {
log.Fatalf("Could not create Docker client: %s", err.Error())
}
info, err := client.Info()
if err != nil {
log.Fatalf("Could not get Docker info: %s", err.Error())
}
log.Infof("Connected to Docker daemon: %s @ %s", info.Name, info.ServerVersion)
// init AWS client
log.Info("Creating AWS client")
cfg, err := awsconfig.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatalf("Unable to load AWS SDK config, " + err.Error())
}
// pre-load ECR repositories
var ecrManager ecrManager
if !isPrivateECR {
// Override the AWS region with the ecrPublicRegion for ECR authentication.
cfg.Region = ecrPublicRegion
ecrManager = &ecrPublicManager{client: ecrpublic.NewFromConfig(cfg)}
} else {
ecrManager = &ecrPrivateManager{client: ecr.NewFromConfig(cfg)}
}
backoffSettings := backoff.NewExponentialBackOff()
backoffSettings.InitialInterval = 1 * time.Second
backoffSettings.MaxElapsedTime = 10 * time.Second
notifyError := func(err error, d time.Duration) {
log.Errorf("%v (%s)", err, d.String())
}
if err = backoff.RetryNotify(ecrManager.buildCacheBackoff(), backoffSettings, notifyError); err != nil {
log.Fatalf("Could not build ECR cache: %s", err)
}
workerCh := make(chan Repository, 5)
var wg sync.WaitGroup
// start background workers
for i := 0; i < config.Workers; i++ {
go worker(&wg, workerCh, &client, ecrManager)
}
prefix := os.Getenv("PREFIX")
// add jobs for the workers
for _, repo := range config.Repositories {
if prefix != "" && !strings.HasPrefix(repo.Name, prefix) {
continue
}
wg.Add(1)
workerCh <- repo
}
// wait for all workers to complete
wg.Wait()
log.Info("Done")
}
func worker(wg *sync.WaitGroup, workerCh chan Repository, dc *DockerClient, ecrm ecrManager) {
log.Debug("Starting worker")
for {
select {
case repo := <-workerCh:
// Check if the given host is from our support list.
if repo.Host != "" && repo.Host != dockerHub && repo.Host != quay && repo.Host != gcr && repo.Host != k8s {
log.Errorf("Could not pull images from host: %s. We support %s, %s, %s, and %s", repo.Host, dockerHub, quay, gcr, k8s)
wg.Done()
continue
}
// If Host is not specified, will mirror repos from Docker Hub.
if repo.Host == "" {
repo.Host = dockerHub
}
m := mirror{
dockerClient: dc,
ecrManager: ecrm,
}
if err := m.setup(repo); err != nil {
log.Errorf("Failed to setup mirror for repository %s: %s", repo.Name, err)
wg.Done()
continue
}
m.work()
wg.Done()
}
}
}