Skip to content

Commit

Permalink
Merge pull request #2223 from hashicorp/f-env-addr
Browse files Browse the repository at this point in the history
Setting the Addrs of sibling tasks in the env
  • Loading branch information
diptanu committed Jan 23, 2017
2 parents 49a8ce1 + bfb01d9 commit af66ea3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
24 changes: 24 additions & 0 deletions client/driver/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type TaskEnvironment struct {
VaultToken string
InjectVaultToken bool
JobName string
Alloc *structs.Allocation

// taskEnv is the variables that will be set in the tasks environment
TaskEnv map[string]string
Expand Down Expand Up @@ -193,6 +194,28 @@ func (t *TaskEnvironment) Build() *TaskEnvironment {
t.TaskEnv[JobName] = t.JobName
}

// Build the addr of the other tasks
if t.Alloc != nil {
for taskName, resources := range t.Alloc.TaskResources {
if taskName == t.TaskName {
continue
}
for _, nw := range resources.Networks {
ports := make([]*structs.Port, 0, len(nw.ReservedPorts)+len(nw.DynamicPorts))
for _, port := range nw.ReservedPorts {
ports = append(ports, &port)
}
for _, port := range nw.DynamicPorts {
ports = append(ports, &port)
}
for _, p := range ports {
key := fmt.Sprintf("%s%s_%s", AddrPrefix, taskName, p.Label)
t.TaskEnv[key] = fmt.Sprintf("%s:%d", nw.IP, p.Value)
}
}
}
}

// Build the node
if t.Node != nil {
// Set up the node values.
Expand Down Expand Up @@ -393,6 +416,7 @@ func (t *TaskEnvironment) SetAlloc(alloc *structs.Allocation) *TaskEnvironment {
t.AllocId = alloc.ID
t.AllocName = alloc.Name
t.AllocIndex = alloc.Index()
t.Alloc = alloc
return t
}

Expand Down
12 changes: 10 additions & 2 deletions client/driver/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,13 @@ func TestEnvironment_ReplaceEnv_Mixed(t *testing.T) {

func TestEnvironment_AsList(t *testing.T) {
n := mock.Node()
a := mock.Alloc()
env := NewTaskEnvironment(n).
SetNetworks(networks).
SetPortMap(portMap).
SetTaskMeta(map[string]string{"foo": "baz"}).Build()
SetTaskMeta(map[string]string{"foo": "baz"}).
SetAlloc(a).
SetTaskName("taskA").Build()

act := env.EnvList()
exp := []string{
Expand All @@ -153,11 +156,16 @@ func TestEnvironment_AsList(t *testing.T) {
"NOMAD_HOST_PORT_http=80",
"NOMAD_HOST_PORT_https=8080",
"NOMAD_META_FOO=baz",
"NOMAD_ADDR_web_main=192.168.0.100:5000",
"NOMAD_ADDR_web_http=192.168.0.100:2000",
"NOMAD_TASK_NAME=taskA",
}
allocID := fmt.Sprintf("NOMAD_ALLOC_ID=%s", a.ID)
exp = append(exp, allocID)
sort.Strings(act)
sort.Strings(exp)
if !reflect.DeepEqual(act, exp) {
t.Fatalf("env.List() returned %v; want %v", act, exp)
t.Fatalf("env.List() returned %v;\n want %v", act, exp)
}
}

Expand Down
2 changes: 1 addition & 1 deletion nomad/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func Alloc() *structs.Allocation {
IP: "192.168.0.100",
ReservedPorts: []structs.Port{{Label: "main", Value: 5000}},
MBits: 50,
DynamicPorts: []structs.Port{{Label: "http"}},
DynamicPorts: []structs.Port{{Label: "http", Value: 2000}},
},
},
},
Expand Down
4 changes: 4 additions & 0 deletions website/source/docs/runtime/environment.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ 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>

## Task Identifiers
Expand Down

0 comments on commit af66ea3

Please sign in to comment.