From b7e83591b4f097963a7c58b72d16d4a66bb08e91 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Tue, 8 Oct 2019 19:19:09 +0000 Subject: [PATCH] connect: enable setting tags on consul connect sidecar service in jobspec (#6415) --- api/services.go | 1 + command/agent/consul/client.go | 1 + command/agent/job_endpoint.go | 1 + command/agent/job_endpoint_test.go | 14 ++++++++++++++ jobspec/parse_service.go | 3 ++- jobspec/parse_test.go | 1 + jobspec/test-fixtures/tg-network.hcl | 1 + nomad/structs/services.go | 9 +++++++++ nomad/structs/services_test.go | 1 + 9 files changed, 31 insertions(+), 1 deletion(-) diff --git a/api/services.go b/api/services.go index dbe0fcfe275f..8630126ceb4a 100644 --- a/api/services.go +++ b/api/services.go @@ -143,6 +143,7 @@ type ConsulConnect struct { // ConsulSidecarService represents a Consul Connect SidecarService jobspec // stanza. type ConsulSidecarService struct { + Tags []string Port string Proxy *ConsulProxy } diff --git a/command/agent/consul/client.go b/command/agent/consul/client.go index cdcd0b48c436..9224fba1f680 100644 --- a/command/agent/consul/client.go +++ b/command/agent/consul/client.go @@ -1473,6 +1473,7 @@ func newConnect(serviceName string, nc *structs.ConsulConnect, networks structs. // Advertise host IP:port cc.SidecarService = &api.AgentServiceRegistration{ + Tags: helper.CopySliceString(nc.SidecarService.Tags), Address: net.IP, Port: port.Value, diff --git a/command/agent/job_endpoint.go b/command/agent/job_endpoint.go index 7e7f206b95cc..e0f86ee4ef17 100644 --- a/command/agent/job_endpoint.go +++ b/command/agent/job_endpoint.go @@ -1062,6 +1062,7 @@ func ApiConsulConnectToStructs(in *api.ConsulConnect) *structs.ConsulConnect { if in.SidecarService != nil { out.SidecarService = &structs.ConsulSidecarService{ + Tags: helper.CopySliceString(in.SidecarService.Tags), Port: in.SidecarService.Port, } diff --git a/command/agent/job_endpoint_test.go b/command/agent/job_endpoint_test.go index e1934e29e2b2..88deda125c8b 100644 --- a/command/agent/job_endpoint_test.go +++ b/command/agent/job_endpoint_test.go @@ -1537,6 +1537,13 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) { TaskName: "task1", }, }, + Connect: &api.ConsulConnect{ + Native: false, + SidecarService: &api.ConsulSidecarService{ + Tags: []string{"f", "g"}, + Port: "9000", + }, + }, }, }, Tasks: []*api.Task{ @@ -1877,6 +1884,13 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) { TaskName: "task1", }, }, + Connect: &structs.ConsulConnect{ + Native: false, + SidecarService: &structs.ConsulSidecarService{ + Tags: []string{"f", "g"}, + Port: "9000", + }, + }, }, }, Tasks: []*structs.Task{ diff --git a/jobspec/parse_service.go b/jobspec/parse_service.go index 2100953c5d8a..783b006bca37 100644 --- a/jobspec/parse_service.go +++ b/jobspec/parse_service.go @@ -193,6 +193,7 @@ func parseSidecarService(o *ast.ObjectItem) (*api.ConsulSidecarService, error) { valid := []string{ "port", "proxy", + "tags", } if err := helper.CheckHCLKeys(o.Val, valid); err != nil { @@ -216,7 +217,7 @@ func parseSidecarService(o *ast.ObjectItem) (*api.ConsulSidecarService, error) { return nil, err } if err := dec.Decode(m); err != nil { - return nil, fmt.Errorf("foo: %v", err) + return nil, fmt.Errorf("sidecar_service: %v", err) } var proxyList *ast.ObjectList diff --git a/jobspec/parse_test.go b/jobspec/parse_test.go index cd38573884d5..0297d77071f5 100644 --- a/jobspec/parse_test.go +++ b/jobspec/parse_test.go @@ -943,6 +943,7 @@ func TestParse(t *testing.T) { PortLabel: "1234", Connect: &api.ConsulConnect{ SidecarService: &api.ConsulSidecarService{ + Tags: []string{"side1", "side2"}, Proxy: &api.ConsulProxy{ Upstreams: []*api.ConsulUpstream{ { diff --git a/jobspec/test-fixtures/tg-network.hcl b/jobspec/test-fixtures/tg-network.hcl index 08a65b0955fc..f35f6a83d75b 100644 --- a/jobspec/test-fixtures/tg-network.hcl +++ b/jobspec/test-fixtures/tg-network.hcl @@ -21,6 +21,7 @@ job "foo" { connect { sidecar_service { + tags = ["side1", "side2"] proxy { local_service_port = 8080 diff --git a/nomad/structs/services.go b/nomad/structs/services.go index 1b310d48fb02..abc79256eb10 100644 --- a/nomad/structs/services.go +++ b/nomad/structs/services.go @@ -597,6 +597,10 @@ func (c *ConsulConnect) Validate() error { // ConsulSidecarService represents a Consul Connect SidecarService jobspec // stanza. type ConsulSidecarService struct { + // Tags are optional service tags that get registered with the sidecar service + // in Consul. If unset, the sidecar service inherits the parent service tags. + Tags []string + // Port is the service's port that the sidecar will connect to. May be // a port label or a literal port number. Port string @@ -613,6 +617,7 @@ func (s *ConsulSidecarService) HasUpstreams() bool { // Copy the stanza recursively. Returns nil if nil. func (s *ConsulSidecarService) Copy() *ConsulSidecarService { return &ConsulSidecarService{ + Tags: helper.CopySliceString(s.Tags), Port: s.Port, Proxy: s.Proxy.Copy(), } @@ -628,6 +633,10 @@ func (s *ConsulSidecarService) Equals(o *ConsulSidecarService) bool { return false } + if !helper.CompareSliceSetString(s.Tags, o.Tags) { + return false + } + return s.Proxy.Equals(o.Proxy) } diff --git a/nomad/structs/services_test.go b/nomad/structs/services_test.go index ddd5d23d5eca..9e92a7d5d4fc 100644 --- a/nomad/structs/services_test.go +++ b/nomad/structs/services_test.go @@ -34,6 +34,7 @@ func TestConsulConnect_CopyEquals(t *testing.T) { c := &ConsulConnect{ SidecarService: &ConsulSidecarService{ + Tags: []string{"tag1", "tag2"}, Port: "9001", Proxy: &ConsulProxy{ LocalServiceAddress: "127.0.0.1",