Rename IPPort env variable and add a mapped host port to the env.

This commit is contained in:
Ivo Verberk 2016-01-25 20:46:01 +01:00
parent e58bda9056
commit 061445468b
5 changed files with 30 additions and 33 deletions

View file

@ -341,12 +341,6 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, dri
d.logger.Printf("[DEBUG] driver.docker: exposed port %s", containerPort) d.logger.Printf("[DEBUG] driver.docker: exposed port %s", containerPort)
} }
// This was set above in a call to GetTaskEnv but if we
// have mapped any ports we will need to override them.
//
// TODO refactor the implementation in GetTaskEnv to match
// the 0.2 ports world view. Docker seems to be the only place where
// this is actually needed, but this is kinda hacky.
d.taskEnv.SetPortMap(driverConfig.PortMap) d.taskEnv.SetPortMap(driverConfig.PortMap)
hostConfig.PortBindings = publishedPorts hostConfig.PortBindings = publishedPorts

View file

@ -551,8 +551,8 @@ func TestDockerPortsNoMap(t *testing.T) {
} }
expectedEnvironment := map[string]string{ expectedEnvironment := map[string]string{
"NOMAD_IP_main": fmt.Sprintf("127.0.0.1:%d", res), "NOMAD_ADDR_main": fmt.Sprintf("127.0.0.1:%d", res),
"NOMAD_IP_REDIS": fmt.Sprintf("127.0.0.1:%d", dyn), "NOMAD_ADDR_REDIS": fmt.Sprintf("127.0.0.1:%d", dyn),
} }
for key, val := range expectedEnvironment { for key, val := range expectedEnvironment {
@ -606,8 +606,9 @@ func TestDockerPortsMapping(t *testing.T) {
} }
expectedEnvironment := map[string]string{ expectedEnvironment := map[string]string{
"NOMAD_IP_main": "127.0.0.1:8080", "NOMAD_ADDR_main": "127.0.0.1:8080",
"NOMAD_IP_REDIS": "127.0.0.1:6379", "NOMAD_ADDR_REDIS": "127.0.0.1:6379",
"NOMAD_HOST_PORT_main": "8080",
} }
for key, val := range expectedEnvironment { for key, val := range expectedEnvironment {

View file

@ -113,13 +113,12 @@ func TestDriver_GetTaskEnv(t *testing.T) {
exp := map[string]string{ exp := map[string]string{
"NOMAD_CPU_LIMIT": "1000", "NOMAD_CPU_LIMIT": "1000",
"NOMAD_MEMORY_LIMIT": "500", "NOMAD_MEMORY_LIMIT": "500",
"NOMAD_IP": "1.2.3.4", "NOMAD_ADDR_one": "1.2.3.4:80",
"NOMAD_IP_one": "1.2.3.4:80", "NOMAD_ADDR_two": "1.2.3.4:443",
"NOMAD_IP_two": "1.2.3.4:443", "NOMAD_ADDR_three": "1.2.3.4:8080",
"NOMAD_IP_three": "1.2.3.4:8080", "NOMAD_ADDR_four": "1.2.3.4:12345",
"NOMAD_IP_four": "1.2.3.4:12345", "NOMAD_ADDR_admin": "1.2.3.4:8081",
"NOMAD_IP_admin": "1.2.3.4:8081", "NOMAD_ADDR_web": "1.2.3.4:8086",
"NOMAD_IP_web": "1.2.3.4:8086",
"NOMAD_META_CHOCOLATE": "cake", "NOMAD_META_CHOCOLATE": "cake",
"NOMAD_META_STRAWBERRY": "icecream", "NOMAD_META_STRAWBERRY": "icecream",
"HELLO": "world", "HELLO": "world",

View file

@ -31,7 +31,10 @@ const (
// Prefix for passing both dynamic and static port allocations to // Prefix for passing both dynamic and static port allocations to
// tasks. // tasks.
// E.g. $NOMAD_IP_1=127.0.0.1:1 or $NOMAD_IP_http=127.0.0.1:80 // E.g. $NOMAD_IP_1=127.0.0.1:1 or $NOMAD_IP_http=127.0.0.1:80
IPPortPrefix = "NOMAD_IP_" AddrPrefix = "NOMAD_ADDR_"
// Prefix for passing the host port when a portmap is specified.
HostPortPrefix = "NOMAD_HOST_PORT_"
// Prefix for passing task meta data. // Prefix for passing task meta data.
MetaPrefix = "NOMAD_META_" MetaPrefix = "NOMAD_META_"
@ -106,7 +109,12 @@ func (t *TaskEnvironment) Build() *TaskEnvironment {
for _, network := range t.networks { for _, network := range t.networks {
for label, value := range network.MapLabelToValues(t.portMap) { for label, value := range network.MapLabelToValues(t.portMap) {
IPPort := fmt.Sprintf("%s:%d", network.IP, value) IPPort := fmt.Sprintf("%s:%d", network.IP, value)
t.taskEnv[fmt.Sprintf("%s%s", IPPortPrefix, label)] = IPPort t.taskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort
// 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)
}
} }
} }
@ -126,11 +134,6 @@ func (t *TaskEnvironment) Build() *TaskEnvironment {
t.taskEnv[CpuLimit] = strconv.Itoa(t.cpuLimit) t.taskEnv[CpuLimit] = strconv.Itoa(t.cpuLimit)
} }
// Build the IP
if len(t.networks) > 0 {
t.taskEnv[TaskIP] = t.networks[0].IP
}
// Build the node // Build the node
if t.node != nil { if t.node != nil {
// Set up the node values. // Set up the node values.

View file

@ -142,9 +142,9 @@ func TestEnvironment_AsList(t *testing.T) {
act := env.EnvList() act := env.EnvList()
exp := []string{ exp := []string{
"NOMAD_IP=127.0.0.1", "NOMAD_ADDR_http=127.0.0.1:80",
"NOMAD_IP_http=127.0.0.1:80", "NOMAD_ADDR_https=127.0.0.1:443",
"NOMAD_IP_https=127.0.0.1:443", "NOMAD_HOST_PORT_https=443",
"NOMAD_META_FOO=baz", "NOMAD_META_FOO=baz",
} }
sort.Strings(act) sort.Strings(act)
@ -163,9 +163,9 @@ func TestEnvironment_ClearEnvvars(t *testing.T) {
act := env.EnvList() act := env.EnvList()
exp := []string{ exp := []string{
"NOMAD_IP=127.0.0.1", "NOMAD_ADDR_http=127.0.0.1:80",
"NOMAD_IP_http=127.0.0.1:80", "NOMAD_ADDR_https=127.0.0.1:443",
"NOMAD_IP_https=127.0.0.1:443", "NOMAD_HOST_PORT_https=443",
"bar=bang", "bar=bang",
"foo=baz", "foo=baz",
} }
@ -180,9 +180,9 @@ func TestEnvironment_ClearEnvvars(t *testing.T) {
act = env.EnvList() act = env.EnvList()
exp = []string{ exp = []string{
"NOMAD_IP=127.0.0.1", "NOMAD_ADDR_http=127.0.0.1:80",
"NOMAD_IP_http=127.0.0.1:80", "NOMAD_ADDR_https=127.0.0.1:443",
"NOMAD_IP_https=127.0.0.1:443", "NOMAD_HOST_PORT_https=443",
} }
sort.Strings(act) sort.Strings(act)
sort.Strings(exp) sort.Strings(exp)