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.