Skip to content

Commit

Permalink
fix: prune volumes without all for pre-Docker 1.42
Browse files Browse the repository at this point in the history
Before Docker API version 1.42, the `all` flag in Docker.VolumePrune`
is not required.

Podman prints an error like the following when the `all` flag
is passed ([Podman's only compatible with Docker API v1.41][1]):

```
failed to parse filters for all=true&label=com.supabase.cli.project%3Dtest: "all" is an invalid volume filter
```

[1]: https://github.com/containers/podman/blob/290d94d3c00857dd582ffbee6bd0677a0904c783/version/version.go#L46

Fixes: bec537e
Fixes: #2348 (comment)
  • Loading branch information
aloisklink committed Oct 20, 2024
1 parent 8d4d6db commit 3c8ddf4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
19 changes: 19 additions & 0 deletions internal/stop/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
"github.com/h2non/gock"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -206,6 +207,24 @@ func TestStopServices(t *testing.T) {
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("skips all filter when removing data volumes with Docker version pre-v1.42", func(t *testing.T) {
utils.DbId = "test-db"
utils.ConfigId = "test-config"
utils.StorageId = "test-storage"
utils.EdgeRuntimeId = "test-functions"
utils.InbucketId = "test-inbucket"
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
require.NoError(t, client.WithVersion("1.41")(utils.Docker))
defer gock.OffAll()
apitest.MockDockerStop(utils.Docker)
// Run test
err := stop(context.Background(), false, io.Discard, utils.Config.ProjectId)
// Check error
assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("throws error on prune failure", func(t *testing.T) {
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
Expand Down
8 changes: 8 additions & 0 deletions internal/testing/apitest/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/go-errors/errors"
"github.com/h2non/gock"
)

Expand Down Expand Up @@ -64,6 +66,12 @@ func MockDockerStop(docker *client.Client) {
Post("/v" + docker.ClientVersion() + "/containers/prune").
Reply(http.StatusOK).
JSON(container.PruneReport{})
if !versions.GreaterThanOrEqualTo(docker.ClientVersion(), "1.42") {
gock.New(docker.DaemonHost()).
Post("/v"+docker.ClientVersion()+"/volumes/prune").
MatchParam("filters", `"all":{"true":true}`).
ReplyError(errors.New(`failed to parse filters for all=true&label=com.supabase.cli.project%3Dtest: "all" is an invalid volume filter`))
}
gock.New(docker.DaemonHost()).
Post("/v" + docker.ClientVersion() + "/volumes/prune").
Reply(http.StatusOK).
Expand Down
9 changes: 6 additions & 3 deletions internal/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
Expand Down Expand Up @@ -125,10 +126,12 @@ func DockerRemoveAll(ctx context.Context, w io.Writer, projectId string) error {
}
// Remove named volumes
if NoBackupVolume {
// Since docker engine 25.0.3, all flag is required to include named volumes.
// https://github.com/docker/cli/blob/master/cli/command/volume/prune.go#L76
vargs := args.Clone()
vargs.Add("all", "true")
if versions.GreaterThanOrEqualTo(Docker.ClientVersion(), "1.42") {
// Since docker engine 25.0.3, all flag is required to include named volumes.
// https://github.com/docker/cli/blob/master/cli/command/volume/prune.go#L76
vargs.Add("all", "true")
}
if report, err := Docker.VolumesPrune(ctx, vargs); err != nil {
return errors.Errorf("failed to prune volumes: %w", err)
} else if viper.GetBool("DEBUG") {
Expand Down

0 comments on commit 3c8ddf4

Please sign in to comment.