Skip to content

Commit

Permalink
connect: add meta on ConsulSidecarService (#16705)
Browse files Browse the repository at this point in the history
Co-authored-by: Sol-Stiep <[email protected]>
  • Loading branch information
horaciomonsalvo and Sol-Stiep authored Mar 30, 2023
1 parent 1721e68 commit 5957880
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/16705.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
connect: Added support for meta field on sidecar service block
```
13 changes: 9 additions & 4 deletions api/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ func (cc *ConsulConnect) Canonicalize() {
// ConsulSidecarService represents a Consul Connect SidecarService jobspec
// block.
type ConsulSidecarService struct {
Tags []string `hcl:"tags,optional"`
Port string `hcl:"port,optional"`
Proxy *ConsulProxy `hcl:"proxy,block"`
DisableDefaultTCPCheck bool `mapstructure:"disable_default_tcp_check" hcl:"disable_default_tcp_check,optional"`
Tags []string `hcl:"tags,optional"`
Port string `hcl:"port,optional"`
Proxy *ConsulProxy `hcl:"proxy,block"`
DisableDefaultTCPCheck bool `mapstructure:"disable_default_tcp_check" hcl:"disable_default_tcp_check,optional"`
Meta map[string]string `hcl:"meta,block"`
}

func (css *ConsulSidecarService) Canonicalize() {
Expand All @@ -76,6 +77,10 @@ func (css *ConsulSidecarService) Canonicalize() {
css.Tags = nil
}

if len(css.Meta) == 0 {
css.Meta = nil
}

css.Proxy.Canonicalize()
}

Expand Down
6 changes: 6 additions & 0 deletions api/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func TestConsulSidecarService_Canonicalize(t *testing.T) {
LocalServiceAddress: "lsa",
LocalServicePort: 80,
},
Meta: map[string]string{
"test-key": "test-value",
},
}
css.Canonicalize()
must.Eq(t, &ConsulSidecarService{
Expand All @@ -115,6 +118,9 @@ func TestConsulSidecarService_Canonicalize(t *testing.T) {
Proxy: &ConsulProxy{
LocalServiceAddress: "lsa",
LocalServicePort: 80},
Meta: map[string]string{
"test-key": "test-value",
},
}, css)
})
}
Expand Down
1 change: 1 addition & 0 deletions command/agent/consul/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func connectSidecarRegistration(serviceID string, info structs.AllocInfo, css *s
Address: cMapping.HostIP,
Proxy: proxy,
Checks: checks,
Meta: maps.Clone(css.Meta),
}, nil
}

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 @@ -1645,6 +1645,7 @@ func apiConnectSidecarServiceToStructs(in *api.ConsulSidecarService) *structs.Co
Tags: slices.Clone(in.Tags),
Proxy: apiConnectSidecarServiceProxyToStructs(in.Proxy),
DisableDefaultTCPCheck: in.DisableDefaultTCPCheck,
Meta: maps.Clone(in.Meta),
}
}

Expand Down
12 changes: 12 additions & 0 deletions command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2557,6 +2557,9 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
Tags: []string{"f", "g"},
Port: "9000",
DisableDefaultTCPCheck: true,
Meta: map[string]string{
"test-key": "test-value",
},
},
},
},
Expand Down Expand Up @@ -2965,6 +2968,9 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
Tags: []string{"f", "g"},
Port: "9000",
DisableDefaultTCPCheck: true,
Meta: map[string]string{
"test-key": "test-value",
},
},
},
},
Expand Down Expand Up @@ -3766,12 +3772,18 @@ func TestConversion_apiConnectSidecarServiceToStructs(t *testing.T) {
Proxy: &structs.ConsulProxy{
LocalServiceAddress: "192.168.30.1",
},
Meta: map[string]string{
"test-key": "test-value",
},
}, apiConnectSidecarServiceToStructs(&api.ConsulSidecarService{
Tags: []string{"foo"},
Port: "myPort",
Proxy: &api.ConsulProxy{
LocalServiceAddress: "192.168.30.1",
},
Meta: map[string]string{
"test-key": "test-value",
},
}))
}

Expand Down
1 change: 1 addition & 0 deletions jobspec/parse_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ func parseSidecarService(o *ast.ObjectItem) (*api.ConsulSidecarService, error) {
"proxy",
"tags",
"disable_default_tcp_check",
"meta",
}

if err := checkHCLKeys(o.Val, valid); err != nil {
Expand Down
25 changes: 25 additions & 0 deletions jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,31 @@ func TestParse(t *testing.T) {
},
false,
},
{
"tg-service-connect-sidecar_meta.hcl",
&api.Job{
ID: stringToPtr("sidecar_meta"),
Name: stringToPtr("sidecar_meta"),
Type: stringToPtr("service"),
TaskGroups: []*api.TaskGroup{{
Name: stringToPtr("group"),
Services: []*api.Service{{
Name: "example",
Connect: &api.ConsulConnect{
Native: false,
SidecarService: &api.ConsulSidecarService{
Meta: map[string]string{
"test-key": "test-value",
"test-key1": "test-value1",
"test-key2": "test-value2",
},
},
},
}},
}},
},
false,
},
{
"tg-service-connect-resources.hcl",
&api.Job{
Expand Down
19 changes: 19 additions & 0 deletions jobspec/test-fixtures/tg-service-connect-sidecar_meta.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
job "sidecar_meta" {
type = "service"

group "group" {
service {
name = "example"

connect {
sidecar_service {
meta {
test-key = "test-value"
test-key1 = "test-value1"
test-key2 = "test-value2"
}
}
}
}
}
}
78 changes: 78 additions & 0 deletions nomad/structs/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8260,6 +8260,84 @@ func TestServicesDiff(t *testing.T) {
},
},
},
}, {
Name: "SidecarService with different meta",
Contextual: false,
Old: []*Service{
{
Name: "webapp",
Provider: "consul",
PortLabel: "http",
Connect: &ConsulConnect{
SidecarService: &ConsulSidecarService{
Port: "http",
Proxy: &ConsulProxy{},
Meta: map[string]string{
"foo": "qux",
},
},
Gateway: &ConsulGateway{
Ingress: &ConsulIngressConfigEntry{},
},
},
},
},
New: []*Service{
{
Name: "webapp",
Provider: "consul",
PortLabel: "http",
Connect: &ConsulConnect{
SidecarService: &ConsulSidecarService{
Port: "http",
Proxy: &ConsulProxy{},
Meta: map[string]string{
"foo": "var",
"testKey": "testValue",
},
},
Gateway: &ConsulGateway{
Ingress: &ConsulIngressConfigEntry{},
},
},
},
},
Expected: []*ObjectDiff{
{
Type: DiffTypeEdited,
Name: "Service",
Objects: []*ObjectDiff{
{
Type: "Edited",
Name: "ConsulConnect",
Fields: nil,
Objects: []*ObjectDiff{
{
Type: "Edited",
Name: "SidecarService",
Fields: []*FieldDiff{
{
Type: "Edited",
Name: "Meta[foo]",
Old: "qux",
New: "var",
Annotations: nil,
},
{
Type: "Added",
Name: "Meta[testKey]",
Old: "",
New: "testValue",
Annotations: nil,
},
},
Objects: nil,
},
},
},
},
},
},
},
}

Expand Down
8 changes: 8 additions & 0 deletions nomad/structs/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,9 @@ type ConsulSidecarService struct {
// DisableDefaultTCPCheck, if true, instructs Nomad to avoid setting a
// default TCP check for the sidecar service.
DisableDefaultTCPCheck bool

// Meta specifies arbitrary KV metadata linked to the sidecar service.
Meta map[string]string
}

// HasUpstreams checks if the sidecar service has any upstreams configured
Expand All @@ -1118,6 +1121,7 @@ func (s *ConsulSidecarService) Copy() *ConsulSidecarService {
Port: s.Port,
Proxy: s.Proxy.Copy(),
DisableDefaultTCPCheck: s.DisableDefaultTCPCheck,
Meta: maps.Clone(s.Meta),
}
}

Expand All @@ -1139,6 +1143,10 @@ func (s *ConsulSidecarService) Equal(o *ConsulSidecarService) bool {
return false
}

if !maps.Equal(s.Meta, o.Meta) {
return false
}

return s.Proxy.Equal(o.Proxy)
}

Expand Down
12 changes: 12 additions & 0 deletions nomad/structs/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ func TestService_Hash(t *testing.T) {
Config: map[string]any{"foo": "bar"},
}},
},
Meta: map[string]string{
"test-key": "test-value",
},
},
// SidecarTask: nil // not hashed
}}
Expand Down Expand Up @@ -530,6 +533,9 @@ func TestConsulConnect_CopyEqual(t *testing.T) {
"foo": 1,
},
},
Meta: map[string]string{
"test-key": "test-value",
},
},
}

Expand Down Expand Up @@ -833,12 +839,18 @@ func TestConsulSidecarService_Copy(t *testing.T) {
Tags: []string{"foo", "bar"},
Port: "port1",
Proxy: &ConsulProxy{LocalServiceAddress: "10.0.0.1"},
Meta: map[string]string{
"test-key": "test-value",
},
}
result := s.Copy()
require.Equal(t, &ConsulSidecarService{
Tags: []string{"foo", "bar"},
Port: "port1",
Proxy: &ConsulProxy{LocalServiceAddress: "10.0.0.1"},
Meta: map[string]string{
"test-key": "test-value",
},
}, result)
})
}
Expand Down
7 changes: 6 additions & 1 deletion website/content/docs/job-specification/sidecar_service.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ job "countdash" {
- `disable_default_tcp_check` `(bool: false)` - disable the default TCP health
check.

- `meta` <code>(map&lt;string|string&gt;: nil)</code> - Specifies arbitrary KV metadata pairs.

- `port` `(string: )` - Port label for sidecar service.

- `proxy` <code>([proxy][]: nil)</code> - This is used to configure the
Expand All @@ -67,7 +69,7 @@ The following example is a minimal `sidecar_service` block with defaults
}
```

The following example includes specifying upstreams.
The following example includes specifying upstreams and meta.

```hcl
sidecar_service {
Expand All @@ -77,6 +79,9 @@ The following example includes specifying upstreams.
local_bind_port = 8080
}
}
meta {
"test-key" = "test-value"
}
}
```
Expand Down

0 comments on commit 5957880

Please sign in to comment.