Skip to content

Commit

Permalink
api/types/volume: add PruneOptions
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Apr 25, 2023
1 parent 8b9469d commit e121f17
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
9 changes: 9 additions & 0 deletions api/types/volume/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ import "github.com/docker/docker/api/types/filters"
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
}
2 changes: 1 addition & 1 deletion client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,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) (types.VolumesPruneReport, error)
VolumesPrune(ctx context.Context, options volume.PruneOptions) (types.VolumesPruneReport, error)
VolumeUpdate(ctx context.Context, volumeID string, version swarm.Version, options volume.UpdateOptions) error
}

Expand Down
18 changes: 15 additions & 3 deletions client/volume_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,30 @@ import (
"fmt"

"github.com/docker/docker/api/types"
"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) (types.VolumesPruneReport, error) {
func (cli *Client) VolumesPrune(ctx context.Context, opts volume.PruneOptions) (types.VolumesPruneReport, error) {
var report types.VolumesPruneReport

if err := cli.NewVersionError("1.25", "volume prune"); err != nil {
return report, 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 report, 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 @@ -12,7 +12,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 @@ -118,7 +117,7 @@ func TestAuthZPluginV2RejectVolumeRequests(t *testing.T) {
assert.Assert(t, err != nil)
assert.Assert(t, strings.Contains(err.Error(), fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag)))

_, err = c.VolumesPrune(context.Background(), filters.Args{})
_, err = c.VolumesPrune(context.Background(), volume.PruneOptions{})
assert.Assert(t, err != nil)
assert.Assert(t, strings.Contains(err.Error(), 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 @@ -335,7 +344,7 @@ VOLUME ` + volDest
err = client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{})
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))
}

0 comments on commit e121f17

Please sign in to comment.