forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mappings.go
355 lines (315 loc) · 8.29 KB
/
mappings.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package mirror
import (
"bufio"
"fmt"
"os"
"strings"
"sync"
"github.com/golang/glog"
"github.com/docker/distribution/registry/client/auth"
godigest "github.com/opencontainers/go-digest"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
)
// ErrAlreadyExists may be returned by the blob Create function to indicate that the blob already exists.
var ErrAlreadyExists = fmt.Errorf("blob already exists in the target location")
type Mapping struct {
Source imageapi.DockerImageReference
Destination imageapi.DockerImageReference
Type DestinationType
}
func parseSource(ref string) (imageapi.DockerImageReference, error) {
src, err := imageapi.ParseDockerImageReference(ref)
if err != nil {
return src, fmt.Errorf("%q is not a valid image reference: %v", ref, err)
}
if len(src.Tag) == 0 && len(src.ID) == 0 {
return src, fmt.Errorf("you must specify a tag or digest for SRC")
}
return src, nil
}
func parseDestination(ref string) (imageapi.DockerImageReference, DestinationType, error) {
dstType := DestinationRegistry
switch {
case strings.HasPrefix(ref, "s3://"):
dstType = DestinationS3
ref = strings.TrimPrefix(ref, "s3://")
}
dst, err := imageapi.ParseDockerImageReference(ref)
if err != nil {
return dst, dstType, fmt.Errorf("%q is not a valid image reference: %v", ref, err)
}
if len(dst.ID) != 0 {
return dst, dstType, fmt.Errorf("you must specify a tag for DST or leave it blank to only push by digest")
}
return dst, dstType, nil
}
func parseArgs(args []string, overlap map[string]string) ([]Mapping, error) {
var remainingArgs []string
var mappings []Mapping
for _, s := range args {
parts := strings.SplitN(s, "=", 2)
if len(parts) != 2 {
remainingArgs = append(remainingArgs, s)
continue
}
if len(parts[0]) == 0 || len(parts[1]) == 0 {
return nil, fmt.Errorf("all arguments must be valid SRC=DST mappings")
}
src, err := parseSource(parts[0])
if err != nil {
return nil, err
}
dst, dstType, err := parseDestination(parts[1])
if err != nil {
return nil, err
}
if _, ok := overlap[dst.String()]; ok {
return nil, fmt.Errorf("each destination tag may only be specified once: %s", dst.String())
}
overlap[dst.String()] = src.String()
mappings = append(mappings, Mapping{Source: src, Destination: dst, Type: dstType})
}
switch {
case len(remainingArgs) > 1 && len(mappings) == 0:
src, err := parseSource(remainingArgs[0])
if err != nil {
return nil, err
}
for i := 1; i < len(remainingArgs); i++ {
if len(remainingArgs[i]) == 0 {
continue
}
dst, dstType, err := parseDestination(remainingArgs[i])
if err != nil {
return nil, err
}
if _, ok := overlap[dst.String()]; ok {
return nil, fmt.Errorf("each destination tag may only be specified once: %s", dst.String())
}
overlap[dst.String()] = src.String()
mappings = append(mappings, Mapping{Source: src, Destination: dst, Type: dstType})
}
case len(remainingArgs) == 1 && len(mappings) == 0:
return nil, fmt.Errorf("all arguments must be valid SRC=DST mappings, or you must specify one SRC argument and one or more DST arguments")
}
return mappings, nil
}
func parseFile(filename string, overlap map[string]string) ([]Mapping, error) {
var fileMappings []Mapping
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
s := bufio.NewScanner(f)
lineNumber := 0
for s.Scan() {
line := s.Text()
lineNumber++
// remove comments and whitespace
if i := strings.Index(line, "#"); i != -1 {
line = line[0:i]
}
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
args := strings.Split(line, " ")
mappings, err := parseArgs(args, overlap)
if err != nil {
return nil, fmt.Errorf("file %s, line %d: %v", filename, lineNumber, err)
}
fileMappings = append(fileMappings, mappings...)
}
if err := s.Err(); err != nil {
return nil, err
}
return fileMappings, nil
}
type key struct {
registry string
repository string
}
type DestinationType string
var (
DestinationRegistry DestinationType = "docker"
DestinationS3 DestinationType = "s3"
)
type destination struct {
t DestinationType
ref imageapi.DockerImageReference
tags []string
}
type pushTargets map[key]destination
type destinations struct {
ref imageapi.DockerImageReference
lock sync.Mutex
tags map[string]pushTargets
digests map[string]pushTargets
}
func (d *destinations) mergeIntoDigests(srcDigest godigest.Digest, target pushTargets) {
d.lock.Lock()
defer d.lock.Unlock()
srcKey := srcDigest.String()
current, ok := d.digests[srcKey]
if !ok {
d.digests[srcKey] = target
return
}
for repo, dst := range target {
existing, ok := current[repo]
if !ok {
current[repo] = dst
continue
}
existing.tags = append(existing.tags, dst.tags...)
}
}
type targetTree map[key]*destinations
func buildTargetTree(mappings []Mapping) targetTree {
tree := make(targetTree)
for _, m := range mappings {
srcKey := key{registry: m.Source.Registry, repository: m.Source.RepositoryName()}
dstKey := key{registry: m.Destination.Registry, repository: m.Destination.RepositoryName()}
src, ok := tree[srcKey]
if !ok {
src = &destinations{}
src.ref = m.Source.AsRepository()
src.digests = make(map[string]pushTargets)
src.tags = make(map[string]pushTargets)
tree[srcKey] = src
}
var current pushTargets
if tag := m.Source.Tag; len(tag) != 0 {
current = src.tags[tag]
if current == nil {
current = make(pushTargets)
src.tags[tag] = current
}
} else {
current = src.digests[m.Source.ID]
if current == nil {
current = make(pushTargets)
src.digests[m.Source.ID] = current
}
}
dst, ok := current[dstKey]
if !ok {
dst.ref = m.Destination.AsRepository()
dst.t = m.Type
}
if len(m.Destination.Tag) > 0 {
dst.tags = append(dst.tags, m.Destination.Tag)
}
current[dstKey] = dst
}
return tree
}
func addDockerRegistryScopes(scopes map[string]map[string]bool, targets map[string]pushTargets, srcKey key) {
for _, target := range targets {
for dstKey, t := range target {
m := scopes[dstKey.registry]
if m == nil {
m = make(map[string]bool)
scopes[dstKey.registry] = m
}
m[dstKey.repository] = true
if t.t != DestinationRegistry || dstKey.registry != srcKey.registry || dstKey.repository == srcKey.repository {
continue
}
m = scopes[srcKey.registry]
if m == nil {
m = make(map[string]bool)
scopes[srcKey.registry] = m
}
if _, ok := m[srcKey.repository]; !ok {
m[srcKey.repository] = false
}
}
}
}
func calculateDockerRegistryScopes(tree targetTree) map[string][]auth.Scope {
scopes := make(map[string]map[string]bool)
for srcKey, dst := range tree {
addDockerRegistryScopes(scopes, dst.tags, srcKey)
addDockerRegistryScopes(scopes, dst.digests, srcKey)
}
uniqueScopes := make(map[string][]auth.Scope)
for registry, repos := range scopes {
var repoScopes []auth.Scope
for name, push := range repos {
if push {
repoScopes = append(repoScopes, auth.RepositoryScope{Repository: name, Actions: []string{"pull", "push"}})
} else {
repoScopes = append(repoScopes, auth.RepositoryScope{Repository: name, Actions: []string{"pull"}})
}
}
uniqueScopes[registry] = repoScopes
}
return uniqueScopes
}
type workQueue struct {
ch chan workUnit
wg *sync.WaitGroup
}
func newWorkQueue(workers int, stopCh <-chan struct{}) *workQueue {
q := &workQueue{
ch: make(chan workUnit, 100),
wg: &sync.WaitGroup{},
}
go q.run(workers, stopCh)
return q
}
func (q *workQueue) run(workers int, stopCh <-chan struct{}) {
for i := 0; i < workers; i++ {
go func(i int) {
defer glog.V(4).Infof("worker %d stopping", i)
for {
select {
case work, ok := <-q.ch:
if !ok {
return
}
work.fn()
work.wg.Done()
case <-stopCh:
return
}
}
}(i)
}
<-stopCh
}
func (q *workQueue) Batch(fn func(Work)) {
w := &worker{
wg: &sync.WaitGroup{},
ch: q.ch,
}
fn(w)
w.wg.Wait()
}
func (q *workQueue) Queue(fn func(Work)) {
w := &worker{
wg: q.wg,
ch: q.ch,
}
fn(w)
}
func (q *workQueue) Done() {
q.wg.Wait()
}
type workUnit struct {
fn func()
wg *sync.WaitGroup
}
type Work interface {
Parallel(fn func())
}
type worker struct {
wg *sync.WaitGroup
ch chan workUnit
}
func (w *worker) Parallel(fn func()) {
w.wg.Add(1)
w.ch <- workUnit{wg: w.wg, fn: fn}
}