-
Notifications
You must be signed in to change notification settings - Fork 570
/
imgutil.go
375 lines (338 loc) · 11.6 KB
/
imgutil.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
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package imgutil
import (
"context"
"encoding/json"
"fmt"
"io"
"reflect"
"github.com/containerd/containerd"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/platforms"
refdocker "github.com/containerd/containerd/reference/docker"
"github.com/containerd/containerd/remotes"
"github.com/containerd/imgcrypt"
"github.com/containerd/imgcrypt/images/encryption"
"github.com/containerd/nerdctl/pkg/errutil"
"github.com/containerd/nerdctl/pkg/idutil/imagewalker"
"github.com/containerd/nerdctl/pkg/imgutil/dockerconfigresolver"
"github.com/containerd/nerdctl/pkg/imgutil/pull"
"github.com/docker/docker/errdefs"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/sirupsen/logrus"
)
type EnsuredImage struct {
Ref string
Image containerd.Image
ImageConfig ocispec.ImageConfig
Snapshotter string
Remote bool // true for stargz or overlaybd
}
// PullMode is either one of "always", "missing", "never"
type PullMode = string
// GetExistingImage returns the specified image if exists in containerd. May return errdefs.NotFound() if not exists.
func GetExistingImage(ctx context.Context, client *containerd.Client, snapshotter, rawRef string, platform ocispec.Platform) (*EnsuredImage, error) {
var res *EnsuredImage
imagewalker := &imagewalker.ImageWalker{
Client: client,
OnFound: func(ctx context.Context, found imagewalker.Found) error {
if res != nil {
return nil
}
image := containerd.NewImageWithPlatform(client, found.Image, platforms.OnlyStrict(platform))
imgConfig, err := getImageConfig(ctx, image)
if err != nil {
// Image found but blob not found for foreign arch
// Ignore err and return nil, so that the walker can visit the next candidate.
return nil
}
res = &EnsuredImage{
Ref: found.Image.Name,
Image: image,
ImageConfig: *imgConfig,
Snapshotter: snapshotter,
Remote: getSnapshotterOpts(snapshotter).isRemote(),
}
if unpacked, err := image.IsUnpacked(ctx, snapshotter); err == nil && !unpacked {
if err := image.Unpack(ctx, snapshotter); err != nil {
return err
}
}
return nil
},
}
count, err := imagewalker.Walk(ctx, rawRef)
if err != nil {
return nil, err
}
if count == 0 {
return nil, errdefs.NotFound(fmt.Errorf("got count 0 after walking"))
}
if res == nil {
return nil, errdefs.NotFound(fmt.Errorf("got nil res after walking"))
}
return res, nil
}
// EnsureImage ensures the image.
//
// When insecure is set, skips verifying certs, and also falls back to HTTP when the registry does not speak HTTPS
//
// FIXME: this func has too many args
func EnsureImage(ctx context.Context, client *containerd.Client, stdout, stderr io.Writer, snapshotter, rawRef string, mode PullMode, insecure bool, hostsDirs []string, ocispecPlatforms []ocispec.Platform, unpack *bool, quiet bool) (*EnsuredImage, error) {
switch mode {
case "always", "missing", "never":
// NOP
default:
return nil, fmt.Errorf("unexpected pull mode: %q", mode)
}
if mode != "always" && len(ocispecPlatforms) == 1 {
res, err := GetExistingImage(ctx, client, snapshotter, rawRef, ocispecPlatforms[0])
if err == nil {
return res, nil
}
if !errdefs.IsNotFound(err) {
return nil, err
}
}
if mode == "never" {
return nil, fmt.Errorf("image %q is not available", rawRef)
}
named, err := refdocker.ParseDockerRef(rawRef)
if err != nil {
return nil, err
}
ref := named.String()
refDomain := refdocker.Domain(named)
var dOpts []dockerconfigresolver.Opt
if insecure {
logrus.Warnf("skipping verifying HTTPS certs for %q", refDomain)
dOpts = append(dOpts, dockerconfigresolver.WithSkipVerifyCerts(true))
}
dOpts = append(dOpts, dockerconfigresolver.WithHostsDirs(hostsDirs))
resolver, err := dockerconfigresolver.New(ctx, refDomain, dOpts...)
if err != nil {
return nil, err
}
img, err := PullImage(ctx, client, stdout, stderr, snapshotter, resolver, ref, ocispecPlatforms, unpack, quiet)
if err != nil {
// In some circumstance (e.g. people just use 80 port to support pure http), the error will contain message like "dial tcp <port>: connection refused".
if !errutil.IsErrHTTPResponseToHTTPSClient(err) && !errutil.IsErrConnectionRefused(err) {
return nil, err
}
if insecure {
logrus.WithError(err).Warnf("server %q does not seem to support HTTPS, falling back to plain HTTP", refDomain)
dOpts = append(dOpts, dockerconfigresolver.WithPlainHTTP(true))
resolver, err = dockerconfigresolver.New(ctx, refDomain, dOpts...)
if err != nil {
return nil, err
}
return PullImage(ctx, client, stdout, stderr, snapshotter, resolver, ref, ocispecPlatforms, unpack, quiet)
} else {
logrus.WithError(err).Errorf("server %q does not seem to support HTTPS", refDomain)
logrus.Info("Hint: you may want to try --insecure-registry to allow plain HTTP (if you are in a trusted network)")
return nil, err
}
}
return img, nil
}
func ResolveDigest(ctx context.Context, rawRef string, insecure bool, hostsDirs []string) (string, error) {
named, err := refdocker.ParseDockerRef(rawRef)
if err != nil {
return "", err
}
ref := named.String()
refDomain := refdocker.Domain(named)
var dOpts []dockerconfigresolver.Opt
if insecure {
logrus.Warnf("skipping verifying HTTPS certs for %q", refDomain)
dOpts = append(dOpts, dockerconfigresolver.WithSkipVerifyCerts(true))
}
dOpts = append(dOpts, dockerconfigresolver.WithHostsDirs(hostsDirs))
resolver, err := dockerconfigresolver.New(ctx, refDomain, dOpts...)
if err != nil {
return "", err
}
_, desc, err := resolver.Resolve(ctx, ref)
if err != nil {
return "", err
}
return desc.Digest.String(), nil
}
// PullImage pulls an image using the specified resolver.
func PullImage(ctx context.Context, client *containerd.Client, stdout, stderr io.Writer, snapshotter string, resolver remotes.Resolver, ref string, ocispecPlatforms []ocispec.Platform, unpack *bool, quiet bool) (*EnsuredImage, error) {
ctx, done, err := client.WithLease(ctx)
if err != nil {
return nil, err
}
defer done(ctx)
var containerdImage containerd.Image
config := &pull.Config{
Resolver: resolver,
RemoteOpts: []containerd.RemoteOpt{},
Platforms: ocispecPlatforms, // empty for all-platforms
}
if !quiet {
config.ProgressOutput = stderr
}
var unpackB bool
if unpack != nil {
unpackB = *unpack
if unpackB && len(ocispecPlatforms) != 1 {
return nil, fmt.Errorf("unpacking requires a single platform to be specified (e.g., --platform=amd64)")
}
} else {
unpackB = len(ocispecPlatforms) == 1
}
snOpt := getSnapshotterOpts(snapshotter)
if unpackB {
logrus.Debugf("The image will be unpacked for platform %q, snapshotter %q.", ocispecPlatforms[0], snapshotter)
imgcryptPayload := imgcrypt.Payload{}
imgcryptUnpackOpt := encryption.WithUnpackConfigApplyOpts(encryption.WithDecryptedUnpack(&imgcryptPayload))
config.RemoteOpts = append(config.RemoteOpts,
containerd.WithPullUnpack,
containerd.WithUnpackOpts([]containerd.UnpackOpt{imgcryptUnpackOpt}))
// different remote snapshotters will update pull.Config separately
snOpt.apply(config, ref)
} else {
logrus.Debugf("The image will not be unpacked. Platforms=%v.", ocispecPlatforms)
}
containerdImage, err = pull.Pull(ctx, client, ref, config)
if err != nil {
return nil, err
}
imgConfig, err := getImageConfig(ctx, containerdImage)
if err != nil {
return nil, err
}
res := &EnsuredImage{
Ref: ref,
Image: containerdImage,
ImageConfig: *imgConfig,
Snapshotter: snapshotter,
Remote: snOpt.isRemote(),
}
return res, nil
}
func getImageConfig(ctx context.Context, image containerd.Image) (*ocispec.ImageConfig, error) {
desc, err := image.Config(ctx)
if err != nil {
return nil, err
}
switch desc.MediaType {
case ocispec.MediaTypeImageConfig, images.MediaTypeDockerSchema2Config:
var ocispecImage ocispec.Image
b, err := content.ReadBlob(ctx, image.ContentStore(), desc)
if err != nil {
return nil, err
}
if err := json.Unmarshal(b, &ocispecImage); err != nil {
return nil, err
}
return &ocispecImage.Config, nil
default:
return nil, fmt.Errorf("unknown media type %q", desc.MediaType)
}
}
// ReadIndex returns the index .
// ReadIndex returns nil for non-indexed image.
func ReadIndex(ctx context.Context, img containerd.Image) (*ocispec.Index, *ocispec.Descriptor, error) {
desc := img.Target()
if !images.IsIndexType(desc.MediaType) {
return nil, nil, nil
}
b, err := content.ReadBlob(ctx, img.ContentStore(), desc)
if err != nil {
return nil, &desc, err
}
var idx ocispec.Index
if err := json.Unmarshal(b, &idx); err != nil {
return nil, &desc, err
}
return &idx, &desc, nil
}
// ReadManifest returns the manifest for img.platform.
// ReadManifest returns nil if no manifest was found.
func ReadManifest(ctx context.Context, img containerd.Image) (*ocispec.Manifest, *ocispec.Descriptor, error) {
cs := img.ContentStore()
targetDesc := img.Target()
if images.IsManifestType(targetDesc.MediaType) {
b, err := content.ReadBlob(ctx, img.ContentStore(), targetDesc)
if err != nil {
return nil, &targetDesc, err
}
var mani ocispec.Manifest
if err := json.Unmarshal(b, &mani); err != nil {
return nil, &targetDesc, err
}
return &mani, &targetDesc, nil
}
if images.IsIndexType(targetDesc.MediaType) {
idx, _, err := ReadIndex(ctx, img)
if err != nil {
return nil, nil, err
}
configDesc, err := img.Config(ctx) // aware of img.platform
if err != nil {
return nil, nil, err
}
// We can't access the private `img.platform` variable.
// So, we find the manifest object by comparing the config desc.
for _, maniDesc := range idx.Manifests {
maniDesc := maniDesc
// ignore non-nil err
if b, err := content.ReadBlob(ctx, cs, maniDesc); err == nil {
var mani ocispec.Manifest
if err := json.Unmarshal(b, &mani); err != nil {
return nil, nil, err
}
if reflect.DeepEqual(configDesc, mani.Config) {
return &mani, &maniDesc, nil
}
}
}
}
// no manifest was found
return nil, nil, nil
}
// ReadImageConfig reads the config spec (`application/vnd.oci.image.config.v1+json`) for img.platform from content store.
func ReadImageConfig(ctx context.Context, img containerd.Image) (ocispec.Image, ocispec.Descriptor, error) {
var config ocispec.Image
configDesc, err := img.Config(ctx) // aware of img.platform
if err != nil {
return config, configDesc, err
}
p, err := content.ReadBlob(ctx, img.ContentStore(), configDesc)
if err != nil {
return config, configDesc, err
}
if err := json.Unmarshal(p, &config); err != nil {
return config, configDesc, err
}
return config, configDesc, nil
}
func ParseRepoTag(imgName string) (string, string) {
logrus.Debugf("raw image name=%q", imgName)
ref, err := refdocker.ParseDockerRef(imgName)
if err != nil {
logrus.WithError(err).Warnf("unparsable image name %q", imgName)
return "", ""
}
var tag string
if tagged, ok := ref.(refdocker.Tagged); ok {
tag = tagged.Tag()
}
repository := refdocker.FamiliarName(ref)
return repository, tag
}