-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
275 lines (254 loc) · 8.57 KB
/
image.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
package compose
import (
"context"
"encoding/json"
"fmt"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/reference"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
const (
ImageRootMaxSize = 100 * 1024
)
type (
ImageRoot struct {
specs.Versioned
MediaType string `json:"mediaType,omitempty"`
Config json.RawMessage `json:"config,omitempty"`
Layers json.RawMessage `json:"layers,omitempty"`
Manifests json.RawMessage `json:"manifests,omitempty"`
FSLayers json.RawMessage `json:"fsLayers,omitempty"` // schema 1
Type BlobType
Children []TreeNode
}
ImageTree TreeNode
ImageRef struct {
reference.Spec
Digest digest.Digest
}
)
func ParseImageRef(ref string) (*ImageRef, error) {
refSpec, err := reference.Parse(ref)
if err != nil {
return nil, err
}
return &ImageRef{
Spec: refSpec,
Digest: refSpec.Digest(),
}, nil
}
func (r ImageRef) GetBlobRef(digest digest.Digest) string {
return r.Locator + "@" + digest.String()
}
func (t ImageTree) Print(initDepth ...int) {
startDepth := 0
if len(initDepth) > 0 {
startDepth = initDepth[0]
}
rootType := "unknown"
err := TreeNode(t).Walk(func(node *TreeNode, depth int) error {
printDepth := (startDepth + depth) * 10
if depth == 0 {
if images.IsIndexType(node.Descriptor.MediaType) {
rootType = "index"
} else if images.IsManifestType(node.Descriptor.MediaType) {
rootType = "manifest"
}
id := node.Descriptor.Digest.String()
if len(node.Descriptor.URLs) > 0 {
id = node.Descriptor.URLs[0]
}
fmt.Printf("%*s%s: %s, %d\n", printDepth, "|—>", rootType, id, node.Descriptor.Size)
} else if depth == 1 && rootType == "index" {
fmt.Printf("%*smanifest: %s, %s, %d\n", printDepth, " ", node.Descriptor.Digest.String(), node.Descriptor.Platform.Architecture, node.Descriptor.Size)
} else {
if images.IsConfigType(node.Descriptor.MediaType) {
fmt.Printf("%*sconfig: %s, %d\n", printDepth, " ", node.Descriptor.Digest.String(), node.Descriptor.Size)
} else if images.IsLayerType(node.Descriptor.MediaType) {
fmt.Printf("%*slayer: %s, %d\n", printDepth, " ", node.Descriptor.Digest.String(), node.Descriptor.Size)
}
}
return nil
})
if err != nil {
fmt.Printf("Failed to print image tree: %s\n", err.Error())
}
}
func LoadImageTree(ctx context.Context, provider BlobProvider, platform platforms.MatchComparer, ref string) (*TreeNode, error) {
// root node
rootRef, imageRoot, rootDesc, err := ReadImageRoot(WithBlobType(ctx, BlobTypeImageIndex), provider, ref)
if err != nil {
return nil, err
}
imageTree := TreeNode{
Descriptor: rootDesc,
Type: imageRoot.Type,
}
// depth 1, arch specific manifests or config + layers
var childrenDescs []*ocispec.Descriptor
switch imageRoot.Type {
case BlobTypeSkopeoImageIndex, BlobTypeImageIndex:
{
if err := json.Unmarshal(imageRoot.Manifests, &childrenDescs); err != nil {
return nil, err
}
}
case BlobTypeImageManifest:
{
// parse config and layers and add them as image root children
configDesc, layerDescs, err := ParseImageRootAsManifest(imageRoot)
if err != nil {
return nil, err
}
configDesc.URLs = []string{rootRef.GetBlobRef(configDesc.Digest)}
imageTree.Children = append(imageTree.Children, &TreeNode{
Descriptor: configDesc, Type: BlobTypeImageConfig, Children: nil,
})
for _, l := range layerDescs {
l.URLs = []string{rootRef.GetBlobRef(l.Digest)}
imageTree.Children = append(imageTree.Children, &TreeNode{
Descriptor: l, Type: BlobTypeImageLayer, Children: nil,
})
}
return &imageTree, nil
}
default:
return nil, fmt.Errorf("unsupported image root type: %s", rootDesc.MediaType)
}
// depth 2 if image root is index
if imageRoot.Type != BlobTypeImageIndex && imageRoot.Type != BlobTypeSkopeoImageIndex {
panic("expected image index got " + imageRoot.MediaType)
}
for _, c := range childrenDescs {
var grandchildren []*TreeNode
if imageRoot.Type != BlobTypeSkopeoImageIndex && !platform.Match(*c.Platform) {
continue
}
manifestRef := rootRef.GetBlobRef(c.Digest)
manifest, err := ReadImageManifest(ctx, provider, manifestRef, c.Size)
if err != nil {
return nil, err
}
c.URLs = []string{manifestRef}
manifest.Config.URLs = []string{rootRef.GetBlobRef(manifest.Config.Digest)}
grandchildren = append(grandchildren, &TreeNode{Descriptor: &manifest.Config, Type: BlobTypeImageConfig, Children: nil})
for ii := 0; ii < len(manifest.Layers); ii++ {
l := &manifest.Layers[ii]
l.URLs = []string{rootRef.GetBlobRef(l.Digest)}
grandchildren = append(grandchildren, &TreeNode{Descriptor: l, Type: BlobTypeImageLayer, Children: nil})
}
imageTree.Children = append(imageTree.Children, &TreeNode{
Descriptor: c, Type: BlobTypeImageManifest, Children: grandchildren,
})
}
return &imageTree, nil
}
func ReadImageRoot(ctx context.Context, provider BlobProvider, ref string) (*ImageRef, *ImageRoot, *ocispec.Descriptor, error) {
imageRef, err := ParseImageRef(ref)
if err != nil {
return nil, nil, nil, err
}
if len(imageRef.Digest) == 0 {
return imageRef, nil, nil, fmt.Errorf("unsupported image reference format; digest is required")
}
b, err := ReadBlobWithReadLimit(ctx, provider, ref, ImageRootMaxSize)
if err != nil {
return imageRef, nil, nil, err
}
var imageRoot ImageRoot
if err := json.Unmarshal(b, &imageRoot); err != nil {
return imageRef, nil, nil, err
}
imageRoot.Type, err = validateRoot(&imageRoot)
if err != nil {
return imageRef, nil, nil, fmt.Errorf("invalid image root: %s", err.Error())
}
return imageRef, &imageRoot, &ocispec.Descriptor{
MediaType: imageRoot.MediaType,
Digest: imageRef.Digest,
Size: int64(len(b)),
URLs: []string{ref},
}, nil
}
func ReadImageManifest(ctx context.Context, provider BlobProvider, ref string, size int64) (*ocispec.Manifest, error) {
refSpec, err := reference.Parse(ref)
if err != nil {
return nil, err
}
if len(refSpec.Digest()) == 0 {
return nil, fmt.Errorf("unsupported image reference format; digest is required")
}
b, err := ReadBlobWithResolving(ctx, provider, ref, size)
if err != nil {
return nil, err
}
var manifest ocispec.Manifest
if err := json.Unmarshal(b, &manifest); err != nil {
return nil, err
}
return &manifest, nil
}
func ParseImageRootAsManifest(root *ImageRoot) (*ocispec.Descriptor, []*ocispec.Descriptor, error) {
if !images.IsManifestType(root.MediaType) {
return nil, nil, fmt.Errorf("invalid image root type; expected manifest, got: %s", root.MediaType)
}
var configDesc ocispec.Descriptor
var layerDescs []*ocispec.Descriptor
var err error
if err = json.Unmarshal(root.Config, &configDesc); err == nil {
err = json.Unmarshal(root.Layers, &layerDescs)
}
if err != nil {
return nil, nil, err
}
return &configDesc, layerDescs, nil
}
func (t *TreeNode) GetImageConfigAndLayers() (*ocispec.Descriptor, []*ocispec.Descriptor, error) {
var children []*TreeNode
if images.IsIndexType(t.Descriptor.MediaType) {
for _, c := range t.Children {
if c.Children != nil {
children = c.Children
}
}
} else if images.IsManifestType(t.Descriptor.MediaType) {
children = t.Children
} else {
return nil, nil, fmt.Errorf("invalid image root blob format: %s", t.Descriptor.MediaType)
}
var config *ocispec.Descriptor
var layers []*ocispec.Descriptor
for _, c := range children {
if images.IsConfigType(c.Descriptor.MediaType) {
config = c.Descriptor
} else if images.IsLayerType(c.Descriptor.MediaType) {
layers = append(layers, c.Descriptor)
} else {
return nil, nil, fmt.Errorf("invalid image config or layer format: %s", t.Descriptor.MediaType)
}
}
return config, layers, nil
}
func validateRoot(root *ImageRoot) (BlobType, error) {
blobType := BlobTypeUnknown
if len(root.FSLayers) != 0 {
return blobType, fmt.Errorf("media-type: schema 1 not supported")
}
if images.IsManifestType(root.MediaType) {
if len(root.Manifests) != 0 || images.IsIndexType(root.MediaType) {
return blobType, fmt.Errorf("media-type: expected manifest but found index (%s)", root.MediaType)
}
blobType = BlobTypeImageManifest
} else if images.IsIndexType(root.MediaType) {
if len(root.Config) != 0 || len(root.Layers) != 0 || images.IsManifestType(root.MediaType) {
return blobType, fmt.Errorf("media-type: expected index but found manifest (%s)", root.MediaType)
}
blobType = BlobTypeImageIndex
} else if root.SchemaVersion == 2 && len(root.Manifests) > 0 {
blobType = BlobTypeSkopeoImageIndex
}
return blobType, nil
}