This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
/
client.go
677 lines (620 loc) · 21.1 KB
/
client.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// Copyright (c) 2018 Uber Technologies, Inc.
//
// 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 registry
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"regexp"
"time"
"github.com/cenkalti/backoff"
"github.com/juju/ratelimit"
"github.com/uber/makisu/lib/concurrency"
"github.com/uber/makisu/lib/docker/image"
"github.com/uber/makisu/lib/log"
"github.com/uber/makisu/lib/storage"
"github.com/uber/makisu/lib/utils"
"github.com/uber/makisu/lib/utils/httputil"
)
const (
baseManifestQuery = "http://%s/v2/%s/manifests/%s"
baseLayerQuery = "http://%s/v2/%s/blobs/%s"
baseStartQuery = "http://%s/v2/%s/blobs/uploads/"
)
// Client is the interface through which we can interact with a docker registry. It is used when
// pulling and pushing images to that registry.
type Client interface {
Pull(tag string) (*image.DistributionManifest, error)
Push(tag string) error
PullManifest(tag string) (*image.DistributionManifest, error)
PushManifest(tag string, manifest *image.DistributionManifest) error
PullLayer(layerDigest image.Digest) (os.FileInfo, error)
PushLayer(layerDigest image.Digest) error
PullImageConfig(layerDigest image.Digest) (os.FileInfo, error)
PushImageConfig(layerDigest image.Digest) error
}
var _ Client = (*DockerRegistryClient)(nil)
// DockerRegistryClient is a real registry client that is able to push and pull images. It uses a
// storage.ImageStore to interact with images on the local filesystem.
// It implements the Client interface.
type DockerRegistryClient struct {
config Config
store *storage.ImageStore
registry string
repository string
// TODO: there must be a better way to test this.
client *http.Client
}
// New returns a new default Client.
func New(store *storage.ImageStore, registry, repository string) *DockerRegistryClient {
return newClient(store, registry, repository, nil)
}
// NewWithClient returns a new Client with a customized http.Client.
func NewWithClient(store *storage.ImageStore, registry, repository string, client *http.Client) *DockerRegistryClient {
return newClient(store, registry, repository, client)
}
func newClient(store *storage.ImageStore, registry, repository string, client *http.Client) *DockerRegistryClient {
config := Config{}
if registry == image.DockerHubRegistry {
config = DefaultDockerHubConfiguration
}
repoConfig, ok := ConfigurationMap[registry]
if ok {
for repo, c := range repoConfig {
r := regexp.MustCompile(repo)
if r.MatchString(repository) {
config = c
break
}
}
}
return &DockerRegistryClient{
config: config.applyDefaults(),
registry: registry,
repository: repository,
store: store,
client: client,
}
}
// Pull tries to pull an image from its docker registry.
// If the pull succeeded, it would store the image in the ImageStore of the client, and returns the
// distribution manifest.
func (c DockerRegistryClient) Pull(tag string) (*image.DistributionManifest, error) {
name := image.NewImageName(c.registry, c.repository, tag)
log.Infof("* Started pulling image %s", name)
starttime := time.Now()
manifest, err := c.PullManifest(tag)
if err != nil {
return nil, fmt.Errorf("pull manifest: %s", err)
}
multiError := utils.NewMultiErrors()
workers := concurrency.NewWorkerPool(c.config.Concurrency)
layerSet := make(map[string]interface{})
for _, layer := range manifest.GetLayerDigests() {
l := layer
if _, ok := layerSet[l.Hex()]; ok {
// Duplicate layer.
continue
} else {
layerSet[l.Hex()] = struct{}{}
}
workers.Do(func() {
if _, err := c.PullLayer(l); err != nil {
multiError.Add(fmt.Errorf("pull layer %s: %s", l, err))
workers.Stop()
return
}
})
}
l := manifest.GetConfigDigest()
workers.Do(func() {
if _, err := c.PullLayer(l); err != nil {
multiError.Add(fmt.Errorf("pull image config %s: %s", l, err))
workers.Stop()
return
}
})
workers.Wait()
if err := multiError.Collect(); err != nil {
return nil, err
}
if err := c.saveManifest(tag, manifest); err != nil {
return nil, fmt.Errorf("save manifest: %s", err)
}
log.Infow(fmt.Sprintf("* Pulled image %s", name), "duration", time.Since(starttime))
return manifest, nil
}
// Push tries to push an image to docker registry, using the ImageStore of the client.
func (c DockerRegistryClient) Push(tag string) error {
name := image.NewImageName(c.registry, c.repository, tag)
log.Infof("* Started pushing image %s", name)
starttime := time.Now()
if found, err := c.manifestExists(tag); err != nil {
return fmt.Errorf("check manifest exists for image %s: %s", name, err)
} else if found {
log.Infof("* Image %s already exists, overwriting", name)
}
manifest, err := c.loadManifest(tag)
if err != nil {
return fmt.Errorf("load manifest: %s", err)
}
multiError := utils.NewMultiErrors()
workers := concurrency.NewWorkerPool(c.config.Concurrency)
layerSet := make(map[string]interface{})
for _, layer := range manifest.GetLayerDigests() {
l := layer
if _, ok := layerSet[l.Hex()]; ok {
// Duplicate layer.
continue
} else {
layerSet[l.Hex()] = struct{}{}
}
workers.Do(func() {
if err := c.PushLayer(l); err != nil {
multiError.Add(fmt.Errorf("push layer %s: %s", l, err))
workers.Stop()
return
}
})
}
l := manifest.GetConfigDigest()
workers.Do(func() {
if err := c.PushImageConfig(l); err != nil {
multiError.Add(fmt.Errorf("push image config %s: %s", l, err))
workers.Stop()
return
}
})
workers.Wait()
if err := multiError.Collect(); err != nil {
return err
}
if err := c.PushManifest(tag, manifest); err != nil {
return fmt.Errorf("push manifest: %s", err)
}
log.Infow(fmt.Sprintf("* Pushed image %s", name), "duration", time.Since(starttime))
return nil
}
// PullManifest pulls docker image manifest from the docker registry.
// It does not save the manifest to the store.
func (c DockerRegistryClient) PullManifest(tag string) (*image.DistributionManifest, error) {
opt, err := c.config.Security.GetHTTPOption(c.registry, c.repository)
if err != nil {
return nil, fmt.Errorf("get security opt: %s", err)
}
URL := fmt.Sprintf(baseManifestQuery, c.registry, c.repository, tag)
resp, err := httputil.Send(
"GET",
URL,
httputil.SendClient(c.client),
opt,
httputil.SendTimeout(c.config.Timeout),
c.config.sendRetry(),
httputil.SendAcceptedCodes(http.StatusOK, http.StatusNotFound, http.StatusBadRequest),
httputil.SendHeaders(map[string]string{"Accept": image.MediaTypeManifest}))
if err != nil {
return nil, fmt.Errorf("http send error: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusBadRequest {
return nil, fmt.Errorf("manifest not found")
} else if resp.StatusCode != 200 {
return nil, fmt.Errorf("bad pull manifest request resp code: %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read resp body: %s", err)
}
// Parse the manifest according to the content type.
ctHeader := resp.Header.Get("Content-Type")
manifest, _, err := image.UnmarshalDistributionManifest(ctHeader, body)
if err != nil {
return nil, fmt.Errorf("unmarshal distribution manifest: %s", err)
}
return &manifest, nil
}
// PushManifest pushes the manifest to the registry.
func (c DockerRegistryClient) PushManifest(tag string, manifest *image.DistributionManifest) error {
payload, err := json.MarshalIndent(manifest, "", " ")
if err != nil {
return fmt.Errorf("marshal manifest: %s", err)
}
headers := map[string]string{
"Content-Type": manifest.MediaType,
"Host": c.registry,
}
opt, err := c.config.Security.GetHTTPOption(c.registry, c.repository)
if err != nil {
return fmt.Errorf("get security opt: %s", err)
}
URL := fmt.Sprintf(baseManifestQuery, c.registry, c.repository, tag)
resp, err := httputil.Send(
"PUT",
URL,
httputil.SendClient(c.client),
opt,
httputil.SendTimeout(c.config.Timeout),
c.config.sendRetry(),
httputil.SendAcceptedCodes(http.StatusOK, http.StatusCreated),
httputil.SendHeaders(headers),
httputil.SendBody(bytes.NewReader(payload)))
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
// PullLayer pulls image layer from the registry, and verifies that the contents
// of that layer match the digest of the manifest.
// If the layer already exists in the imagestore, the download is skipped.
func (c DockerRegistryClient) PullLayer(layerDigest image.Digest) (os.FileInfo, error) {
return c.pullLayerHelper(layerDigest, false)
}
// PullImageConfig pulls image config blob from the registry.
// Same as PullLayer, with slightly different log message.
func (c DockerRegistryClient) PullImageConfig(layerDigest image.Digest) (os.FileInfo, error) {
return c.pullLayerHelper(layerDigest, true)
}
func (c DockerRegistryClient) pullLayerHelper(
layerDigest image.Digest, isConfig bool) (os.FileInfo, error) {
if info, err := c.store.Layers.GetDownloadOrCacheFileStat(layerDigest.Hex()); err == nil {
if isConfig {
log.Infof("* Skipped pulling existing image config %s:%s", c.repository, layerDigest)
} else {
log.Infof("* Skipped pulling existing layer %s:%s", c.repository, layerDigest)
}
return info, nil
}
opt, err := c.config.Security.GetHTTPOption(c.registry, c.repository)
if err != nil {
return nil, fmt.Errorf("get security opt: %s", err)
}
if isConfig {
log.Infof("* Started pulling image config %s/%s:%s", c.registry, c.repository, layerDigest)
} else {
log.Infof("* Started pulling layer %s/%s:%s", c.registry, c.repository, layerDigest)
}
URL := fmt.Sprintf(baseLayerQuery, c.registry, c.repository, string(layerDigest))
resp, err := httputil.Send(
"GET",
URL,
httputil.SendClient(c.client),
opt,
httputil.SendTimeout(c.config.Timeout),
c.config.sendRetry())
if err != nil {
return nil, fmt.Errorf("send pull layer request %s: %s", URL, err)
}
defer resp.Body.Close()
if err := c.store.Layers.CreateDownloadFile(layerDigest.Hex(), 0); err != nil {
return nil, fmt.Errorf("create layer file: %s", err)
}
w, err := c.store.Layers.GetDownloadFileReadWriter(layerDigest.Hex())
if err != nil {
return nil, fmt.Errorf("get layer file readwriter: %s", err)
}
defer w.Close()
if _, err := io.Copy(w, resp.Body); err != nil {
return nil, fmt.Errorf("copy layer file: %s", err)
}
if err := c.saveLayer(layerDigest); err != nil {
return nil, fmt.Errorf("save layer file: %s", err)
}
info, err := c.store.Layers.GetDownloadOrCacheFileStat(layerDigest.Hex())
if err != nil {
return nil, fmt.Errorf("get layer stat: %s", err)
}
if isConfig {
log.Infof("* Finished pulling image config %s:%s", c.repository, layerDigest.Hex())
} else {
log.Infof("* Finished pulling layer %s:%s", c.repository, layerDigest.Hex())
}
return info, nil
}
// PushLayer pushes the image layer to the registry.
func (c DockerRegistryClient) PushLayer(layerDigest image.Digest) error {
return c.pushLayerWithBackoff(layerDigest, false)
}
// PushImageConfig pushes image config blob to the registry.
// Same as PushLayer, with slightly different log message.
func (c DockerRegistryClient) PushImageConfig(layerDigest image.Digest) error {
return c.pushLayerWithBackoff(layerDigest, true)
}
func (c DockerRegistryClient) pushLayerWithBackoff(layerDigest image.Digest, isConfig bool) error {
multiError := utils.NewMultiErrors()
b := c.config.backoff()
for {
err := c.pushLayerHelper(layerDigest, isConfig)
if err == nil {
break
}
multiError.Add(err)
d := b.NextBackOff()
if d == backoff.Stop {
break
}
// Retry when registry returns network error, retryable error or
// unexpected code 500. Since building an image could be rather
// expensive, we allow the client to be more forgiving on
// temporarily unexpected condition on registry side.
if httputil.IsNetworkError(err) ||
httputil.IsRetryable(err) ||
httputil.IsStatus(err, http.StatusInternalServerError) {
log.Infof("* Failed to push layer: %s, retrying...", err)
time.Sleep(d)
continue
}
break
}
return multiError.Collect()
}
func (c DockerRegistryClient) pushLayerHelper(layerDigest image.Digest, isConfig bool) error {
if found, err := c.layerExists(layerDigest); err != nil {
return fmt.Errorf("check layer exists: %s/%s (%s): %w", c.registry, c.repository, layerDigest, err)
} else if found {
if isConfig {
log.Infof("* Skipped pushing existing image config %s:%s", c.repository, layerDigest)
} else {
log.Infof("* Skipped pushing existing layer %s:%s", c.repository, layerDigest)
}
return nil
}
opt, err := c.config.Security.GetHTTPOption(c.registry, c.repository)
if err != nil {
return fmt.Errorf("get security opt: %s", err)
}
URL := fmt.Sprintf(baseStartQuery, c.registry, c.repository)
resp, err := httputil.Send(
"POST",
URL,
httputil.SendClient(c.client),
opt,
httputil.SendTimeout(c.config.Timeout),
c.config.sendRetry(),
httputil.SendAcceptedCodes(http.StatusAccepted),
httputil.SendHeaders(map[string]string{"Host": c.registry}))
if err != nil {
return fmt.Errorf("send start push layer request %s: %w", URL, err)
}
defer resp.Body.Close()
URL = resp.Header.Get("Location")
if URL == "" {
return fmt.Errorf("empty layer upload URL")
}
if isConfig {
log.Infof("* Started pushing image config %s", layerDigest)
} else {
log.Infof("* Started pushing layer %s", layerDigest)
}
URL, err = c.pushLayerContent(layerDigest, URL)
if err != nil {
return fmt.Errorf("push layer content %s: %w", layerDigest, err)
}
parsed, err := url.Parse(URL)
if err != nil {
return fmt.Errorf("failed to parse location: %s", err)
}
q := parsed.Query()
q.Add("digest", string(layerDigest))
parsed.RawQuery = q.Encode()
if err := c.commitLayer(parsed.String()); err != nil {
return fmt.Errorf("commit layer push %s: %w", layerDigest, err)
}
if isConfig {
log.Infof("* Finished pushing image config %s", layerDigest)
} else {
log.Infof("* Finished pushing layer %s", layerDigest)
}
return nil
}
// manifestExists checks with the registry to see if an image is present and available for download.
func (c DockerRegistryClient) manifestExists(tag string) (bool, error) {
opt, err := c.config.Security.GetHTTPOption(c.registry, c.repository)
if err != nil {
return false, fmt.Errorf("get security opt: %s", err)
}
URL := fmt.Sprintf(baseManifestQuery, c.registry, c.repository, tag)
resp, err := httputil.Send(
"HEAD",
URL,
httputil.SendClient(c.client),
opt,
httputil.SendTimeout(c.config.Timeout),
c.config.sendRetry(),
httputil.SendAcceptedCodes(http.StatusOK, http.StatusNotFound, http.StatusBadRequest))
if err != nil {
return false, fmt.Errorf("check manifest exists: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusBadRequest {
return false, nil
}
return true, nil
}
// layerExists checks with the registry to see if a layer exists and is downloadable.
func (c DockerRegistryClient) layerExists(digest image.Digest) (bool, error) {
opt, err := c.config.Security.GetHTTPOption(c.registry, c.repository)
if err != nil {
return false, fmt.Errorf("get security opt: %s", err)
}
URL := fmt.Sprintf(baseLayerQuery, c.registry, c.repository, digest)
resp, err := httputil.Send(
"HEAD",
URL,
httputil.SendClient(c.client),
opt,
httputil.SendTimeout(c.config.Timeout),
c.config.sendRetry(),
httputil.SendAcceptedCodes(http.StatusOK, http.StatusNotFound))
if err != nil {
return false, fmt.Errorf("check manifest exists: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return false, nil
}
return true, nil
}
func (c DockerRegistryClient) pushLayerContent(digest image.Digest, location string) (string, error) {
info, err := c.store.Layers.GetStoreFileStat(digest.Hex())
if err != nil {
return "", fmt.Errorf("get layer file stat: %s", err)
}
size := info.Size()
pushChunk := c.config.PushChunk
if pushChunk == -1 {
pushChunk = size
}
start, endInclusive := int64(0), utils.Min(pushChunk-1, size-1)
r, err := c.store.Layers.GetStoreFileReader(digest.Hex())
if err != nil {
return "", fmt.Errorf("get layer file reader: %s", err)
}
defer r.Close()
for start < size {
location, err = c.pushOneLayerChunk(location, start, endInclusive, r)
if err != nil {
return location, fmt.Errorf("push layer chunk: %w", err)
}
start, endInclusive = endInclusive+1, utils.Min(start+pushChunk-1, size-1)
}
return location, nil
}
func (c DockerRegistryClient) pushOneLayerChunk(location string, start, endIncluded int64, r io.Reader) (string, error) {
opt, err := c.config.Security.GetHTTPOption(c.registry, c.repository)
if err != nil {
return "", fmt.Errorf("get security opt: %s", err)
}
chunckSize := endIncluded + 1 - start
r = io.LimitReader(r, chunckSize)
readerOptions := ratelimit.NewBucketWithRate(c.config.PushRate, 1)
headers := map[string]string{
"Host": c.registry,
"Content-Type": "application/octet-stream",
"Content-Length": fmt.Sprintf("%d", chunckSize),
"Content-Range": fmt.Sprintf("%d-%d", start, endIncluded),
}
resp, err := httputil.Send(
"PATCH",
location,
httputil.SendClient(c.client),
opt,
httputil.SendTimeout(c.config.Timeout),
c.config.sendRetry(),
// Docker registry returns 202
// GCR returns 204 on success
// AWS ECR returns 201 on success
httputil.SendAcceptedCodes(http.StatusAccepted, http.StatusNoContent, http.StatusCreated),
httputil.SendHeaders(headers),
httputil.SendBody(ratelimit.Reader(r, readerOptions)))
if err != nil {
return "", fmt.Errorf("send push chunk request: %w", err)
}
defer resp.Body.Close()
newLocation := resp.Header.Get("Location")
if newLocation == "" {
return "", fmt.Errorf("empty layer upload URL")
}
return newLocation, nil
}
func (c DockerRegistryClient) commitLayer(location string) error {
opt, err := c.config.Security.GetHTTPOption(c.registry, c.repository)
if err != nil {
return fmt.Errorf("get security opt: %s", err)
}
headers := map[string]string{
"Host": c.registry,
"Content-Type": "application/octet-stream",
"Content-Length": fmt.Sprintf("%d", 0),
}
resp, err := httputil.Send(
"PUT",
location,
httputil.SendClient(c.client),
opt,
httputil.SendTimeout(c.config.Timeout),
c.config.sendRetry(),
// Docker registry returns 201 but gcr returns 204 on success.
httputil.SendAcceptedCodes(http.StatusCreated, http.StatusNoContent),
httputil.SendHeaders(headers))
if err != nil {
return fmt.Errorf("commit: %w", err)
}
defer resp.Body.Close()
return nil
}
// saveLayer moves the layer from the download file to the permanent storage in store.
func (c DockerRegistryClient) saveLayer(layerDigest image.Digest) error {
// Verify that the layers downloaded were correct.
r, err := c.store.Layers.GetDownloadFileReader(layerDigest.Hex())
if err != nil {
return fmt.Errorf("get layer file reader: %s", err)
}
defer r.Close()
if verified, err := layerDigest.Equals(r); err != nil {
return fmt.Errorf("verify layer: %s", err)
} else if !verified {
return fmt.Errorf("layer digest did not match")
}
if err := c.store.Layers.MoveDownloadFileToStore(layerDigest.Hex()); err != nil && !os.IsExist(err) {
return fmt.Errorf("commit layer to store: %s", err)
}
return nil
}
// saveManifest saves given distribution manifest into local store.
func (c DockerRegistryClient) saveManifest(tag string, manifest *image.DistributionManifest) error {
if _, err := c.store.Manifests.GetDownloadOrCacheFileStat(c.repository, tag); err == nil {
return nil
}
if err := c.store.Manifests.CreateDownloadFile(c.repository, tag, 0); err != nil {
return fmt.Errorf("create manifest file: %s", err)
}
w, err := c.store.Manifests.GetDownloadFileReadWriter(c.repository, tag)
if err != nil {
return fmt.Errorf("create manifest file readwriter: %s", err)
}
defer w.Close()
manifestJSON, err := json.Marshal(manifest)
if err != nil {
return fmt.Errorf("marshal manifest: %s", err)
}
if _, err := w.Write(manifestJSON); err != nil {
return fmt.Errorf("write manifest json: %s", err)
}
if err := c.store.Manifests.MoveDownloadFileToStore(c.repository, tag); err != nil {
return fmt.Errorf("commit manifest to store: %s", err)
}
return nil
}
// loadManifest reads distribution manifest content from local manifest store.
func (c DockerRegistryClient) loadManifest(tag string) (*image.DistributionManifest, error) {
r, err := c.store.Manifests.GetStoreFileReader(c.repository, tag)
if err != nil {
return nil, fmt.Errorf("get manifest file reader: %s", err)
}
manifestBytes, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("read manifest: %s", err)
}
manifest := new(image.DistributionManifest)
if err := json.Unmarshal(manifestBytes, manifest); err != nil {
return nil, fmt.Errorf("unmarshal manifest: %s", err)
}
return manifest, nil
}