Skip to content

Commit

Permalink
Merge pull request #3846 from flyinprogrammer/driverctx
Browse files Browse the repository at this point in the history
drivers: use ctx.TaskEnv for mount points
  • Loading branch information
schmichael authored Feb 9, 2018
2 parents 7ab0111 + 83ca7be commit 1bdd2f0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
12 changes: 6 additions & 6 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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, ":"))
Expand All @@ -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
}
Expand Down
22 changes: 13 additions & 9 deletions client/driver/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
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)

// 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)
}
}

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

return task, driver, execCtx, hostfile, cleanup
}

Expand Down
6 changes: 3 additions & 3 deletions client/driver/rkt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1bdd2f0

Please sign in to comment.