From fdc96d90109f1ed028b0690b92a6d09de9eff5ba Mon Sep 17 00:00:00 2001 From: James Rasell Date: Mon, 20 Sep 2021 09:15:28 +0100 Subject: [PATCH 1/2] client: task env vars should take precendece over host env vars. --- client/taskenv/env.go | 9 +++++++-- client/taskenv/env_test.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/client/taskenv/env.go b/client/taskenv/env.go index 248672589c89..06341cada309 100644 --- a/client/taskenv/env.go +++ b/client/taskenv/env.go @@ -551,9 +551,14 @@ func (b *Builder) buildEnv(allocDir, localDir, secretsDir string, envMap[hargs.ReplaceEnv(k, nodeAttrs, envMap)] = hargs.ReplaceEnv(v, nodeAttrs, envMap) } - // Interpolate and add environment variables + // Interpolate and add environment variables from the host. Only do this if + // the variable is not present in the map; we do not want to override task + // variables in favour of the same variable found within the host OS env + // vars. for k, v := range b.hostEnv { - envMap[k] = hargs.ReplaceEnv(v, nodeAttrs, envMap) + if _, ok := envMap[k]; !ok { + envMap[k] = hargs.ReplaceEnv(v, nodeAttrs, envMap) + } } // Copy interpolated task env vars second as they override host env vars diff --git a/client/taskenv/env_test.go b/client/taskenv/env_test.go index 1f9f6c496302..b4cde402975f 100644 --- a/client/taskenv/env_test.go +++ b/client/taskenv/env_test.go @@ -299,6 +299,17 @@ func TestEnvironment_AllValues(t *testing.T) { &drivers.DriverNetwork{PortMap: map[string]int{"https": 443}}, ) + // Add a host environment variable which matches a task variable. It means + // we can test to ensure the allocation ID variable from the task overrides + // that found on the host. The second entry tests to ensure other host env + // vars are added as expected. + env.mu.Lock() + env.hostEnv = map[string]string{ + AllocID: "94fa69a3-73a5-4099-85c3-7a1b6e228796", + "LC_CTYPE": "C.UTF-8", + } + env.mu.Unlock() + values, errs, err := env.Build().AllValues() require.NoError(t, err) @@ -385,6 +396,9 @@ func TestEnvironment_AllValues(t *testing.T) { "NOMAD_ALLOC_PORT_admin": "9000", "NOMAD_HOST_PORT_admin": "32000", + // Env vars from the host. + "LC_CTYPE": "C.UTF-8", + // 0.9 style env map `env["taskEnvKey"]`: "taskEnvVal", `env["NOMAD_ADDR_http"]`: "127.0.0.1:80", From 81faab7ceb46966602278a1a293269bb254ec18e Mon Sep 17 00:00:00 2001 From: James Rasell Date: Mon, 20 Sep 2021 18:05:42 +0100 Subject: [PATCH 2/2] changelog: add entry for #11206 --- .changelog/11206.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/11206.txt diff --git a/.changelog/11206.txt b/.changelog/11206.txt new file mode 100644 index 000000000000..081f96bb304c --- /dev/null +++ b/.changelog/11206.txt @@ -0,0 +1,3 @@ +```release-note:bug +client: Task vars should take precedence over host vars when performing interpolation. +```