Skip to content

Commit

Permalink
Merge pull request #2426 from hashicorp/f-tg-port-ip-env-var
Browse files Browse the repository at this point in the history
Add NOMAD_{IP,PORT}_<task>_<label> env vars
  • Loading branch information
schmichael authored Mar 13, 2017
2 parents ef22127 + 9073b21 commit f443476
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ IMPROVEMENTS:
* client: Don't exec `uname -r` for node attribute kernel.version [GH-2380]
* client: Artifact support for git and hg as well as netrc support [GH-2386]
* client: Add metrics to show number of allocations on in each state [GH-2425]
* client: Add `NOMAD_{IP,PORT}_<task>_<label>` environment variables [GH-2426]
* client: Reproducible Node ID on OSes that provide system-level UUID
[GH-2277]
* driver/docker: Add support for volume drivers [GH-2351]
Expand Down
6 changes: 5 additions & 1 deletion client/driver/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const (

// AddrPrefix is the prefix for passing both dynamic and static port
// allocations to tasks.
// E.g$NOMAD_ADDR_http=127.0.0.1:80
// E.g $NOMAD_ADDR_http=127.0.0.1:80
AddrPrefix = "NOMAD_ADDR_"

// IpPrefix is the prefix for passing the IP of a port allocation to a task.
Expand Down Expand Up @@ -213,6 +213,10 @@ func (t *TaskEnvironment) Build() *TaskEnvironment {
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)
key = fmt.Sprintf("%s%s_%s", IpPrefix, taskName, p.Label)
t.TaskEnv[key] = nw.IP
key = fmt.Sprintf("%s%s_%s", PortPrefix, taskName, p.Label)
t.TaskEnv[key] = strconv.Itoa(p.Value)
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion client/driver/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,18 @@ func TestEnvironment_AsList(t *testing.T) {
"NOMAD_META_foo=baz",
"NOMAD_ADDR_web_main=192.168.0.100:5000",
"NOMAD_ADDR_web_http=192.168.0.100:2000",
"NOMAD_PORT_web_main=5000",
"NOMAD_PORT_web_http=2000",
"NOMAD_IP_web_main=192.168.0.100",
"NOMAD_IP_web_http=192.168.0.100",
"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;\n want %v", act, exp)
t.Fatalf("env.List() returned %v;\n want:\n%v", strings.Join(act, "\n"), strings.Join(exp, "\n"))
}
}

Expand Down
10 changes: 9 additions & 1 deletion website/source/docs/runtime/environment.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,17 @@ environment variables.
<td>The IP:Port pair of the port with the given label</td>
</tr>
<tr>
<td>`NOMAD_ADDR_<task>_<label`></td>
<td>`NOMAD_ADDR_<task>_<label>`</td>
<td>The allocated address, given as IP:Port for the given label of other tasks in the same group</td>
</tr>
<tr>
<td>`NOMAD_PORT_<task>_<label>`</td>
<td>The allocated port for the given label of other tasks in the same group</td>
</tr>
<tr>
<td>`NOMAD_IP_<task>_<label>`</td>
<td>The allocated IP address for the given label of other tasks in the same group</td>
</tr>
<tr>
<td>`NOMAD_HOST_PORT_<label>`</td>
<td>The host port for the given label if the port is port mapped</td>
Expand Down

0 comments on commit f443476

Please sign in to comment.