From a087a8be7974ec8d641a0b41586f328151d3dfe7 Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Fri, 6 Jan 2017 12:19:32 -0800 Subject: [PATCH] Fix inconsistent task env setting Consolidate task environment building in GetTaskEnv since it can determine what kind of filesystem isolation is used. This means drivers no longer have to manipulate task environment paths. --- client/driver/docker.go | 4 ---- client/driver/driver.go | 14 +++++++++++++- client/driver/driver_test.go | 6 ++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/client/driver/docker.go b/client/driver/docker.go index 1e9f21dc9..7ac703d46 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -352,10 +352,6 @@ func (d *DockerDriver) FSIsolation() cstructs.FSIsolation { } func (d *DockerDriver) Prestart(ctx *ExecContext, task *structs.Task) error { - // Set environment variables. - d.taskEnv.SetAllocDir(allocdir.SharedAllocContainerPath). - SetTaskLocalDir(allocdir.TaskLocalContainerPath).SetSecretsDir(allocdir.TaskSecretsContainerPath).Build() - driverConfig, err := NewDockerDriverConfig(task, d.taskEnv) if err != nil { return err diff --git a/client/driver/driver.go b/client/driver/driver.go index ac25458b0..c20ac22bb 100644 --- a/client/driver/driver.go +++ b/client/driver/driver.go @@ -164,10 +164,22 @@ func GetTaskEnv(taskDir *allocdir.TaskDir, node *structs.Node, SetEnvvars(task.Env). SetTaskName(task.Name) - if taskDir != nil { + // Vary paths by filesystem isolation used + drv, err := NewDriver(task.Driver, NewEmptyDriverContext()) + if err != nil { + return nil, err + } + switch drv.FSIsolation() { + case cstructs.FSIsolationNone: + // Use host paths env.SetAllocDir(taskDir.SharedAllocDir) env.SetTaskLocalDir(taskDir.LocalDir) env.SetSecretsDir(taskDir.SecretsDir) + default: + // filesystem isolation; use container paths + env.SetAllocDir(allocdir.SharedAllocContainerPath) + env.SetTaskLocalDir(allocdir.TaskLocalContainerPath) + env.SetSecretsDir(allocdir.TaskSecretsContainerPath) } if task.Resources != nil { diff --git a/client/driver/driver_test.go b/client/driver/driver_test.go index 8c922aa71..360452866 100644 --- a/client/driver/driver_test.go +++ b/client/driver/driver_test.go @@ -130,7 +130,7 @@ func testDriverContexts(t *testing.T, task *structs.Task) *testContext { func TestDriver_GetTaskEnv(t *testing.T) { task := &structs.Task{ Name: "Foo", - Driver: "mock", + Driver: "mock_driver", Env: map[string]string{ "HELLO": "world", "lorem": "ipsum", @@ -155,7 +155,9 @@ func TestDriver_GetTaskEnv(t *testing.T) { alloc := mock.Alloc() alloc.Job.TaskGroups[0].Tasks[0] = task alloc.Name = "Bar" - env, err := GetTaskEnv(nil, nil, task, alloc, testConfig(), "") + conf := testConfig() + allocDir := allocdir.NewAllocDir(testLogger(), filepath.Join(conf.AllocDir, alloc.ID)) + env, err := GetTaskEnv(allocDir.NewTaskDir(task.Name), nil, task, alloc, conf, "") if err != nil { t.Fatalf("GetTaskEnv() failed: %v", err) }