diff --git a/client/allocrunner/taskrunner/task_runner.go b/client/allocrunner/taskrunner/task_runner.go index 699245eb5b9..9ea342d621c 100644 --- a/client/allocrunner/taskrunner/task_runner.go +++ b/client/allocrunner/taskrunner/task_runner.go @@ -462,8 +462,29 @@ func (tr *TaskRunner) runDriver() error { // TODO(nickethier): make sure this uses alloc.AllocatedResources once #4750 is rebased taskConfig := tr.buildTaskConfig() + // Build hcl context variables + vars, errs, err := tr.envBuilder.Build().AllValues() + if err != nil { + return err + } + + // Handle per-key errors + if len(errs) > 0 { + keys := make([]string, 0, len(errs)) + for k, err := range errs { + keys = append(keys, k) + + if tr.logger.IsTrace() { + // Verbosely log every diagnostic for debugging + tr.logger.Trace("error building environment variables", "key", k, "error", err) + } + } + + tr.logger.Warn("some environment variables not available for rendering", "keys", strings.Join(keys, ", ")) + } + evalCtx := &hcl.EvalContext{ - Variables: tr.envBuilder.Build().AllValues(), + Variables: vars, Functions: shared.GetStdlibFuncs(), } diff --git a/client/allocrunner/taskrunner/task_runner_test.go b/client/allocrunner/taskrunner/task_runner_test.go index ce75341653a..41b196dcb0f 100644 --- a/client/allocrunner/taskrunner/task_runner_test.go +++ b/client/allocrunner/taskrunner/task_runner_test.go @@ -176,9 +176,8 @@ func TestTaskRunner_TaskEnv(t *testing.T) { // Use interpolation from both node attributes and meta vars task.Config = map[string]interface{}{ "run_for": time.Millisecond, - "stdout_string": `${node.region} ${NOMAD_META_foo}`, + "stdout_string": `${node.region} ${NOMAD_META_foo} ${NOMAD_META_common_user}`, } - task.User = "${NOMAD_META_common_user}" conf, cleanup := testTaskRunnerConfig(t, alloc, task.Name) defer cleanup() @@ -211,6 +210,5 @@ func TestTaskRunner_TaskEnv(t *testing.T) { driverCfg, mockCfg := mockDriver.GetTaskConfig() require.NotNil(driverCfg) require.NotNil(mockCfg) - assert.Equal(t, "somebody", driverCfg.User) - assert.Equal(t, "global bar", mockCfg.StdoutString) + assert.Equal(t, "global bar somebody", mockCfg.StdoutString) }