diff --git a/CHANGELOG.md b/CHANGELOG.md index 90e396c68..f8776288d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ IMPROVEMENTS: BUG FIXES: * consul: Fixed a bug where updating a task to include services would not work [[GH-9707](https://github.com/hashicorp/nomad/issues/9707)] + * consul: Fixed alloc address mode port advertisement to use the mapped `to` port value [[GH-9730](https://github.com/hashicorp/nomad/issues/9730)] * consul/connect: Fixed a bug where absent ingress envoy proxy configuration could panic client [[GH-9669](https://github.com/hashicorp/nomad/issues/9669)] * template: Fixed multiple issues in template src/dest and artifact dest interpolation [[GH-9671](https://github.com/hashicorp/nomad/issues/9671)] * template: Fixed a bug where dynamic secrets did not trigger the template `change_mode` after a client restart. [[GH-9636](https://github.com/hashicorp/nomad/issues/9636)] diff --git a/command/agent/consul/service_client.go b/command/agent/consul/service_client.go index 620fcd281..798e2e451 100644 --- a/command/agent/consul/service_client.go +++ b/command/agent/consul/service_client.go @@ -1547,7 +1547,7 @@ func getAddress(addrMode, portLabel string, networks structs.Networks, driverNet return driverNet.IP, port, nil - case "alloc": + case structs.AddressModeAlloc: if netStatus == nil { return "", 0, fmt.Errorf(`cannot use address_mode="alloc": no allocation network status reported`) } @@ -1559,6 +1559,10 @@ func getAddress(addrMode, portLabel string, networks structs.Networks, driverNet // If port is a label and is found then return it if port, ok := ports.Get(portLabel); ok { + // Use port.To value unless not set + if port.To > 0 { + return netStatus.Address, port.To, nil + } return netStatus.Address, port.Value, nil } diff --git a/command/agent/consul/unit_test.go b/command/agent/consul/unit_test.go index 29f1a1c81..01f2c003d 100644 --- a/command/agent/consul/unit_test.go +++ b/command/agent/consul/unit_test.go @@ -1632,6 +1632,24 @@ func TestGetAddress(t *testing.T) { Address: "172.26.0.1", }, ExpectedIP: "172.26.0.1", + ExpectedPort: 6379, + }, + { + Name: "Alloc no to value", + Mode: structs.AddressModeAlloc, + PortLabel: "db", + Ports: []structs.AllocatedPortMapping{ + { + Label: "db", + Value: 12345, + HostIP: HostIP, + }, + }, + Status: &structs.AllocNetworkStatus{ + InterfaceName: "eth0", + Address: "172.26.0.1", + }, + ExpectedIP: "172.26.0.1", ExpectedPort: 12345, }, { diff --git a/website/content/docs/job-specification/service.mdx b/website/content/docs/job-specification/service.mdx index c8af46898..b55752b63 100644 --- a/website/content/docs/job-specification/service.mdx +++ b/website/content/docs/job-specification/service.mdx @@ -106,9 +106,13 @@ Connect][connect] integration. service. The value of `port` depends on which [`address_mode`](#address_mode) is being used: + - `alloc` - Advertise the mapped `to` value of the labeled port and the allocation address. + If a `to` value is not set, the port falls back to using the allocated host port. The `port` + field may be a numeric port or a port label specified in the same group's network stanza. + - `driver` - Advertise the port determined by the driver (e.g. Docker or rkt). The `port` may be a numeric port or a port label specified in the driver's - `port_map`. + `ports` field. - `host` - Advertise the host port for this service. `port` must match a port _label_ specified in the [`network`][network] stanza.