From febcc25d1aff14ee87f6b7c722831c321b8aee09 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Tue, 2 Aug 2022 12:11:41 +0100 Subject: [PATCH 1/2] prune: fix filter until option Previously, when specifying the filter option with the until value, no cache would be cleaned, preventing users from clearing by time. This bug arises from passing the until field through into buildkit, where, on filtering, a non-existent field returns false for a match. The fix is simple, as we build up our list of filters to pass to buildkit, we skip over the until key, so create a valid list of filters for buildkit. Signed-off-by: Justin Chadwell --- commands/prune.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/commands/prune.go b/commands/prune.go index 237ce9ed..4040b8b3 100644 --- a/commands/prune.go +++ b/commands/prune.go @@ -176,6 +176,10 @@ func toBuildkitPruneInfo(f filters.Args) (*client.PruneInfo, error) { bkFilter := make([]string, 0, f.Len()) for _, field := range f.Keys() { + if field == filterKey { + continue + } + values := f.Get(field) switch len(values) { case 0: From 406799eb1c32242fd8283b7b6684392d47e4d4ba Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Fri, 5 Aug 2022 17:20:52 +0100 Subject: [PATCH 2/2] prune: cleanup variable names for clarity Signed-off-by: Justin Chadwell --- commands/prune.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/commands/prune.go b/commands/prune.go index 4040b8b3..9011d66d 100644 --- a/commands/prune.go +++ b/commands/prune.go @@ -155,9 +155,9 @@ func toBuildkitPruneInfo(f filters.Args) (*client.PruneInfo, error) { if len(untilValues) > 0 && len(unusedForValues) > 0 { return nil, errors.Errorf("conflicting filters %q and %q", "until", "unused-for") } - filterKey := "until" + untilKey := "until" if len(unusedForValues) > 0 { - filterKey = "unused-for" + untilKey = "unused-for" } untilValues = append(untilValues, unusedForValues...) @@ -168,27 +168,27 @@ func toBuildkitPruneInfo(f filters.Args) (*client.PruneInfo, error) { var err error until, err = time.ParseDuration(untilValues[0]) if err != nil { - return nil, errors.Wrapf(err, "%q filter expects a duration (e.g., '24h')", filterKey) + return nil, errors.Wrapf(err, "%q filter expects a duration (e.g., '24h')", untilKey) } default: return nil, errors.Errorf("filters expect only one value") } - bkFilter := make([]string, 0, f.Len()) - for _, field := range f.Keys() { - if field == filterKey { + filters := make([]string, 0, f.Len()) + for _, filterKey := range f.Keys() { + if filterKey == untilKey { continue } - values := f.Get(field) + values := f.Get(filterKey) switch len(values) { case 0: - bkFilter = append(bkFilter, field) + filters = append(filters, filterKey) case 1: - if field == "id" { - bkFilter = append(bkFilter, field+"~="+values[0]) + if filterKey == "id" { + filters = append(filters, filterKey+"~="+values[0]) } else { - bkFilter = append(bkFilter, field+"=="+values[0]) + filters = append(filters, filterKey+"=="+values[0]) } default: return nil, errors.Errorf("filters expect only one value") @@ -196,6 +196,6 @@ func toBuildkitPruneInfo(f filters.Args) (*client.PruneInfo, error) { } return &client.PruneInfo{ KeepDuration: until, - Filter: []string{strings.Join(bkFilter, ",")}, + Filter: []string{strings.Join(filters, ",")}, }, nil }