-
Notifications
You must be signed in to change notification settings - Fork 187
/
manifest_list.go
401 lines (356 loc) · 11.5 KB
/
manifest_list.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
package libimage
import (
"context"
"fmt"
"time"
"github.com/containers/common/libimage/manifests"
imageCopy "github.com/containers/image/v5/copy"
"github.com/containers/image/v5/docker"
"github.com/containers/image/v5/manifest"
"github.com/containers/image/v5/transports/alltransports"
"github.com/containers/image/v5/types"
"github.com/containers/storage"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
)
// NOTE: the abstractions and APIs here are a first step to further merge
// `libimage/manifests` into `libimage`.
// ErrNotAManifestList indicates that an image was found in the local
// containers storage but it is not a manifest list as requested.
var ErrNotAManifestList = errors.New("image is not a manifest list")
// ManifestList represents a manifest list (Docker) or an image index (OCI) in
// the local containers storage.
type ManifestList struct {
// NOTE: the *List* suffix is intentional as the term "manifest" is
// used ambiguously across the ecosystem. It may refer to the (JSON)
// manifest of an ordinary image OR to a manifest *list* (Docker) or to
// image index (OCI).
// It's a bit more work when typing but without ambiguity.
// The underlying image in the containers storage.
image *Image
// The underlying manifest list.
list manifests.List
}
// ID returns the ID of the manifest list.
func (m *ManifestList) ID() string {
return m.image.ID()
}
// CreateManifestList creates a new empty manifest list with the specified
// name.
func (r *Runtime) CreateManifestList(name string) (*ManifestList, error) {
normalized, err := NormalizeName(name)
if err != nil {
return nil, err
}
list := manifests.Create()
listID, err := list.SaveToImage(r.store, "", []string{normalized.String()}, manifest.DockerV2ListMediaType)
if err != nil {
return nil, err
}
mList, err := r.LookupManifestList(listID)
if err != nil {
return nil, err
}
return mList, nil
}
// LookupManifestList looks up a manifest list with the specified name in the
// containers storage.
func (r *Runtime) LookupManifestList(name string) (*ManifestList, error) {
image, list, err := r.lookupManifestList(name)
if err != nil {
return nil, err
}
return &ManifestList{image: image, list: list}, nil
}
func (r *Runtime) lookupManifestList(name string) (*Image, manifests.List, error) {
lookupOptions := &LookupImageOptions{
lookupManifest: true,
}
image, _, err := r.LookupImage(name, lookupOptions)
if err != nil {
return nil, nil, err
}
if err := image.reload(); err != nil {
return nil, nil, err
}
list, err := image.getManifestList()
if err != nil {
return nil, nil, err
}
return image, list, nil
}
// ToManifestList converts the image into a manifest list. An error is thrown
// if the image is no manifest list.
func (i *Image) ToManifestList() (*ManifestList, error) {
list, err := i.getManifestList()
if err != nil {
return nil, err
}
return &ManifestList{image: i, list: list}, nil
}
// LookupInstance looks up an instance of the manifest list matching the
// specified platform. The local machine's platform is used if left empty.
func (m *ManifestList) LookupInstance(ctx context.Context, architecture, os, variant string) (*Image, error) {
sys := m.image.runtime.systemContextCopy()
if architecture != "" {
sys.ArchitectureChoice = architecture
}
if os != "" {
sys.OSChoice = os
}
if architecture != "" {
sys.VariantChoice = variant
}
// Now look at the *manifest* and select a matching instance.
rawManifest, manifestType, err := m.image.Manifest(ctx)
if err != nil {
return nil, err
}
list, err := manifest.ListFromBlob(rawManifest, manifestType)
if err != nil {
return nil, err
}
instanceDigest, err := list.ChooseInstance(sys)
if err != nil {
return nil, err
}
allImages, err := m.image.runtime.ListImages(ctx, nil, nil)
if err != nil {
return nil, err
}
for _, image := range allImages {
for _, imageDigest := range append(image.Digests(), image.Digest()) {
if imageDigest == instanceDigest {
return image, nil
}
}
}
return nil, errors.Wrapf(storage.ErrImageUnknown, "could not find image instance %s of manifest list %s in local containers storage", instanceDigest, m.ID())
}
// Saves the specified manifest list and reloads it from storage with the new ID.
func (m *ManifestList) saveAndReload() error {
newID, err := m.list.SaveToImage(m.image.runtime.store, m.image.ID(), nil, "")
if err != nil {
return err
}
// Make sure to reload the image from the containers storage to fetch
// the latest data (e.g., new or delete digests).
if err := m.image.reload(); err != nil {
return err
}
image, list, err := m.image.runtime.lookupManifestList(newID)
if err != nil {
return err
}
m.image = image
m.list = list
return nil
}
// getManifestList is a helper to obtain a manifest list
func (i *Image) getManifestList() (manifests.List, error) {
_, list, err := manifests.LoadFromImage(i.runtime.store, i.ID())
return list, err
}
// IsManifestList returns true if the image is a manifest list (Docker) or an
// image index (OCI). This information may be critical to make certain
// execution paths more robust (e.g., suppress certain errors).
func (i *Image) IsManifestList(ctx context.Context) (bool, error) {
ref, err := i.StorageReference()
if err != nil {
return false, err
}
imgRef, err := ref.NewImageSource(ctx, i.runtime.systemContextCopy())
if err != nil {
return false, err
}
_, manifestType, err := imgRef.GetManifest(ctx, nil)
if err != nil {
return false, err
}
return manifest.MIMETypeIsMultiImage(manifestType), nil
}
// Inspect returns a dockerized version of the manifest list.
func (m *ManifestList) Inspect() (*manifest.Schema2List, error) {
return m.list.Docker(), nil
}
// Options for adding a manifest list.
type ManifestListAddOptions struct {
// Add all images to the list if the to-be-added image itself is a
// manifest list.
All bool `json:"all"`
// containers-auth.json(5) file to use when authenticating against
// container registries.
AuthFilePath string
// Path to the certificates directory.
CertDirPath string
// Allow contacting registries over HTTP, or HTTPS with failed TLS
// verification. Note that this does not affect other TLS connections.
InsecureSkipTLSVerify types.OptionalBool
// Username to use when authenticating at a container registry.
Username string
// Password to use when authenticating at a container registry.
Password string
}
// Add adds one or more manifests to the manifest list and returns the digest
// of the added instance.
func (m *ManifestList) Add(ctx context.Context, name string, options *ManifestListAddOptions) (digest.Digest, error) {
if options == nil {
options = &ManifestListAddOptions{}
}
ref, err := alltransports.ParseImageName(name)
if err != nil {
withDocker := fmt.Sprintf("%s://%s", docker.Transport.Name(), name)
ref, err = alltransports.ParseImageName(withDocker)
if err != nil {
return "", err
}
}
// Now massage in the copy-related options into the system context.
systemContext := m.image.runtime.systemContextCopy()
if options.AuthFilePath != "" {
systemContext.AuthFilePath = options.AuthFilePath
}
if options.CertDirPath != "" {
systemContext.DockerCertPath = options.CertDirPath
}
if options.InsecureSkipTLSVerify != types.OptionalBoolUndefined {
systemContext.DockerInsecureSkipTLSVerify = options.InsecureSkipTLSVerify
systemContext.OCIInsecureSkipTLSVerify = options.InsecureSkipTLSVerify == types.OptionalBoolTrue
systemContext.DockerDaemonInsecureSkipTLSVerify = options.InsecureSkipTLSVerify == types.OptionalBoolTrue
}
if options.Username != "" {
systemContext.DockerAuthConfig = &types.DockerAuthConfig{
Username: options.Username,
Password: options.Password,
}
}
newDigest, err := m.list.Add(ctx, systemContext, ref, options.All)
if err != nil {
return "", err
}
// Write the changes to disk.
if err := m.saveAndReload(); err != nil {
return "", err
}
return newDigest, nil
}
// Options for annotationg a manifest list.
type ManifestListAnnotateOptions struct {
// Add the specified annotations to the added image.
Annotations map[string]string
// Add the specified architecture to the added image.
Architecture string
// Add the specified features to the added image.
Features []string
// Add the specified OS to the added image.
OS string
// Add the specified OS features to the added image.
OSFeatures []string
// Add the specified OS version to the added image.
OSVersion string
// Add the specified variant to the added image.
Variant string
}
// Annotate an image instance specified by `d` in the manifest list.
func (m *ManifestList) AnnotateInstance(d digest.Digest, options *ManifestListAnnotateOptions) error {
if options == nil {
return nil
}
if len(options.OS) > 0 {
if err := m.list.SetOS(d, options.OS); err != nil {
return err
}
}
if len(options.OSVersion) > 0 {
if err := m.list.SetOSVersion(d, options.OSVersion); err != nil {
return err
}
}
if len(options.Features) > 0 {
if err := m.list.SetFeatures(d, options.Features); err != nil {
return err
}
}
if len(options.OSFeatures) > 0 {
if err := m.list.SetOSFeatures(d, options.OSFeatures); err != nil {
return err
}
}
if len(options.Architecture) > 0 {
if err := m.list.SetArchitecture(d, options.Architecture); err != nil {
return err
}
}
if len(options.Variant) > 0 {
if err := m.list.SetVariant(d, options.Variant); err != nil {
return err
}
}
if len(options.Annotations) > 0 {
if err := m.list.SetAnnotations(&d, options.Annotations); err != nil {
return err
}
}
// Write the changes to disk.
if err := m.saveAndReload(); err != nil {
return err
}
return nil
}
// RemoveInstance removes the instance specified by `d` from the manifest list.
// Returns the new ID of the image.
func (m *ManifestList) RemoveInstance(d digest.Digest) error {
if err := m.list.Remove(d); err != nil {
return err
}
// Write the changes to disk.
if err := m.saveAndReload(); err != nil {
return err
}
return nil
}
// ManifestListPushOptions allow for customizing pushing a manifest list.
type ManifestListPushOptions struct {
CopyOptions
// For tweaking the list selection.
ImageListSelection imageCopy.ImageListSelection
// Use when selecting only specific imags.
Instances []digest.Digest
}
// Push pushes a manifest to the specified destination.
func (m *ManifestList) Push(ctx context.Context, destination string, options *ManifestListPushOptions) (digest.Digest, error) {
if options == nil {
options = &ManifestListPushOptions{}
}
dest, err := alltransports.ParseImageName(destination)
if err != nil {
oldErr := err
dest, err = alltransports.ParseImageName("docker://" + destination)
if err != nil {
return "", oldErr
}
}
if m.image.runtime.eventChannel != nil {
defer m.image.runtime.writeEvent(&Event{ID: m.ID(), Name: destination, Time: time.Now(), Type: EventTypeImagePush})
}
// NOTE: we're using the logic in copier to create a proper
// types.SystemContext. This prevents us from having an error prone
// code duplicate here.
copier, err := m.image.runtime.newCopier(&options.CopyOptions)
if err != nil {
return "", err
}
defer copier.close()
pushOptions := manifests.PushOptions{
Store: m.image.runtime.store,
SystemContext: copier.systemContext,
ImageListSelection: options.ImageListSelection,
Instances: options.Instances,
ReportWriter: options.Writer,
SignBy: options.SignBy,
RemoveSignatures: options.RemoveSignatures,
ManifestType: options.ManifestMIMEType,
}
_, d, err := m.list.Push(ctx, dest, pushOptions)
return d, err
}