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

Replace "-" in port labels with "_" for env vars #2406

Merged
merged 3 commits into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~> 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>`.

This will make it a popped out bubble to draw attention. See yellow box: https://www.consul.io/docs/guides/outage.html

Up to you to include.

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