-
Notifications
You must be signed in to change notification settings - Fork 570
/
imgutil.go
252 lines (221 loc) · 7.16 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
/*
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"
"io"
"strings"
"github.com/containerd/containerd"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images"
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/imgutil/dockerconfigresolver"
"github.com/containerd/nerdctl/pkg/imgutil/pull"
"github.com/containerd/stargz-snapshotter/fs/source"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type EnsuredImage struct {
Ref string
Image containerd.Image
ImageConfig ocispec.ImageConfig
Snapshotter string
Remote bool // true for stargz
}
// PullMode is either one of "always", "missing", "never"
type PullMode = string
// EnsureImage ensures the image.
//
// When insecure is set, skips verifying certs, and also falls back to HTTP when the registry does not speak HTTPS
func EnsureImage(ctx context.Context, client *containerd.Client, stdout io.Writer, snapshotter, rawRef string, mode PullMode, insecure bool) (*EnsuredImage, error) {
named, err := refdocker.ParseDockerRef(rawRef)
if err != nil {
return nil, err
}
ref := named.String()
if mode != "always" {
if i, err := client.ImageService().Get(ctx, ref); err == nil {
image := containerd.NewImage(client, i)
imgConfig, err := getImageConfig(ctx, image)
if err != nil {
return nil, err
}
res := &EnsuredImage{
Ref: ref,
Image: image,
ImageConfig: *imgConfig,
Snapshotter: snapshotter,
Remote: isStargz(snapshotter),
}
if unpacked, err := image.IsUnpacked(ctx, snapshotter); err == nil && !unpacked {
if err := image.Unpack(ctx, snapshotter); err != nil {
return nil, err
}
}
return res, nil
}
}
if mode == "never" {
return nil, errors.Errorf("image %q is not available", rawRef)
}
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))
}
resolver, err := dockerconfigresolver.New(refDomain, dOpts...)
if err != nil {
return nil, err
}
img, err := pullImage(ctx, client, stdout, snapshotter, resolver, ref)
if err != nil {
if !IsErrHTTPResponseToHTTPSClient(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(refDomain, dOpts...)
if err != nil {
return nil, err
}
return pullImage(ctx, client, stdout, snapshotter, resolver, ref)
} 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
}
// IsErrHTTPResponseToHTTPSClient returns whether err is
// "http: server gave HTTP response to HTTPS client"
func IsErrHTTPResponseToHTTPSClient(err error) bool {
// The error string is unexposed as of Go 1.16, so we can't use `errors.Is`.
// https://github.com/golang/go/issues/44855
const unexposed = "server gave HTTP response to HTTPS client"
return strings.Contains(err.Error(), unexposed)
}
func pullImage(ctx context.Context, client *containerd.Client, stdout io.Writer, snapshotter string, resolver remotes.Resolver, ref string) (*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,
ProgressOutput: stdout,
RemoteOpts: []containerd.RemoteOpt{
containerd.WithPullUnpack,
containerd.WithPullSnapshotter(snapshotter),
},
}
imgcryptPayload := imgcrypt.Payload{}
imgcryptUnpackOpt := encryption.WithUnpackConfigApplyOpts(encryption.WithDecryptedUnpack(&imgcryptPayload))
config.RemoteOpts = append(config.RemoteOpts,
containerd.WithUnpackOpts([]containerd.UnpackOpt{imgcryptUnpackOpt}))
sgz := isStargz(snapshotter)
if sgz {
// TODO: support "skip-content-verify"
config.RemoteOpts = append(
config.RemoteOpts,
containerd.WithImageHandlerWrapper(source.AppendDefaultLabelsHandlerWrapper(ref, 10*1024*1024)),
)
}
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: sgz,
}
return res, nil
}
func isStargz(sn string) bool {
if !strings.Contains(sn, "stargz") {
return false
}
if sn != "stargz" {
logrus.Debugf("assuming %q to be a stargz-compatible snapshotter", sn)
}
return true
}
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, errors.Errorf("unknown media type %q", desc.MediaType)
}
}
// readImageConfig reads config spec from content store.
func ReadImageConfig(ctx context.Context, img containerd.Image) (ocispec.Image, error) {
var config ocispec.Image
configDesc, err := img.Config(ctx)
if err != nil {
return config, err
}
p, err := content.ReadBlob(ctx, img.ContentStore(), configDesc)
if err != nil {
return config, err
}
if err := json.Unmarshal(p, &config); err != nil {
return config, err
}
return config, nil
}
func ParseRepoTag(imgName string) (string, string) {
logrus.Debugf("raw image name=%q", imgName)
if strings.Contains(imgName, "@") {
logrus.Warnf("unparsable image name %q", imgName)
return "", ""
}
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
}