Skip to content

Commit

Permalink
Merge pull request #6448 from hashicorp/f-set-connect-sidecar-tags
Browse files Browse the repository at this point in the history
connect: enable setting tags on consul connect sidecar service in job…
  • Loading branch information
shoenig committed Oct 17, 2019
2 parents 0078b79 + b7e8359 commit 8c7a7b6
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type ConsulConnect struct {
// ConsulSidecarService represents a Consul Connect SidecarService jobspec
// stanza.
type ConsulSidecarService struct {
Tags []string
Port string
Proxy *ConsulProxy
}
Expand Down
1 change: 1 addition & 0 deletions command/agent/consul/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
1 change: 1 addition & 0 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,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,
}

Expand Down
14 changes: 14 additions & 0 deletions command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
3 changes: 2 additions & 1 deletion jobspec/parse_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down
1 change: 1 addition & 0 deletions jobspec/test-fixtures/tg-network.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ job "foo" {

connect {
sidecar_service {
tags = ["side1", "side2"]
proxy {
local_service_port = 8080

Expand Down
9 changes: 9 additions & 0 deletions nomad/structs/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
}
Expand All @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions nomad/structs/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8c7a7b6

Please sign in to comment.