-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
278 lines (231 loc) · 8.8 KB
/
manifest.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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
)
type ImageConfigRootfs struct {
DiffIDs []string `json:"diff_ids"`
Type string `json:"type"`
}
type ImageConfig struct {
Rootfs ImageConfigRootfs `json:"rootfs"`
}
type ConfigManifest struct {
// This descriptor property has additional restrictions for config.
// Implementations MUST NOT attempt to parse the referenced content if this media type is unknown and instead consider the referenced content as arbitrary binary data (e.g.: as application/octet-stream).
// Implementations storing or copying image manifests MUST NOT error on encountering a value that is unknown to the implementation
// Implementations MUST support at least the following media types:
// - application/vnd.oci.image.config.v1+json
// Manifests for container images concerned with portability SHOULD use one of the above media types. Manifests for artifacts concerned with portability SHOULD use config.mediaType as described in Guidelines for Artifact Usage.
// If the manifest uses a different media type than the above, it MUST comply with RFC 6838, including the naming requirements in its section 4.2, and MAY be registered with IANA.
MediaType string `json:"mediaType"`
Size uint64 `json:"size"`
Digest string `json:"digest"`
}
type LayerManifest struct {
// This descriptor property has additional restrictions for layers[]. Implementations MUST support at least the following media types:
// application/vnd.oci.image.layer.v1.tar
// application/vnd.oci.image.layer.v1.tar+gzip
// application/vnd.oci.image.layer.nondistributable.v1.tar
// application/vnd.oci.image.layer.nondistributable.v1.tar+gzip
// Manifests concerned with portability SHOULD use one of the above media types. Implementations storing or copying image manifests MUST NOT error on encountering a mediaType that is unknown to the implementation.
// Entries in this field will frequently use the +gzip types.
MediaType string `json:"mediaType"`
Size uint64 `json:"size"`
Digest string `json:"digest"`
}
type Manifest struct {
// This REQUIRED property specifies the image manifest schema version. For this version of the specification,
// this MUST be 2 to ensure backward compatibility with older versions of Docker. The value of this field will not change.
// This field MAY be removed in a future version of the specification.
SchemaVersion int `json:"schemaVersion"`
// This property SHOULD be used and remain compatible with earlier versions of this specification and with other similar external formats.
// When used, this field MUST contain the media type application/vnd.oci.image.manifest.v1+json.
// This field usage differs from the descriptor use of mediaType.
MediaType string `json:"mediaType"`
// This OPTIONAL property contains the type of an artifact when the manifest is used for an artifact. This MUST be set when config.mediaType is set to the empty value. If defined, the value MUST comply with RFC 6838, including the naming requirements in its section 4.2, and MAY be registered with IANA.
// Implementations storing or copying image manifests MUST NOT error on encountering an artifactType that is unknown to the implementation.
ArtifactType string `json:"artifactType,omitempty"`
// This REQUIRED property references a configuration object for a container, by digest.
Config ConfigManifest `json:"config"`
// Each item in the array MUST be a descriptor.
// For portability, layers SHOULD have at least one entry. See the guidance for an empty descriptor below,
// and DescriptorEmptyJSON of the reference code.
Layers []LayerManifest `json:"layers"`
}
const storageDriver = "overlay2"
type RepositoryTag string
func (r RepositoryTag) getDockerPath() string {
return fmt.Sprintf("/var/lib/docker/image/overlay2/imagedb/content/sha256/%s", strings.Split(string(r), ":")[1])
}
func (r RepositoryTag) IntoImageConfig(folder string) (ImageConfig, int64, error) {
p := r.getDockerPath()
fd, err := os.Open(p)
if err != nil {
return ImageConfig{}, 0, fmt.Errorf("open %q: %w", p, err)
}
defer fd.Close()
fdLayer, err := os.Create(filepath.Join(folder, string(r)))
if err != nil {
return ImageConfig{}, 0, fmt.Errorf("create layers %q: %w", r, err)
}
defer fdLayer.Close()
reader := io.TeeReader(fd, fdLayer)
decoder := json.NewDecoder(reader)
config := ImageConfig{}
if err := decoder.Decode(&config); err != nil {
return ImageConfig{}, 0, fmt.Errorf("decode: %w", err)
}
size := decoder.InputOffset()
return config, size, nil
}
type Repository = map[string]RepositoryTag
type Repositories struct {
Repositories map[string]Repository `json:"Repositories"`
}
func (r Repositories) getConfigSHA256(name, tag string) (RepositoryTag, error) {
repos, ok := r.Repositories[name]
if !ok {
return "", fmt.Errorf("name %q not found", name)
}
fullName := fmt.Sprintf("%s:%s", name, tag)
sha, ok := repos[fullName]
if !ok {
return "", fmt.Errorf("full tag %q not found", fullName)
}
return sha, nil
}
const layerFolder = "/var/lib/push/layers"
func generateManifestFromDocker(ctx context.Context, imageURL string, db *db) (Manifest, error) {
if err := os.MkdirAll(layerFolder, os.ModePerm); err != nil {
return Manifest{}, fmt.Errorf("layers: %w", err)
}
repositories, err := getRepositories(ctx)
if err != nil {
return Manifest{}, fmt.Errorf("get repositories: %w", err)
}
i := strings.LastIndex(imageURL, ":")
if i == -1 {
return Manifest{}, fmt.Errorf("unexpected ':' not found on %s", imageURL)
}
sha, err := repositories.getConfigSHA256(imageURL[:i], imageURL[i+1:])
if err != nil {
return Manifest{}, fmt.Errorf("get config: %w", err)
}
// TODO: Cache the manifest and don't do this multiple times
config, configSize, err := sha.IntoImageConfig(layerFolder)
if err != nil {
return Manifest{}, fmt.Errorf("image config: %w", err)
}
layers := make([]LayerReader, 0, len(config.Rootfs.DiffIDs))
var prevNode *node
wg := sync.WaitGroup{}
for _, layer := range config.Rootfs.DiffIDs {
wg.Add(1)
if prevNode == nil {
prevNode = db.Get(layer)
} else {
prevNode = db.GetChild(prevNode, layer)
}
// we don't need to calculate size if we are compressing and it's not a dry run
if *inMemory && *compressionLevel != 0 && !*dryRun {
layers = append(layers, noopLayer{size: 0, hash: layer})
continue
}
p := filepath.Join(layerFolder, layer)
if stat, err := os.Stat(p); err == nil && stat != nil {
layers = append(layers, noopLayer{size: uint64(stat.Size()), hash: layer})
continue
}
reader, err := newLayerReader(prevNode)
if err != nil {
return Manifest{}, fmt.Errorf("create layer reader %q: %w", layer, err)
}
layers = append(layers, reader)
defer reader.Close()
}
errs := make(chan error, len(layers))
for i, layer := range layers {
if _, isNoop := layer.(noopLayer); isNoop {
wg.Done()
continue
}
i, layer := i, layer
go func() {
defer wg.Done()
// skip if it's in memory
if *inMemory {
if _, err := io.Copy(io.Discard, layer); err != nil {
errs <- fmt.Errorf("copying layer %s: %w", config.Rootfs.DiffIDs[i], err)
}
return
}
p := filepath.Join(layerFolder, config.Rootfs.DiffIDs[i])
fd, err := os.Create(p)
if err != nil {
errs <- fmt.Errorf("creating file %s: %w", config.Rootfs.DiffIDs[i], err)
return
}
if _, err := io.Copy(fd, layer); err != nil {
errs <- fmt.Errorf("copying layer %s: %w", config.Rootfs.DiffIDs[i], err)
}
}()
}
wg.Wait()
containsErr := false
forLoop:
for {
select {
case err := <-errs:
containsErr = true
fmt.Fprintln(os.Stderr, "Error", err.Error())
default:
break forLoop
}
}
if containsErr {
return Manifest{}, errors.New("there has been errors, see stderr output")
}
m := Manifest{}
// TODO: Dry run won't spit the correct hashes, sizes and media types if the user wants to see the compressed one
m.Config = ConfigManifest{
MediaType: "application/vnd.oci.image.config.v1+json",
Size: uint64(configSize),
Digest: string(sha),
}
m.Layers = make([]LayerManifest, 0, len(layers))
for _, layer := range layers {
m.Layers = append(m.Layers, LayerManifest{
Size: layer.Size(),
Digest: layer.Hash(),
MediaType: "application/vnd.oci.image.layer.v1.tar",
})
}
m.SchemaVersion = 2
m.MediaType = "application/vnd.oci.image.manifest.v1+json"
return m, nil
}
func getRepositoriesPath() string {
return fmt.Sprintf("/var/lib/docker/image/%s/repositories.json", storageDriver)
}
func getRepositories(ctx context.Context) (Repositories, error) {
p := getRepositoriesPath()
fd, err := os.Open(p)
if err != nil {
return Repositories{}, fmt.Errorf("open %q: %w", p, err)
}
defer fd.Close()
decoder := json.NewDecoder(fd)
r := Repositories{}
if err := decoder.Decode(&r); err != nil {
return r, fmt.Errorf("decode: %w", err)
}
return r, nil
}