Skip to content

Commit 369da26

Browse files
author
Tibor Vass
committed
builder: deprecate prune filter unused-for in favor of until
This is to keep the UX consistent. `unused-for` is still accepted and a synonym. Signed-off-by: Tibor Vass <tibor@docker.com>
1 parent 68cbc37 commit 369da26

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

‎api/swagger.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6436,7 +6436,7 @@ paths:
64366436
type: "string"
64376437
description: |
64386438
A JSON encoded value of the filters (a `map[string][]string`) to process on the list of build cache objects. Available filters:
6439-
- `unused-for=<duration>`: duration relative to daemon's time, during which build cache was not used, in Go's duration format (e.g., '24h')
6439+
- `until=<duration>`: duration relative to daemon's time, during which build cache was not used, in Go's duration format (e.g., '24h')
64406440
- `id=<id>`
64416441
- `parent=<id>`
64426442
- `type=<string>`

‎builder/builder-next/builder.go‎

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package buildkit
22

33
import (
44
"context"
5+
"fmt"
56
"io"
67
"net"
78
"strings"
@@ -32,7 +33,21 @@ import (
3233
grpcmetadata "google.golang.org/grpc/metadata"
3334
)
3435

35-
var errMultipleFilterValues = errors.New("filters expect only one value")
36+
type errMultipleFilterValues struct{}
37+
38+
func (errMultipleFilterValues) Error() string { return "filters expect only one value" }
39+
40+
func (errMultipleFilterValues) InvalidParameter() {}
41+
42+
type errConflictFilter struct {
43+
a, b string
44+
}
45+
46+
func (e errConflictFilter) Error() string {
47+
return fmt.Sprintf("conflicting filters: %q and %q", e.a, e.b)
48+
}
49+
50+
func (errConflictFilter) InvalidParameter() {}
3651

3752
var cacheFields = map[string]bool{
3853
"id": true,
@@ -130,6 +145,7 @@ func (b *Builder) Prune(ctx context.Context, opts types.BuildCachePruneOptions)
130145

131146
validFilters := make(map[string]bool, 1+len(cacheFields))
132147
validFilters["unused-for"] = true
148+
validFilters["until"] = true
133149
for k, v := range cacheFields {
134150
validFilters[k] = v
135151
}
@@ -502,6 +518,7 @@ func toBuildkitExtraHosts(inp []string) (string, error) {
502518
hosts := make([]string, 0, len(inp))
503519
for _, h := range inp {
504520
parts := strings.Split(h, ":")
521+
505522
if len(parts) != 2 || parts[0] == "" || net.ParseIP(parts[1]) == nil {
506523
return "", errors.Errorf("invalid host %s", h)
507524
}
@@ -511,21 +528,30 @@ func toBuildkitExtraHosts(inp []string) (string, error) {
511528
}
512529

513530
func toBuildkitPruneInfo(opts types.BuildCachePruneOptions) (client.PruneInfo, error) {
514-
var unusedFor time.Duration
515-
unusedForValues := opts.Filters.Get("unused-for")
531+
var until time.Duration
532+
untilValues := opts.Filters.Get("until") // canonical
533+
unusedForValues := opts.Filters.Get("unused-for") // deprecated synonym for "until" filter
516534

517-
switch len(unusedForValues) {
518-
case 0:
535+
if len(untilValues) > 0 && len(unusedForValues) > 0 {
536+
return client.PruneInfo{}, errConflictFilter{"until", "unused-for"}
537+
}
538+
filterKey := "until"
539+
if len(unusedForValues) > 0 {
540+
filterKey = "unused-for"
541+
}
542+
untilValues = append(untilValues, unusedForValues...)
519543

544+
switch len(untilValues) {
545+
case 0:
546+
// nothing to do
520547
case 1:
521548
var err error
522-
unusedFor, err = time.ParseDuration(unusedForValues[0])
549+
until, err = time.ParseDuration(untilValues[0])
523550
if err != nil {
524-
return client.PruneInfo{}, errors.Wrap(err, "unused-for filter expects a duration (e.g., '24h')")
551+
return client.PruneInfo{}, errors.Wrapf(err, "%q filter expects a duration (e.g., '24h')", filterKey)
525552
}
526-
527553
default:
528-
return client.PruneInfo{}, errMultipleFilterValues
554+
return client.PruneInfo{}, errMultipleFilterValues{}
529555
}
530556

531557
bkFilter := make([]string, 0, opts.Filters.Len())
@@ -542,13 +568,13 @@ func toBuildkitPruneInfo(opts types.BuildCachePruneOptions) (client.PruneInfo, e
542568
bkFilter = append(bkFilter, cacheField+"=="+values[0])
543569
}
544570
default:
545-
return client.PruneInfo{}, errMultipleFilterValues
571+
return client.PruneInfo{}, errMultipleFilterValues{}
546572
}
547573
}
548574
}
549575
return client.PruneInfo{
550576
All: opts.All,
551-
KeepDuration: unusedFor,
577+
KeepDuration: until,
552578
KeepBytes: opts.KeepStorage,
553579
Filter: []string{strings.Join(bkFilter, ",")},
554580
}, nil

0 commit comments

Comments
 (0)