From 6fccb935c4b6c267d0dc805f2cc7c15ea6747723 Mon Sep 17 00:00:00 2001 From: Carlo Mandelli Date: Wed, 16 Oct 2019 14:46:51 +0200 Subject: [PATCH] docker: mount multiple volumes --- internal/services/executor/driver/docker.go | 19 ++-- .../services/executor/driver/docker_test.go | 96 ++++++++++++++++++- internal/services/executor/driver/k8s_test.go | 52 +++++++++- 3 files changed, 156 insertions(+), 11 deletions(-) diff --git a/internal/services/executor/driver/docker.go b/internal/services/executor/driver/docker.go index 67dec75..8015bd3 100644 --- a/internal/services/executor/driver/docker.go +++ b/internal/services/executor/driver/docker.go @@ -294,21 +294,24 @@ func (d *DockerDriver) createContainer(ctx context.Context, index int, podConfig cliHostConfig.NetworkMode = container.NetworkMode(fmt.Sprintf("container:%s", maincontainerID)) } + var mounts []mount.Mount + for _, vol := range containerConfig.Volumes { if vol.TmpFS != nil { - cliHostConfig.Mounts = []mount.Mount{ - mount.Mount{ - Type: mount.TypeTmpfs, - Target: vol.Path, - TmpfsOptions: &mount.TmpfsOptions{ - SizeBytes: vol.TmpFS.Size, - }, + mounts = append(mounts, mount.Mount{ + Type: mount.TypeTmpfs, + Target: vol.Path, + TmpfsOptions: &mount.TmpfsOptions{ + SizeBytes: vol.TmpFS.Size, }, - } + }) } else { return nil, errors.Errorf("missing volume config") } } + if mounts != nil { + cliHostConfig.Mounts = mounts + } resp, err := d.client.ContainerCreate(ctx, cliContainerConfig, cliHostConfig, nil, "") return &resp, err diff --git a/internal/services/executor/driver/docker_test.go b/internal/services/executor/driver/docker_test.go index b7caa44..7219fd5 100644 --- a/internal/services/executor/driver/docker_test.go +++ b/internal/services/executor/driver/docker_test.go @@ -397,7 +397,7 @@ func TestDockerPod(t *testing.T) { defer func() { _ = pod.Remove(ctx) }() ce, err := pod.Exec(ctx, &ExecConfig{ - Cmd: []string{"sh", "-c", "if [ $(cat /proc/mounts | grep /mnt/tmpfs | grep size=1024k | wc -l ) -ne 1 ]; then exit 1; fi"}, + Cmd: []string{"sh", "-c", "if [ $(grep /mnt/tmpfs /proc/mounts | grep -c size=1024k) -ne 1 ]; then exit 1; fi"}, }) if err != nil { t.Fatalf("unexpected err: %v", err) @@ -436,7 +436,99 @@ func TestDockerPod(t *testing.T) { defer func() { _ = pod.Remove(ctx) }() ce, err := pod.Exec(ctx, &ExecConfig{ - Cmd: []string{"sh", "-c", "if [ $(cat /proc/mounts | grep /mnt/tmpfs | wc -l ) -ne 1 ]; then exit 1; fi"}, + Cmd: []string{"sh", "-c", "if [ $(grep -c /mnt/tmpfs /proc/mounts) -ne 1 ]; then exit 1; fi"}, + }) + if err != nil { + t.Fatalf("unexpected err: %v", err) + } + + code, err := ce.Wait(ctx) + if err != nil { + t.Fatalf("unexpected err: %v", err) + } + if code != 0 { + t.Fatalf("unexpected exit code: %d", code) + } + }) + + t.Run("test pod with two tmpfs volumes with size limit", func(t *testing.T) { + pod, err := d.NewPod(ctx, &PodConfig{ + ID: uuid.NewV4().String(), + TaskID: uuid.NewV4().String(), + Containers: []*ContainerConfig{ + &ContainerConfig{ + Cmd: []string{"cat"}, + Image: "busybox", + Volumes: []Volume{ + { + Path: "/mnt/vol1", + TmpFS: &VolumeTmpFS{ + Size: 1024 * 1024, + }, + }, + { + Path: "/mnt/vol2", + TmpFS: &VolumeTmpFS{ + Size: 1024 * 1024, + }, + }, + }, + }, + }, + InitVolumeDir: "/tmp/agola", + }, ioutil.Discard) + if err != nil { + t.Fatalf("unexpected err: %v", err) + } + defer func() { _ = pod.Remove(ctx) }() + + ce, err := pod.Exec(ctx, &ExecConfig{ + Cmd: []string{"sh", "-c", "if [ $(grep /mnt/vol1 /proc/mounts | grep -c size=1024k) -ne 1 -o $(grep /mnt/vol2 /proc/mounts | grep -c size=1024k) -ne 1 ]; then exit 1; fi"}, + }) + if err != nil { + t.Fatalf("unexpected err: %v", err) + } + + code, err := ce.Wait(ctx) + if err != nil { + t.Fatalf("unexpected err: %v", err) + } + if code != 0 { + t.Fatalf("unexpected exit code: %d", code) + } + }) + + t.Run("test pod with two tmpfs volumes one with size limit and one without", func(t *testing.T) { + pod, err := d.NewPod(ctx, &PodConfig{ + ID: uuid.NewV4().String(), + TaskID: uuid.NewV4().String(), + Containers: []*ContainerConfig{ + &ContainerConfig{ + Cmd: []string{"cat"}, + Image: "busybox", + Volumes: []Volume{ + { + Path: "/mnt/vol1", + TmpFS: &VolumeTmpFS{ + Size: 1024 * 1024, + }, + }, + { + Path: "/mnt/vol2", + TmpFS: &VolumeTmpFS{}, + }, + }, + }, + }, + InitVolumeDir: "/tmp/agola", + }, ioutil.Discard) + if err != nil { + t.Fatalf("unexpected err: %v", err) + } + defer func() { _ = pod.Remove(ctx) }() + + ce, err := pod.Exec(ctx, &ExecConfig{ + Cmd: []string{"sh", "-c", "if [ $(grep /mnt/vol1 /proc/mounts | grep -c size=1024k) -ne 1 -o $(grep -c /mnt/vol2 /proc/mounts) -ne 1 ]; then exit 1; fi"}, }) if err != nil { t.Fatalf("unexpected err: %v", err) diff --git a/internal/services/executor/driver/k8s_test.go b/internal/services/executor/driver/k8s_test.go index a3a15f3..44d1493 100644 --- a/internal/services/executor/driver/k8s_test.go +++ b/internal/services/executor/driver/k8s_test.go @@ -287,7 +287,57 @@ func TestK8sPod(t *testing.T) { var buf bytes.Buffer ce, err := pod.Exec(ctx, &ExecConfig{ // k8s doesn't set size=1024k in the tmpf mount options but uses other modes to detect the size - Cmd: []string{"sh", "-c", "if [ $(cat /proc/mounts | grep /mnt/tmpfs | wc -l ) -ne 1 ]; then exit 1; fi"}, + Cmd: []string{"sh", "-c", "if [ $(grep -c /mnt/tmpfs /proc/mounts) -ne 1 ]; then exit 1; fi"}, + Stdout: &buf, + }) + if err != nil { + t.Fatalf("unexpected err: %v", err) + } + + code, err := ce.Wait(ctx) + if err != nil { + t.Fatalf("unexpected err: %v", err) + } + if code != 0 { + t.Fatalf("unexpected exit code: %d", code) + } + }) + + t.Run("test pod with two tmpfs volumes", func(t *testing.T) { + pod, err := d.NewPod(ctx, &PodConfig{ + ID: uuid.NewV4().String(), + TaskID: uuid.NewV4().String(), + Containers: []*ContainerConfig{ + &ContainerConfig{ + Cmd: []string{"cat"}, + Image: "busybox", + Volumes: []Volume{ + { + Path: "/mnt/vol1", + TmpFS: &VolumeTmpFS{ + Size: 1024 * 1024, + }, + }, + { + Path: "/mnt/vol2", + TmpFS: &VolumeTmpFS{ + Size: 1024 * 1024, + }, + }, + }, + }, + }, + InitVolumeDir: "/tmp/agola", + }, ioutil.Discard) + if err != nil { + t.Fatalf("unexpected err: %v", err) + } + defer func() { _ = pod.Remove(ctx) }() + + var buf bytes.Buffer + ce, err := pod.Exec(ctx, &ExecConfig{ + // k8s doesn't set size=1024k in the tmpf mount options but uses other modes to detect the size + Cmd: []string{"sh", "-c", "if [ $(grep -c /mnt/vol1 /proc/mounts) -ne 1 -o $(grep -c /mnt/vol2 /proc/mounts) ]; then exit 1; fi"}, Stdout: &buf, }) if err != nil {