Skip to content

Commit

Permalink
Merge pull request #6080 from lchayoun/bug-6079
Browse files Browse the repository at this point in the history
Allow dash in environment variable names
  • Loading branch information
Mahmood Ali committed Sep 11, 2019
2 parents 97448cf + 4244265 commit 8177d45
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
19 changes: 8 additions & 11 deletions client/taskenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,22 +490,19 @@ func (b *Builder) Build() *TaskEnv {
}

// Clean keys (see #2405)
prefixesToClean := [...]string{AddrPrefix, IpPrefix, PortPrefix, HostPortPrefix, MetaPrefix}
cleanedEnv := make(map[string]string, len(envMap))
for k, v := range envMap {
cleanedK := helper.CleanEnvVar(k, '_')
cleanedEnv[cleanedK] = v
}

var cleanedDeviceEnvs map[string]string
if deviceEnvs != nil {
cleanedDeviceEnvs = make(map[string]string, len(deviceEnvs))
for k, v := range deviceEnvs {
cleanedK := helper.CleanEnvVar(k, '_')
cleanedDeviceEnvs[cleanedK] = v
cleanedK := k
for i := range prefixesToClean {
if strings.HasPrefix(k, prefixesToClean[i]) {
cleanedK = helper.CleanEnvVar(k, '_')
}
}
cleanedEnv[cleanedK] = v
}

return NewTaskEnv(cleanedEnv, cleanedDeviceEnvs, nodeAttrs)
return NewTaskEnv(cleanedEnv, deviceEnvs, nodeAttrs)
}

// Update task updates the environment based on a new alloc and task.
Expand Down
12 changes: 9 additions & 3 deletions client/taskenv/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,11 +660,17 @@ func TestEnvironment_AppendHostEnvvars(t *testing.T) {
func TestEnvironment_DashesInTaskName(t *testing.T) {
a := mock.Alloc()
task := a.Job.TaskGroups[0].Tasks[0]
task.Env = map[string]string{"test-one-two": "three-four"}
task.Env = map[string]string{
"test-one-two": "three-four",
"NOMAD_test_one_two": "three-five",
}
envMap := NewBuilder(mock.Node(), a, task, "global").Build().Map()

if envMap["test_one_two"] != "three-four" {
t.Fatalf("Expected test_one_two=three-four in TaskEnv; found:\n%#v", envMap)
if envMap["test-one-two"] != "three-four" {
t.Fatalf("Expected test-one-two=three-four in TaskEnv; found:\n%#v", envMap)
}
if envMap["NOMAD_test_one_two"] != "three-five" {
t.Fatalf("Expected NOMAD_test_one_two=three-five in TaskEnv; found:\n%#v", envMap)
}
}

Expand Down

0 comments on commit 8177d45

Please sign in to comment.