-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimagesync.go
277 lines (250 loc) · 7.78 KB
/
imagesync.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
package imagesync
import (
"context"
"errors"
"fmt"
"os"
"regexp"
"strings"
"sync"
"github.com/containers/image/v5/copy"
"github.com/containers/image/v5/docker"
dockerarchive "github.com/containers/image/v5/docker/archive"
ociarchive "github.com/containers/image/v5/oci/archive"
ocilayout "github.com/containers/image/v5/oci/layout"
"github.com/containers/image/v5/signature"
"github.com/containers/image/v5/types"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var Version string
var ErrInvalidTag = errors.New("invalid tag")
func Execute() error {
app := cli.NewApp()
app.Name = "imagesync"
app.Usage = "Sync images in registries."
app.Version = Version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "src, s",
Usage: "Reference for the source image/repository.",
Required: true,
},
cli.BoolFlag{
Name: "src-strict-tls",
Usage: "Enable strict TLS for connections to source registry.",
},
cli.StringFlag{
Name: "dest, d",
Usage: "Reference for the destination repository.",
Required: true,
},
cli.BoolFlag{
Name: "dest-strict-tls",
Usage: "Enable strict TLS for connections to destination registry.",
},
cli.StringFlag{
Name: "tags-pattern",
Usage: "Regex pattern to select tags for syncing.",
},
cli.StringFlag{
Name: "skip-tags-pattern",
Usage: "Regex pattern to exclude tags.",
},
cli.StringFlag{
Name: "skip-tags",
Usage: "Comma separated list of tags to be skipped.",
},
cli.BoolFlag{
Name: "overwrite",
Usage: "Use this to copy/override all the tags.",
},
cli.IntFlag{
Name: "max-concurrent-tags",
Usage: "Maximum number of tags to be synced/copied in parallel.",
Value: 1,
},
}
app.Action = cli.ActionFunc(DetectAndCopyImage)
if err := app.Run(os.Args); err != nil {
return err
}
return nil
}
// DetectAndCopyImage will try to detect the source type and will
// copy the image. Detection is based on following rules if:
//
// - src is a directory assume it is an OCI layout.
// - src is file detect for oci-archive or docker-archive.
// - src is an image with a tag copy single image to dest.
// - none of the above then it is an entire repository sync
// to sync the repositories.
func DetectAndCopyImage(c *cli.Context) error {
dest := c.String("dest")
destRef, err := docker.ParseReference(fmt.Sprintf("//%s", dest))
if err != nil {
return fmt.Errorf("parsing destination ref: %w", err)
}
// setup copy options
opts := copy.Options{
ReportWriter: os.Stdout,
ImageListSelection: copy.CopyAllImages,
}
if !c.Bool("dest-strict-tls") {
opts.DestinationCtx = &types.SystemContext{DockerInsecureSkipTLSVerify: types.NewOptionalBool(true)}
}
if !c.Bool("src-strict-tls") {
opts.SourceCtx = &types.SystemContext{DockerInsecureSkipTLSVerify: types.NewOptionalBool(true)}
}
ctx := context.Background()
src := c.String("src")
if info, err := os.Stat(src); err == nil {
// copy oci layout
if info.IsDir() {
srcRef, err := ocilayout.ParseReference(src)
if err != nil {
return fmt.Errorf("parsing source oci ref: %w", err)
}
if err = copyImage(ctx, destRef, srcRef, &opts); err != nil {
return fmt.Errorf("copy oci layout: %w", err)
}
logrus.Info("Image(s) sync completed.")
return nil
}
// try copying oci archive with docker archive as fallback
srcRef, _ := ociarchive.ParseReference(src)
if err = copyImage(ctx, destRef, srcRef, &opts); err != nil {
srcRef, err = dockerarchive.ParseReference(src)
if err != nil {
return fmt.Errorf("parsing source docker-archive ref: %w", err)
}
if err = copyImage(ctx, destRef, srcRef, &opts); err != nil {
return fmt.Errorf("copy docker-archive layout: %w", err)
}
}
} else {
// copy single tag sync entire repository
srcRef, err := docker.ParseReference(fmt.Sprintf("//%s", src))
if err != nil {
return fmt.Errorf("parsing source docker ref: %w", err)
}
if hasTag(src, srcRef) {
if err = copyImage(ctx, destRef, srcRef, &opts); err != nil {
return fmt.Errorf("copy tag: %w", err)
}
} else {
if hasTag(dest, destRef) {
return fmt.Errorf("tag shouldn't be provided in dest: %w", ErrInvalidTag)
}
if err = copyRepository(ctx, c, destRef, srcRef, opts); err != nil {
return fmt.Errorf("copy repository: %w", err)
}
}
}
logrus.Info("Image(s) sync completed.")
return nil
}
func copyRepository(ctx context.Context, cliCtx *cli.Context, destRepository, srcRepository types.ImageReference, opts copy.Options) error {
srcTags, err := docker.GetRepositoryTags(ctx, opts.SourceCtx, srcRepository)
if err != nil {
return fmt.Errorf("getting source tags: %w", err)
}
// skip tags
shouldSkip := cliCtx.String("skip-tags")
if shouldSkip != "" {
srcTags = subtract(srcTags, strings.Split(shouldSkip, ","))
}
// match tags
if pattern := cliCtx.String("tags-pattern"); pattern != "" {
re, err := regexp.Compile(pattern)
if err != nil {
return fmt.Errorf("%q is not valid regexp", pattern)
}
srcTags = lo.Filter(srcTags, func(item string, index int) bool { return re.MatchString(item) })
}
// exclude tags
if pattern := cliCtx.String("skip-tags-pattern"); pattern != "" {
re, err := regexp.Compile(pattern)
if err != nil {
return fmt.Errorf("%q is not valid regexp", pattern)
}
srcTags = lo.Filter(srcTags, func(item string, index int) bool { return !re.MatchString(item) })
}
var tags []string
destTags, err := docker.GetRepositoryTags(ctx, opts.DestinationCtx, destRepository)
if cliCtx.Bool("overwrite") || err != nil {
tags = srcTags
} else {
tags = subtract(srcTags, destTags)
}
if len(tags) == 0 {
logrus.Info("Image in repositories are already synced")
os.Exit(0)
}
logrus.Infof("Starting image sync with total-tags=%d tags=%v source=%s destination=%s", len(tags), tags, srcRepository.DockerReference().Name(), destRepository.DockerReference().Name())
// limit the go routines to avoid 429 on registries
maxConcurrentTags := cliCtx.Int("max-concurrent-tags")
numberOfConcurrentTags := maxConcurrentTags
if len(tags) < maxConcurrentTags {
numberOfConcurrentTags = len(tags)
}
// sync repository by copying each tag. Errors are ignored on purpose
// and only warning are shown via ReportWriter for failing tags.
var wg sync.WaitGroup
ch := make(chan string, len(tags))
wg.Add(numberOfConcurrentTags)
for i := 0; i < numberOfConcurrentTags; i++ {
go func() {
for {
tag, ok := <-ch
if !ok {
wg.Done()
return
}
destTagRef, err := docker.ParseReference(fmt.Sprintf("//%s:%s", cliCtx.String("dest"), tag))
if err != nil {
logrus.Warnf("failed parsing dest ref: %s", err)
}
srcTagRef, err := docker.ParseReference(fmt.Sprintf("//%s:%s", cliCtx.String("src"), tag))
if err != nil {
logrus.Warnf("failed parsing src ref: %s", err)
}
if err = copyImage(ctx, destTagRef, srcTagRef, &opts); err != nil {
logrus.Warnf("failed copying image: %s", err)
}
}
}()
}
for _, tag := range tags {
ch <- tag
}
close(ch)
wg.Wait()
return nil
}
func copyImage(ctx context.Context, destRef, srcRef types.ImageReference, opts *copy.Options) error {
policyContext, err := signature.NewPolicyContext(&signature.Policy{
Default: []signature.PolicyRequirement{signature.NewPRInsecureAcceptAnything()},
})
if err != nil {
return fmt.Errorf("creating policy context: %w", err)
}
if _, err = copy.Image(ctx, policyContext, destRef, srcRef, opts); err != nil {
return fmt.Errorf("copying image: %w", err)
}
return nil
}
func hasTag(ref string, imageRef types.ImageReference) bool {
return strings.HasSuffix(imageRef.DockerReference().String(), ref)
}
func subtract(ts1 []string, ts2 []string) []string {
var diff []string
for _, term := range ts1 {
if lo.Contains(ts2, term) {
continue
}
diff = append(diff, term)
}
return diff
}