Skip to content

Commit 47b7e9a

Browse files
authored
fix: prune volumes without all flag for pre-Docker v1.42 (#2781)
fix: prune volumes without all for pre-Docker 1.42 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)
1 parent 68e3663 commit 47b7e9a

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

internal/stop/stop_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/docker/docker/api/types/container"
1414
"github.com/docker/docker/api/types/network"
1515
"github.com/docker/docker/api/types/volume"
16+
"github.com/docker/docker/client"
1617
"github.com/h2non/gock"
1718
"github.com/spf13/afero"
1819
"github.com/stretchr/testify/assert"
@@ -206,6 +207,24 @@ func TestStopServices(t *testing.T) {
206207
assert.Empty(t, apitest.ListUnmatchedRequests())
207208
})
208209

210+
t.Run("skips all filter when removing data volumes with Docker version pre-v1.42", func(t *testing.T) {
211+
utils.DbId = "test-db"
212+
utils.ConfigId = "test-config"
213+
utils.StorageId = "test-storage"
214+
utils.EdgeRuntimeId = "test-functions"
215+
utils.InbucketId = "test-inbucket"
216+
// Setup mock docker
217+
require.NoError(t, apitest.MockDocker(utils.Docker))
218+
require.NoError(t, client.WithVersion("1.41")(utils.Docker))
219+
defer gock.OffAll()
220+
apitest.MockDockerStop(utils.Docker)
221+
// Run test
222+
err := stop(context.Background(), false, io.Discard, utils.Config.ProjectId)
223+
// Check error
224+
assert.NoError(t, err)
225+
assert.Empty(t, apitest.ListUnmatchedRequests())
226+
})
227+
209228
t.Run("throws error on prune failure", func(t *testing.T) {
210229
// Setup mock docker
211230
require.NoError(t, apitest.MockDocker(utils.Docker))

internal/testing/apitest/docker.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
"github.com/docker/docker/api/types"
1010
"github.com/docker/docker/api/types/container"
1111
"github.com/docker/docker/api/types/network"
12+
"github.com/docker/docker/api/types/versions"
1213
"github.com/docker/docker/api/types/volume"
1314
"github.com/docker/docker/client"
1415
"github.com/docker/docker/pkg/stdcopy"
16+
"github.com/go-errors/errors"
1517
"github.com/h2non/gock"
1618
)
1719

@@ -64,6 +66,12 @@ func MockDockerStop(docker *client.Client) {
6466
Post("/v" + docker.ClientVersion() + "/containers/prune").
6567
Reply(http.StatusOK).
6668
JSON(container.PruneReport{})
69+
if !versions.GreaterThanOrEqualTo(docker.ClientVersion(), "1.42") {
70+
gock.New(docker.DaemonHost()).
71+
Post("/v"+docker.ClientVersion()+"/volumes/prune").
72+
MatchParam("filters", `"all":{"true":true}`).
73+
ReplyError(errors.New(`failed to parse filters for all=true&label=com.supabase.cli.project%3Dtest: "all" is an invalid volume filter`))
74+
}
6775
gock.New(docker.DaemonHost()).
6876
Post("/v" + docker.ClientVersion() + "/volumes/prune").
6977
Reply(http.StatusOK).

internal/utils/docker.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/docker/docker/api/types/image"
2626
"github.com/docker/docker/api/types/mount"
2727
"github.com/docker/docker/api/types/network"
28+
"github.com/docker/docker/api/types/versions"
2829
"github.com/docker/docker/api/types/volume"
2930
"github.com/docker/docker/client"
3031
"github.com/docker/docker/errdefs"
@@ -125,10 +126,12 @@ func DockerRemoveAll(ctx context.Context, w io.Writer, projectId string) error {
125126
}
126127
// Remove named volumes
127128
if NoBackupVolume {
128-
// Since docker engine 25.0.3, all flag is required to include named volumes.
129-
// https://github.com/docker/cli/blob/master/cli/command/volume/prune.go#L76
130129
vargs := args.Clone()
131-
vargs.Add("all", "true")
130+
if versions.GreaterThanOrEqualTo(Docker.ClientVersion(), "1.42") {
131+
// Since docker engine 25.0.3, all flag is required to include named volumes.
132+
// https://github.com/docker/cli/blob/master/cli/command/volume/prune.go#L76
133+
vargs.Add("all", "true")
134+
}
132135
if report, err := Docker.VolumesPrune(ctx, vargs); err != nil {
133136
return errors.Errorf("failed to prune volumes: %w", err)
134137
} else if viper.GetBool("DEBUG") {

0 commit comments

Comments
 (0)