client: expose group network ports in env vars

Fixes #6375

Intentionally omitted IPs prior to 0.10.0 release to minimize changes
and risk.
This commit is contained in:
Michael Schurter 2019-10-18 15:42:28 -07:00
parent a05cf13d48
commit 460bd63db0
3 changed files with 93 additions and 44 deletions

View File

@ -631,6 +631,17 @@ func (b *Builder) setAlloc(alloc *structs.Allocation) *Builder {
}
}
}
// Add ports from group networks
//TODO Expose IPs but possibly only via variable interpolation
for _, nw := range alloc.AllocatedResources.Shared.Networks {
for _, p := range nw.ReservedPorts {
addGroupPort(b.otherPorts, p)
}
for _, p := range nw.DynamicPorts {
addGroupPort(b.otherPorts, p)
}
}
} else if alloc.TaskResources != nil {
if tr, ok := alloc.TaskResources[b.taskName]; ok {
// Copy networks to prevent sharing
@ -849,3 +860,15 @@ func addPort(m map[string]string, taskName, ip, portLabel string, port int) {
key = fmt.Sprintf("%s%s_%s", PortPrefix, taskName, portLabel)
m[key] = strconv.Itoa(port)
}
// addGroupPort adds a group network port. The To value is used if one is
// specified.
func addGroupPort(m map[string]string, port structs.Port) {
if port.To > 0 {
m[PortPrefix+port.Label] = strconv.Itoa(port.To)
} else {
m[PortPrefix+port.Label] = strconv.Itoa(port.Value)
}
m[HostPortPrefix+port.Label] = strconv.Itoa(port.Value)
}

View File

@ -334,7 +334,7 @@ func TestEnvironment_AllValues(t *testing.T) {
"nested.meta.key": "a",
"invalid...metakey": "b",
}
a := mock.Alloc()
a := mock.ConnectAlloc()
a.AllocatedResources.Tasks["web"].Networks[0] = &structs.NetworkResource{
Device: "eth0",
IP: "127.0.0.1",
@ -355,6 +355,22 @@ func TestEnvironment_AllValues(t *testing.T) {
},
},
}
sharedNet := a.AllocatedResources.Shared.Networks[0]
// Add group network port with only a host port.
sharedNet.DynamicPorts = append(sharedNet.DynamicPorts, structs.Port{
Label: "hostonly",
Value: 9998,
})
// Add group network reserved port with a To value.
sharedNet.ReservedPorts = append(sharedNet.ReservedPorts, structs.Port{
Label: "static",
Value: 9997,
To: 97,
})
task := a.Job.TaskGroups[0].Tasks[0]
task.Env = map[string]string{
"taskEnvKey": "taskEnvVal",
@ -437,6 +453,12 @@ func TestEnvironment_AllValues(t *testing.T) {
"NOMAD_JOB_NAME": "my-job",
"NOMAD_ALLOC_ID": a.ID,
"NOMAD_ALLOC_INDEX": "0",
"NOMAD_PORT_connect_proxy_testconnect": "9999",
"NOMAD_HOST_PORT_connect_proxy_testconnect": "9999",
"NOMAD_PORT_hostonly": "9998",
"NOMAD_HOST_PORT_hostonly": "9998",
"NOMAD_PORT_static": "97",
"NOMAD_HOST_PORT_static": "9997",
// 0.9 style env map
`env["taskEnvKey"]`: "taskEnvVal",

View File

@ -76,19 +76,21 @@ job "docs" {
- `mode` `(string: "host")` - Mode of the network. The following modes are available:
- “none” - Task group will have an isolated network without any network interfaces.
- “bridge” - Task group will have an isolated network namespace with an interface
- `none` - Task group will have an isolated network without any network interfaces.
- `bridge` - Task group will have an isolated network namespace with an interface
that is bridged with the host. Note that bridge networking is only
currently supported for the `docker`, `exec`, `raw_exec`, and `java` task
drivers.
- “host” - Each task will join the host network namespace and a shared network
- `host` - Each task will join the host network namespace and a shared network
namespace is not created. This matches the current behavior in Nomad 0.9.
### `port` Parameters
- `static` `(int: nil)` - Specifies the static TCP/UDP port to allocate. If omitted, a dynamic port is chosen. We **do not recommend** using static ports, except
for `system` or specialized jobs like load balancers.
- `to` `(string:nil)` - Applicable when using "bridge" mode to configure port to map to inside the task's network namespace.
- `to` `(string:nil)` - Applicable when using "bridge" mode to configure port
to map to inside the task's network namespace. The `NOMAD_PORT_<label>`
environment variable will contain the `to` value.
The label assigned to the port is used to identify the port in service
discovery, and used in the name of the environment variable that indicates
@ -210,10 +212,12 @@ network {
}
```
### Limitations
* Only one `network` stanza can be specified, when it is defined at the task group level.
* Only the `NOMAD_PORT_<label>` and `NOMAD_HOST_PORT_<label>` environment
variables are set for group network ports.
[docker-driver]: /docs/drivers/docker.html "Nomad Docker Driver"
[qemu-driver]: /docs/drivers/qemu.html "Nomad QEMU Driver"
[Connect]: /docs/job-specification/connect.html "Nomad Consul Connect Integration"
### Limitations
Only one `network` stanza can be specified, when it is defined at the task group level.