Skip to content

Commit

Permalink
Add IP and PORT environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Apr 15, 2016
1 parent fb5fa2c commit 636c3fc
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 17 deletions.
12 changes: 9 additions & 3 deletions client/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestDriver_GetTaskEnv(t *testing.T) {
Networks: []*structs.NetworkResource{
&structs.NetworkResource{
IP: "1.2.3.4",
ReservedPorts: []structs.Port{{"one", 80}, {"two", 443}, {"three", 8080}, {"four", 12345}},
ReservedPorts: []structs.Port{{"one", 80}, {"two", 443}},
DynamicPorts: []structs.Port{{"admin", 8081}, {"web", 8086}},
},
},
Expand All @@ -127,11 +127,17 @@ func TestDriver_GetTaskEnv(t *testing.T) {
"NOMAD_CPU_LIMIT": "1000",
"NOMAD_MEMORY_LIMIT": "500",
"NOMAD_ADDR_one": "1.2.3.4:80",
"NOMAD_IP_one": "1.2.3.4",
"NOMAD_PORT_one": "80",
"NOMAD_ADDR_two": "1.2.3.4:443",
"NOMAD_ADDR_three": "1.2.3.4:8080",
"NOMAD_ADDR_four": "1.2.3.4:12345",
"NOMAD_IP_two": "1.2.3.4",
"NOMAD_PORT_two": "443",
"NOMAD_ADDR_admin": "1.2.3.4:8081",
"NOMAD_IP_admin": "1.2.3.4",
"NOMAD_PORT_admin": "8081",
"NOMAD_ADDR_web": "1.2.3.4:8086",
"NOMAD_IP_web": "1.2.3.4",
"NOMAD_PORT_web": "8086",
"NOMAD_META_CHOCOLATE": "cake",
"NOMAD_META_STRAWBERRY": "icecream",
"NOMAD_META_ELB_CHECK_INTERVAL": "30s",
Expand Down
10 changes: 9 additions & 1 deletion client/driver/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ const (

// AddrPrefix is the prefix for passing both dynamic and static port
// allocations to tasks.
// E.g. $NOMAD_IP_1=127.0.0.1:1 or $NOMAD_IP_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.
IpPrefix = "NOMAD_IP_"

// PortPrefix is the prefix for passing the port allocation to a task.
PortPrefix = "NOMAD_PORT_"

// HostPortPrefix is the prefix for passing the host port when a portmap is
// specified.
HostPortPrefix = "NOMAD_HOST_PORT_"
Expand Down Expand Up @@ -130,6 +136,8 @@ func (t *TaskEnvironment) Build() *TaskEnvironment {
for label, value := range network.MapLabelToValues(t.PortMap) {
IPPort := fmt.Sprintf("%s:%d", network.IP, value)
t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort
t.TaskEnv[fmt.Sprintf("%s%s", IpPrefix, label)] = network.IP
t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = fmt.Sprintf("%d", value)

// Pass an explicit port mapping to the environment
if port, ok := t.PortMap[label]; ok {
Expand Down
12 changes: 12 additions & 0 deletions client/driver/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ func TestEnvironment_AsList(t *testing.T) {
act := env.EnvList()
exp := []string{
"NOMAD_ADDR_http=127.0.0.1:80",
"NOMAD_PORT_http=80",
"NOMAD_IP_http=127.0.0.1",
"NOMAD_ADDR_https=127.0.0.1:443",
"NOMAD_PORT_https=443",
"NOMAD_IP_https=127.0.0.1",
"NOMAD_HOST_PORT_https=443",
"NOMAD_META_FOO=baz",
"NOMAD_META_BAZ=bam",
Expand All @@ -168,7 +172,11 @@ func TestEnvironment_ClearEnvvars(t *testing.T) {
act := env.EnvList()
exp := []string{
"NOMAD_ADDR_http=127.0.0.1:80",
"NOMAD_PORT_http=80",
"NOMAD_IP_http=127.0.0.1",
"NOMAD_ADDR_https=127.0.0.1:443",
"NOMAD_PORT_https=443",
"NOMAD_IP_https=127.0.0.1",
"NOMAD_HOST_PORT_https=443",
"bar=bang",
"foo=baz",
Expand All @@ -185,7 +193,11 @@ func TestEnvironment_ClearEnvvars(t *testing.T) {
act = env.EnvList()
exp = []string{
"NOMAD_ADDR_http=127.0.0.1:80",
"NOMAD_PORT_http=80",
"NOMAD_IP_http=127.0.0.1",
"NOMAD_ADDR_https=127.0.0.1:443",
"NOMAD_PORT_https=443",
"NOMAD_IP_https=127.0.0.1",
"NOMAD_HOST_PORT_https=443",
}
sort.Strings(act)
Expand Down
16 changes: 12 additions & 4 deletions website/source/docs/jobspec/environment.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,23 @@ environment variables.
<td>The task's name</td>
</tr>
<tr>
<td>NOMAD_ADDR_\<label\></td>
<td>NOMAD_IP_"label"</td>
<td>The IP of the the port with the given label</td>
</tr>
<tr>
<td>NOMAD_PORT_"label"</td>
<td>The port value with the given label</td>
</tr>
<tr>
<td>NOMAD_ADDR_"label"</td>
<td>The IP:Port pair of the the port with the given label</td>
</tr>
<tr>
<td>NOMAD_HOST_PORT_\<label\></td>
<td>NOMAD_HOST_PORT_"label"</td>
<td>The host port for the given label if the port is port mapped</td>
</tr>
<tr>
<td>NOMAD_META_\<key\></td>
<td>NOMAD_META_"key"</td>
<td>The metadata of the task</td>
</tr>
</table>
Expand Down Expand Up @@ -128,7 +136,7 @@ directories can be read through the following environment variables:
The job specification also allows you to specify a `meta` block to supply arbitrary
configuration to a task. This allows you to easily provide job-specific
configuration even if you use the same executable unit in multiple jobs. These
key-value pairs are passed through to the job as `NOMAD_META_{KEY}={value}`,
key-value pairs are passed through to the job as `NOMAD_META_"key"={value}`,
where `key` is UPPERCASED from the job specification.

Currently there is no enforcement that the meta values be lowercase, but using
Expand Down
18 changes: 14 additions & 4 deletions website/source/docs/jobspec/interpreted.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ driver.
<td>linux-64bit</td>
</tr>
<tr>
<td>${attr.\<key\>}</td>
<td>${attr."key"}</td>
<td>The attribute given by `key` on the client node.</td>
<td>platform.aws.instance-type:r3.large</td>
</tr>
<tr>
<td>${meta.\<key\>}</td>
<td>${meta."key"}</td>
<td>The metadata value given by `key` on the client node.</td>
<td></td>
</tr>
Expand All @@ -119,7 +119,7 @@ Below is a table documenting common node attributes:
<td>Number of CPU cores on the client</td>
</tr>
<tr>
<td>driver.\<key\></td>
<td>driver."key"</td>
<td>See the [task drivers](/docs/drivers/index.html) for attribute documentation</td>
</tr>
<tr>
Expand Down Expand Up @@ -200,7 +200,17 @@ a particular node and as such can not be used in constraints.
<td>The task's name</td>
</tr>
<tr>
<td>${NOMAD_ADDR_"label"}></td>
<td>${NOMAD_IP_"label"}</td>
<td>The IP for the given port `label`. See
[here](/docs/jobspec/networking.html) for more information.</td>
</tr>
<tr>
<td>${NOMAD_PORT_"label"}</td>
<td>The port for the port `label`. See [here](/docs/jobspec/networking.html)
for more information.</td>
</tr>
<tr>
<td>${NOMAD_ADDR_"label"}</td>
<td>The `ip:port` pair for the given port `label`. See
[here](/docs/jobspec/networking.html) for more information.</td>
</tr>
Expand Down
11 changes: 6 additions & 5 deletions website/source/docs/jobspec/networking.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ port `http`:
port "http" {}
```

When the task is started, it is passed an environment variable named
`NOMAD_ADDR_http` which indicates a combination of the interface IP and port.
When the task is started, it is passed the following environment variables:

```
NOMAD_ADDR_http=127.0.0.1:53423 ./start-command
```
* `NOMAD_IP_http` - The IP to bind on for the given port label.

* `NOMAD_PORT_http` - The port value for the given port label.

* `NOMAD_ADDR_http` - A combined `IP:Port` that can be used for convenience.

### Mapped Ports <a id="mapped_ports"></a>

Expand Down

0 comments on commit 636c3fc

Please sign in to comment.