Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Keep volume labels when cloning #65

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Keep volume labels when cloning
  • Loading branch information
felipecruz91 committed Sep 12, 2022
commit 51fdc3537fe8da4d5a032dde6be6f8996b35d399
14 changes: 14 additions & 0 deletions vm/internal/handler/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"fmt"
volumetypes "github.com/docker/docker/api/types/volume"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -71,6 +72,19 @@ func (h *Handler) CloneVolume(ctx echo.Context) error {
return err
}

// Create destination volume with the same labels as the source volume
volInspect, err := cli.VolumeInspect(ctx.Request().Context(), volumeName)
if err != nil {
return err
}
_, err = cli.VolumeCreate(ctx.Request().Context(), volumetypes.VolumeCreateBody{
Labels: volInspect.Labels,
Name: destVolume,
})
if err != nil {
return err
}

// Clone
resp, err := cli.ContainerCreate(ctx.Request().Context(), &container.Config{
Image: internal.BusyboxImage,
Expand Down
15 changes: 15 additions & 0 deletions vm/internal/handler/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func TestCloneVolume(t *testing.T) {
_, err := cli.VolumeCreate(c.Request().Context(), volumetypes.VolumeCreateBody{
Driver: "local",
Name: volume,
Labels: map[string]string{
"com.docker.compose.project": "my-compose-project",
"com.docker.compose.version": "2.10.2",
"com.docker.compose.volume": "foo-bar",
},
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -99,4 +104,14 @@ func TestCloneVolume(t *testing.T) {
sizes := backend.GetVolumesSize(context.Background(), dockerClient, destVolume)
require.Equal(t, int64(16000), sizes[destVolume].Bytes)
require.Equal(t, "16.0 kB", sizes[destVolume].Human)

// Check volume labels
volInspect, err := cli.VolumeInspect(context.Background(), destVolume)
if err != nil {
t.Fatal(err)
}
require.Len(t, volInspect.Labels, 3)
require.Equal(t, "my-compose-project", volInspect.Labels["com.docker.compose.project"])
require.Equal(t, "2.10.2", volInspect.Labels["com.docker.compose.version"])
require.Equal(t, "foo-bar", volInspect.Labels["com.docker.compose.volume"])
}