...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package daemon
16
17 import (
18 "fmt"
19 "io"
20
21 "github.com/google/go-containerregistry/pkg/name"
22 v1 "github.com/google/go-containerregistry/pkg/v1"
23 "github.com/google/go-containerregistry/pkg/v1/tarball"
24 )
25
26
27 func Tag(src, dest name.Tag, options ...Option) error {
28 o, err := makeOptions(options...)
29 if err != nil {
30 return err
31 }
32
33 return o.client.ImageTag(o.ctx, src.String(), dest.String())
34 }
35
36
37 func Write(tag name.Tag, img v1.Image, options ...Option) (string, error) {
38 o, err := makeOptions(options...)
39 if err != nil {
40 return "", err
41 }
42
43
44 id, err := img.ConfigName()
45 if err != nil {
46 return "", fmt.Errorf("computing image ID: %w", err)
47 }
48 if resp, _, err := o.client.ImageInspectWithRaw(o.ctx, id.String()); err == nil {
49 want := tag.String()
50
51
52 for _, have := range resp.RepoTags {
53 if have == want {
54 return "", nil
55 }
56 }
57
58 return "", o.client.ImageTag(o.ctx, id.String(), want)
59 }
60
61 pr, pw := io.Pipe()
62 go func() {
63 pw.CloseWithError(tarball.Write(tag, img, pw))
64 }()
65
66
67 resp, err := o.client.ImageLoad(o.ctx, pr, false)
68 if err != nil {
69 return "", fmt.Errorf("error loading image: %w", err)
70 }
71 defer resp.Body.Close()
72 b, err := io.ReadAll(resp.Body)
73 response := string(b)
74 if err != nil {
75 return response, fmt.Errorf("error reading load response body: %w", err)
76 }
77 return response, nil
78 }
79
View as plain text