forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mirror.go
459 lines (415 loc) · 14.3 KB
/
mirror.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package release
import (
"bytes"
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"sync"
"time"
digest "github.com/opencontainers/go-digest"
"github.com/spf13/cobra"
"k8s.io/klog"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/util/retry"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/util/templates"
imagev1 "github.com/openshift/api/image/v1"
imageclient "github.com/openshift/client-go/image/clientset/versioned"
"github.com/openshift/origin/pkg/image/apis/image/docker10"
imagereference "github.com/openshift/origin/pkg/image/apis/image/reference"
"github.com/openshift/origin/pkg/oc/cli/image/extract"
imagemanifest "github.com/openshift/origin/pkg/oc/cli/image/manifest"
"github.com/openshift/origin/pkg/oc/cli/image/mirror"
)
// NewMirrorOptions creates the options for mirroring a release.
func NewMirrorOptions(streams genericclioptions.IOStreams) *MirrorOptions {
return &MirrorOptions{
IOStreams: streams,
ParallelOptions: imagemanifest.ParallelOptions{MaxPerRegistry: 6},
}
}
// NewMirror creates a command to mirror an existing release.
//
// Example command to mirror a release to a local repository to work offline
//
// $ oc adm release mirror \
// --from=registry.svc.ci.openshift.org/openshift/v4.0 \
// --to=mycompany.com/myrepository/repo
//
func NewMirror(f kcmdutil.Factory, parentName string, streams genericclioptions.IOStreams) *cobra.Command {
o := NewMirrorOptions(streams)
cmd := &cobra.Command{
Use: "mirror",
Short: "Mirror a release to a different image registry location",
Long: templates.LongDesc(`
Mirror an OpenShift release image to another registry
Copies the images and update payload for a given release from one registry to another.
By default this command will not alter the payload and will print out the configuration
that must be applied to a cluster to use the mirror, but you may opt to rewrite the
update to point to the new location and lose the cryptographic integrity of the update.
The common use for this command is to mirror a specific OpenShift release version to
a private registry for use in a disconnected or offline context. The command copies all
images that are part of a release into the target repository and then prints the
correct information to give to OpenShift to use that content offline. An alternate mode
is to specify --to-image-stream, which imports the images directly into an OpenShift
image stream.
`),
Run: func(cmd *cobra.Command, args []string) {
kcmdutil.CheckErr(o.Complete(cmd, f, args))
kcmdutil.CheckErr(o.Run())
},
}
flags := cmd.Flags()
o.SecurityOptions.Bind(flags)
o.ParallelOptions.Bind(flags)
flags.StringVar(&o.From, "from", o.From, "Image containing the release payload.")
flags.StringVar(&o.To, "to", o.To, "An image repository to push to.")
flags.StringVar(&o.ToImageStream, "to-image-stream", o.ToImageStream, "An image stream to tag images into.")
flags.BoolVar(&o.DryRun, "dry-run", o.DryRun, "Display information about the mirror without actually executing it.")
flags.BoolVar(&o.SkipRelease, "skip-release-image", o.SkipRelease, "Do not push the release image.")
flags.StringVar(&o.ToRelease, "to-release-image", o.ToRelease, "Specify an alternate locations for the release image instead as tag 'release' in --to")
return cmd
}
type MirrorOptions struct {
genericclioptions.IOStreams
SecurityOptions imagemanifest.SecurityOptions
ParallelOptions imagemanifest.ParallelOptions
From string
To string
ToImageStream string
ToRelease string
SkipRelease bool
DryRun bool
ClientFn func() (imageclient.Interface, string, error)
ImageStream *imagev1.ImageStream
TargetFn func(component string) imagereference.DockerImageReference
}
func (o *MirrorOptions) Complete(cmd *cobra.Command, f kcmdutil.Factory, args []string) error {
switch {
case len(args) == 0 && len(o.From) == 0:
return fmt.Errorf("must specify a release image with --from")
case len(args) == 1 && len(o.From) == 0:
o.From = args[0]
case len(args) == 1 && len(o.From) > 0:
return fmt.Errorf("you may not specify an argument and --from")
case len(args) > 1:
return fmt.Errorf("only one argument is accepted")
}
o.ClientFn = func() (imageclient.Interface, string, error) {
cfg, err := f.ToRESTConfig()
if err != nil {
return nil, "", err
}
client, err := imageclient.NewForConfig(cfg)
if err != nil {
return nil, "", err
}
ns, _, err := f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return nil, "", err
}
return client, ns, nil
}
return nil
}
const replaceComponentMarker = "X-X-X-X-X-X-X"
func (o *MirrorOptions) Run() error {
if len(o.From) == 0 && o.ImageStream == nil {
return fmt.Errorf("must specify a release image with --from")
}
if (len(o.To) == 0) == (len(o.ToImageStream) == 0) {
return fmt.Errorf("must specify an image repository or image stream to mirror the release to")
}
if o.SkipRelease && len(o.ToRelease) > 0 {
return fmt.Errorf("--skip-release-image and --to-release-image may not both be specified")
}
var recreateRequired bool
var hasPrefix bool
var targetFn func(name string) mirror.MirrorReference
var dst string
if len(o.ToImageStream) > 0 {
dst = imagereference.DockerImageReference{
Registry: "example.com",
Namespace: "somenamespace",
Name: "mirror",
}.Exact()
} else {
dst = o.To
}
if strings.Contains(dst, "${component}") {
format := strings.Replace(dst, "${component}", replaceComponentMarker, -1)
dstRef, err := mirror.ParseMirrorReference(format)
if err != nil {
return fmt.Errorf("--to must be a valid image reference: %v", err)
}
targetFn = func(name string) mirror.MirrorReference {
value := strings.Replace(dst, "${component}", name, -1)
ref, err := mirror.ParseMirrorReference(value)
if err != nil {
klog.Fatalf("requested component %q could not be injected into %s: %v", name, dst, err)
}
return ref
}
replaceCount := strings.Count(dst, "${component}")
recreateRequired = replaceCount > 1 || (replaceCount == 1 && !strings.Contains(dstRef.Tag, replaceComponentMarker))
} else {
ref, err := mirror.ParseMirrorReference(dst)
if err != nil {
return fmt.Errorf("--to must be a valid image repository: %v", err)
}
if len(ref.ID) > 0 || len(ref.Tag) > 0 {
return fmt.Errorf("--to must be to an image repository and may not contain a tag or digest")
}
targetFn = func(name string) mirror.MirrorReference {
copied := ref
copied.Tag = name
return copied
}
hasPrefix = true
}
o.TargetFn = func(name string) imagereference.DockerImageReference {
ref := targetFn(name)
return ref.DockerImageReference
}
if recreateRequired {
return fmt.Errorf("when mirroring to multiple repositories, use the new release command with --from-release and --mirror")
}
verifier := imagemanifest.NewVerifier()
is := o.ImageStream
if is == nil {
o.ImageStream = &imagev1.ImageStream{}
is = o.ImageStream
// load image references
buf := &bytes.Buffer{}
extractOpts := NewExtractOptions(genericclioptions.IOStreams{Out: buf, ErrOut: o.ErrOut})
extractOpts.SecurityOptions = o.SecurityOptions
extractOpts.ImageMetadataCallback = func(m *extract.Mapping, dgst, contentDigest digest.Digest, config *docker10.DockerImageConfig) {
verifier.Verify(dgst, contentDigest)
}
extractOpts.From = o.From
extractOpts.File = "image-references"
if err := extractOpts.Run(); err != nil {
return fmt.Errorf("unable to retrieve release image info: %v", err)
}
if err := json.Unmarshal(buf.Bytes(), &is); err != nil {
return fmt.Errorf("unable to load image-references from release payload: %v", err)
}
if is.Kind != "ImageStream" || is.APIVersion != "image.openshift.io/v1" {
return fmt.Errorf("unrecognized image-references in release payload")
}
if !verifier.Verified() {
err := fmt.Errorf("the release image failed content verification and may have been tampered with")
if !o.SecurityOptions.SkipVerification {
return err
}
fmt.Fprintf(o.ErrOut, "warning: %v\n", err)
}
}
var mappings []mirror.Mapping
if len(o.From) > 0 && !o.SkipRelease {
src := o.From
srcRef, err := imagereference.Parse(src)
if err != nil {
return err
}
if len(o.ToRelease) > 0 {
dstRef, err := imagereference.Parse(o.ToRelease)
if err != nil {
return fmt.Errorf("invalid --to-release-image: %v", err)
}
mappings = append(mappings, mirror.Mapping{
Type: mirror.DestinationRegistry,
Source: srcRef,
Destination: dstRef,
Name: o.ToRelease,
})
} else if !o.SkipRelease {
dstRef := targetFn("release")
mappings = append(mappings, mirror.Mapping{
Source: srcRef,
Type: dstRef.Type(),
Destination: dstRef.Combined(),
Name: "release",
})
}
}
// build the mapping list for mirroring and rewrite if necessary
for i := range is.Spec.Tags {
tag := &is.Spec.Tags[i]
if tag.From == nil || tag.From.Kind != "DockerImage" {
continue
}
from, err := imagereference.Parse(tag.From.Name)
if err != nil {
return fmt.Errorf("release tag %q is not valid: %v", tag.Name, err)
}
if len(from.Tag) > 0 || len(from.ID) == 0 {
return fmt.Errorf("image-references should only contain pointers to images by digest: %s", tag.From.Name)
}
dstMirrorRef := targetFn(tag.Name)
mappings = append(mappings, mirror.Mapping{
Source: from,
Type: dstMirrorRef.Type(),
Destination: dstMirrorRef.Combined(),
Name: tag.Name,
})
klog.V(2).Infof("Mapping %#v", mappings[len(mappings)-1])
dstRef := targetFn(tag.Name)
dstRef.Tag = ""
dstRef.ID = from.ID
tag.From.Name = dstRef.Exact()
}
if len(mappings) == 0 {
fmt.Fprintf(o.ErrOut, "warning: Release image contains no image references - is this a valid release?\n")
}
if len(o.ToImageStream) > 0 {
remaining := make(map[string]mirror.Mapping)
for _, mapping := range mappings {
remaining[mapping.Name] = mapping
}
client, ns, err := o.ClientFn()
if err != nil {
return err
}
hasErrors := make(map[string]error)
maxPerIteration := 12
for retries := 4; (len(remaining) > 0 || len(hasErrors) > 0) && retries > 0; {
if len(remaining) == 0 {
for _, mapping := range mappings {
if _, ok := hasErrors[mapping.Name]; ok {
remaining[mapping.Name] = mapping
delete(hasErrors, mapping.Name)
}
}
retries--
}
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
isi := &imagev1.ImageStreamImport{
ObjectMeta: metav1.ObjectMeta{
Name: o.ToImageStream,
},
Spec: imagev1.ImageStreamImportSpec{
Import: !o.DryRun,
},
}
for _, mapping := range remaining {
isi.Spec.Images = append(isi.Spec.Images, imagev1.ImageImportSpec{
From: corev1.ObjectReference{
Kind: "DockerImage",
Name: mapping.Source.Exact(),
},
To: &corev1.LocalObjectReference{
Name: mapping.Name,
},
})
if len(isi.Spec.Images) > maxPerIteration {
break
}
}
// use RESTClient directly here to be able to extend request timeout
result := &imagev1.ImageStreamImport{}
if err := client.ImageV1().RESTClient().Post().
Namespace(ns).
Resource(imagev1.Resource("imagestreamimports").Resource).
Body(isi).
// this instructs the api server to allow our request to take up to an hour - chosen as a high boundary
Timeout(3 * time.Minute).
Do().
Into(result); err != nil {
return err
}
for i, image := range result.Status.Images {
name := result.Spec.Images[i].To.Name
klog.V(4).Infof("Import result for %s: %#v", name, image.Status)
if image.Status.Status == metav1.StatusSuccess {
delete(remaining, name)
delete(hasErrors, name)
} else {
delete(remaining, name)
err := errors.FromObject(&image.Status)
hasErrors[name] = err
klog.V(2).Infof("Failed to import %s as tag %s: %v", remaining[name].Source, name, err)
}
}
return nil
})
if err != nil {
return err
}
}
if len(hasErrors) > 0 {
var messages []string
for k, v := range hasErrors {
messages = append(messages, fmt.Sprintf("%s: %v", k, v))
}
sort.Strings(messages)
if len(messages) == 1 {
return fmt.Errorf("unable to import a release image: %s", messages[0])
}
return fmt.Errorf("unable to import some release images:\n* %s", strings.Join(messages, "\n* "))
}
fmt.Fprintf(os.Stderr, "Mirrored %d images to %s/%s\n", len(mappings), ns, o.ToImageStream)
return nil
}
fmt.Fprintf(os.Stderr, "info: Mirroring %d images to %s ...\n", len(mappings), dst)
var lock sync.Mutex
opts := mirror.NewMirrorImageOptions(genericclioptions.IOStreams{Out: o.Out, ErrOut: o.ErrOut})
opts.SecurityOptions = o.SecurityOptions
opts.ParallelOptions = o.ParallelOptions
opts.Mappings = mappings
opts.DryRun = o.DryRun
opts.ManifestUpdateCallback = func(registry string, manifests map[digest.Digest]digest.Digest) error {
lock.Lock()
defer lock.Unlock()
// when uploading to a schema1 registry, manifest ids change and we must remap them
for i := range is.Spec.Tags {
tag := &is.Spec.Tags[i]
if tag.From == nil || tag.From.Kind != "DockerImage" {
continue
}
ref, err := imagereference.Parse(tag.From.Name)
if err != nil {
return fmt.Errorf("unable to parse image reference %s (%s): %v", tag.Name, tag.From.Name, err)
}
if ref.Registry != registry {
continue
}
if changed, ok := manifests[digest.Digest(ref.ID)]; ok {
ref.ID = changed.String()
klog.V(4).Infof("During mirroring, image %s was updated to digest %s", tag.From.Name, changed)
tag.From.Name = ref.Exact()
}
}
return nil
}
if err := opts.Run(); err != nil {
return err
}
to := o.ToRelease
if len(to) == 0 {
to = targetFn("release").String()
}
if hasPrefix {
fmt.Fprintf(o.Out, "\nSuccess\nUpdate image: %s\nMirror prefix: %s\n", to, o.To)
} else {
fmt.Fprintf(o.Out, "\nSuccess\nUpdate image: %s\nMirrored to: %s\n", to, o.To)
}
return nil
}
func sourceImageRef(is *imagev1.ImageStream, name string) (string, bool) {
for _, tag := range is.Spec.Tags {
if tag.Name != name {
continue
}
if tag.From == nil || tag.From.Kind != "DockerImage" {
return "", false
}
return tag.From.Name, true
}
return "", false
}