Browse code

Merge pull request #50772 from austinvazquez/move-build-cache-prune-options-from-api-to-client

api/types/build: move build cache prune options from api to client

Sebastiaan van Stijn authored on 2025/09/05 17:33:23
Showing 19 changed files
... ...
@@ -97,6 +97,8 @@ keywords: "API, Docker, rcli, REST, documentation"
97 97
   `GET /info` response are now always be `false` and will be omitted in API
98 98
   v1.49. The netfilter module is now loaded on-demand, and no longer during
99 99
   daemon startup, making these fields obsolete.
100
+* Deprecated: The `POST /build/prune` `keep-storage` query parameter has been
101
+  renamed to `reserved-space`. `keep-storage` support will be removed in API v1.52.
100 102
 * `GET /images/{name}/history` now supports a `platform` parameter (JSON
101 103
   encoded OCI Platform type) that allows to specify a platform to show the
102 104
   history of.
... ...
@@ -151,7 +153,7 @@ keywords: "API, Docker, rcli, REST, documentation"
151 151
   `GET /debug/pprof/profile`, `GET /debug/pprof/symbol`, `GET /debug/pprof/trace`,
152 152
   `GET /debug/pprof/{name}`) are now also accessible through the versioned-API
153 153
   paths (`/v<API-version>/<endpoint>`).
154
-* `POST /build/prune` renames `keep-bytes` to `reserved-space` and now supports
154
+* `POST /build/prune` renames `keep-storage` to `reserved-space` and now supports
155 155
   additional prune parameters `max-used-space` and `min-free-space`.
156 156
 * `GET /containers/json` now returns an `ImageManifestDescriptor` field
157 157
   matching the same field in `/containers/{name}/json`.
... ...
@@ -9509,7 +9509,7 @@ paths:
9509 9509
             Amount of disk space in bytes to keep for cache
9510 9510
 
9511 9511
             > **Deprecated**: This parameter is deprecated and has been renamed to "reserved-space".
9512
-            > It is kept for backward compatibility and will be removed in API v1.49.
9512
+            > It is kept for backward compatibility and will be removed in API v1.52.
9513 9513
           type: "integer"
9514 9514
           format: "int64"
9515 9515
         - name: "reserved-space"
... ...
@@ -9509,7 +9509,7 @@ paths:
9509 9509
             Amount of disk space in bytes to keep for cache
9510 9510
 
9511 9511
             > **Deprecated**: This parameter is deprecated and has been renamed to "reserved-space".
9512
-            > It is kept for backward compatibility and will be removed in API v1.49.
9512
+            > It is kept for backward compatibility and will be removed in API v1.52.
9513 9513
           type: "integer"
9514 9514
           format: "int64"
9515 9515
         - name: "reserved-space"
... ...
@@ -9393,7 +9393,7 @@ paths:
9393 9393
             Amount of disk space in bytes to keep for cache
9394 9394
 
9395 9395
             > **Deprecated**: This parameter is deprecated and has been renamed to "reserved-space".
9396
-            > It is kept for backward compatibility and will be removed in API v1.49.
9396
+            > It is kept for backward compatibility and will be removed in API v1.52.
9397 9397
           type: "integer"
9398 9398
           format: "int64"
9399 9399
         - name: "reserved-space"
... ...
@@ -9394,7 +9394,7 @@ paths:
9394 9394
             Amount of disk space in bytes to keep for cache
9395 9395
 
9396 9396
             > **Deprecated**: This parameter is deprecated and has been renamed to "reserved-space".
9397
-            > It is kept for backward compatibility and will be removed in API v1.49.
9397
+            > It is kept for backward compatibility and will be removed in API v1.52.
9398 9398
           type: "integer"
9399 9399
           format: "int64"
9400 9400
         - name: "reserved-space"
... ...
@@ -9376,15 +9376,6 @@ paths:
9376 9376
         - "application/json"
9377 9377
       operationId: "BuildPrune"
9378 9378
       parameters:
9379
-        - name: "keep-storage"
9380
-          in: "query"
9381
-          description: |
9382
-            Amount of disk space in bytes to keep for cache
9383
-
9384
-            > **Deprecated**: This parameter is deprecated and has been renamed to "reserved-space".
9385
-            > It is kept for backward compatibility and will be removed in API v1.49.
9386
-          type: "integer"
9387
-          format: "int64"
9388 9379
         - name: "reserved-space"
9389 9380
           in: "query"
9390 9381
           description: "Amount of disk space in bytes to keep for cache"
... ...
@@ -9376,15 +9376,6 @@ paths:
9376 9376
         - "application/json"
9377 9377
       operationId: "BuildPrune"
9378 9378
       parameters:
9379
-        - name: "keep-storage"
9380
-          in: "query"
9381
-          description: |
9382
-            Amount of disk space in bytes to keep for cache
9383
-
9384
-            > **Deprecated**: This parameter is deprecated and has been renamed to "reserved-space".
9385
-            > It is kept for backward compatibility and will be removed in API v1.49.
9386
-          type: "integer"
9387
-          format: "int64"
9388 9379
         - name: "reserved-space"
9389 9380
           in: "query"
9390 9381
           description: "Amount of disk space in bytes to keep for cache"
... ...
@@ -2,8 +2,6 @@ package build
2 2
 
3 3
 import (
4 4
 	"time"
5
-
6
-	"github.com/moby/moby/api/types/filters"
7 5
 )
8 6
 
9 7
 // CacheRecord contains information about a build cache record.
... ...
@@ -33,17 +31,6 @@ type CacheRecord struct {
33 33
 	UsageCount int
34 34
 }
35 35
 
36
-// CachePruneOptions hold parameters to prune the build cache.
37
-type CachePruneOptions struct {
38
-	All           bool
39
-	ReservedSpace int64
40
-	MaxUsedSpace  int64
41
-	MinFreeSpace  int64
42
-	Filters       filters.Args
43
-
44
-	KeepStorage int64 // Deprecated: deprecated in API 1.48.
45
-}
46
-
47 36
 // CachePruneReport contains the response for Engine API:
48 37
 // POST "/build/prune"
49 38
 type CachePruneReport struct {
... ...
@@ -9,10 +9,20 @@ import (
9 9
 
10 10
 	"github.com/moby/moby/api/types/build"
11 11
 	"github.com/moby/moby/api/types/filters"
12
+	"github.com/moby/moby/api/types/versions"
12 13
 )
13 14
 
15
+// BuildCachePruneOptions hold parameters to prune the build cache.
16
+type BuildCachePruneOptions struct {
17
+	All           bool
18
+	ReservedSpace int64
19
+	MaxUsedSpace  int64
20
+	MinFreeSpace  int64
21
+	Filters       filters.Args
22
+}
23
+
14 24
 // BuildCachePrune requests the daemon to delete unused cache data.
15
-func (cli *Client) BuildCachePrune(ctx context.Context, opts build.CachePruneOptions) (*build.CachePruneReport, error) {
25
+func (cli *Client) BuildCachePrune(ctx context.Context, opts BuildCachePruneOptions) (*build.CachePruneReport, error) {
16 26
 	if err := cli.NewVersionError(ctx, "1.31", "build prune"); err != nil {
17 27
 		return nil, err
18 28
 	}
... ...
@@ -22,11 +32,14 @@ func (cli *Client) BuildCachePrune(ctx context.Context, opts build.CachePruneOpt
22 22
 		query.Set("all", "1")
23 23
 	}
24 24
 
25
-	if opts.KeepStorage != 0 {
26
-		query.Set("keep-storage", strconv.Itoa(int(opts.KeepStorage)))
27
-	}
28 25
 	if opts.ReservedSpace != 0 {
29
-		query.Set("reserved-space", strconv.Itoa(int(opts.ReservedSpace)))
26
+		// Prior to API v1.48, 'keep-storage' was used to set the reserved space for the build cache.
27
+		// TODO(austinvazquez): remove once API v1.47 is no longer supported. See https://github.com/moby/moby/issues/50902
28
+		if versions.LessThanOrEqualTo(cli.version, "1.47") {
29
+			query.Set("keep-storage", strconv.Itoa(int(opts.ReservedSpace)))
30
+		} else {
31
+			query.Set("reserved-space", strconv.Itoa(int(opts.ReservedSpace)))
32
+		}
30 33
 	}
31 34
 	if opts.MaxUsedSpace != 0 {
32 35
 		query.Set("max-used-space", strconv.Itoa(int(opts.MaxUsedSpace)))
... ...
@@ -107,7 +107,7 @@ type DistributionAPIClient interface {
107 107
 // ImageAPIClient defines API client methods for the images
108 108
 type ImageAPIClient interface {
109 109
 	ImageBuild(ctx context.Context, context io.Reader, options build.ImageBuildOptions) (build.ImageBuildResponse, error)
110
-	BuildCachePrune(ctx context.Context, opts build.CachePruneOptions) (*build.CachePruneReport, error)
110
+	BuildCachePrune(ctx context.Context, opts BuildCachePruneOptions) (*build.CachePruneReport, error)
111 111
 	BuildCancel(ctx context.Context, id string) error
112 112
 	ImageCreate(ctx context.Context, parentReference string, options ImageCreateOptions) (io.ReadCloser, error)
113 113
 	ImageImport(ctx context.Context, source ImageImportSource, ref string, options ImageImportOptions) (io.ReadCloser, error)
... ...
@@ -14,6 +14,7 @@ import (
14 14
 	"github.com/moby/moby/v2/daemon/internal/image"
15 15
 	"github.com/moby/moby/v2/daemon/internal/stringid"
16 16
 	"github.com/moby/moby/v2/daemon/server/backend"
17
+	"github.com/moby/moby/v2/daemon/server/buildbackend"
17 18
 	"github.com/pkg/errors"
18 19
 	"google.golang.org/grpc"
19 20
 )
... ...
@@ -97,7 +98,7 @@ func (b *Backend) Build(ctx context.Context, config backend.BuildConfig) (string
97 97
 }
98 98
 
99 99
 // PruneCache removes all cached build sources
100
-func (b *Backend) PruneCache(ctx context.Context, opts build.CachePruneOptions) (*build.CachePruneReport, error) {
100
+func (b *Backend) PruneCache(ctx context.Context, opts buildbackend.CachePruneOptions) (*build.CachePruneReport, error) {
101 101
 	buildCacheSize, cacheIDs, err := b.buildkit.Prune(ctx, opts)
102 102
 	if err != nil {
103 103
 		return nil, errors.Wrap(err, "failed to prune build cache")
... ...
@@ -33,6 +33,7 @@ import (
33 33
 	"github.com/moby/moby/v2/daemon/libnetwork"
34 34
 	"github.com/moby/moby/v2/daemon/pkg/opts"
35 35
 	"github.com/moby/moby/v2/daemon/server/backend"
36
+	"github.com/moby/moby/v2/daemon/server/buildbackend"
36 37
 	"github.com/moby/moby/v2/errdefs"
37 38
 	"github.com/moby/sys/user"
38 39
 	"github.com/pkg/errors"
... ...
@@ -187,7 +188,7 @@ func (b *Builder) DiskUsage(ctx context.Context) ([]*build.CacheRecord, error) {
187 187
 }
188 188
 
189 189
 // Prune clears all reclaimable build cache.
190
-func (b *Builder) Prune(ctx context.Context, opts build.CachePruneOptions) (int64, []string, error) {
190
+func (b *Builder) Prune(ctx context.Context, opts buildbackend.CachePruneOptions) (int64, []string, error) {
191 191
 	ch := make(chan *controlapi.UsageRecord)
192 192
 
193 193
 	eg, ctx := errgroup.WithContext(ctx)
... ...
@@ -641,7 +642,7 @@ func toBuildkitUlimits(inp []*container.Ulimit) (string, error) {
641 641
 	return strings.Join(ulimits, ","), nil
642 642
 }
643 643
 
644
-func toBuildkitPruneInfo(opts build.CachePruneOptions) (client.PruneInfo, error) {
644
+func toBuildkitPruneInfo(opts buildbackend.CachePruneOptions) (client.PruneInfo, error) {
645 645
 	var until time.Duration
646 646
 	untilValues := opts.Filters.Get("until")          // canonical
647 647
 	unusedForValues := opts.Filters.Get("unused-for") // deprecated synonym for "until" filter
... ...
@@ -696,10 +697,6 @@ func toBuildkitPruneInfo(opts build.CachePruneOptions) (client.PruneInfo, error)
696 696
 		}
697 697
 	}
698 698
 
699
-	if opts.ReservedSpace == 0 && opts.KeepStorage != 0 {
700
-		opts.ReservedSpace = opts.KeepStorage
701
-	}
702
-
703 699
 	return client.PruneInfo{
704 700
 		All:           opts.All,
705 701
 		KeepDuration:  until,
... ...
@@ -45,7 +45,6 @@ import (
45 45
 	"github.com/moby/buildkit/worker"
46 46
 	"github.com/moby/buildkit/worker/containerd"
47 47
 	"github.com/moby/buildkit/worker/label"
48
-	"github.com/moby/moby/api/types/build"
49 48
 	"github.com/moby/moby/api/types/filters"
50 49
 	"github.com/moby/moby/v2/daemon/config"
51 50
 	"github.com/moby/moby/v2/daemon/graphdriver"
... ...
@@ -56,6 +55,7 @@ import (
56 56
 	"github.com/moby/moby/v2/daemon/internal/builder-next/imagerefchecker"
57 57
 	mobyworker "github.com/moby/moby/v2/daemon/internal/builder-next/worker"
58 58
 	wlabel "github.com/moby/moby/v2/daemon/internal/builder-next/worker/label"
59
+	"github.com/moby/moby/v2/daemon/server/buildbackend"
59 60
 	"github.com/pkg/errors"
60 61
 	bolt "go.etcd.io/bbolt"
61 62
 	"go.opentelemetry.io/otel/sdk/trace"
... ...
@@ -484,7 +484,7 @@ func getGCPolicy(conf config.BuilderConfig, root string) ([]client.PruneInfo, er
484 484
 					return nil, err
485 485
 				}
486 486
 
487
-				gcPolicy[i], err = toBuildkitPruneInfo(build.CachePruneOptions{
487
+				gcPolicy[i], err = toBuildkitPruneInfo(buildbackend.CachePruneOptions{
488 488
 					All:           p.All,
489 489
 					ReservedSpace: reservedSpace,
490 490
 					MaxUsedSpace:  maxUsedSpace,
491 491
new file mode 100644
... ...
@@ -0,0 +1,11 @@
0
+package buildbackend
1
+
2
+import "github.com/moby/moby/api/types/filters"
3
+
4
+type CachePruneOptions struct {
5
+	All           bool
6
+	ReservedSpace int64
7
+	MaxUsedSpace  int64
8
+	MinFreeSpace  int64
9
+	Filters       filters.Args
10
+}
... ...
@@ -5,6 +5,7 @@ import (
5 5
 
6 6
 	"github.com/moby/moby/api/types/build"
7 7
 	"github.com/moby/moby/v2/daemon/server/backend"
8
+	"github.com/moby/moby/v2/daemon/server/buildbackend"
8 9
 )
9 10
 
10 11
 // Backend abstracts an image builder whose only purpose is to build an image referenced by an imageID.
... ...
@@ -14,7 +15,7 @@ type Backend interface {
14 14
 	Build(context.Context, backend.BuildConfig) (string, error)
15 15
 
16 16
 	// PruneCache prunes the build cache.
17
-	PruneCache(context.Context, build.CachePruneOptions) (*build.CachePruneReport, error)
17
+	PruneCache(context.Context, buildbackend.CachePruneOptions) (*build.CachePruneReport, error)
18 18
 	Cancel(context.Context, string) error
19 19
 }
20 20
 
... ...
@@ -24,6 +24,7 @@ import (
24 24
 	"github.com/moby/moby/api/types/registry"
25 25
 	"github.com/moby/moby/api/types/versions"
26 26
 	"github.com/moby/moby/v2/daemon/server/backend"
27
+	"github.com/moby/moby/v2/daemon/server/buildbackend"
27 28
 	"github.com/moby/moby/v2/daemon/server/httputils"
28 29
 	"github.com/moby/moby/v2/pkg/ioutils"
29 30
 	"github.com/pkg/errors"
... ...
@@ -179,7 +180,7 @@ func (br *buildRouter) postPrune(ctx context.Context, w http.ResponseWriter, r *
179 179
 		return err
180 180
 	}
181 181
 
182
-	opts := build.CachePruneOptions{
182
+	opts := buildbackend.CachePruneOptions{
183 183
 		All:     httputils.BoolValue(r, "all"),
184 184
 		Filters: fltrs,
185 185
 	}
... ...
@@ -2,8 +2,6 @@ package build
2 2
 
3 3
 import (
4 4
 	"time"
5
-
6
-	"github.com/moby/moby/api/types/filters"
7 5
 )
8 6
 
9 7
 // CacheRecord contains information about a build cache record.
... ...
@@ -33,17 +31,6 @@ type CacheRecord struct {
33 33
 	UsageCount int
34 34
 }
35 35
 
36
-// CachePruneOptions hold parameters to prune the build cache.
37
-type CachePruneOptions struct {
38
-	All           bool
39
-	ReservedSpace int64
40
-	MaxUsedSpace  int64
41
-	MinFreeSpace  int64
42
-	Filters       filters.Args
43
-
44
-	KeepStorage int64 // Deprecated: deprecated in API 1.48.
45
-}
46
-
47 36
 // CachePruneReport contains the response for Engine API:
48 37
 // POST "/build/prune"
49 38
 type CachePruneReport struct {
... ...
@@ -9,10 +9,20 @@ import (
9 9
 
10 10
 	"github.com/moby/moby/api/types/build"
11 11
 	"github.com/moby/moby/api/types/filters"
12
+	"github.com/moby/moby/api/types/versions"
12 13
 )
13 14
 
15
+// BuildCachePruneOptions hold parameters to prune the build cache.
16
+type BuildCachePruneOptions struct {
17
+	All           bool
18
+	ReservedSpace int64
19
+	MaxUsedSpace  int64
20
+	MinFreeSpace  int64
21
+	Filters       filters.Args
22
+}
23
+
14 24
 // BuildCachePrune requests the daemon to delete unused cache data.
15
-func (cli *Client) BuildCachePrune(ctx context.Context, opts build.CachePruneOptions) (*build.CachePruneReport, error) {
25
+func (cli *Client) BuildCachePrune(ctx context.Context, opts BuildCachePruneOptions) (*build.CachePruneReport, error) {
16 26
 	if err := cli.NewVersionError(ctx, "1.31", "build prune"); err != nil {
17 27
 		return nil, err
18 28
 	}
... ...
@@ -22,11 +32,14 @@ func (cli *Client) BuildCachePrune(ctx context.Context, opts build.CachePruneOpt
22 22
 		query.Set("all", "1")
23 23
 	}
24 24
 
25
-	if opts.KeepStorage != 0 {
26
-		query.Set("keep-storage", strconv.Itoa(int(opts.KeepStorage)))
27
-	}
28 25
 	if opts.ReservedSpace != 0 {
29
-		query.Set("reserved-space", strconv.Itoa(int(opts.ReservedSpace)))
26
+		// Prior to API v1.48, 'keep-storage' was used to set the reserved space for the build cache.
27
+		// TODO(austinvazquez): remove once API v1.47 is no longer supported. See https://github.com/moby/moby/issues/50902
28
+		if versions.LessThanOrEqualTo(cli.version, "1.47") {
29
+			query.Set("keep-storage", strconv.Itoa(int(opts.ReservedSpace)))
30
+		} else {
31
+			query.Set("reserved-space", strconv.Itoa(int(opts.ReservedSpace)))
32
+		}
30 33
 	}
31 34
 	if opts.MaxUsedSpace != 0 {
32 35
 		query.Set("max-used-space", strconv.Itoa(int(opts.MaxUsedSpace)))
... ...
@@ -107,7 +107,7 @@ type DistributionAPIClient interface {
107 107
 // ImageAPIClient defines API client methods for the images
108 108
 type ImageAPIClient interface {
109 109
 	ImageBuild(ctx context.Context, context io.Reader, options build.ImageBuildOptions) (build.ImageBuildResponse, error)
110
-	BuildCachePrune(ctx context.Context, opts build.CachePruneOptions) (*build.CachePruneReport, error)
110
+	BuildCachePrune(ctx context.Context, opts BuildCachePruneOptions) (*build.CachePruneReport, error)
111 111
 	BuildCancel(ctx context.Context, id string) error
112 112
 	ImageCreate(ctx context.Context, parentReference string, options ImageCreateOptions) (io.ReadCloser, error)
113 113
 	ImageImport(ctx context.Context, source ImageImportSource, ref string, options ImageImportOptions) (io.ReadCloser, error)