mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
669fbed1ac
Not all filters are implemented yet, so make sure an error is returned if a not-yet implemented filter is used. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
148 lines
4 KiB
Go
148 lines
4 KiB
Go
package containerd
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/containerd/containerd"
|
|
"github.com/containerd/containerd/snapshots"
|
|
"github.com/docker/distribution/reference"
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"github.com/opencontainers/image-spec/identity"
|
|
)
|
|
|
|
var acceptedImageFilterTags = map[string]bool{
|
|
"dangling": false, // TODO(thaJeztah): implement "dangling" filter: see https://github.com/moby/moby/issues/43846
|
|
"label": true,
|
|
"before": true,
|
|
"since": true,
|
|
"reference": false, // TODO(thaJeztah): implement "reference" filter: see https://github.com/moby/moby/issues/43847
|
|
}
|
|
|
|
// Images returns a filtered list of images.
|
|
//
|
|
// TODO(thaJeztah): sort the results by created (descending); see https://github.com/moby/moby/issues/43848
|
|
func (i *ImageService) Images(ctx context.Context, opts types.ImageListOptions) ([]*types.ImageSummary, error) {
|
|
if err := opts.Filters.Validate(acceptedImageFilterTags); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
filter, err := i.setupFilters(ctx, opts.Filters)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
imgs, err := i.client.ListImages(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
snapshotter := i.client.SnapshotService(containerd.DefaultSnapshotter)
|
|
|
|
var ret []*types.ImageSummary
|
|
for _, img := range imgs {
|
|
if !filter(img) {
|
|
continue
|
|
}
|
|
|
|
size, err := img.Size(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
virtualSize, err := computeVirtualSize(ctx, img, snapshotter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ret = append(ret, &types.ImageSummary{
|
|
RepoDigests: []string{img.Name() + "@" + img.Target().Digest.String()}, // "hello-world@sha256:bfea6278a0a267fad2634554f4f0c6f31981eea41c553fdf5a83e95a41d40c38"},
|
|
RepoTags: []string{img.Name()},
|
|
Containers: -1,
|
|
ParentID: "",
|
|
SharedSize: -1,
|
|
VirtualSize: virtualSize,
|
|
ID: img.Target().Digest.String(),
|
|
Created: img.Metadata().CreatedAt.Unix(),
|
|
Size: size,
|
|
})
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
|
|
type imageFilterFunc func(image containerd.Image) bool
|
|
|
|
// setupFilters constructs an imageFilterFunc from the given imageFilters.
|
|
//
|
|
// TODO(thaJeztah): reimplement filters using containerd filters: see https://github.com/moby/moby/issues/43845
|
|
func (i *ImageService) setupFilters(ctx context.Context, imageFilters filters.Args) (imageFilterFunc, error) {
|
|
var fltrs []imageFilterFunc
|
|
err := imageFilters.WalkValues("before", func(value string) error {
|
|
ref, err := reference.ParseDockerRef(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
img, err := i.client.GetImage(ctx, ref.String())
|
|
if img != nil {
|
|
t := img.Metadata().CreatedAt
|
|
fltrs = append(fltrs, func(image containerd.Image) bool {
|
|
created := image.Metadata().CreatedAt
|
|
return created.Equal(t) || created.After(t)
|
|
})
|
|
}
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = imageFilters.WalkValues("since", func(value string) error {
|
|
ref, err := reference.ParseDockerRef(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
img, err := i.client.GetImage(ctx, ref.String())
|
|
if img != nil {
|
|
t := img.Metadata().CreatedAt
|
|
fltrs = append(fltrs, func(image containerd.Image) bool {
|
|
created := image.Metadata().CreatedAt
|
|
return created.Equal(t) || created.Before(t)
|
|
})
|
|
}
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if imageFilters.Contains("label") {
|
|
fltrs = append(fltrs, func(image containerd.Image) bool {
|
|
return imageFilters.MatchKVList("label", image.Labels())
|
|
})
|
|
}
|
|
return func(image containerd.Image) bool {
|
|
for _, filter := range fltrs {
|
|
if !filter(image) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}, nil
|
|
}
|
|
|
|
func computeVirtualSize(ctx context.Context, image containerd.Image, snapshotter snapshots.Snapshotter) (int64, error) {
|
|
var virtualSize int64
|
|
diffIDs, err := image.RootFS(ctx)
|
|
if err != nil {
|
|
return virtualSize, err
|
|
}
|
|
for _, chainID := range identity.ChainIDs(diffIDs) {
|
|
usage, err := snapshotter.Usage(ctx, chainID.String())
|
|
if err != nil {
|
|
return virtualSize, err
|
|
}
|
|
virtualSize += usage.Size
|
|
}
|
|
return virtualSize, nil
|
|
}
|