From a9e3f2cf3edd4963ee8d0aa6ed7c4d5242cb9a27 Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Mon, 6 Mar 2017 17:09:44 -0800 Subject: [PATCH 1/3] Replace "-" in env var keys with "_" Fixes #2405 --- client/driver/env/env.go | 8 ++++++++ client/driver/env/env_test.go | 23 +++++++++++++++++++++++ scripts/build.sh | 2 +- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 56f1cf322da..d496209bda3 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -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 } diff --git a/client/driver/env/env_test.go b/client/driver/env/env_test.go index ad0ded87df1..60a441cdea9 100644 --- a/client/driver/env/env_test.go +++ b/client/driver/env/env_test.go @@ -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) + } +} diff --git a/scripts/build.sh b/scripts/build.sh index c01ea0ab799..fb8aaf67218 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -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 From e0e539c26a55fc543749f0c6c7d689e65d117999 Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Mon, 6 Mar 2017 20:04:41 -0800 Subject: [PATCH 2/3] Add documentation and changelog entry --- CHANGELOG.md | 1 + website/source/docs/runtime/environment.html.md | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6de5e8653d..2211cced374 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/website/source/docs/runtime/environment.html.md b/website/source/docs/runtime/environment.html.md index 88bdd42670f..04ea1e4e8f4 100644 --- a/website/source/docs/runtime/environment.html.md +++ b/website/source/docs/runtime/environment.html.md @@ -72,6 +72,10 @@ environment variables. `NOMAD_ADDR_