diff --git a/client/driver/driver_test.go b/client/driver/driver_test.go index 66f75b113..78ef98a0d 100644 --- a/client/driver/driver_test.go +++ b/client/driver/driver_test.go @@ -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", diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 7f414d886..ac98a3fca 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -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 { diff --git a/client/driver/env/env_test.go b/client/driver/env/env_test.go index de831d573..2ea76f394 100644 --- a/client/driver/env/env_test.go +++ b/client/driver/env/env_test.go @@ -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) diff --git a/website/source/docs/jobspec/environment.html.md b/website/source/docs/jobspec/environment.html.md index d649fb9bc..8c1004d39 100644 --- a/website/source/docs/jobspec/environment.html.md +++ b/website/source/docs/jobspec/environment.html.md @@ -53,15 +53,23 @@ environment variables. The task's name - NOMAD_ADDR_\ + NOMAD_IP_"label" + The IP of the the port with the given label + + + NOMAD_PORT_"label" + The port value with the given label + + + NOMAD_ADDR_"label" The IP:Port pair of the the port with the given label - NOMAD_HOST_PORT_\ + NOMAD_HOST_PORT_"label" The host port for the given label if the port is port mapped - NOMAD_META_\ + NOMAD_META_"key" The metadata of the task @@ -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 diff --git a/website/source/docs/jobspec/interpreted.html.md b/website/source/docs/jobspec/interpreted.html.md index a912784e8..db14725ed 100644 --- a/website/source/docs/jobspec/interpreted.html.md +++ b/website/source/docs/jobspec/interpreted.html.md @@ -88,12 +88,12 @@ driver. linux-64bit - ${attr.\} + ${attr."key"} The attribute given by `key` on the client node. platform.aws.instance-type:r3.large - ${meta.\} + ${meta."key"} The metadata value given by `key` on the client node. @@ -119,7 +119,7 @@ Below is a table documenting common node attributes: Number of CPU cores on the client - driver.\ + driver."key" See the [task drivers](/docs/drivers/index.html) for attribute documentation @@ -200,7 +200,17 @@ a particular node and as such can not be used in constraints. The task's name - ${NOMAD_ADDR_"label"}> + ${NOMAD_IP_"label"} + The IP for the given port `label`. See + [here](/docs/jobspec/networking.html) for more information. + + + ${NOMAD_PORT_"label"} + The port for the port `label`. See [here](/docs/jobspec/networking.html) + for more information. + + + ${NOMAD_ADDR_"label"} The `ip:port` pair for the given port `label`. See [here](/docs/jobspec/networking.html) for more information. diff --git a/website/source/docs/jobspec/networking.html.md b/website/source/docs/jobspec/networking.html.md index d9888e0f6..d134b9131 100644 --- a/website/source/docs/jobspec/networking.html.md +++ b/website/source/docs/jobspec/networking.html.md @@ -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