Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: task env vars should take precedence over host env vars. #11206

Merged
merged 2 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/11206.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
client: Task vars should take precedence over host vars when performing interpolation.
```
9 changes: 7 additions & 2 deletions client/taskenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions client/taskenv/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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",
Expand Down