forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
281 lines (240 loc) · 9.65 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
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
279
280
281
package util
import (
"encoding/json"
"fmt"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/manifest/schema2"
"github.com/golang/glog"
godigest "github.com/opencontainers/go-digest"
imagev1 "github.com/openshift/api/image/v1"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
"github.com/openshift/origin/pkg/image/apis/image/docker10"
)
func getImageLayers(manifest docker10.DockerImageManifest) ([]imageapi.ImageLayer, error) {
var imageLayers []imageapi.ImageLayer
switch manifest.SchemaVersion {
case 1:
if len(manifest.History) != len(manifest.FSLayers) {
return nil, fmt.Errorf("mismatched history and fslayer cardinality (%d != %d)", len(manifest.History), len(manifest.FSLayers))
}
imageLayers = make([]imageapi.ImageLayer, len(manifest.FSLayers))
for i, obj := range manifest.History {
layer := manifest.FSLayers[i]
var size docker10.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
imageLayers[revidx].Name = layer.DockerBlobSum
imageLayers[revidx].LayerSize = size.Size
imageLayers[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.
imageLayers = make([]imageapi.ImageLayer, len(manifest.Layers))
for i, layer := range manifest.Layers {
imageLayers[i].Name = layer.Digest
imageLayers[i].LayerSize = layer.Size
imageLayers[i].MediaType = layer.MediaType
}
default:
return nil, fmt.Errorf("unrecognized Docker image manifest schema %d", manifest.SchemaVersion)
}
return imageLayers, 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(imageLayers []imageapi.ImageLayer, layersOrder, imageManifestMediaType string) bool {
if imageLayers == nil || len(imageLayers) == 0 {
return false
}
if layersOrder == "" {
switch imageManifestMediaType {
case schema1.MediaTypeManifest, schema1.MediaTypeSignedManifest:
layersOrder = imageapi.DockerImageLayersOrderAscending
case schema2.MediaTypeManifest:
layersOrder = imageapi.DockerImageLayersOrderDescending
default:
return false
}
}
if layersOrder == imageapi.DockerImageLayersOrderDescending {
// reverse order of the layers (lowest = 0, highest = i)
for i, j := 0, len(imageLayers)-1; i < j; i, j = i+1, j-1 {
imageLayers[i], imageLayers[j] = imageLayers[j], imageLayers[i]
}
}
return true
}
func convertImageLayers(imageLayers []imagev1.ImageLayer) []imageapi.ImageLayer {
if imageLayers == nil {
return nil
}
result := make([]imageapi.ImageLayer, len(imageLayers))
for i := range imageLayers {
result[i].MediaType = imageLayers[i].MediaType
result[i].Name = imageLayers[i].Name
result[i].LayerSize = imageLayers[i].LayerSize
}
return result
}
func GetImageMetadata(image *imagev1.Image) (imageapi.DockerImage, error) {
if len(image.DockerImageManifest) == 0 {
return imageapi.DockerImage{}, nil
}
imageLayers := convertImageLayers(image.DockerImageLayers)
reorderImageLayers(imageLayers, image.Annotations[imageapi.DockerImageLayersOrderAnnotation], image.DockerImageManifestMediaType)
_, imageMetadata, _, _, err := getImageMetadata(image.Name, image.DockerImageReference,
image.DockerImageManifest, image.DockerImageConfig, imageLayers)
return imageMetadata, err
}
// 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
}
if ok := reorderImageLayers(image.DockerImageLayers,
image.Annotations[imageapi.DockerImageLayersOrderAnnotation], image.DockerImageManifestMediaType); ok {
if image.Annotations == nil {
image.Annotations = map[string]string{}
}
image.Annotations[imageapi.DockerImageLayersOrderAnnotation] = imageapi.DockerImageLayersOrderAscending
}
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
}
imageManifestMediaType, imageMetadata, imageLayers, orderAscending, err := getImageMetadata(image.Name, image.DockerImageReference,
image.DockerImageManifest, image.DockerImageConfig, image.DockerImageLayers)
if err != nil {
return err
}
image.DockerImageManifestMediaType = imageManifestMediaType
image.DockerImageMetadata = imageMetadata
image.DockerImageLayers = imageLayers
if orderAscending {
if image.Annotations == nil {
image.Annotations = map[string]string{}
}
image.Annotations[imageapi.DockerImageLayersOrderAnnotation] = imageapi.DockerImageLayersOrderAscending
}
return nil
}
func getImageMetadata(imageName, imageReference, imageManifest, imageConfig string,
imageLayers []imageapi.ImageLayer) (string, imageapi.DockerImage, []imageapi.ImageLayer, bool, error) {
manifest := docker10.DockerImageManifest{}
if err := json.Unmarshal([]byte(imageManifest), &manifest); err != nil {
return "", imageapi.DockerImage{}, []imageapi.ImageLayer{}, false, err
}
var err error
var orderAscending bool
if len(imageLayers) == 0 {
imageLayers, err = getImageLayers(manifest)
if err != nil {
return "", imageapi.DockerImage{}, []imageapi.ImageLayer{}, false, fmt.Errorf("the image %s (%s) failed reading layers: %v", imageName, imageReference, err)
}
orderAscending = true
}
var imageManifestMediaType string
var imageMetadata imageapi.DockerImage
switch manifest.SchemaVersion {
case 1:
imageManifestMediaType = schema1.MediaTypeManifest
if len(manifest.History) == 0 {
// It should never have an empty history, but just in case.
return "", imageapi.DockerImage{}, []imageapi.ImageLayer{}, false, fmt.Errorf("the image %s (%s) has a schema 1 manifest, but it doesn't have history", imageName, imageReference)
}
v1Metadata := docker10.DockerV1CompatibilityImage{}
if err := json.Unmarshal([]byte(manifest.History[0].DockerV1Compatibility), &v1Metadata); err != nil {
return "", imageapi.DockerImage{}, []imageapi.ImageLayer{}, false, err
}
if err := imageapi.Convert_compatibility_to_api_DockerImage(&v1Metadata, &imageMetadata); err != nil {
return "", imageapi.DockerImage{}, []imageapi.ImageLayer{}, false, err
}
case 2:
imageManifestMediaType = schema2.MediaTypeManifest
if len(imageConfig) == 0 {
return "", imageapi.DockerImage{}, []imageapi.ImageLayer{}, false, fmt.Errorf("dockerImageConfig must not be empty for manifest schema 2")
}
config := docker10.DockerImageConfig{}
if err := json.Unmarshal([]byte(imageConfig), &config); err != nil {
return "", imageapi.DockerImage{}, []imageapi.ImageLayer{}, false, fmt.Errorf("failed to parse dockerImageConfig: %v", err)
}
if err := imageapi.Convert_imageconfig_to_api_DockerImage(&config, &imageMetadata); err != nil {
return "", imageapi.DockerImage{}, []imageapi.ImageLayer{}, false, err
}
imageMetadata.ID = manifest.Config.Digest
default:
return "", imageapi.DockerImage{}, []imageapi.ImageLayer{}, false, fmt.Errorf("unrecognized Docker image manifest schema %d for %q (%s)", manifest.SchemaVersion, imageName, imageReference)
}
layerSet := sets.NewString()
if manifest.SchemaVersion == 2 {
layerSet.Insert(manifest.Config.Digest)
imageMetadata.Size = int64(len(imageConfig))
} else {
imageMetadata.Size = 0
}
for _, layer := range imageLayers {
if layerSet.Has(layer.Name) {
continue
}
layerSet.Insert(layer.Name)
imageMetadata.Size += layer.LayerSize
}
return imageManifestMediaType, imageMetadata, imageLayers, orderAscending, nil
}
// ManifestMatchesImage returns true if the provided manifest matches the name of the image.
func ManifestMatchesImage(image *imageapi.Image, newManifest []byte) (bool, error) {
dgst, err := godigest.Parse(image.Name)
if err != nil {
return false, err
}
v := dgst.Verifier()
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 := m.Config.Digest.Verifier()
if _, err := v.Write(imageConfig); err != nil {
return false, err
}
return v.Verified(), nil
}