forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
importer.go
750 lines (672 loc) · 25.1 KB
/
importer.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
package importer
import (
"fmt"
"net/url"
"runtime"
"strings"
"github.com/golang/glog"
gocontext "golang.org/x/net/context"
"github.com/docker/distribution"
"github.com/docker/distribution/manifest/manifestlist"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/manifest/schema2"
"github.com/docker/distribution/reference"
"github.com/docker/distribution/registry/api/errcode"
"github.com/docker/distribution/registry/api/v2"
godigest "github.com/opencontainers/go-digest"
kapierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/util/flowcontrol"
"github.com/openshift/api/image"
"github.com/openshift/origin/pkg/api/legacy"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
"github.com/openshift/origin/pkg/image/importer/dockerv1client"
"github.com/openshift/origin/pkg/image/util"
)
// Add a dockerregistry.Client to the passed context with this key to support v1 Docker registry importing
const ContextKeyV1RegistryClient = "v1-registry-client"
// Interface loads images into an image stream import request.
type Interface interface {
Import(ctx gocontext.Context, isi *imageapi.ImageStreamImport, stream *imageapi.ImageStream) error
}
// RepositoryRetriever fetches a Docker distribution.Repository.
type RepositoryRetriever interface {
// Repository returns a properly authenticated distribution.Repository for the given registry, repository
// name, and insecure toleration behavior.
Repository(ctx gocontext.Context, registry *url.URL, repoName string, insecure bool) (distribution.Repository, error)
}
// ImageStreamImport implements an import strategy for Docker images. It keeps a cache of images
// per distinct auth context to reduce duplicate loads. This type is not thread safe.
type ImageStreamImporter struct {
maximumTagsPerRepo int
retriever RepositoryRetriever
limiter flowcontrol.RateLimiter
digestToRepositoryCache map[gocontext.Context]map[manifestKey]*imageapi.Image
// digestToLayerSizeCache maps layer digests to size.
digestToLayerSizeCache *ImageStreamLayerCache
}
// NewImageStreamImport creates an importer that will load images from a remote Docker registry into an
// ImageStreamImport object. Limiter may be nil.
func NewImageStreamImporter(retriever RepositoryRetriever, maximumTagsPerRepo int, limiter flowcontrol.RateLimiter, cache *ImageStreamLayerCache) *ImageStreamImporter {
if limiter == nil {
limiter = flowcontrol.NewFakeAlwaysRateLimiter()
}
if cache == nil {
glog.V(5).Infof("the global layer cache is disabled")
}
return &ImageStreamImporter{
maximumTagsPerRepo: maximumTagsPerRepo,
retriever: retriever,
limiter: limiter,
digestToRepositoryCache: make(map[gocontext.Context]map[manifestKey]*imageapi.Image),
digestToLayerSizeCache: cache,
}
}
// Import tries to complete the provided isi object with images loaded from remote registries.
func (i *ImageStreamImporter) Import(ctx gocontext.Context, isi *imageapi.ImageStreamImport, stream *imageapi.ImageStream) error {
// Initialize layer size cache if not given.
if i.digestToLayerSizeCache == nil {
cache, err := NewImageStreamLayerCache(DefaultImageStreamLayerCacheSize)
if err != nil {
return err
}
i.digestToLayerSizeCache = &cache
}
// Initialize the image cache entry for a context.
if _, ok := i.digestToRepositoryCache[ctx]; !ok {
i.digestToRepositoryCache[ctx] = make(map[manifestKey]*imageapi.Image)
}
i.importImages(ctx, i.retriever, isi, stream, i.limiter)
i.importFromRepository(ctx, i.retriever, isi, i.maximumTagsPerRepo, i.limiter)
return nil
}
// importImages updates the passed ImageStreamImport object and sets Status for each image based on whether the import
// succeeded or failed. Cache is updated with any loaded images. Limiter is optional and controls how fast images are updated.
func (i *ImageStreamImporter) importImages(ctx gocontext.Context, retriever RepositoryRetriever, isi *imageapi.ImageStreamImport, stream *imageapi.ImageStream, limiter flowcontrol.RateLimiter) {
tags := make(map[manifestKey][]int)
ids := make(map[manifestKey][]int)
repositories := make(map[repositoryKey]*importRepository)
cache := i.digestToRepositoryCache[ctx]
isi.Status.Images = make([]imageapi.ImageImportStatus, len(isi.Spec.Images))
for i := range isi.Spec.Images {
spec := &isi.Spec.Images[i]
from := spec.From
if from.Kind != "DockerImage" {
continue
}
// TODO: This should be removed in 1.6
// See for more info: https://github.com/openshift/origin/pull/11774#issuecomment-258905994
var (
err error
ref imageapi.DockerImageReference
)
if from.Name != "*" {
ref, err = imageapi.ParseDockerImageReference(from.Name)
if err != nil {
isi.Status.Images[i].Status = invalidStatus("", field.Invalid(field.NewPath("from", "name"), from.Name, fmt.Sprintf("invalid name: %v", err)))
continue
}
} else {
ref = imageapi.DockerImageReference{Name: from.Name}
}
defaultRef := ref.DockerClientDefaults()
repoName := defaultRef.RepositoryName()
registryURL := defaultRef.RegistryURL()
key := repositoryKey{url: *registryURL, name: repoName}
repo, ok := repositories[key]
if !ok {
repo = &importRepository{
Ref: ref,
Registry: &key.url,
Name: key.name,
Insecure: spec.ImportPolicy.Insecure,
}
repositories[key] = repo
}
if len(defaultRef.ID) > 0 {
id := manifestKey{repositoryKey: key}
id.value = defaultRef.ID
ids[id] = append(ids[id], i)
if len(ids[id]) == 1 {
repo.Digests = append(repo.Digests, importDigest{
Name: defaultRef.ID,
Image: cache[id],
})
}
} else {
var toName string
if spec.To != nil {
toName = spec.To.Name
} else {
toName = defaultRef.Tag
}
tagReference := stream.Spec.Tags[toName]
preferArch := tagReference.Annotations[imageapi.ImporterPreferArchAnnotation]
preferOS := tagReference.Annotations[imageapi.ImporterPreferOSAnnotation]
tag := manifestKey{repositoryKey: key, preferArch: preferArch, preferOS: preferOS}
tag.value = defaultRef.Tag
tags[tag] = append(tags[tag], i)
if len(tags[tag]) == 1 {
repo.Tags = append(repo.Tags, importTag{
Name: defaultRef.Tag,
PreferArch: preferArch,
PreferOS: preferOS,
Image: cache[tag],
})
}
}
}
// for each repository we found, import all tags and digests
for key, repo := range repositories {
i.importRepositoryFromDocker(ctx, retriever, repo, limiter)
for _, tag := range repo.Tags {
j := manifestKey{repositoryKey: key, preferArch: tag.PreferArch, preferOS: tag.PreferOS}
j.value = tag.Name
if tag.Image != nil {
cache[j] = tag.Image
}
for _, index := range tags[j] {
if tag.Err != nil {
setImageImportStatus(isi, index, tag.Name, tag.Err)
continue
}
copied := *tag.Image
image := &isi.Status.Images[index]
ref := repo.Ref
ref.Tag, ref.ID = tag.Name, copied.Name
copied.DockerImageReference = ref.MostSpecific().Exact()
image.Tag = tag.Name
image.Image = &copied
image.Status.Status = metav1.StatusSuccess
}
}
for _, digest := range repo.Digests {
j := manifestKey{repositoryKey: key}
j.value = digest.Name
if digest.Image != nil {
cache[j] = digest.Image
}
for _, index := range ids[j] {
if digest.Err != nil {
setImageImportStatus(isi, index, "", digest.Err)
continue
}
image := &isi.Status.Images[index]
copied := *digest.Image
ref := repo.Ref
ref.Tag, ref.ID = "", copied.Name
copied.DockerImageReference = ref.MostSpecific().Exact()
image.Image = &copied
image.Status.Status = metav1.StatusSuccess
}
}
}
}
// importFromRepository imports the repository named on the ImageStreamImport, if any, importing up to maximumTags, and reporting
// status on each image that is attempted to be imported. If the repository cannot be found or tags cannot be retrieved, the repository
// status field is set.
func (i *ImageStreamImporter) importFromRepository(ctx gocontext.Context, retriever RepositoryRetriever, isi *imageapi.ImageStreamImport, maximumTags int, limiter flowcontrol.RateLimiter) {
if isi.Spec.Repository == nil {
return
}
cache := i.digestToRepositoryCache[ctx]
isi.Status.Repository = &imageapi.RepositoryImportStatus{}
status := isi.Status.Repository
spec := isi.Spec.Repository
from := spec.From
if from.Kind != "DockerImage" {
return
}
// TODO: This should be removed in 1.6
// See for more info: https://github.com/openshift/origin/pull/11774#issuecomment-258905994
var (
err error
ref imageapi.DockerImageReference
)
if from.Name != "*" {
ref, err = imageapi.ParseDockerImageReference(from.Name)
if err != nil {
status.Status = invalidStatus("", field.Invalid(field.NewPath("from", "name"), from.Name, fmt.Sprintf("invalid name: %v", err)))
return
}
} else {
ref = imageapi.DockerImageReference{Name: from.Name}
}
defaultRef := ref.DockerClientDefaults()
repoName := defaultRef.RepositoryName()
registryURL := defaultRef.RegistryURL()
key := repositoryKey{url: *registryURL, name: repoName}
repo := &importRepository{
Ref: ref,
Registry: &key.url,
Name: key.name,
Insecure: spec.ImportPolicy.Insecure,
MaximumTags: maximumTags,
}
i.importRepositoryFromDocker(ctx, retriever, repo, limiter)
if repo.Err != nil {
status.Status = imageImportStatus(repo.Err, "", "repository")
return
}
additional := []string{}
tagKey := manifestKey{repositoryKey: key}
for _, s := range repo.AdditionalTags {
tagKey.value = s
if image, ok := cache[tagKey]; ok {
repo.Tags = append(repo.Tags, importTag{
Name: s,
Image: image,
})
} else {
additional = append(additional, s)
}
}
status.AdditionalTags = additional
failures := 0
status.Status.Status = metav1.StatusSuccess
status.Images = make([]imageapi.ImageImportStatus, len(repo.Tags))
for i, tag := range repo.Tags {
status.Images[i].Tag = tag.Name
if tag.Err != nil {
failures++
status.Images[i].Status = imageImportStatus(tag.Err, "", "repository")
continue
}
status.Images[i].Status.Status = metav1.StatusSuccess
copied := *tag.Image
ref.Tag, ref.ID = tag.Name, copied.Name
copied.DockerImageReference = ref.MostSpecific().Exact()
status.Images[i].Image = &copied
}
if failures > 0 {
status.Status.Status = metav1.StatusFailure
status.Status.Reason = metav1.StatusReason("ImportFailed")
switch failures {
case 1:
status.Status.Message = "one of the images from this repository failed to import"
default:
status.Status.Message = fmt.Sprintf("%d of the images from this repository failed to import", failures)
}
}
}
func applyErrorToRepository(repository *importRepository, err error) {
repository.Err = err
for i := range repository.Tags {
repository.Tags[i].Err = err
}
for i := range repository.Digests {
repository.Digests[i].Err = err
}
}
func formatRepositoryError(ref imageapi.DockerImageReference, err error) error {
switch {
case isDockerError(err, v2.ErrorCodeManifestUnknown):
err = kapierrors.NewNotFound(image.Resource("dockerimage"), ref.Exact())
case isDockerError(err, errcode.ErrorCodeUnauthorized):
err = kapierrors.NewUnauthorized(fmt.Sprintf("you may not have access to the Docker image %q", ref.Exact()))
case strings.HasSuffix(err.Error(), "no basic auth credentials"):
err = kapierrors.NewUnauthorized(fmt.Sprintf("you may not have access to the Docker image %q", ref.Exact()))
case strings.HasSuffix(err.Error(), "incorrect username or password"):
err = kapierrors.NewUnauthorized(fmt.Sprintf("incorrect username or password for image %q", ref.Exact()))
}
return err
}
// calculateImageSize gets and updates size of each image layer. If manifest v2 is converted to v1,
// then it loses information about layers size. We have to get this information from server again.
func (isi *ImageStreamImporter) calculateImageSize(ctx gocontext.Context, bs distribution.BlobStore, image *imageapi.Image) error {
blobSet := sets.NewString()
size := int64(0)
for i := range image.DockerImageLayers {
layer := &image.DockerImageLayers[i]
if blobSet.Has(layer.Name) {
continue
}
blobSet.Insert(layer.Name)
if layerSize, ok := isi.digestToLayerSizeCache.Get(layer.Name); ok {
layerSize := layerSize.(int64)
layer.LayerSize = layerSize
size += layerSize
continue
}
desc, err := bs.Stat(ctx, godigest.Digest(layer.Name))
if err != nil {
return err
}
isi.digestToLayerSizeCache.Add(layer.Name, desc.Size)
layer.LayerSize = desc.Size
size += desc.Size
}
if len(image.DockerImageConfig) > 0 && !blobSet.Has(image.DockerImageMetadata.ID) {
blobSet.Insert(image.DockerImageMetadata.ID)
size += int64(len(image.DockerImageConfig))
}
image.DockerImageMetadata.Size = size
return nil
}
func manifestFromManifestList(ctx gocontext.Context, manifestList *manifestlist.DeserializedManifestList, ref imageapi.DockerImageReference, s distribution.ManifestService, preferArch, preferOS string) (distribution.Manifest, error) {
if len(manifestList.Manifests) == 0 {
return nil, fmt.Errorf("no manifests in manifest list %s", ref.Exact())
}
if preferArch == "" {
preferArch = runtime.GOARCH
}
if preferOS == "" {
preferOS = runtime.GOOS
}
var manifestDigest godigest.Digest
for _, manifestDescriptor := range manifestList.Manifests {
if manifestDescriptor.Platform.Architecture == preferArch && manifestDescriptor.Platform.OS == preferOS {
manifestDigest = manifestDescriptor.Digest
break
}
}
if manifestDigest == "" {
glog.V(5).Infof("unable to find %s/%s manifest in manifest list %s, doing conservative fail by switching to the first one: %#+v", preferOS, preferArch, ref.Exact(), manifestList.Manifests[0])
manifestDigest = manifestList.Manifests[0].Digest
}
manifest, err := s.Get(ctx, manifestDigest)
if err != nil {
glog.V(5).Infof("unable to get %s/%s manifest by digest %q for image %s: %#v", preferOS, preferArch, manifestDigest, ref.Exact(), err)
return nil, formatRepositoryError(ref, err)
}
return manifest, err
}
func (isi *ImageStreamImporter) importManifest(ctx gocontext.Context, manifest distribution.Manifest, ref imageapi.DockerImageReference, d godigest.Digest, s distribution.ManifestService, b distribution.BlobStore, preferArch, preferOS string) (image *imageapi.Image, err error) {
if manifestList, ok := manifest.(*manifestlist.DeserializedManifestList); ok {
manifest, err = manifestFromManifestList(ctx, manifestList, ref, s, preferArch, preferOS)
if err != nil {
return nil, err
}
}
if signedManifest, isSchema1 := manifest.(*schema1.SignedManifest); isSchema1 {
image, err = schema1ToImage(signedManifest, d)
} else if deserializedManifest, isSchema2 := manifest.(*schema2.DeserializedManifest); isSchema2 {
imageConfig, getImportConfigErr := b.Get(ctx, deserializedManifest.Config.Digest)
if getImportConfigErr != nil {
glog.V(5).Infof("unable to get image config by digest %q for image %s: %#v", d, ref.Exact(), getImportConfigErr)
return image, formatRepositoryError(ref, getImportConfigErr)
}
image, err = schema2ToImage(deserializedManifest, imageConfig, d)
} else {
err = fmt.Errorf("unsupported image manifest type: %T", manifest)
glog.V(5).Info(err)
}
if err != nil {
return
}
if err := util.InternalImageWithMetadata(image); err != nil {
return image, err
}
if image.DockerImageMetadata.Size == 0 {
if err := isi.calculateImageSize(ctx, b, image); err != nil {
return image, err
}
}
return
}
// importRepositoryFromDocker loads the tags and images requested in the passed importRepository, obeying the
// optional rate limiter. Errors are set onto the individual tags and digest objects.
func (isi *ImageStreamImporter) importRepositoryFromDocker(ctx gocontext.Context, retriever RepositoryRetriever, repository *importRepository, limiter flowcontrol.RateLimiter) {
glog.V(5).Infof("importing remote Docker repository registry=%s repository=%s insecure=%t", repository.Registry, repository.Name, repository.Insecure)
// retrieve the repository
repo, err := retriever.Repository(ctx, repository.Registry, repository.Name, repository.Insecure)
if err != nil {
glog.V(5).Infof("unable to access repository %#v: %#v", repository, err)
switch {
case err == reference.ErrReferenceInvalidFormat:
err = field.Invalid(field.NewPath("from", "name"), repository.Name, "the provided repository name is not valid")
case isDockerError(err, v2.ErrorCodeNameUnknown):
err = kapierrors.NewNotFound(image.Resource("dockerimage"), repository.Ref.Exact())
case isDockerError(err, errcode.ErrorCodeUnauthorized):
err = kapierrors.NewUnauthorized(fmt.Sprintf("you may not have access to the Docker image %q", repository.Ref.Exact()))
case strings.Contains(err.Error(), "tls: oversized record received with length") && !repository.Insecure:
err = kapierrors.NewBadRequest("this repository is HTTP only and requires the insecure flag to import")
case strings.HasSuffix(err.Error(), "no basic auth credentials"):
err = kapierrors.NewUnauthorized(fmt.Sprintf("you may not have access to the Docker image %q and did not have credentials to the repository", repository.Ref.Exact()))
case strings.HasSuffix(err.Error(), "does not support v2 API"):
importRepositoryFromDockerV1(ctx, repository, limiter)
return
}
applyErrorToRepository(repository, err)
return
}
// get a manifest context
s, err := repo.Manifests(ctx)
if err != nil {
glog.V(5).Infof("unable to access manifests for repository %#v: %#v", repository, err)
switch {
case isDockerError(err, v2.ErrorCodeNameUnknown):
err = kapierrors.NewNotFound(image.Resource("dockerimage"), repository.Ref.Exact())
case isDockerError(err, errcode.ErrorCodeUnauthorized):
err = kapierrors.NewUnauthorized(fmt.Sprintf("you may not have access to the Docker image %q", repository.Ref.Exact()))
case strings.HasSuffix(err.Error(), "no basic auth credentials"):
err = kapierrors.NewUnauthorized(fmt.Sprintf("you may not have access to the Docker image %q and did not have credentials to the repository", repository.Ref.Exact()))
}
applyErrorToRepository(repository, err)
return
}
// get a blob context
b := repo.Blobs(ctx)
// if repository import is requested (MaximumTags), attempt to load the tags, sort them, and request the first N
if count := repository.MaximumTags; count > 0 || count == -1 {
tags, err := repo.Tags(ctx).All(ctx)
if err != nil {
glog.V(5).Infof("unable to access tags for repository %#v: %#v", repository, err)
switch {
case isDockerError(err, v2.ErrorCodeNameUnknown):
err = kapierrors.NewNotFound(image.Resource("dockerimage"), repository.Ref.Exact())
case isDockerError(err, errcode.ErrorCodeUnauthorized):
err = kapierrors.NewUnauthorized(fmt.Sprintf("you may not have access to the Docker image %q", repository.Ref.Exact()))
}
repository.Err = err
return
}
// some images on the Hub have empty tags - treat those as "latest"
set := sets.NewString(tags...)
if set.Has("") {
set.Delete("")
set.Insert(imageapi.DefaultImageTag)
}
tags = set.List()
// include only the top N tags in the result, put the rest in AdditionalTags
imageapi.PrioritizeTags(tags)
for _, s := range tags {
if count <= 0 && repository.MaximumTags != -1 {
repository.AdditionalTags = append(repository.AdditionalTags, s)
continue
}
count--
repository.Tags = append(repository.Tags, importTag{
Name: s,
})
}
}
// load digests
for i := range repository.Digests {
importDigest := &repository.Digests[i]
if importDigest.Err != nil || importDigest.Image != nil {
continue
}
d, err := godigest.Parse(importDigest.Name)
if err != nil {
importDigest.Err = err
continue
}
ref := repository.Ref
ref.Tag = ""
ref.ID = string(d)
limiter.Accept()
manifest, err := s.Get(ctx, d)
if err != nil {
glog.V(5).Infof("unable to get manifest by digest %q for image %s: %#v", d, ref.Exact(), err)
importDigest.Err = formatRepositoryError(ref, err)
continue
}
importDigest.Image, importDigest.Err = isi.importManifest(ctx, manifest, ref, d, s, b, "", "")
}
for i := range repository.Tags {
importTag := &repository.Tags[i]
if importTag.Err != nil || importTag.Image != nil {
continue
}
ref := repository.Ref
ref.Tag = importTag.Name
ref.ID = ""
limiter.Accept()
manifest, err := s.Get(ctx, "", distribution.WithTag(importTag.Name))
if err != nil {
glog.V(5).Infof("unable to get manifest by tag %q for image %s: %#v", importTag.Name, ref.Exact(), err)
importTag.Err = formatRepositoryError(ref, err)
continue
}
importTag.Image, importTag.Err = isi.importManifest(ctx, manifest, ref, "", s, b, importTag.PreferArch, importTag.PreferOS)
}
}
func importRepositoryFromDockerV1(ctx gocontext.Context, repository *importRepository, limiter flowcontrol.RateLimiter) {
value := ctx.Value(ContextKeyV1RegistryClient)
if value == nil {
err := kapierrors.NewForbidden(image.Resource(""), "", fmt.Errorf("registry %q does not support the v2 Registry API", repository.Registry.Host))
err.ErrStatus.Reason = "NotV2Registry"
applyErrorToRepository(repository, err)
return
}
client, ok := value.(dockerv1client.Client)
if !ok {
err := kapierrors.NewForbidden(image.Resource(""), "", fmt.Errorf("registry %q does not support the v2 Registry API", repository.Registry.Host))
err.ErrStatus.Reason = "NotV2Registry"
return
}
conn, err := client.Connect(repository.Registry.Host, repository.Insecure)
if err != nil {
applyErrorToRepository(repository, err)
return
}
// if repository import is requested (MaximumTags), attempt to load the tags, sort them, and request the first N
if count := repository.MaximumTags; count > 0 || count == -1 {
tagMap, err := conn.ImageTags(repository.Ref.Namespace, repository.Ref.Name)
if err != nil {
repository.Err = err
return
}
tags := make([]string, 0, len(tagMap))
for tag := range tagMap {
tags = append(tags, tag)
}
// some images on the Hub have empty tags - treat those as "latest"
set := sets.NewString(tags...)
if set.Has("") {
set.Delete("")
set.Insert(imageapi.DefaultImageTag)
}
tags = set.List()
// include only the top N tags in the result, put the rest in AdditionalTags
imageapi.PrioritizeTags(tags)
for _, s := range tags {
if count <= 0 && repository.MaximumTags != -1 {
repository.AdditionalTags = append(repository.AdditionalTags, s)
continue
}
count--
repository.Tags = append(repository.Tags, importTag{
Name: s,
})
}
}
// load digests
for i := range repository.Digests {
importDigest := &repository.Digests[i]
if importDigest.Err != nil || importDigest.Image != nil {
continue
}
limiter.Accept()
image, err := conn.ImageByID(repository.Ref.Namespace, repository.Ref.Name, importDigest.Name)
if err != nil {
importDigest.Err = err
continue
}
// we do not preserve manifests of legacy images
importDigest.Image, err = schema0ToImage(image)
if err != nil {
importDigest.Err = err
continue
}
}
for i := range repository.Tags {
importTag := &repository.Tags[i]
if importTag.Err != nil || importTag.Image != nil {
continue
}
limiter.Accept()
image, err := conn.ImageByTag(repository.Ref.Namespace, repository.Ref.Name, importTag.Name)
if err != nil {
importTag.Err = err
continue
}
// we do not preserve manifests of legacy images
importTag.Image, err = schema0ToImage(image)
if err != nil {
importTag.Err = err
continue
}
}
}
type importTag struct {
Name string
PreferArch string
PreferOS string
Image *imageapi.Image
Err error
}
type importDigest struct {
Name string
Image *imageapi.Image
Err error
}
type importRepository struct {
Ref imageapi.DockerImageReference
Registry *url.URL
Name string
Insecure bool
Tags []importTag
Digests []importDigest
MaximumTags int
AdditionalTags []string
Err error
}
// repositoryKey is the key used to cache information loaded from a remote Docker repository.
type repositoryKey struct {
// The URL of the server
url url.URL
// The name of the image repository (contains both namespace and path)
name string
}
// manifestKey is a key for a map between a Docker image tag or image ID and a retrieved imageapi.Image, used
// to ensure we don't fetch the same image multiple times.
type manifestKey struct {
repositoryKey
// The tag or ID of the image, not used within the same map
value string
// An architecture of the image which should be selected from a manifest list.
preferArch string
// An operation system of the image which should be selected from a manifest list.
preferOS string
}
func imageImportStatus(err error, kind, position string) metav1.Status {
switch t := err.(type) {
case kapierrors.APIStatus:
return t.Status()
case *field.Error:
return kapierrors.NewInvalid(image.Kind(kind), position, field.ErrorList{t}).ErrStatus
default:
return kapierrors.NewInternalError(err).ErrStatus
}
}
func setImageImportStatus(images *imageapi.ImageStreamImport, i int, tag string, err error) {
images.Status.Images[i].Tag = tag
images.Status.Images[i].Status = imageImportStatus(err, "", "")
}
func invalidStatus(position string, errs ...*field.Error) metav1.Status {
return kapierrors.NewInvalid(legacy.Kind(""), position, errs).ErrStatus
}