1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

add support for since, before, labels images filters

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Nicolas De Loof 2022-07-06 17:46:39 +02:00 committed by Sebastiaan van Stijn
parent 7624f8aeb1
commit 4d3b32bf3d
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -5,12 +5,21 @@ import (
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/snapshots" "github.com/containerd/containerd/snapshots"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/opencontainers/image-spec/identity" "github.com/opencontainers/image-spec/identity"
) )
// Images returns a filtered list of images. // 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) { func (i *ImageService) Images(ctx context.Context, opts types.ImageListOptions) ([]*types.ImageSummary, error) {
filter, err := i.setupFilters(ctx, opts.Filters)
if err != nil {
return nil, err
}
imgs, err := i.client.ListImages(ctx) imgs, err := i.client.ListImages(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
@ -20,6 +29,10 @@ func (i *ImageService) Images(ctx context.Context, opts types.ImageListOptions)
var ret []*types.ImageSummary var ret []*types.ImageSummary
for _, img := range imgs { for _, img := range imgs {
if !filter(img) {
continue
}
size, err := img.Size(ctx) size, err := img.Size(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
@ -46,6 +59,66 @@ func (i *ImageService) Images(ctx context.Context, opts types.ImageListOptions)
return ret, nil 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) { func computeVirtualSize(ctx context.Context, image containerd.Image, snapshotter snapshots.Snapshotter) (int64, error) {
var virtualSize int64 var virtualSize int64
diffIDs, err := image.RootFS(ctx) diffIDs, err := image.RootFS(ctx)