Merge pull request #1099 from hashicorp/f-ip-port-var

Add IP and PORT environment variables
This commit is contained in:
Alex Dadgar 2016-04-15 11:33:16 -07:00
commit 3818d3013d
6 changed files with 62 additions and 17 deletions

View File

@ -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}},
},
},
@ -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",

View File

@ -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_"
@ -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 {

View File

@ -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",
@ -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",
@ -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)

View File

@ -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>
@ -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

View File

@ -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>
@ -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>
@ -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>

View File

@ -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>