Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api/types/volume: add PruneOptions #45370

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
api/types/volume: add PruneOptions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Nov 25, 2024
commit 7065e742ac35b3a9312e54b807cc2523e987f29c
9 changes: 9 additions & 0 deletions api/types/volume/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ type ListOptions struct {
Filters filters.Args
}

// PruneOptions holds parameters to prune volumes.
type PruneOptions struct {
// All controls whether named volumes should also be pruned.
All bool

// Filters to apply when pruning.
Filters filters.Args
}

// PruneReport contains the response for Engine API:
// POST "/volumes/prune"
type PruneReport struct {
Expand Down
2 changes: 1 addition & 1 deletion client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ type VolumeAPIClient interface {
VolumeInspectWithRaw(ctx context.Context, volumeID string) (volume.Volume, []byte, error)
VolumeList(ctx context.Context, options volume.ListOptions) (volume.ListResponse, error)
VolumeRemove(ctx context.Context, volumeID string, force bool) error
VolumesPrune(ctx context.Context, pruneFilter filters.Args) (volume.PruneReport, error)
VolumesPrune(ctx context.Context, options volume.PruneOptions) (volume.PruneReport, error)
VolumeUpdate(ctx context.Context, volumeID string, version swarm.Version, options volume.UpdateOptions) error
}

Expand Down
17 changes: 14 additions & 3 deletions client/volume_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,28 @@ import (
"encoding/json"
"fmt"

"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/errdefs"
"github.com/pkg/errors"
)

// VolumesPrune requests the daemon to delete unused data
func (cli *Client) VolumesPrune(ctx context.Context, pruneFilters filters.Args) (volume.PruneReport, error) {
func (cli *Client) VolumesPrune(ctx context.Context, opts volume.PruneOptions) (volume.PruneReport, error) {
if err := cli.NewVersionError(ctx, "1.25", "volume prune"); err != nil {
return volume.PruneReport{}, err
}

query, err := getFiltersQuery(pruneFilters)
if versions.GreaterThanOrEqualTo(cli.version, "1.42") {
if opts.All {
if opts.Filters.Contains("all") {
return report, errdefs.InvalidParameter(errors.New(`conflicting options: cannot specify both "all"" and "all" filter"`))
}
opts.Filters.Add("all", "true")
}
}

query, err := getFiltersQuery(opts.Filters)
if err != nil {
return volume.PruneReport{}, err
}
Expand Down
27 changes: 27 additions & 0 deletions client/volume_prune_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package client

import (
"context"
"net/http"
"testing"

"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/errdefs"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)

func TestVolumePruneError(t *testing.T) {
client := &Client{
version: "1.42",
client: &http.Client{},
}

_, err := client.VolumesPrune(context.Background(), volume.PruneOptions{
All: true,
Filters: filters.NewArgs(filters.Arg("all", "true")),
})
assert.Check(t, is.ErrorType(err, errdefs.IsInvalidParameter))
assert.Check(t, is.Error(err, `conflicting options: cannot specify both "all"" and "all" filter"`))
}
3 changes: 1 addition & 2 deletions integration/plugin/authz/authz_plugin_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
"github.com/docker/docker/integration/internal/container"
Expand Down Expand Up @@ -112,7 +111,7 @@ func TestAuthZPluginV2RejectVolumeRequests(t *testing.T) {
assert.Assert(t, err != nil)
assert.ErrorContains(t, err, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))

_, err = c.VolumesPrune(ctx, filters.Args{})
_, err = c.VolumesPrune(ctx, volume.PruneOptions{})
assert.Assert(t, err != nil)
assert.ErrorContains(t, err, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))
}
Expand Down
17 changes: 13 additions & 4 deletions integration/volume/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func TestVolumePruneAnonymous(t *testing.T) {
assert.NilError(t, err)

// Prune anonymous volumes
pruneReport, err := client.VolumesPrune(ctx, filters.Args{})
pruneReport, err := client.VolumesPrune(ctx, volume.PruneOptions{})
assert.NilError(t, err)
assert.Check(t, is.Equal(len(pruneReport.VolumesDeleted), 1))
assert.Check(t, is.Equal(pruneReport.VolumesDeleted[0], v.Name))
Expand All @@ -283,8 +283,17 @@ func TestVolumePruneAnonymous(t *testing.T) {
// Prune all volumes
_, err = client.VolumeCreate(ctx, volume.CreateOptions{})
assert.NilError(t, err)
pruneReport, err = client.VolumesPrune(ctx, volume.PruneOptions{Filters: filters.NewArgs(filters.Arg("all", "1"))})
assert.NilError(t, err)
assert.Check(t, is.Equal(len(pruneReport.VolumesDeleted), 2))

// Create a named volume and an anonymous volume, and prune all
_, err = client.VolumeCreate(ctx, volume.CreateOptions{})
assert.NilError(t, err)
_, err = client.VolumeCreate(ctx, volume.CreateOptions{Name: "test"})
assert.NilError(t, err)

pruneReport, err = client.VolumesPrune(ctx, filters.NewArgs(filters.Arg("all", "1")))
pruneReport, err = client.VolumesPrune(ctx, volume.PruneOptions{All: true})
assert.NilError(t, err)
assert.Check(t, is.Equal(len(pruneReport.VolumesDeleted), 2))

Expand All @@ -299,7 +308,7 @@ func TestVolumePruneAnonymous(t *testing.T) {
vNamed, err = client.VolumeCreate(ctx, volume.CreateOptions{Name: "test-api141"})
assert.NilError(t, err)

pruneReport, err = clientOld.VolumesPrune(ctx, filters.Args{})
pruneReport, err = clientOld.VolumesPrune(ctx, volume.PruneOptions{})
assert.NilError(t, err)
assert.Check(t, is.Equal(len(pruneReport.VolumesDeleted), 2))
assert.Check(t, is.Contains(pruneReport.VolumesDeleted, v.Name))
Expand Down Expand Up @@ -334,7 +343,7 @@ VOLUME ` + volDest
err = client.ContainerRemove(ctx, id, containertypes.RemoveOptions{})
assert.NilError(t, err)

pruneReport, err := client.VolumesPrune(ctx, filters.Args{})
pruneReport, err := client.VolumesPrune(ctx, volume.PruneOptions{})
assert.NilError(t, err)
assert.Assert(t, is.Contains(pruneReport.VolumesDeleted, volumeName))
}
Loading