From 1784d98182b12f1c1b041c16dd0e19cd61113875 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Fri, 8 Jul 2016 15:37:44 -0700 Subject: [PATCH 1/3] Fixed the host port environment variable --- client/driver/env/env.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 460c8e22e..9fd40c4b3 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -133,15 +133,13 @@ func (t *TaskEnvironment) Build() *TaskEnvironment { // Build the ports for _, network := range t.Networks { - 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 + for label, value := range network.MapLabelToValues(nil) { 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 { - t.TaskEnv[fmt.Sprintf("%s%s", HostPortPrefix, label)] = strconv.Itoa(port) + t.TaskEnv[fmt.Sprintf("%s%s", HostPortPrefix, label)] = strconv.Itoa(value) + if value, ok := t.PortMap[label]; ok { + t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = fmt.Sprintf("%d", value) + IPPort := fmt.Sprintf("%s:%d", network.IP, value) + t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort } } } From 3c4002c48b89a4ab747d3fd39f00d4ef97c3ab91 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Fri, 8 Jul 2016 17:42:34 -0700 Subject: [PATCH 2/3] Fixed the client tests --- client/driver/env/env.go | 10 ++++++---- client/driver/env/env_test.go | 9 ++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 9fd40c4b3..1ac9b7510 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -136,11 +136,13 @@ func (t *TaskEnvironment) Build() *TaskEnvironment { for label, value := range network.MapLabelToValues(nil) { t.TaskEnv[fmt.Sprintf("%s%s", IpPrefix, label)] = network.IP t.TaskEnv[fmt.Sprintf("%s%s", HostPortPrefix, label)] = strconv.Itoa(value) - if value, ok := t.PortMap[label]; ok { - t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = fmt.Sprintf("%d", value) - IPPort := fmt.Sprintf("%s:%d", network.IP, value) - t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort + if forwardedPort, ok := t.PortMap[label]; ok { + value = forwardedPort } + t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = fmt.Sprintf("%d", value) + IPPort := fmt.Sprintf("%s:%d", network.IP, value) + t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort + } } diff --git a/client/driver/env/env_test.go b/client/driver/env/env_test.go index 2ea76f394..19c980ee1 100644 --- a/client/driver/env/env_test.go +++ b/client/driver/env/env_test.go @@ -151,7 +151,8 @@ func TestEnvironment_AsList(t *testing.T) { "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_HOST_PORT_http=80", + "NOMAD_HOST_PORT_https=8080", "NOMAD_META_FOO=baz", "NOMAD_META_BAZ=bam", } @@ -177,7 +178,8 @@ func TestEnvironment_ClearEnvvars(t *testing.T) { "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_HOST_PORT_http=80", + "NOMAD_HOST_PORT_https=8080", "bar=bang", "foo=baz", } @@ -198,7 +200,8 @@ func TestEnvironment_ClearEnvvars(t *testing.T) { "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_HOST_PORT_https=8080", + "NOMAD_HOST_PORT_http=80", } sort.Strings(act) sort.Strings(exp) From 5d61fa01f1326aabd0ed062ab4fc17d273048278 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Fri, 8 Jul 2016 18:27:51 -0700 Subject: [PATCH 3/3] Fixed tests --- client/driver/docker_test.go | 3 ++- client/driver/driver_test.go | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/client/driver/docker_test.go b/client/driver/docker_test.go index d5596fcb8..3229c0fd3 100644 --- a/client/driver/docker_test.go +++ b/client/driver/docker_test.go @@ -9,6 +9,7 @@ import ( "path/filepath" "reflect" "runtime/debug" + "strconv" "strings" "testing" "time" @@ -709,7 +710,7 @@ func TestDockerPortsMapping(t *testing.T) { expectedEnvironment := map[string]string{ "NOMAD_ADDR_main": "127.0.0.1:8080", "NOMAD_ADDR_REDIS": "127.0.0.1:6379", - "NOMAD_HOST_PORT_main": "8080", + "NOMAD_HOST_PORT_main": strconv.Itoa(docker_reserved), } for key, val := range expectedEnvironment { diff --git a/client/driver/driver_test.go b/client/driver/driver_test.go index 477c1832f..c8b37dda4 100644 --- a/client/driver/driver_test.go +++ b/client/driver/driver_test.go @@ -129,15 +129,19 @@ func TestDriver_GetTaskEnv(t *testing.T) { "NOMAD_ADDR_one": "1.2.3.4:80", "NOMAD_IP_one": "1.2.3.4", "NOMAD_PORT_one": "80", + "NOMAD_HOST_PORT_one": "80", "NOMAD_ADDR_two": "1.2.3.4:443", "NOMAD_IP_two": "1.2.3.4", "NOMAD_PORT_two": "443", + "NOMAD_HOST_PORT_two": "443", "NOMAD_ADDR_admin": "1.2.3.4:8081", "NOMAD_IP_admin": "1.2.3.4", "NOMAD_PORT_admin": "8081", + "NOMAD_HOST_PORT_admin": "8081", "NOMAD_ADDR_web": "1.2.3.4:8086", "NOMAD_IP_web": "1.2.3.4", "NOMAD_PORT_web": "8086", + "NOMAD_HOST_PORT_web": "8086", "NOMAD_META_CHOCOLATE": "cake", "NOMAD_META_STRAWBERRY": "icecream", "NOMAD_META_ELB_CHECK_INTERVAL": "30s",