From a5e77252e5a3ebb09f94f7d74b2386848c57086f Mon Sep 17 00:00:00 2001 From: Nick Wales Date: Wed, 9 Jan 2019 20:38:39 +0000 Subject: [PATCH 1/2] Adds optional Consul service tags to nomad server and agent services, gh#4297 --- command/agent/agent.go | 8 ++++---- command/agent/config_parse.go | 1 + nomad/structs/config/consul.go | 6 +++++- website/source/docs/configuration/consul.html.md | 3 +++ 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index a936282ad..c9fcbcfb6 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -580,7 +580,7 @@ func (a *Agent) setupServer() error { httpServ := &structs.Service{ Name: a.config.Consul.ServerServiceName, PortLabel: a.config.AdvertiseAddrs.HTTP, - Tags: []string{consul.ServiceTagHTTP}, + Tags: append(a.config.Consul.Tags, consul.ServiceTagHTTP), } const isServer = true if check := a.agentHTTPCheck(isServer); check != nil { @@ -589,7 +589,7 @@ func (a *Agent) setupServer() error { rpcServ := &structs.Service{ Name: a.config.Consul.ServerServiceName, PortLabel: a.config.AdvertiseAddrs.RPC, - Tags: []string{consul.ServiceTagRPC}, + Tags: append(a.config.Consul.Tags, consul.ServiceTagRPC), Checks: []*structs.ServiceCheck{ { Name: a.config.Consul.ServerRPCCheckName, @@ -603,7 +603,7 @@ func (a *Agent) setupServer() error { serfServ := &structs.Service{ Name: a.config.Consul.ServerServiceName, PortLabel: a.config.AdvertiseAddrs.Serf, - Tags: []string{consul.ServiceTagSerf}, + Tags: append(a.config.Consul.Tags, consul.ServiceTagSerf), Checks: []*structs.ServiceCheck{ { Name: a.config.Consul.ServerSerfCheckName, @@ -742,7 +742,7 @@ func (a *Agent) setupClient() error { httpServ := &structs.Service{ Name: a.config.Consul.ClientServiceName, PortLabel: a.config.AdvertiseAddrs.HTTP, - Tags: []string{consul.ServiceTagHTTP}, + Tags: append(a.config.Consul.Tags, consul.ServiceTagHTTP), } const isServer = false if check := a.agentHTTPCheck(isServer); check != nil { diff --git a/command/agent/config_parse.go b/command/agent/config_parse.go index 641a10c2c..2ce1d26c6 100644 --- a/command/agent/config_parse.go +++ b/command/agent/config_parse.go @@ -788,6 +788,7 @@ func parseConsulConfig(result **config.ConsulConfig, list *ast.ObjectList) error "server_serf_check_name", "server_rpc_check_name", "ssl", + "tags", "timeout", "token", "verify_ssl", diff --git a/nomad/structs/config/consul.go b/nomad/structs/config/consul.go index 9473ea814..e805cdd75 100644 --- a/nomad/structs/config/consul.go +++ b/nomad/structs/config/consul.go @@ -43,6 +43,10 @@ type ConsulConfig struct { // to register the client HTTP health check with Consul ClientHTTPCheckName string `mapstructure:"client_http_check_name"` + // Tags are optional service tags that get registered with the service + // in Consul + Tags []string `mapstructure:"tags"` + // AutoAdvertise determines if this Nomad Agent will advertise its // services via Consul. When true, Nomad Agent will register // services with Consul. @@ -132,6 +136,7 @@ func (a *ConsulConfig) Merge(b *ConsulConfig) *ConsulConfig { if b.ClientHTTPCheckName != "" { result.ClientHTTPCheckName = b.ClientHTTPCheckName } + result.Tags = append(result.Tags, b.Tags...) if b.AutoAdvertise != nil { result.AutoAdvertise = helper.BoolToPtr(*b.AutoAdvertise) } @@ -226,7 +231,6 @@ func (c *ConsulConfig) ApiConfig() (*consul.Config, error) { } config.Transport.TLSClientConfig = tlsConfig } - return config, nil } diff --git a/website/source/docs/configuration/consul.html.md b/website/source/docs/configuration/consul.html.md index 2f183652e..9ba788d55 100644 --- a/website/source/docs/configuration/consul.html.md +++ b/website/source/docs/configuration/consul.html.md @@ -108,6 +108,9 @@ configuring Nomad to talk to Consul via DNS such as consul.service.consul - `ssl` `(bool: false)` - Specifies if the transport scheme should use HTTPS to communicate with the Consul agent. +- `tags` `(Array["string"]: ""])` - Specifies optional Consul tags to be + registered with the Nomad server and agent services. + - `token` `(string: "")` - Specifies the token used to provide a per-request ACL token. This option overrides the Consul Agent's default token. If the token is not set here or on the Consul agent, it will default to Consul's anonymous policy, From 40a16ddc270a09486e9e451d9e0fec4467c9b52d Mon Sep 17 00:00:00 2001 From: Nick Ethier Date: Tue, 14 May 2019 14:37:34 -0400 Subject: [PATCH 2/2] fixup #5172 and rebase against master --- command/agent/agent.go | 8 ++++---- nomad/structs/config/consul.go | 4 ++-- website/source/docs/configuration/consul.html.md | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index 4cde61fff..0761946a4 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -576,7 +576,7 @@ func (a *Agent) setupServer() error { httpServ := &structs.Service{ Name: a.config.Consul.ServerServiceName, PortLabel: a.config.AdvertiseAddrs.HTTP, - Tags: append(a.config.Consul.Tags, consul.ServiceTagHTTP), + Tags: append([]string{consul.ServiceTagHTTP}, a.config.Consul.Tags...), } const isServer = true if check := a.agentHTTPCheck(isServer); check != nil { @@ -585,7 +585,7 @@ func (a *Agent) setupServer() error { rpcServ := &structs.Service{ Name: a.config.Consul.ServerServiceName, PortLabel: a.config.AdvertiseAddrs.RPC, - Tags: append(a.config.Consul.Tags, consul.ServiceTagRPC), + Tags: append([]string{consul.ServiceTagRPC}, a.config.Consul.Tags...), Checks: []*structs.ServiceCheck{ { Name: a.config.Consul.ServerRPCCheckName, @@ -599,7 +599,7 @@ func (a *Agent) setupServer() error { serfServ := &structs.Service{ Name: a.config.Consul.ServerServiceName, PortLabel: a.config.AdvertiseAddrs.Serf, - Tags: append(a.config.Consul.Tags, consul.ServiceTagSerf), + Tags: append([]string{consul.ServiceTagSerf}, a.config.Consul.Tags...), Checks: []*structs.ServiceCheck{ { Name: a.config.Consul.ServerSerfCheckName, @@ -741,7 +741,7 @@ func (a *Agent) setupClient() error { httpServ := &structs.Service{ Name: a.config.Consul.ClientServiceName, PortLabel: a.config.AdvertiseAddrs.HTTP, - Tags: append(a.config.Consul.Tags, consul.ServiceTagHTTP), + Tags: append([]string{consul.ServiceTagHTTP}, a.config.Consul.Tags...), } const isServer = false if check := a.agentHTTPCheck(isServer); check != nil { diff --git a/nomad/structs/config/consul.go b/nomad/structs/config/consul.go index 6e8ba4f89..a3d17dcc2 100644 --- a/nomad/structs/config/consul.go +++ b/nomad/structs/config/consul.go @@ -44,8 +44,8 @@ type ConsulConfig struct { ClientHTTPCheckName string `hcl:"client_http_check_name"` // Tags are optional service tags that get registered with the service - // in Consul - Tags []string `mapstructure:"tags"` + // in Consul + Tags []string `hcl:"tags"` // AutoAdvertise determines if this Nomad Agent will advertise its // services via Consul. When true, Nomad Agent will register diff --git a/website/source/docs/configuration/consul.html.md b/website/source/docs/configuration/consul.html.md index 9ba788d55..274c55e70 100644 --- a/website/source/docs/configuration/consul.html.md +++ b/website/source/docs/configuration/consul.html.md @@ -108,7 +108,7 @@ configuring Nomad to talk to Consul via DNS such as consul.service.consul - `ssl` `(bool: false)` - Specifies if the transport scheme should use HTTPS to communicate with the Consul agent. -- `tags` `(Array["string"]: ""])` - Specifies optional Consul tags to be +- `tags` `(array: [])` - Specifies optional Consul tags to be registered with the Nomad server and agent services. - `token` `(string: "")` - Specifies the token used to provide a per-request ACL