diff --git a/client/serviceregistration/address.go b/client/serviceregistration/address.go index af4974d0b..9ddb47740 100644 --- a/client/serviceregistration/address.go +++ b/client/serviceregistration/address.go @@ -9,8 +9,9 @@ import ( ) // GetAddress returns the IP (or custom advertise address) and port to use for a -// service or check registration. If no port label is specified (an empty value), -// zero values are returned because no address could be resolved. +// service or check registration. If no port label is specified (an empty value) +// and no custom address is specified, zero values are returned because no address +// could be resolved. func GetAddress( address, // custom address, if set addressMode, @@ -30,7 +31,7 @@ func GetAddress( } // A custom advertise address can be used with a port map; using the // Value and ignoring the IP. The routing from your custom address to - // the group network address is DIY. + // the group network address is DIY. (e.g. EC2 public address) if mapping, exists := ports.Get(portLabel); exists { return address, mapping.Value, nil } diff --git a/client/taskenv/services_test.go b/client/taskenv/services_test.go index bc1ce6d46..07d7a3e52 100644 --- a/client/taskenv/services_test.go +++ b/client/taskenv/services_test.go @@ -26,6 +26,7 @@ func TestInterpolateServices(t *testing.T) { CanaryMeta: map[string]string{ "canarymeta-key": "${canarymeta}", }, + Address: "${address}", Checks: []*structs.ServiceCheck{ { Name: "${checkname}", @@ -51,6 +52,7 @@ func TestInterpolateServices(t *testing.T) { "portlabel": "portlabel", "tags": "tags", "meta": "meta-value", + "address": "example.com", "canarymeta": "canarymeta-value", "checkname": "checkname", "checktype": "checktype", @@ -80,6 +82,7 @@ func TestInterpolateServices(t *testing.T) { CanaryMeta: map[string]string{ "canarymeta-key": "canarymeta-value", }, + Address: "example.com", Checks: []*structs.ServiceCheck{ { Name: "checkname", diff --git a/nomad/structs/services.go b/nomad/structs/services.go index 8b6df3d9f..c0643e05d 100644 --- a/nomad/structs/services.go +++ b/nomad/structs/services.go @@ -49,7 +49,7 @@ type ServiceCheck struct { Protocol string // Protocol to use if check is http, defaults to http PortLabel string // The port to use for tcp/http checks Expose bool // Whether to have Envoy expose the check path (connect-enabled group-services only) - AddressMode string // Must be empty, "alloc", "host", or "driver' + AddressMode string // Must be empty, "alloc", "host", or "driver" Interval time.Duration // Interval of the check Timeout time.Duration // Timeout of the response from the check before consul fails the check InitialStatus string // Initial status of the check @@ -449,12 +449,12 @@ type Service struct { // address, specify an empty host in the PortLabel (e.g. `:port`). PortLabel string - // AddressMode specifies how the Advertise address used in service registration - // is determined. Must be "auto" (default), "host", "driver", or "alloc". + // AddressMode specifies how the address in service registration is + // determined. Must be "auto" (default), "host", "driver", or "alloc". AddressMode string - // Advertise enables explicitly setting and advertise address used in service - // registration. AddressMode must be "auto" if Advertise is set. + // Address enables explicitly setting a custom address to use in service + // registration. AddressMode must be "auto" if Address is set. Address string // EnableTagOverride will disable Consul's anti-entropy mechanism for the diff --git a/nomad/structs/services_test.go b/nomad/structs/services_test.go index 7917c3245..39a733613 100644 --- a/nomad/structs/services_test.go +++ b/nomad/structs/services_test.go @@ -247,7 +247,7 @@ func TestService_Hash(t *testing.T) { // these tests use tweaker to modify 1 field and make the false assertion // on comparing the resulting hash output - t.Run("mod advertise", func(t *testing.T) { + t.Run("mod address", func(t *testing.T) { try(t, func(s *svc) { s.Address = "example.com" }) }) diff --git a/website/content/docs/job-specification/service.mdx b/website/content/docs/job-specification/service.mdx index 600856e3c..e00338a9b 100644 --- a/website/content/docs/job-specification/service.mdx +++ b/website/content/docs/job-specification/service.mdx @@ -142,8 +142,9 @@ Connect][connect] integration. for more information. Only available where `provider = "consul"`. - `address` `(string: )` - Specifies a custom address to advertise in - Consul or Nomad service registration. Can be an IP address or domain name. If - set, `address_mode` must be in `auto` mode. + Consul or Nomad service registration. If set, `address_mode` must be in `auto` + mode. Useful with interpolation - for example to advertise the public IP address + of an AWS EC2 instance set this to `${attr.unique.platform.aws.public-ipv4}`. - `address_mode` `(string: "auto")` - Specifies which address (host, alloc or driver-specific) this service should advertise. See [below for