forked from buildpacks/imgutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnb_index.go
394 lines (353 loc) · 10.5 KB
/
cnb_index.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
package imgutil
import (
"fmt"
"os"
"path/filepath"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/match"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/pkg/errors"
)
var (
ErrManifestUndefined = errors.New("encountered unexpected error while parsing image: manifest or index manifest is nil")
ErrUnknownMediaType = func(format types.MediaType) error {
return fmt.Errorf("unsupported media type encountered in image: '%s'", format)
}
)
type CNBIndex struct {
// required
v1.ImageIndex // the working image index
// local options
XdgPath string
// push options
KeyChain authn.Keychain
RepoName string
}
func (h *CNBIndex) getDescriptorFrom(digest name.Digest) (v1.Descriptor, error) {
indexManifest, err := getIndexManifest(h.ImageIndex)
if err != nil {
return v1.Descriptor{}, err
}
for _, current := range indexManifest.Manifests {
if current.Digest.String() == digest.Identifier() {
return current, nil
}
}
return v1.Descriptor{}, fmt.Errorf("failed to find image with digest %s in index", digest.Identifier())
}
// OS returns `OS` of an existing Image.
func (h *CNBIndex) OS(digest name.Digest) (os string, err error) {
desc, err := h.getDescriptorFrom(digest)
if err != nil {
return "", err
}
if desc.Platform != nil {
return desc.Platform.OS, nil
}
return "", nil
}
// Architecture return the Architecture of an Image/Index based on given Digest.
// Returns an error if no Image/Index found with given Digest.
func (h *CNBIndex) Architecture(digest name.Digest) (arch string, err error) {
desc, err := h.getDescriptorFrom(digest)
if err != nil {
return "", err
}
if desc.Platform != nil {
return desc.Platform.Architecture, nil
}
return "", nil
}
// Variant return the `Variant` of an Image.
// Returns an error if no Image/Index found with given Digest.
func (h *CNBIndex) Variant(digest name.Digest) (osVariant string, err error) {
desc, err := h.getDescriptorFrom(digest)
if err != nil {
return "", err
}
if desc.Platform != nil {
return desc.Platform.Variant, nil
}
return "", nil
}
// OSVersion returns the `OSVersion` of an Image with given Digest.
// Returns an error if no Image/Index found with given Digest.
func (h *CNBIndex) OSVersion(digest name.Digest) (osVersion string, err error) {
desc, err := h.getDescriptorFrom(digest)
if err != nil {
return "", err
}
if desc.Platform != nil {
return desc.Platform.OSVersion, nil
}
return "", nil
}
// OSFeatures returns the `OSFeatures` of an Image with given Digest.
// Returns an error if no Image/Index found with given Digest.
func (h *CNBIndex) OSFeatures(digest name.Digest) (osFeatures []string, err error) {
desc, err := h.getDescriptorFrom(digest)
if err != nil {
return nil, err
}
if desc.Platform != nil {
return desc.Platform.OSFeatures, nil
}
return []string{}, nil
}
// Annotations return the `Annotations` of an Image with given Digest.
// Returns an error if no Image/Index found with given Digest.
// For Docker images and Indexes it returns an error.
func (h *CNBIndex) Annotations(digest name.Digest) (annotations map[string]string, err error) {
desc, err := h.getDescriptorFrom(digest)
if err != nil {
return nil, err
}
return desc.Annotations, nil
}
// setters
func (h *CNBIndex) SetAnnotations(digest name.Digest, annotations map[string]string) (err error) {
return h.replaceDescriptor(digest, func(descriptor v1.Descriptor) (v1.Descriptor, error) {
if len(descriptor.Annotations) == 0 {
descriptor.Annotations = make(map[string]string)
}
for k, v := range annotations {
descriptor.Annotations[k] = v
}
return descriptor, nil
})
}
func (h *CNBIndex) SetArchitecture(digest name.Digest, arch string) (err error) {
return h.replaceDescriptor(digest, func(descriptor v1.Descriptor) (v1.Descriptor, error) {
descriptor.Platform.Architecture = arch
return descriptor, nil
})
}
func (h *CNBIndex) SetOS(digest name.Digest, os string) (err error) {
return h.replaceDescriptor(digest, func(descriptor v1.Descriptor) (v1.Descriptor, error) {
descriptor.Platform.OS = os
return descriptor, nil
})
}
func (h *CNBIndex) SetVariant(digest name.Digest, osVariant string) (err error) {
return h.replaceDescriptor(digest, func(descriptor v1.Descriptor) (v1.Descriptor, error) {
descriptor.Platform.Variant = osVariant
return descriptor, nil
})
}
func (h *CNBIndex) replaceDescriptor(digest name.Digest, withFun func(descriptor v1.Descriptor) (v1.Descriptor, error)) (err error) {
desc, err := h.getDescriptorFrom(digest)
if err != nil {
return err
}
mediaType := desc.MediaType
if desc.Platform == nil {
desc.Platform = &v1.Platform{}
}
desc, err = withFun(desc)
if err != nil {
return err
}
add := mutate.IndexAddendum{
Add: h.ImageIndex,
Descriptor: desc,
}
h.ImageIndex = mutate.AppendManifests(mutate.RemoveManifests(h.ImageIndex, match.Digests(desc.Digest)), add)
// Avoid overriding the original media-type
mediaTypeAfter, err := h.ImageIndex.MediaType()
if err != nil {
return err
}
if mediaTypeAfter != mediaType {
h.ImageIndex = mutate.IndexMediaType(h.ImageIndex, mediaType)
}
return nil
}
func (h *CNBIndex) Image(hash v1.Hash) (v1.Image, error) {
index, err := h.IndexManifest()
if err != nil {
return nil, err
}
if !indexContains(index.Manifests, hash) {
return nil, fmt.Errorf("failed to find image with digest %s in index", hash.String())
}
return h.ImageIndex.Image(hash)
}
func indexContains(manifests []v1.Descriptor, hash v1.Hash) bool {
for _, m := range manifests {
if m.Digest.String() == hash.String() {
return true
}
}
return false
}
// AddManifest adds an image to the index.
func (h *CNBIndex) AddManifest(image v1.Image) {
desc, _ := descriptor(image)
h.ImageIndex = mutate.AppendManifests(h.ImageIndex, mutate.IndexAddendum{
Add: image,
Descriptor: desc,
})
}
// SaveDir will locally save the index.
func (h *CNBIndex) SaveDir() error {
layoutPath := filepath.Join(h.XdgPath, MakeFileSafeName(h.RepoName)) // FIXME: do we create an OCI-layout compatible directory structure?
var (
path layout.Path
err error
)
if _, err = os.Stat(layoutPath); !os.IsNotExist(err) {
// We need to always init an empty index when saving
if err = os.RemoveAll(layoutPath); err != nil {
return err
}
}
indexType, err := h.ImageIndex.MediaType()
if err != nil {
return err
}
if path, err = newEmptyLayoutPath(indexType, layoutPath); err != nil {
return err
}
var errs SaveError
index, err := h.ImageIndex.IndexManifest()
if err != nil {
return err
}
for _, desc := range index.Manifests {
appendManifest(desc, path, &errs)
}
if len(errs.Errors) != 0 {
return errs
}
return nil
}
func appendManifest(desc v1.Descriptor, path layout.Path, errs *SaveError) {
if err := path.RemoveDescriptors(match.Digests(desc.Digest)); err != nil {
errs.Errors = append(errs.Errors, SaveDiagnostic{
Cause: err,
})
}
if err := path.AppendDescriptor(desc); err != nil {
errs.Errors = append(errs.Errors, SaveDiagnostic{
Cause: err,
})
}
}
func newEmptyLayoutPath(indexType types.MediaType, path string) (layout.Path, error) {
if indexType == types.OCIImageIndex {
return layout.Write(path, empty.Index)
}
return layout.Write(path, NewEmptyDockerIndex())
}
// Push Publishes ImageIndex to the registry assuming every image it referes exists in registry.
//
// It will only push the IndexManifest to registry.
func (h *CNBIndex) Push(ops ...IndexOption) error {
var pushOps = &IndexOptions{}
for _, op := range ops {
if err := op(pushOps); err != nil {
return err
}
}
if pushOps.MediaType != "" {
if !pushOps.MediaType.IsIndex() {
return ErrUnknownMediaType(pushOps.MediaType)
}
existingType, err := h.ImageIndex.MediaType()
if err != nil {
return err
}
if pushOps.MediaType != existingType {
h.ImageIndex = mutate.IndexMediaType(h.ImageIndex, pushOps.MediaType)
}
}
ref, err := name.ParseReference(
h.RepoName,
name.WeakValidation,
name.Insecure,
)
if err != nil {
return err
}
indexManifest, err := getIndexManifest(h.ImageIndex)
if err != nil {
return err
}
var taggableIndex = NewTaggableIndex(indexManifest)
multiWriteTagables := map[name.Reference]remote.Taggable{
ref: taggableIndex,
}
for _, tag := range pushOps.DestinationTags {
multiWriteTagables[ref.Context().Tag(tag)] = taggableIndex
}
// Note: this will only push the index manifest, assuming that all the images it refers to exists in the registry
err = remote.MultiWrite(
multiWriteTagables,
remote.WithAuthFromKeychain(h.KeyChain),
remote.WithTransport(GetTransport(pushOps.Insecure)),
)
if err != nil {
return err
}
if pushOps.Purge {
return h.DeleteDir()
}
return h.SaveDir()
}
// Inspect Displays IndexManifest.
func (h *CNBIndex) Inspect() (string, error) {
rawManifest, err := h.RawManifest()
if err != nil {
return "", err
}
return string(rawManifest), nil
}
// RemoveManifest removes an image with a given digest from the index.
func (h *CNBIndex) RemoveManifest(digest name.Digest) (err error) {
hash, err := v1.NewHash(digest.Identifier())
if err != nil {
return err
}
h.ImageIndex = mutate.RemoveManifests(h.ImageIndex, match.Digests(hash))
_, err = h.ImageIndex.Digest() // force compute
return err
}
// DeleteDir removes the index from the local filesystem if it exists.
func (h *CNBIndex) DeleteDir() error {
layoutPath := filepath.Join(h.XdgPath, MakeFileSafeName(h.RepoName))
if _, err := os.Stat(layoutPath); err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
return os.RemoveAll(layoutPath)
}
func getIndexManifest(ii v1.ImageIndex) (mfest *v1.IndexManifest, err error) {
mfest, err = ii.IndexManifest()
if mfest == nil {
return mfest, ErrManifestUndefined
}
return mfest, err
}
// descriptor returns a v1.Descriptor filled with a v1.Platform created from reading
// the image config file.
func descriptor(image v1.Image) (v1.Descriptor, error) {
// Get the image configuration file
cfg, _ := GetConfigFile(image)
platform := v1.Platform{}
platform.Architecture = cfg.Architecture
platform.OS = cfg.OS
platform.OSVersion = cfg.OSVersion
platform.Variant = cfg.Variant
platform.OSFeatures = cfg.OSFeatures
return v1.Descriptor{
Platform: &platform,
}, nil
}