Skip to content

Commit

Permalink
Merge pull request #2406 from hashicorp/b-2405-dashes-in-env-vars
Browse files Browse the repository at this point in the history
Replace "-" in port labels with "_" for env vars
  • Loading branch information
schmichael committed Mar 7, 2017
2 parents be1b4c4 + 238a888 commit 0c52a9e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ BUG FIXES:
* client: Drivers log during fingerprinting [GH-2337]
* client: Fix race condition with deriving vault tokens [GH-2275]
* client: Fix remounting alloc dirs after reboots [GH-2391] [GH-2394]
* client: Replace `-` with `_` in environment variable names [GH-2406]
* client: Fix panic and deadlock during client restore state when prestart
fails [GH-2376]
* config: Fix Consul Config Merging/Copying [GH-2278]
Expand Down
8 changes: 8 additions & 0 deletions client/driver/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@ func (t *TaskEnvironment) Build() *TaskEnvironment {
t.TaskEnv[k] = v
}

// Clean keys (see #2405)
cleanedEnv := make(map[string]string, len(t.TaskEnv))
for k, v := range t.TaskEnv {
cleanedK := strings.Replace(k, "-", "_", -1)
cleanedEnv[cleanedK] = v
}
t.TaskEnv = cleanedEnv

return t
}

Expand Down
23 changes: 23 additions & 0 deletions client/driver/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,26 @@ func TestEnvironment_AppendHostEnvVars(t *testing.T) {
t.Fatalf("Didn't filter environment variable %q", skip)
}
}

// TestEnvironment_DashesInTaskName asserts dashes in port labels are properly
// converted to underscores in environment variables.
// See: https://github.com/hashicorp/nomad/issues/2405
func TestEnvironment_DashesInTaskName(t *testing.T) {
env := testTaskEnvironment()
env.SetNetworks([]*structs.NetworkResource{
{
Device: "eth0",
DynamicPorts: []structs.Port{
{
Label: "just-some-dashes",
Value: 9000,
},
},
},
})
env.Build()

if env.TaskEnv["NOMAD_PORT_just_some_dashes"] != "9000" {
t.Fatalf("Expected NOMAD_PORT_just_some_dashes=9000 in TaskEnv; found:\n%#v", env.TaskEnv)
}
}
2 changes: 1 addition & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ rm -rf pkg/*
mkdir -p bin/

targets=$TARGETS
if [[ ! -z TARGETS ]]; then
if [ -z TARGETS ]; then
if [[ $(uname) == "Linux" ]]; then
targets="linux_386 linux_amd64 linux_amd64-lxc linux_arm linux_arm64 windows_386 windows_amd64"
elif [[ $(uname) == "Darwin" ]]; then
Expand Down
12 changes: 8 additions & 4 deletions website/source/docs/runtime/environment.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ environment variables.
<td>`NOMAD_ADDR_<label>`</td>
<td>The IP:Port pair of the port with the given label</td>
</tr>
<tr>
<td>`NOMAD_ADDR_<task>_<label`></td>
<td>The allocated address, given as IP:Port for the given label of other tasks in the same group</td>
</tr>
<tr>
<td>`NOMAD_HOST_PORT_<label>`</td>
<td>The host port for the given label if the port is port mapped</td>
Expand All @@ -84,12 +88,12 @@ environment variables.
<td>`VAULT_TOKEN`</td>
<td>The task's Vault token. See [Vault Integration](/docs/vault-integration/index.html) for more details</td>
</tr>
<tr>
<td>`NOMAD_ADDR_<task>_<label`></td>
<td>The allocated address, given as IP:Port, for each task, port label</td>
</tr>
</table>

~> Port labels and task names will have any dashes `-` in their names replaced by
underscores `_` when they're used in environment variable names such as
`NOMAD_ADDR_<task>_<label>`.

## Task Identifiers

Nomad will pass both the allocation ID and name as well as the task and job's
Expand Down

0 comments on commit 0c52a9e

Please sign in to comment.