From a146adf2e7c1f3efcf673c8aeca415ebafb7e26e Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Wed, 14 Jun 2023 10:40:12 +0200 Subject: [PATCH] libimage: fix reference filters Make sure that reference filters properly work on digests as well. To keep things simple, try to lookup an image for the user-specified value and compare IDs. This will implicitly fix #containers/podman/issues/18445 and probably more (unknown) issues. Fixes: #containers/podman/issues/18445 Signed-off-by: Valentin Rothberg --- libimage/filters.go | 12 ++++++++++-- libimage/filters_test.go | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/libimage/filters.go b/libimage/filters.go index 441011edd..ff50321b7 100644 --- a/libimage/filters.go +++ b/libimage/filters.go @@ -178,7 +178,7 @@ func (r *Runtime) compileImageFilters(ctx context.Context, options *ListImagesOp filter = filterManifest(ctx, manifest) case "reference": - filter = filterReferences(value) + filter = filterReferences(r, value) case "until": until, err := r.until(value) @@ -268,8 +268,15 @@ func filterManifest(ctx context.Context, value bool) filterFunc { } // filterReferences creates a reference filter for matching the specified value. -func filterReferences(value string) filterFunc { +func filterReferences(r *Runtime, value string) filterFunc { + lookedUp, _, _ := r.LookupImage(value, nil) return func(img *Image) (bool, error) { + if lookedUp != nil { + if lookedUp.ID() == img.ID() { + return true, nil + } + } + refs, err := img.NamesReferences() if err != nil { return false, err @@ -306,6 +313,7 @@ func filterReferences(value string) filterFunc { } } } + return false, nil } } diff --git a/libimage/filters_test.go b/libimage/filters_test.go index e912056fa..12091b130 100644 --- a/libimage/filters_test.go +++ b/libimage/filters_test.go @@ -62,6 +62,7 @@ func TestFilterReference(t *testing.T) { {"quay.io/libpod/*", 2}, {"busybox", 1}, {"alpine", 1}, + {"alpine@" + alpine.Digest().String(), 1}, } { listOptions := &ListImagesOptions{ Filters: []string{"reference=" + test.filter},