forked from containerd/containerd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
119 lines (110 loc) · 2.86 KB
/
import.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
package containerd
import (
"archive/tar"
"context"
"encoding/json"
"io"
"io/ioutil"
"strings"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/reference"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)
func resolveOCIIndex(idx ocispec.Index, refObject string) (*ocispec.Descriptor, error) {
tag, dgst := reference.SplitObject(refObject)
if tag == "" && dgst == "" {
return nil, errors.Errorf("unexpected object: %q", refObject)
}
for _, m := range idx.Manifests {
if m.Digest == dgst {
return &m, nil
}
annot, ok := m.Annotations[ocispec.AnnotationRefName]
if ok && annot == tag && tag != "" {
return &m, nil
}
}
return nil, errors.Errorf("not found: %q", refObject)
}
func (c *Client) importFromOCITar(ctx context.Context, ref string, reader io.Reader, iopts importOpts) (Image, error) {
tr := tar.NewReader(reader)
store := c.ContentStore()
var desc *ocispec.Descriptor
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if hdr.Typeflag != tar.TypeReg && hdr.Typeflag != tar.TypeRegA {
continue
}
if hdr.Name == "index.json" {
desc, err = onUntarIndexJSON(tr, iopts.refObject)
if err != nil {
return nil, err
}
continue
}
if strings.HasPrefix(hdr.Name, "blobs/") {
if err := onUntarBlob(ctx, tr, store, hdr.Name, hdr.Size); err != nil {
return nil, err
}
}
}
if desc == nil {
return nil, errors.Errorf("no descriptor found for reference object %q", iopts.refObject)
}
imgrec := images.Image{
Name: ref,
Target: *desc,
}
is := c.ImageService()
if updated, err := is.Update(ctx, imgrec, "target"); err != nil {
if !errdefs.IsNotFound(err) {
return nil, err
}
created, err := is.Create(ctx, imgrec)
if err != nil {
return nil, err
}
imgrec = created
} else {
imgrec = updated
}
img := &image{
client: c,
i: imgrec,
}
return img, nil
}
func onUntarIndexJSON(r io.Reader, refObject string) (*ocispec.Descriptor, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
var idx ocispec.Index
if err := json.Unmarshal(b, &idx); err != nil {
return nil, err
}
return resolveOCIIndex(idx, refObject)
}
func onUntarBlob(ctx context.Context, r io.Reader, store content.Store, name string, size int64) error {
// name is like "blobs/sha256/deadbeef"
split := strings.Split(name, "/")
if len(split) != 3 {
return errors.Errorf("unexpected name: %q", name)
}
algo := digest.Algorithm(split[1])
if !algo.Available() {
return errors.Errorf("unsupported algorithm: %s", algo)
}
dgst := digest.NewDigestFromHex(algo.String(), split[2])
return content.WriteBlob(ctx, store, "unknown-"+dgst.String(), r, size, dgst)
}