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
|
package images // import "github.com/docker/docker/daemon/images"
import (
"context"
"errors"
"fmt"
"sort"
"time"
"github.com/distribution/reference"
"github.com/docker/docker/api/types/backend"
imagetypes "github.com/docker/docker/api/types/image"
timetypes "github.com/docker/docker/api/types/time"
"github.com/docker/docker/container"
"github.com/docker/docker/image"
"github.com/docker/docker/layer"
)
var acceptedImageFilterTags = map[string]bool{
"dangling": true,
"label": true,
"before": true,
"since": true,
"reference": true,
"until": true,
}
// byCreated is a temporary type used to sort a list of images by creation
// time.
type byCreated []*imagetypes.Summary
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 }
// Images returns a filtered list of images.
func (i *ImageService) Images(ctx context.Context, opts imagetypes.ListOptions) ([]*imagetypes.Summary, error) {
if err := opts.Filters.Validate(acceptedImageFilterTags); err != nil {
return nil, err
}
danglingOnly, err := opts.Filters.GetBoolOrDefault("dangling", false)
if err != nil {
return nil, err
}
var beforeFilter, sinceFilter time.Time
err = opts.Filters.WalkValues("before", func(value string) error {
img, err := i.GetImage(ctx, value, backend.GetImageOpts{})
if err != nil {
return err
}
// Resolve multiple values to the oldest image,
// equivalent to ANDing all the values together.
if img.Created != nil && (beforeFilter.IsZero() || beforeFilter.After(*img.Created)) {
beforeFilter = *img.Created
}
return nil
})
if err != nil {
return nil, err
}
err = opts.Filters.WalkValues("until", func(value string) error {
ts, err := timetypes.GetTimestamp(value, time.Now())
if err != nil {
return err
}
seconds, nanoseconds, err := timetypes.ParseTimestamps(ts, 0)
if err != nil {
return err
}
timestamp := time.Unix(seconds, nanoseconds)
if beforeFilter.IsZero() || beforeFilter.After(timestamp) {
beforeFilter = timestamp
}
return nil
})
if err != nil {
return nil, err
}
err = opts.Filters.WalkValues("since", func(value string) error {
img, err := i.GetImage(ctx, value, backend.GetImageOpts{})
if err != nil {
return err
}
// Resolve multiple values to the newest image,
// equivalent to ANDing all the values together.
if img.Created != nil && sinceFilter.Before(*img.Created) {
sinceFilter = *img.Created
}
return nil
})
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([]*imagetypes.Summary, 0, len(selectedImages))
summaryMap map[*image.Image]*imagetypes.Summary
allContainers []*container.Container
)
for id, img := range selectedImages {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
if !beforeFilter.IsZero() && (img.Created == nil || !img.Created.Before(beforeFilter)) {
continue
}
if !sinceFilter.IsZero() && (img.Created == nil || !img.Created.After(sinceFilter)) {
continue
}
if opts.Filters.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 !opts.Filters.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 err := image.CheckOS(img.OperatingSystem()); err != nil {
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 errors.Is(err, layer.ErrLayerDoesNotExist) {
continue
}
return nil, err
}
size = l.Size()
layer.ReleaseAndLog(i.layerStore, l)
}
summary := newImageSummary(img, size)
for _, ref := range i.referenceStore.References(id.Digest()) {
if opts.Filters.Contains("reference") {
var found bool
var matchErr error
for _, pattern := range opts.Filters.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 opts.All || len(i.imageStore.Children(id)) == 0 {
if opts.Filters.Contains("dangling") && !danglingOnly {
// dangling=false case, so dangling image is not needed
continue
}
if opts.Filters.Contains("reference") { // skip images with no references if filtering by reference
continue
}
} else {
continue
}
} else if danglingOnly && len(summary.RepoTags) > 0 {
continue
}
if opts.ContainerCount {
// Lazily init allContainers.
if allContainers == nil {
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
}
if opts.ContainerCount || opts.SharedSize {
// Lazily init summaryMap.
if summaryMap == nil {
summaryMap = make(map[*image.Image]*imagetypes.Summary, len(selectedImages))
}
summaryMap[img] = summary
}
summaries = append(summaries, summary)
}
if opts.SharedSize {
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)
}
summary.SharedSize += allLayers[chid].DiffSize()
}
}
}
}
sort.Sort(sort.Reverse(byCreated(summaries)))
return summaries, nil
}
func newImageSummary(image *image.Image, size int64) *imagetypes.Summary {
var created int64
if image.Created != nil {
created = image.Created.Unix()
}
summary := &imagetypes.Summary{
ParentID: image.Parent.String(),
ID: image.ID().String(),
Created: created,
Size: 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
}
|