-
Notifications
You must be signed in to change notification settings - Fork 7
/
destination.go
479 lines (432 loc) · 11.3 KB
/
destination.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
package destination
import (
"context"
"encoding/json"
"errors"
"fmt"
"path"
"strings"
"github.com/cnrancher/hangar/pkg/hangar/archive"
"github.com/cnrancher/hangar/pkg/manifest"
"github.com/cnrancher/hangar/pkg/types"
imagemanifest "github.com/containers/image/v5/manifest"
"github.com/containers/image/v5/transports/alltransports"
imagetypes "github.com/containers/image/v5/types"
"github.com/opencontainers/go-digest"
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
)
// Destination represents the destination of the image to be copied。
// The type of the destination image can be:
// docker, docker-daemon, oci or dir
// (docker-archive won't be supported by hangar)
type Destination struct {
// imageType
imageType types.ImageType
// directory
directory string
// registry
registry string
// project (namespace)
project string
// image name
name string
// tag
tag string
// referenceName is the image reference with transport
referenceName string
// mime is the MIME type of image.
// the mime will be empty string if destination image does not exists.
mime string
// if mime is DockerV2ListMediaType
schema2List *imagemanifest.Schema2List
// if mime is MediaTypeImageIndex
ociIndex *imgspecv1.Index
systemCtx *imagetypes.SystemContext
}
// Option is used for create the Destination object.
type Option struct {
// Image Type.
Type types.ImageType
// Directory, need to provide if Type is dir / oci
Directory string
// Registry, need to provide if Type is docker / docker-daemon
Registry string
// Project (also called namespace on some public cloud providers),
// need to provide if Type is docker / docker-daemon
Project string
// Image Name, need to provide if Type is docker / docker-daemon
Name string
// Image Tag, need to provide if Type is docker / docker-daemon
Tag string
SystemContext *imagetypes.SystemContext
}
// NewDestination is the constructor to create a Destination object.
func NewDestination(o *Option) (*Destination, error) {
var (
d *Destination
err error
)
switch o.Type {
case types.TypeDocker:
d, err = newDestinationFromDocker(o)
if err != nil {
return nil, err
}
case types.TypeDockerDaemon:
d, err = newDestinationFromDockerDaemon(o)
if err != nil {
return nil, err
}
case types.TypeOci:
d, err = newDestinationFromOci(o)
if err != nil {
return nil, err
}
case types.TypeDir:
d, err = newDestinationFromDir(o)
if err != nil {
return nil, err
}
default:
return nil, types.ErrInvalidType
}
return d, nil
}
func (d *Destination) Init(ctx context.Context) error {
err := d.initReferenceName()
if err != nil {
return err
}
// Ignore other error
if err = d.initManifest(ctx); err != nil {
if errors.Is(err, context.Canceled) ||
errors.Is(err, context.DeadlineExceeded) ||
strings.Contains(err.Error(), "timeout") {
return err
}
}
return nil
}
// Type returns the type of the image
func (d *Destination) Type() types.ImageType {
return d.imageType
}
func (d *Destination) Directory() string {
return d.directory
}
// ReferenceName returns the reference name with transport of the source image.
//
// Example:
// docker://docker.io/library/hello-world:latest
// docker-daemon://docker.io/library/nginx:1.23
// oci:./path/to/oci-image
func (d *Destination) ReferenceName() string {
return d.referenceName
}
func (d *Destination) MultiArchTag(os, osVersion, arch, variant string) string {
if osVersion != "" {
return fmt.Sprintf("%s-%s-%s-%s%s",
d.referenceName, os, osVersion, arch, variant)
}
return fmt.Sprintf("%s-%s-%s%s", d.referenceName, os, arch, variant)
}
// ReferenceName returns the multi-arch (os, variant) reference name
// with transport of the source image.
//
// Example:
// docker://docker.io/library/hello-world:latest-linux-amd64
// docker://docker.io/library/example:latest-windows-10.0.14393.1066-amd64
// docker-daemon://docker.io/library/nginx:1.23-linux-arm64
// oci:./path/to/oci-image/<sha256sum>
// dir:./path/to/image/<sha256sum>
func (d *Destination) ReferenceNameMultiArch(
os, osVersion, arch, variant, sha256sum string,
) string {
switch d.imageType {
case types.TypeDir,
types.TypeOci:
return path.Join(d.referenceName, sha256sum)
default:
return d.MultiArchTag(os, osVersion, arch, variant)
}
}
func (d *Destination) Reference() (imagetypes.ImageReference, error) {
return alltransports.ParseImageName(d.referenceName)
}
func (d *Destination) ReferenceMultiArch(
os, osVersion, arch, variant, sha256sum string,
) (imagetypes.ImageReference, error) {
refName := d.ReferenceNameMultiArch(os, osVersion, arch, variant, sha256sum)
return alltransports.ParseImageName(refName)
}
func (d *Destination) ReferenceNameWithoutTransport() string {
prefix := d.imageType.Transport()
if prefix == "" {
return ""
}
return strings.TrimPrefix(d.referenceName, prefix)
}
func (d *Destination) ReferenceNameDigest(dig digest.Digest) string {
return fmt.Sprintf("%s@%s",
strings.TrimSuffix(d.referenceName, ":"+d.tag), dig.String())
}
func (d *Destination) MIME() string {
return d.mime
}
func (d *Destination) InspectRAW(ctx context.Context) ([]byte, string, error) {
inspector, err := manifest.NewInspector(ctx, &manifest.InspectorOption{
ReferenceName: d.referenceName,
})
if err != nil {
return nil, "", fmt.Errorf("newInspector: %w", err)
}
defer inspector.Close()
m, mime, err := inspector.Raw(ctx)
if err != nil {
return nil, "", err
}
// Refresh the cached MIME.
d.mime = mime
return m, mime, err
}
// Exists checks the destination image is exists or not.
func (d *Destination) Exists() bool {
return d.mime != ""
}
func (d *Destination) SystemContext() *imagetypes.SystemContext {
return d.systemCtx
}
func (d *Destination) initReferenceName() error {
switch d.imageType {
case types.TypeDocker:
// docker://docker-reference
// example: docker://docker.io/library/nginx:1.23
d.referenceName = fmt.Sprintf("%s%s/%s/%s:%s",
d.imageType.Transport(),
d.registry, d.project, d.name, d.tag)
case types.TypeDockerDaemon:
// docker-daemon:docker-reference
// example: docker-daemon://docker.io/library/nginx:1.23
d.referenceName = fmt.Sprintf("%s%s/%s/%s:%s",
d.imageType.Transport(),
d.registry, d.project, d.name, d.tag)
case types.TypeDir:
// dir:path
// example: dir:path/to/image/
d.referenceName = fmt.Sprintf("%s%s",
d.imageType.Transport(), d.directory)
case types.TypeOci:
// oci:path:tag
// example: oci:path/to/image:tag
d.referenceName = fmt.Sprintf("%s%s",
d.imageType.Transport(), d.directory)
default:
return types.ErrInvalidType
}
return nil
}
func (d *Destination) initManifest(ctx context.Context) error {
var err error
inspector, err := manifest.NewInspector(ctx, &manifest.InspectorOption{
ReferenceName: d.referenceName,
SystemContext: d.systemCtx,
})
if err != nil {
return err
}
defer inspector.Close()
b, mime, err := inspector.Raw(ctx)
if err != nil {
return err
}
// cache the destination MIME
d.mime = mime
// Only record DockerV2ListMediaType and MediaTypeImageIndex here
// since the destination image on registry server should be managed
// by DockerV2ListMediaType or MediaTypeImageIndex.
switch mime {
// Docker image list
case imagemanifest.DockerV2ListMediaType:
s2list, err := imagemanifest.Schema2ListFromManifest(b)
if err != nil {
return err
}
d.schema2List = s2list
// OCI image list
case imgspecv1.MediaTypeImageIndex:
ociIndex := &imgspecv1.Index{}
err = json.Unmarshal(b, ociIndex)
if err != nil {
return fmt.Errorf("initManifest: %w", err)
}
d.ociIndex = ociIndex
}
return nil
}
func newDestinationFromDir(o *Option) (*Destination, error) {
if o.Type != types.TypeDir {
return nil, types.ErrInvalidType
}
d := &Destination{
imageType: o.Type,
directory: o.Directory,
systemCtx: o.SystemContext,
tag: o.Tag,
}
return d, nil
}
func newDestinationFromOci(o *Option) (*Destination, error) {
if o.Type != types.TypeOci {
return nil, types.ErrInvalidType
}
d := &Destination{
imageType: o.Type,
directory: o.Directory,
systemCtx: o.SystemContext,
tag: o.Tag,
}
return d, nil
}
func newDestinationFromDocker(o *Option) (*Destination, error) {
if o.Type != types.TypeDocker {
return nil, types.ErrInvalidType
}
d := &Destination{
imageType: o.Type,
registry: o.Registry,
project: o.Project,
name: o.Name,
tag: o.Tag,
systemCtx: o.SystemContext,
}
if d.tag == "" {
d.tag = "latest"
}
if d.project == "" {
d.project = "library"
}
if d.registry == "" {
d.registry = "docker.io"
}
return d, nil
}
func newDestinationFromDockerDaemon(o *Option) (*Destination, error) {
if o.Type != types.TypeDockerDaemon {
return nil, types.ErrInvalidType
}
d := &Destination{
imageType: o.Type,
registry: o.Registry,
project: o.Project,
name: o.Name,
tag: o.Tag,
systemCtx: o.SystemContext,
}
if d.tag == "" {
d.tag = "latest"
}
if d.project == "" {
d.project = "library"
}
if d.registry == "" {
d.registry = "docker.io"
}
return d, nil
}
func (d *Destination) ImageBySet(set map[string]map[string]bool) *archive.Image {
image := &archive.Image{}
if !d.Exists() {
return image
}
archSet := map[string]bool{}
osSet := map[string]bool{}
switch d.mime {
case imagemanifest.DockerV2ListMediaType:
for _, m := range d.schema2List.Manifests {
p := &m.Platform
if len(set["arch"]) != 0 && !set["arch"][p.Architecture] {
continue
}
if len(set["os"]) != 0 && !set["os"][p.OS] {
continue
}
archSet[p.Architecture] = true
osSet[p.OS] = true
image.Images = append(image.Images, archive.ImageSpec{
Digest: m.Digest,
})
}
case imgspecv1.MediaTypeImageIndex:
for _, m := range d.ociIndex.Manifests {
p := m.Platform
if len(set["arch"]) != 0 && !set["arch"][p.Architecture] {
continue
}
if len(set["os"]) != 0 && !set["os"][p.OS] {
continue
}
archSet[p.Architecture] = true
osSet[p.OS] = true
image.Images = append(image.Images, archive.ImageSpec{
Digest: m.Digest,
})
}
}
for arch := range archSet {
image.ArchList = append(image.ArchList, arch)
}
for os := range osSet {
image.OsList = append(image.OsList, os)
}
return image
}
func (d *Destination) ManifestImages() manifest.Images {
var mis manifest.Images
switch d.mime {
case imagemanifest.DockerV2ListMediaType:
for _, m := range d.schema2List.Manifests {
mi := manifest.NewImage(m.Digest, m.MediaType, m.Size)
mi.UpdatePlatform(
m.Platform.Architecture,
m.Platform.Variant,
m.Platform.OS,
m.Platform.OSVersion,
m.Platform.OSFeatures,
)
mis = append(mis, mi)
}
case imgspecv1.MediaTypeImageIndex:
for _, m := range d.ociIndex.Manifests {
mi := manifest.NewImage(m.Digest, m.MediaType, m.Size)
mi.UpdatePlatform(
m.Platform.Architecture,
m.Platform.Variant,
m.Platform.OS,
m.Platform.OSVersion,
m.Platform.OSFeatures,
)
mis = append(mis, mi)
}
}
return mis
}
func (d *Destination) HaveDigest(imageDigest digest.Digest) bool {
if d.mime == "" || imageDigest == "" {
return false
}
switch d.mime {
case imagemanifest.DockerV2ListMediaType:
for _, m := range d.schema2List.Manifests {
if m.Digest == imageDigest {
return true
}
}
case imgspecv1.MediaTypeImageIndex:
for _, m := range d.ociIndex.Manifests {
if m.Digest == imageDigest {
return true
}
}
}
return false
}