docker: mount multiple volumes

This commit is contained in:
Carlo Mandelli 2019-10-16 14:46:51 +02:00
parent 2d813461d9
commit 6fccb935c4
3 changed files with 156 additions and 11 deletions

View File

@ -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

View File

@ -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)

View File

@ -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 {