1
0
mirror of https://github.com/moby/moby.git synced 2025-09-21 04:22:10 +03:00
Files
moby/client/build_prune.go
Sebastiaan van Stijn 20d8342a4b move endpoint API version constraints to API server
This introduces a `WithMinimumAPIVersion` RouteWrapper to configure the
minimum API version  required for a route. It produces a 400 (Invalid Request)
error when accessing the endpoint on API versions lower than the given version.

Note that technically, it should produce a 404 ("not found") error,
as the endpoint should be considered "non-existing" on such API versions,
but 404 status-codes are used in business logic for various endpoints.

This patch allows removal of corresponding API-version checks from the client,
and other implementation of clients for the API. While the produced error message
is slightly more "technical", these situations should be rare and only happen
when the API version of the client is explicitly overridden, or a client was
implemented with a fixed API version (potentially missing version checks).

Before this patch, these errors were produced by the client:

    DOCKER_API_VERSION=v1.24 docker container prune -f
    docker container prune requires API version 1.25, but the Docker daemon API version is 1.24

With this patch applied, the error is returned by the daemon:

    DOCKER_API_VERSION=v1.24 docker container prune -f
    Error response from daemon: POST /containers/prune requires minimum API version 1.25

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-09-16 12:20:44 +02:00

66 lines
1.8 KiB
Go

package client
import (
"context"
"encoding/json"
"fmt"
"net/url"
"strconv"
"github.com/moby/moby/api/types/build"
"github.com/moby/moby/api/types/filters"
"github.com/moby/moby/api/types/versions"
)
// BuildCachePruneOptions hold parameters to prune the build cache.
type BuildCachePruneOptions struct {
All bool
ReservedSpace int64
MaxUsedSpace int64
MinFreeSpace int64
Filters filters.Args
}
// BuildCachePrune requests the daemon to delete unused cache data.
func (cli *Client) BuildCachePrune(ctx context.Context, opts BuildCachePruneOptions) (*build.CachePruneReport, error) {
query := url.Values{}
if opts.All {
query.Set("all", "1")
}
if opts.ReservedSpace != 0 {
// Prior to API v1.48, 'keep-storage' was used to set the reserved space for the build cache.
// TODO(austinvazquez): remove once API v1.47 is no longer supported. See https://github.com/moby/moby/issues/50902
if versions.LessThanOrEqualTo(cli.version, "1.47") {
query.Set("keep-storage", strconv.Itoa(int(opts.ReservedSpace)))
} else {
query.Set("reserved-space", strconv.Itoa(int(opts.ReservedSpace)))
}
}
if opts.MaxUsedSpace != 0 {
query.Set("max-used-space", strconv.Itoa(int(opts.MaxUsedSpace)))
}
if opts.MinFreeSpace != 0 {
query.Set("min-free-space", strconv.Itoa(int(opts.MinFreeSpace)))
}
f, err := filters.ToJSON(opts.Filters)
if err != nil {
return nil, fmt.Errorf("prune could not marshal filters option: %w", err)
}
query.Set("filters", f)
resp, err := cli.post(ctx, "/build/prune", query, nil, nil)
defer ensureReaderClosed(resp)
if err != nil {
return nil, err
}
report := build.CachePruneReport{}
if err := json.NewDecoder(resp.Body).Decode(&report); err != nil {
return nil, fmt.Errorf("error retrieving disk usage: %w", err)
}
return &report, nil
}