-
Notifications
You must be signed in to change notification settings - Fork 8
/
images.go
366 lines (322 loc) · 10.2 KB
/
images.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package images // import "github.com/docker/docker/daemon/images"
import (
"encoding/json"
"fmt"
"sort"
"time"
"github.com/pkg/errors"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/container"
"github.com/docker/docker/image"
"github.com/docker/docker/layer"
"github.com/docker/docker/pkg/system"
)
var acceptedImageFilterTags = map[string]bool{
"dangling": true,
"label": true,
"before": true,
"since": true,
"reference": true,
}
// byCreated is a temporary type used to sort a list of images by creation
// time.
type byCreated []*types.ImageSummary
func (r byCreated) Len() int { return len(r) }
func (r byCreated) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r byCreated) Less(i, j int) bool { return r[i].Created < r[j].Created }
// Map returns a map of all images in the ImageStore
func (i *ImageService) Map() map[image.ID]*image.Image {
return i.imageStore.Map()
}
// Images returns a filtered list of images. filterArgs is a JSON-encoded set
// of filter arguments which will be interpreted by api/types/filters.
// filter is a shell glob string applied to repository names. The argument
// named all controls whether all images in the graph are filtered, or just
// the heads.
func (i *ImageService) Images(imageFilters filters.Args, all bool, withExtraAttrs bool) ([]*types.ImageSummary, error) {
if err := imageFilters.Validate(acceptedImageFilterTags); err != nil {
return nil, err
}
var danglingOnly bool
if imageFilters.Contains("dangling") {
if imageFilters.ExactMatch("dangling", "true") {
danglingOnly = true
} else if !imageFilters.ExactMatch("dangling", "false") {
return nil, invalidFilter{"dangling", imageFilters.Get("dangling")}
}
}
var (
beforeFilter, sinceFilter *image.Image
err error
)
err = imageFilters.WalkValues("before", func(value string) error {
beforeFilter, err = i.GetImage(value, nil)
return err
})
if err != nil {
return nil, err
}
err = imageFilters.WalkValues("since", func(value string) error {
sinceFilter, err = i.GetImage(value, nil)
return err
})
if err != nil {
return nil, err
}
var selectedImages map[image.ID]*image.Image
if danglingOnly {
selectedImages = i.imageStore.Heads()
} else {
selectedImages = i.imageStore.Map()
}
var (
summaries = make([]*types.ImageSummary, 0, len(selectedImages))
summaryMap map[*image.Image]*types.ImageSummary
allContainers []*container.Container
)
for id, img := range selectedImages {
if beforeFilter != nil {
if img.Created.Equal(beforeFilter.Created) || img.Created.After(beforeFilter.Created) {
continue
}
}
if sinceFilter != nil {
if img.Created.Equal(sinceFilter.Created) || img.Created.Before(sinceFilter.Created) {
continue
}
}
if imageFilters.Contains("label") {
// Very old image that do not have image.Config (or even labels)
if img.Config == nil {
continue
}
// We are now sure image.Config is not nil
if !imageFilters.MatchKVList("label", img.Config.Labels) {
continue
}
}
// Skip any images with an unsupported operating system to avoid a potential
// panic when indexing through the layerstore. Don't error as we want to list
// the other images. This should never happen, but here as a safety precaution.
if !system.IsOSSupported(img.OperatingSystem()) {
continue
}
var size int64
if layerID := img.RootFS.ChainID(); layerID != "" {
l, err := i.layerStore.Get(layerID)
if err != nil {
// The layer may have been deleted between the call to `Map()` or
// `Heads()` and the call to `Get()`, so we just ignore this error
if err == layer.ErrLayerDoesNotExist {
continue
}
return nil, err
}
size, err = l.Size()
layer.ReleaseAndLog(i.layerStore, l)
if err != nil {
return nil, err
}
}
summary := newImageSummary(img, size)
for _, ref := range i.referenceStore.References(id.Digest()) {
if imageFilters.Contains("reference") {
var found bool
var matchErr error
for _, pattern := range imageFilters.Get("reference") {
found, matchErr = reference.FamiliarMatch(pattern, ref)
if matchErr != nil {
return nil, matchErr
}
if found {
break
}
}
if !found {
continue
}
}
if _, ok := ref.(reference.Canonical); ok {
summary.RepoDigests = append(summary.RepoDigests, reference.FamiliarString(ref))
}
if _, ok := ref.(reference.NamedTagged); ok {
summary.RepoTags = append(summary.RepoTags, reference.FamiliarString(ref))
}
}
if summary.RepoDigests == nil && summary.RepoTags == nil {
if all || len(i.imageStore.Children(id)) == 0 {
if imageFilters.Contains("dangling") && !danglingOnly {
// dangling=false case, so dangling image is not needed
continue
}
if imageFilters.Contains("reference") { // skip images with no references if filtering by reference
continue
}
summary.RepoDigests = []string{"<none>@<none>"}
summary.RepoTags = []string{"<none>:<none>"}
} else {
continue
}
} else if danglingOnly && len(summary.RepoTags) > 0 {
continue
}
if withExtraAttrs {
// Lazily init summaryMap and allContainers
if summaryMap == nil {
summaryMap = make(map[*image.Image]*types.ImageSummary, len(selectedImages))
allContainers = i.containers.List()
}
// Get container count
var containers int64
for _, c := range allContainers {
if c.ImageID == id {
containers++
}
}
// NOTE: By default, Containers is -1, or "not set"
summary.Containers = containers
summaryMap[img] = summary
}
summaries = append(summaries, summary)
}
if withExtraAttrs {
allLayers := i.layerStore.Map()
layerRefs := make(map[layer.ChainID]int, len(allLayers))
allImages := selectedImages
if danglingOnly {
// If danglingOnly is true, then selectedImages include only dangling images,
// but we need to consider all existing images to correctly perform reference counting.
// If danglingOnly is false, selectedImages (and, hence, allImages) is already equal to i.imageStore.Map()
// and we can avoid performing an otherwise redundant method call.
allImages = i.imageStore.Map()
}
// Count layer references across all known images
for _, img := range allImages {
rootFS := *img.RootFS
rootFS.DiffIDs = nil
for _, id := range img.RootFS.DiffIDs {
rootFS.Append(id)
layerRefs[rootFS.ChainID()]++
}
}
// Get Shared sizes
for img, summary := range summaryMap {
rootFS := *img.RootFS
rootFS.DiffIDs = nil
// Indicate that we collected shared size information (default is -1, or "not set")
summary.SharedSize = 0
for _, id := range img.RootFS.DiffIDs {
rootFS.Append(id)
chid := rootFS.ChainID()
if layerRefs[chid] > 1 {
if _, ok := allLayers[chid]; !ok {
return nil, fmt.Errorf("layer %v was not found (corruption?)", chid)
}
diffSize, err := allLayers[chid].DiffSize()
if err != nil {
return nil, err
}
summary.SharedSize += diffSize
}
}
}
}
sort.Sort(sort.Reverse(byCreated(summaries)))
return summaries, nil
}
// SquashImage creates a new image with the diff of the specified image and the specified parent.
// This new image contains only the layers from it's parent + 1 extra layer which contains the diff of all the layers in between.
// The existing image(s) is not destroyed.
// If no parent is specified, a new image with the diff of all the specified image's layers merged into a new layer that has no parents.
func (i *ImageService) SquashImage(id, parent string) (string, error) {
var (
img *image.Image
err error
)
if img, err = i.imageStore.Get(image.ID(id)); err != nil {
return "", err
}
var parentImg *image.Image
var parentChainID layer.ChainID
if len(parent) != 0 {
parentImg, err = i.imageStore.Get(image.ID(parent))
if err != nil {
return "", errors.Wrap(err, "error getting specified parent layer")
}
parentChainID = parentImg.RootFS.ChainID()
} else {
rootFS := image.NewRootFS()
parentImg = &image.Image{RootFS: rootFS}
}
if !system.IsOSSupported(img.OperatingSystem()) {
return "", errors.Wrap(err, system.ErrNotSupportedOperatingSystem.Error())
}
l, err := i.layerStore.Get(img.RootFS.ChainID())
if err != nil {
return "", errors.Wrap(err, "error getting image layer")
}
defer i.layerStore.Release(l)
ts, err := l.TarStreamFrom(parentChainID)
if err != nil {
return "", errors.Wrapf(err, "error getting tar stream to parent")
}
defer ts.Close()
newL, err := i.layerStore.Register(ts, parentChainID)
if err != nil {
return "", errors.Wrap(err, "error registering layer")
}
defer i.layerStore.Release(newL)
newImage := *img
newImage.RootFS = nil
rootFS := *parentImg.RootFS
rootFS.DiffIDs = append(rootFS.DiffIDs, newL.DiffID())
newImage.RootFS = &rootFS
for i, hi := range newImage.History {
if i >= len(parentImg.History) {
hi.EmptyLayer = true
}
newImage.History[i] = hi
}
now := time.Now()
var historyComment string
if len(parent) > 0 {
historyComment = fmt.Sprintf("merge %s to %s", id, parent)
} else {
historyComment = fmt.Sprintf("create new from %s", id)
}
newImage.History = append(newImage.History, image.History{
Created: now,
Comment: historyComment,
})
newImage.Created = now
b, err := json.Marshal(&newImage)
if err != nil {
return "", errors.Wrap(err, "error marshalling image config")
}
newImgID, err := i.imageStore.Create(b)
if err != nil {
return "", errors.Wrap(err, "error creating new image after squash")
}
return string(newImgID), nil
}
func newImageSummary(image *image.Image, size int64) *types.ImageSummary {
summary := &types.ImageSummary{
ParentID: image.Parent.String(),
ID: image.ID().String(),
Created: image.Created.Unix(),
Size: size,
VirtualSize: size,
// -1 indicates that the value has not been set (avoids ambiguity
// between 0 (default) and "not set". We cannot use a pointer (nil)
// for this, as the JSON representation uses "omitempty", which would
// consider both "0" and "nil" to be "empty".
SharedSize: -1,
Containers: -1,
}
if image.Config != nil {
summary.Labels = image.Config.Labels
}
return summary
}