-
Notifications
You must be signed in to change notification settings - Fork 1k
/
image_pull.go
365 lines (325 loc) · 12.3 KB
/
image_pull.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
package server
import (
"context"
"encoding/base64"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/containers/image/v5/signature"
imageTypes "github.com/containers/image/v5/types"
encconfig "github.com/containers/ocicrypt/config"
"github.com/cri-o/cri-o/internal/log"
"github.com/cri-o/cri-o/internal/storage"
"github.com/cri-o/cri-o/server/metrics"
"github.com/cri-o/cri-o/utils"
"github.com/docker/distribution/registry/api/errcode"
"github.com/opencontainers/go-digest"
types "k8s.io/cri-api/pkg/apis/runtime/v1"
crierrors "k8s.io/cri-api/pkg/errors"
)
var localRegistryHostname = "localhost"
// PullImage pulls a image with authentication config.
func (s *Server) PullImage(ctx context.Context, req *types.PullImageRequest) (*types.PullImageResponse, error) {
ctx, span := log.StartSpan(ctx)
defer span.End()
// TODO: what else do we need here? (Signatures when the story isn't just pulling from docker://)
var err error
image := ""
img := req.Image
if img != nil {
image = img.Image
}
log.Infof(ctx, "Pulling image: %s", image)
pullArgs := pullArguments{image: image}
sc := req.SandboxConfig
if sc != nil {
if sc.Linux != nil {
pullArgs.sandboxCgroup = sc.Linux.CgroupParent
}
if sc.Metadata != nil {
pullArgs.namespace = sc.Metadata.Namespace
}
}
if req.Auth != nil {
username := req.Auth.Username
password := req.Auth.Password
if req.Auth.Auth != "" {
username, password, err = decodeDockerAuth(req.Auth.Auth)
if err != nil {
log.Debugf(ctx, "Error decoding authentication for image %s: %v", image, err)
return nil, err
}
}
// Specifying a username indicates the user intends to send authentication to the registry.
if username != "" {
pullArgs.credentials = imageTypes.DockerAuthConfig{
Username: username,
Password: password,
}
}
}
// We use the server's pullOperationsInProgress to record which images are
// currently being pulled. This allows for avoiding pulling the same image
// in parallel. Hence, if a given image is currently being pulled, we queue
// into the pullOperation's waitgroup and wait for the pulling goroutine to
// unblock us and re-use its results.
pullOp, pullInProcess := func() (pullOp *pullOperation, inProgress bool) {
s.pullOperationsLock.Lock()
defer s.pullOperationsLock.Unlock()
pullOp, inProgress = s.pullOperationsInProgress[pullArgs]
if !inProgress {
pullOp = &pullOperation{}
s.pullOperationsInProgress[pullArgs] = pullOp
storage.ImageBeingPulled.Store(pullArgs.image, true)
pullOp.wg.Add(1)
}
return pullOp, inProgress
}()
if !pullInProcess {
pullOp.err = errors.New("pullImage was aborted by a Go panic")
defer func() {
s.pullOperationsLock.Lock()
delete(s.pullOperationsInProgress, pullArgs)
storage.ImageBeingPulled.Delete(pullArgs.image)
pullOp.wg.Done()
s.pullOperationsLock.Unlock()
}()
pullOp.imageRef, pullOp.err = s.pullImage(ctx, &pullArgs)
} else {
// Wait for the pull operation to finish.
pullOp.wg.Wait()
}
if pullOp.err != nil {
wrap := func(e error) error { return fmt.Errorf("%w: %w", e, pullOp.err) }
if errors.Is(pullOp.err, syscall.ECONNREFUSED) {
return nil, wrap(crierrors.ErrRegistryUnavailable)
}
var policyErr signature.PolicyRequirementError
if errors.As(pullOp.err, &policyErr) {
return nil, wrap(crierrors.ErrSignatureValidationFailed)
}
return nil, pullOp.err
}
log.Infof(ctx, "Pulled image: %v", pullOp.imageRef)
return &types.PullImageResponse{
ImageRef: pullOp.imageRef,
}, nil
}
// pullImage performs the actual pull operation of PullImage. Used to separate
// the pull implementation from the pullCache logic in PullImage and improve
// readability and maintainability.
func (s *Server) pullImage(ctx context.Context, pullArgs *pullArguments) (string, error) {
var err error
ctx, span := log.StartSpan(ctx)
defer span.End()
sourceCtx := *s.config.SystemContext // A shallow copy we can modify
sourceCtx.DockerLogMirrorChoice = true // Add info level log of the pull source
if pullArgs.credentials.Username != "" {
sourceCtx.DockerAuthConfig = &pullArgs.credentials
}
if pullArgs.namespace != "" {
policyPath := filepath.Join(s.config.SignaturePolicyDir, pullArgs.namespace+".json")
if _, err := os.Stat(policyPath); err == nil {
sourceCtx.SignaturePolicyPath = policyPath
} else if !os.IsNotExist(err) {
return "", fmt.Errorf("read policy path %s: %w", policyPath, err)
}
}
log.Debugf(ctx, "Using pull policy path for image %s: %s", pullArgs.image, sourceCtx.SignaturePolicyPath)
decryptConfig, err := getDecryptionKeys(s.config.DecryptionKeysPath)
if err != nil {
return "", err
}
cgroup := ""
if s.config.SeparatePullCgroup != "" {
if !s.config.CgroupManager().IsSystemd() {
return "", errors.New("--separate-pull-cgroup is supported only with systemd")
}
if s.config.SeparatePullCgroup == utils.PodCgroupName {
cgroup = pullArgs.sandboxCgroup
} else {
cgroup = s.config.SeparatePullCgroup
if !strings.Contains(cgroup, ".slice") {
return "", fmt.Errorf("invalid systemd cgroup %q", cgroup)
}
}
}
remoteCandidates, err := s.StorageImageServer().CandidatesForPotentiallyShortImageName(s.config.SystemContext, pullArgs.image)
if err != nil {
return "", err
}
// CandidatesForPotentiallyShortImageName is defined never to return an empty slice on success, so if the loop considers all candidates
// and they all fail, this error value should be overwritten by a real failure.
lastErr := errors.New("internal error: pullImage failed but reported no error reason")
for _, remoteCandidateName := range remoteCandidates {
err := s.pullImageCandidate(ctx, &sourceCtx, remoteCandidateName, decryptConfig, cgroup)
if err == nil {
// Update metric for successful image pulls
metrics.Instance().MetricImagePullsSuccessesInc(remoteCandidateName)
status, err := s.StorageImageServer().ImageStatusByName(s.config.SystemContext, remoteCandidateName)
if err != nil {
return "", err
}
imageRef := status.ID.IDStringForOutOfProcessConsumptionOnly()
if len(status.RepoDigests) > 0 {
imageRef = status.RepoDigests[0]
}
return imageRef, nil
}
lastErr = err
}
return "", lastErr
}
func (s *Server) pullImageCandidate(ctx context.Context, sourceCtx *imageTypes.SystemContext, remoteCandidateName storage.RegistryImageReference, decryptConfig *encconfig.DecryptConfig, cgroup string) error {
tmpImg, err := s.StorageImageServer().PrepareImage(sourceCtx, remoteCandidateName)
if err != nil {
// We're not able to find the image remotely, check if it's
// available locally, but only for localhost ones.
// This allows pulling localhost images even if the
// `imagePullPolicy` is set to `Always`.
if remoteCandidateName.Registry() == localRegistryHostname {
if _, err := s.StorageImageServer().ImageStatusByName(s.config.SystemContext, remoteCandidateName); err == nil {
return nil
}
}
log.Debugf(ctx, "Error preparing image %s: %v", remoteCandidateName, err)
tryIncrementImagePullFailureMetric(remoteCandidateName, err)
return err
}
defer tmpImg.Close()
storedImage, err := s.StorageImageServer().ImageStatusByName(s.config.SystemContext, remoteCandidateName)
if err == nil {
tmpImgConfigDigest := tmpImg.ConfigInfo().Digest
if tmpImgConfigDigest.String() == "" {
// this means we are playing with a schema1 image, in which
// case, we're going to repull the image in any case
log.Debugf(ctx, "Image config digest is empty, re-pulling image")
} else if tmpImgConfigDigest.String() == storedImage.ConfigDigest.String() {
log.Debugf(ctx, "Image %s already in store, skipping pull", remoteCandidateName)
// Skipped digests metrics
tryRecordSkippedMetric(ctx, remoteCandidateName, tmpImgConfigDigest)
// Skipped bytes metrics
if storedImage.Size != nil {
// Metrics for image pull skipped bytes
metrics.Instance().MetricImagePullsSkippedBytesAdd(float64(*storedImage.Size))
}
return nil
}
log.Debugf(ctx, "Image in store has different ID, re-pulling %s", remoteCandidateName)
}
// Collect pull progress metrics
progress := make(chan imageTypes.ProgressProperties)
defer close(progress) // nolint:gocritic
// Cancel the pull if no progress is made
pullCtx, cancel := context.WithCancel(context.Background())
go consumeImagePullProgress(ctx, cancel, progress, remoteCandidateName)
_, err = s.StorageImageServer().PullImage(pullCtx, remoteCandidateName, &storage.ImageCopyOptions{
SourceCtx: sourceCtx,
DestinationCtx: s.config.SystemContext,
OciDecryptConfig: decryptConfig,
ProgressInterval: time.Second,
Progress: progress,
CgroupPull: storage.CgroupPullConfiguration{
UseNewCgroup: s.config.SeparatePullCgroup != "",
ParentCgroup: cgroup,
},
})
if err != nil {
log.Debugf(ctx, "Error pulling image %s: %v", remoteCandidateName, err)
tryIncrementImagePullFailureMetric(remoteCandidateName, err)
return err
}
return nil
}
// consumeImagePullProgress consumes progress and turns it into metrics updates.
// It also checks if progress is being made within a constant timeout.
// If the timeout is reached because no progress updates have been made, then
// the cancel function will be called.
func consumeImagePullProgress(ctx context.Context, cancel context.CancelFunc, progress <-chan imageTypes.ProgressProperties, remoteCandidateName storage.RegistryImageReference) {
// The progress interval is 1s, but we give it a bit more time just in case
// that the connection revives.
const timeout = 10 * time.Second
timer := time.AfterFunc(timeout, func() {
log.Warnf(ctx, "Timed out on waiting up to %s for image pull progress updates", timeout)
cancel()
})
timer.Stop() // don't start the timer immediately
defer timer.Stop() // ensure that the timer is stopped when we exit the progress loop
for p := range progress {
timer.Reset(timeout)
if p.Event == imageTypes.ProgressEventSkipped {
// Skipped digests metrics
tryRecordSkippedMetric(ctx, remoteCandidateName, p.Artifact.Digest)
}
if p.Artifact.Size > 0 {
log.Debugf(ctx, "ImagePull (%v): %s (%s): %v bytes (%.2f%%)",
p.Event, remoteCandidateName, p.Artifact.Digest, p.Offset,
float64(p.Offset)/float64(p.Artifact.Size)*100,
)
} else {
log.Debugf(ctx, "ImagePull (%v): %s (%s): %v bytes",
p.Event, remoteCandidateName, p.Artifact.Digest, p.Offset,
)
}
// Metrics for image pulls bytes
metrics.Instance().MetricImagePullsBytesAdd(
float64(p.OffsetUpdate),
p.Artifact.MediaType,
p.Artifact.Size,
)
// Metrics for size histogram
if p.Event == imageTypes.ProgressEventDone {
metrics.Instance().MetricImagePullsLayerSizeObserve(p.Artifact.Size)
}
}
}
func tryIncrementImagePullFailureMetric(img storage.RegistryImageReference, err error) {
// We try to cover some basic use-cases
const labelUnknown = "UNKNOWN"
label := labelUnknown
// Docker registry errors
for _, desc := range errcode.GetErrorAllDescriptors() {
if strings.Contains(err.Error(), desc.Message) {
label = desc.Value
break
}
}
if label == labelUnknown {
if strings.Contains(err.Error(), "connection refused") { // nolint: gocritic
label = "CONNECTION_REFUSED"
} else if strings.Contains(err.Error(), "connection timed out") {
label = "CONNECTION_TIMEOUT"
} else if strings.Contains(err.Error(), "404 (Not Found)") {
label = "NOT_FOUND"
}
}
// Update metric for failed image pulls
metrics.Instance().MetricImagePullsFailuresInc(img, label)
}
func tryRecordSkippedMetric(ctx context.Context, name storage.RegistryImageReference, someBlobDigest digest.Digest) {
// NOTE: This "layer" identification looks like a digested image reference, but
// it isn’t one:
// - the digest references a layer or config, not a manifest
// - "name" may contain a digest already, so this results in name@manifestDigest@someOtherdigest
layer := fmt.Sprintf("%s@%s", name.StringForOutOfProcessConsumptionOnly(), someBlobDigest.String())
log.Debugf(ctx, "Skipped layer %s", layer)
metrics.Instance().MetricImageLayerReuseInc(layer)
}
func decodeDockerAuth(s string) (user, password string, _ error) {
decoded, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return "", "", err
}
parts := strings.SplitN(string(decoded), ":", 2)
if len(parts) != 2 {
// if it's invalid just skip, as docker does
return "", "", nil
}
user = parts[0]
password = strings.Trim(parts[1], "\x00")
return user, password, nil
}