From f9db34e0508d8d24818c1b85b9832d635df59079 Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Thu, 8 Jul 2021 16:03:15 -0400 Subject: [PATCH] client: interpolate meta blocks with task environment (#10876) Adds missing interpolation step to the `meta` blocks when building the task environment. Also fixes incorrect parameter order in the test assertion and adds diagnostics to the test. --- .changelog/10876.txt | 3 +++ client/taskenv/env.go | 4 ++-- client/taskenv/env_test.go | 8 +++++++- 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 .changelog/10876.txt diff --git a/.changelog/10876.txt b/.changelog/10876.txt new file mode 100644 index 00000000000..0b8e1619512 --- /dev/null +++ b/.changelog/10876.txt @@ -0,0 +1,3 @@ +```release-note:bug +client: Fixed bug where meta blocks were not interpolated with task environment +``` diff --git a/client/taskenv/env.go b/client/taskenv/env.go index 3ec32db1759..b4d5419f7d9 100644 --- a/client/taskenv/env.go +++ b/client/taskenv/env.go @@ -539,9 +539,9 @@ func (b *Builder) buildEnv(allocDir, localDir, secretsDir string, envMap[VaultNamespace] = b.vaultNamespace } - // Copy task meta + // Copy and interpolate task meta for k, v := range b.taskMeta { - envMap[k] = v + envMap[hargs.ReplaceEnv(k, nodeAttrs, envMap)] = hargs.ReplaceEnv(v, nodeAttrs, envMap) } // Interpolate and add environment variables diff --git a/client/taskenv/env_test.go b/client/taskenv/env_test.go index da9edc18e7c..3584596e902 100644 --- a/client/taskenv/env_test.go +++ b/client/taskenv/env_test.go @@ -399,6 +399,10 @@ func TestEnvironment_AllValues(t *testing.T) { "b.": "b", ".": "c", } + task.Meta = map[string]string{ + "taskMetaKey-${NOMAD_TASK_NAME}": "taskMetaVal-${node.unique.id}", + "foo": "bar", + } env := NewBuilder(n, a, task, "global").SetDriverNetwork( &drivers.DriverNetwork{PortMap: map[string]int{"https": 443}}, ) @@ -469,6 +473,7 @@ func TestEnvironment_AllValues(t *testing.T) { "NOMAD_META_elb_check_type": "http", "NOMAD_META_foo": "bar", "NOMAD_META_owner": "armon", + "NOMAD_META_taskMetaKey_web": "taskMetaVal-" + n.ID, "NOMAD_JOB_ID": a.Job.ID, "NOMAD_JOB_NAME": "my-job", "NOMAD_JOB_PARENT_ID": a.Job.ParentID, @@ -513,7 +518,8 @@ func TestEnvironment_AllValues(t *testing.T) { out := "" diag = gohcl.DecodeExpression(expr, evalCtx, &out) require.Empty(t, diag) - require.Equal(t, out, expectedVal) + require.Equal(t, expectedVal, out, + fmt.Sprintf("expected %q got %q", expectedVal, out)) }) } }