forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
253 lines (215 loc) · 8.49 KB
/
helpers.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
package util
import (
"encoding/json"
"fmt"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/manifest/schema2"
"github.com/golang/glog"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
)
func fillImageLayers(image *imageapi.Image, manifest imageapi.DockerImageManifest) error {
if len(image.DockerImageLayers) != 0 {
// DockerImageLayers is already filled by the registry.
return nil
}
switch manifest.SchemaVersion {
case 1:
if len(manifest.History) != len(manifest.FSLayers) {
return fmt.Errorf("the image %s (%s) has mismatched history and fslayer cardinality (%d != %d)", image.Name, image.DockerImageReference, len(manifest.History), len(manifest.FSLayers))
}
image.DockerImageLayers = make([]imageapi.ImageLayer, len(manifest.FSLayers))
for i, obj := range manifest.History {
layer := manifest.FSLayers[i]
var size imageapi.DockerV1CompatibilityImageSize
if err := json.Unmarshal([]byte(obj.DockerV1Compatibility), &size); err != nil {
size.Size = 0
}
// reverse order of the layers: in schema1 manifests the
// first layer is the youngest (base layers are at the
// end), but we want to store layers in the Image resource
// in order from the oldest to the youngest.
revidx := (len(manifest.History) - 1) - i // n-1, n-2, ..., 1, 0
image.DockerImageLayers[revidx].Name = layer.DockerBlobSum
image.DockerImageLayers[revidx].LayerSize = size.Size
image.DockerImageLayers[revidx].MediaType = schema1.MediaTypeManifestLayer
}
case 2:
// The layer list is ordered starting from the base image (opposite order of schema1).
// So, we do not need to change the order of layers.
image.DockerImageLayers = make([]imageapi.ImageLayer, len(manifest.Layers))
for i, layer := range manifest.Layers {
image.DockerImageLayers[i].Name = layer.Digest
image.DockerImageLayers[i].LayerSize = layer.Size
image.DockerImageLayers[i].MediaType = layer.MediaType
}
default:
return fmt.Errorf("unrecognized Docker image manifest schema %d for %q (%s)", manifest.SchemaVersion, image.Name, image.DockerImageReference)
}
if image.Annotations == nil {
image.Annotations = map[string]string{}
}
image.Annotations[imageapi.DockerImageLayersOrderAnnotation] = imageapi.DockerImageLayersOrderAscending
return nil
}
// ImageWithMetadata mutates the given image. It parses raw DockerImageManifest data stored in the image and
// fills its DockerImageMetadata and other fields.
func ImageWithMetadata(image *imageapi.Image) error {
if len(image.DockerImageManifest) == 0 {
return nil
}
ReorderImageLayers(image)
if len(image.DockerImageLayers) > 0 && image.DockerImageMetadata.Size > 0 && len(image.DockerImageManifestMediaType) > 0 {
glog.V(5).Infof("Image metadata already filled for %s", image.Name)
return nil
}
manifest := imageapi.DockerImageManifest{}
if err := json.Unmarshal([]byte(image.DockerImageManifest), &manifest); err != nil {
return err
}
err := fillImageLayers(image, manifest)
if err != nil {
return err
}
switch manifest.SchemaVersion {
case 1:
image.DockerImageManifestMediaType = schema1.MediaTypeManifest
if len(manifest.History) == 0 {
// It should never have an empty history, but just in case.
return fmt.Errorf("the image %s (%s) has a schema 1 manifest, but it doesn't have history", image.Name, image.DockerImageReference)
}
v1Metadata := imageapi.DockerV1CompatibilityImage{}
if err := json.Unmarshal([]byte(manifest.History[0].DockerV1Compatibility), &v1Metadata); err != nil {
return err
}
image.DockerImageMetadata.ID = v1Metadata.ID
image.DockerImageMetadata.Parent = v1Metadata.Parent
image.DockerImageMetadata.Comment = v1Metadata.Comment
image.DockerImageMetadata.Created = v1Metadata.Created
image.DockerImageMetadata.Container = v1Metadata.Container
image.DockerImageMetadata.ContainerConfig = v1Metadata.ContainerConfig
image.DockerImageMetadata.DockerVersion = v1Metadata.DockerVersion
image.DockerImageMetadata.Author = v1Metadata.Author
image.DockerImageMetadata.Config = v1Metadata.Config
image.DockerImageMetadata.Architecture = v1Metadata.Architecture
case 2:
image.DockerImageManifestMediaType = schema2.MediaTypeManifest
if len(image.DockerImageConfig) == 0 {
return fmt.Errorf("dockerImageConfig must not be empty for manifest schema 2")
}
config := imageapi.DockerImageConfig{}
if err := json.Unmarshal([]byte(image.DockerImageConfig), &config); err != nil {
return fmt.Errorf("failed to parse dockerImageConfig: %v", err)
}
image.DockerImageMetadata.ID = manifest.Config.Digest
image.DockerImageMetadata.Parent = config.Parent
image.DockerImageMetadata.Comment = config.Comment
image.DockerImageMetadata.Created = config.Created
image.DockerImageMetadata.Container = config.Container
image.DockerImageMetadata.ContainerConfig = config.ContainerConfig
image.DockerImageMetadata.DockerVersion = config.DockerVersion
image.DockerImageMetadata.Author = config.Author
image.DockerImageMetadata.Config = config.Config
image.DockerImageMetadata.Architecture = config.Architecture
default:
return fmt.Errorf("unrecognized Docker image manifest schema %d for %q (%s)", manifest.SchemaVersion, image.Name, image.DockerImageReference)
}
layerSet := sets.NewString()
if manifest.SchemaVersion == 2 {
layerSet.Insert(manifest.Config.Digest)
image.DockerImageMetadata.Size = int64(len(image.DockerImageConfig))
} else {
image.DockerImageMetadata.Size = 0
}
for _, layer := range image.DockerImageLayers {
if layerSet.Has(layer.Name) {
continue
}
layerSet.Insert(layer.Name)
image.DockerImageMetadata.Size += layer.LayerSize
}
return nil
}
// ReorderImageLayers mutates the given image. It reorders the layers in ascending order.
// Ascending order matches the order of layers in schema 2. Schema 1 has reversed (descending) order of layers.
func ReorderImageLayers(image *imageapi.Image) {
if len(image.DockerImageLayers) == 0 {
return
}
layersOrder, ok := image.Annotations[imageapi.DockerImageLayersOrderAnnotation]
if !ok {
switch image.DockerImageManifestMediaType {
case schema1.MediaTypeManifest, schema1.MediaTypeSignedManifest:
layersOrder = imageapi.DockerImageLayersOrderAscending
case schema2.MediaTypeManifest:
layersOrder = imageapi.DockerImageLayersOrderDescending
default:
return
}
}
if layersOrder == imageapi.DockerImageLayersOrderDescending {
// reverse order of the layers (lowest = 0, highest = i)
for i, j := 0, len(image.DockerImageLayers)-1; i < j; i, j = i+1, j-1 {
image.DockerImageLayers[i], image.DockerImageLayers[j] = image.DockerImageLayers[j], image.DockerImageLayers[i]
}
}
if image.Annotations == nil {
image.Annotations = map[string]string{}
}
image.Annotations[imageapi.DockerImageLayersOrderAnnotation] = imageapi.DockerImageLayersOrderAscending
}
// ManifestMatchesImage returns true if the provided manifest matches the name of the image.
func ManifestMatchesImage(image *imageapi.Image, newManifest []byte) (bool, error) {
dgst, err := digest.ParseDigest(image.Name)
if err != nil {
return false, err
}
v, err := digest.NewDigestVerifier(dgst)
if err != nil {
return false, err
}
var canonical []byte
switch image.DockerImageManifestMediaType {
case schema2.MediaTypeManifest:
var m schema2.DeserializedManifest
if err := json.Unmarshal(newManifest, &m); err != nil {
return false, err
}
_, canonical, err = m.Payload()
if err != nil {
return false, err
}
case schema1.MediaTypeManifest, "":
var m schema1.SignedManifest
if err := json.Unmarshal(newManifest, &m); err != nil {
return false, err
}
canonical = m.Canonical
default:
return false, fmt.Errorf("unsupported manifest mediatype: %s", image.DockerImageManifestMediaType)
}
if _, err := v.Write(canonical); err != nil {
return false, err
}
return v.Verified(), nil
}
// ImageConfigMatchesImage returns true if the provided image config matches a digest
// stored in the manifest of the image.
func ImageConfigMatchesImage(image *imageapi.Image, imageConfig []byte) (bool, error) {
if image.DockerImageManifestMediaType != schema2.MediaTypeManifest {
return false, nil
}
var m schema2.DeserializedManifest
if err := json.Unmarshal([]byte(image.DockerImageManifest), &m); err != nil {
return false, err
}
v, err := digest.NewDigestVerifier(m.Config.Digest)
if err != nil {
return false, err
}
if _, err := v.Write(imageConfig); err != nil {
return false, err
}
return v.Verified(), nil
}