From 83ca7beeebfbcd552f8d4ff4240bcf1ceaed46f8 Mon Sep 17 00:00:00 2001 From: Alan Scherger Date: Tue, 6 Feb 2018 23:24:16 -0600 Subject: [PATCH] drivers: use ctx.TaskEnv for mount points --- client/driver/docker.go | 12 ++++++------ client/driver/docker_test.go | 24 ++++++++++++++---------- client/driver/rkt.go | 6 +++--- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/client/driver/docker.go b/client/driver/docker.go index e63f7546f..ff6792c0d 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -1013,12 +1013,12 @@ func (d *DockerDriver) dockerClients() (*docker.Client, *docker.Client, error) { return client, waitClient, merr.ErrorOrNil() } -func (d *DockerDriver) containerBinds(driverConfig *DockerDriverConfig, taskDir *allocdir.TaskDir, +func (d *DockerDriver) containerBinds(driverConfig *DockerDriverConfig, ctx *ExecContext, task *structs.Task) ([]string, error) { - allocDirBind := fmt.Sprintf("%s:%s", taskDir.SharedAllocDir, allocdir.SharedAllocContainerPath) - taskLocalBind := fmt.Sprintf("%s:%s", taskDir.LocalDir, allocdir.TaskLocalContainerPath) - secretDirBind := fmt.Sprintf("%s:%s", taskDir.SecretsDir, allocdir.TaskSecretsContainerPath) + allocDirBind := fmt.Sprintf("%s:%s", ctx.TaskDir.SharedAllocDir, ctx.TaskEnv.EnvMap[env.AllocDir]) + taskLocalBind := fmt.Sprintf("%s:%s", ctx.TaskDir.LocalDir, ctx.TaskEnv.EnvMap[env.TaskLocalDir]) + secretDirBind := fmt.Sprintf("%s:%s", ctx.TaskDir.SecretsDir, ctx.TaskEnv.EnvMap[env.SecretsDir]) binds := []string{allocDirBind, taskLocalBind, secretDirBind} volumesEnabled := d.config.ReadBoolDefault(dockerVolumesConfigOption, dockerVolumesConfigDefault) @@ -1051,7 +1051,7 @@ func (d *DockerDriver) containerBinds(driverConfig *DockerDriverConfig, taskDir // Otherwise, we assume we receive a relative path binding in the format relative/to/task:/also/in/container if driverConfig.VolumeDriver == "" { // Expand path relative to alloc dir - parts[0] = filepath.Join(taskDir.Dir, parts[0]) + parts[0] = filepath.Join(ctx.TaskDir.Dir, parts[0]) } binds = append(binds, strings.Join(parts, ":")) @@ -1078,7 +1078,7 @@ func (d *DockerDriver) createContainerConfig(ctx *ExecContext, task *structs.Tas return c, fmt.Errorf("task.Resources is empty") } - binds, err := d.containerBinds(driverConfig, ctx.TaskDir, task) + binds, err := d.containerBinds(driverConfig, ctx, task) if err != nil { return c, err } diff --git a/client/driver/docker_test.go b/client/driver/docker_test.go index 511bc31a4..d8abea032 100644 --- a/client/driver/docker_test.go +++ b/client/driver/docker_test.go @@ -1618,25 +1618,29 @@ func setupDockerVolumes(t *testing.T, cfg *config.Config, hostpath string) (*str allocDir.Destroy() t.Fatalf("failed to build task dir: %v", err) } + copyImage(t, taskDir, "busybox.tar") + // Setup driver alloc := mock.Alloc() - envBuilder := env.NewBuilder(cfg.Node, alloc, task, cfg.Region) - execCtx := NewExecContext(taskDir, envBuilder.Build()) - cleanup := func() { - allocDir.Destroy() - if filepath.IsAbs(hostpath) { - os.RemoveAll(hostpath) - } - } - logger := testLogger() emitter := func(m string, args ...interface{}) { logger.Printf("[EVENT] "+m, args...) } driverCtx := NewDriverContext(task.Name, alloc.ID, cfg, cfg.Node, testLogger(), emitter) driver := NewDockerDriver(driverCtx) - copyImage(t, taskDir, "busybox.tar") + // Setup execCtx + envBuilder := env.NewBuilder(cfg.Node, alloc, task, cfg.Region) + SetEnvvars(envBuilder, driver.FSIsolation(), taskDir, cfg) + execCtx := NewExecContext(taskDir, envBuilder.Build()) + + // Setup cleanup function + cleanup := func() { + allocDir.Destroy() + if filepath.IsAbs(hostpath) { + os.RemoveAll(hostpath) + } + } return task, driver, execCtx, hostfile, cleanup } diff --git a/client/driver/rkt.go b/client/driver/rkt.go index 446a48eae..2a7bed049 100644 --- a/client/driver/rkt.go +++ b/client/driver/rkt.go @@ -438,17 +438,17 @@ func (d *RktDriver) Start(ctx *ExecContext, task *structs.Task) (*StartResponse, // Mount /alloc allocVolName := fmt.Sprintf("%s-%s-alloc", d.DriverContext.allocID, sanitizedName) prepareArgs = append(prepareArgs, fmt.Sprintf("--volume=%s,kind=host,source=%s", allocVolName, ctx.TaskDir.SharedAllocDir)) - prepareArgs = append(prepareArgs, fmt.Sprintf("--mount=volume=%s,target=%s", allocVolName, allocdir.SharedAllocContainerPath)) + prepareArgs = append(prepareArgs, fmt.Sprintf("--mount=volume=%s,target=%s", allocVolName, ctx.TaskEnv.EnvMap[env.AllocDir])) // Mount /local localVolName := fmt.Sprintf("%s-%s-local", d.DriverContext.allocID, sanitizedName) prepareArgs = append(prepareArgs, fmt.Sprintf("--volume=%s,kind=host,source=%s", localVolName, ctx.TaskDir.LocalDir)) - prepareArgs = append(prepareArgs, fmt.Sprintf("--mount=volume=%s,target=%s", localVolName, allocdir.TaskLocalContainerPath)) + prepareArgs = append(prepareArgs, fmt.Sprintf("--mount=volume=%s,target=%s", localVolName, ctx.TaskEnv.EnvMap[env.TaskLocalDir])) // Mount /secrets secretsVolName := fmt.Sprintf("%s-%s-secrets", d.DriverContext.allocID, sanitizedName) prepareArgs = append(prepareArgs, fmt.Sprintf("--volume=%s,kind=host,source=%s", secretsVolName, ctx.TaskDir.SecretsDir)) - prepareArgs = append(prepareArgs, fmt.Sprintf("--mount=volume=%s,target=%s", secretsVolName, allocdir.TaskSecretsContainerPath)) + prepareArgs = append(prepareArgs, fmt.Sprintf("--mount=volume=%s,target=%s", secretsVolName, ctx.TaskEnv.EnvMap[env.SecretsDir])) // Mount arbitrary volumes if enabled if len(driverConfig.Volumes) > 0 {