connect: add meta on ConsulSidecarService (#16705)

Co-authored-by: Sol-Stiep <sol.stiep@southworks.com>
This commit is contained in:
Horacio Monsalvo 2023-03-30 17:09:28 -03:00 committed by GitHub
parent fa4ee68c6a
commit 20372b1721
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 181 additions and 5 deletions

3
.changelog/16705.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
connect: Added support for meta field on sidecar service block
```

View File

@ -61,10 +61,11 @@ func (cc *ConsulConnect) Canonicalize() {
// ConsulSidecarService represents a Consul Connect SidecarService jobspec // ConsulSidecarService represents a Consul Connect SidecarService jobspec
// block. // block.
type ConsulSidecarService struct { type ConsulSidecarService struct {
Tags []string `hcl:"tags,optional"` Tags []string `hcl:"tags,optional"`
Port string `hcl:"port,optional"` Port string `hcl:"port,optional"`
Proxy *ConsulProxy `hcl:"proxy,block"` Proxy *ConsulProxy `hcl:"proxy,block"`
DisableDefaultTCPCheck bool `mapstructure:"disable_default_tcp_check" hcl:"disable_default_tcp_check,optional"` DisableDefaultTCPCheck bool `mapstructure:"disable_default_tcp_check" hcl:"disable_default_tcp_check,optional"`
Meta map[string]string `hcl:"meta,block"`
} }
func (css *ConsulSidecarService) Canonicalize() { func (css *ConsulSidecarService) Canonicalize() {
@ -76,6 +77,10 @@ func (css *ConsulSidecarService) Canonicalize() {
css.Tags = nil css.Tags = nil
} }
if len(css.Meta) == 0 {
css.Meta = nil
}
css.Proxy.Canonicalize() css.Proxy.Canonicalize()
} }

View File

@ -107,6 +107,9 @@ func TestConsulSidecarService_Canonicalize(t *testing.T) {
LocalServiceAddress: "lsa", LocalServiceAddress: "lsa",
LocalServicePort: 80, LocalServicePort: 80,
}, },
Meta: map[string]string{
"test-key": "test-value",
},
} }
css.Canonicalize() css.Canonicalize()
must.Eq(t, &ConsulSidecarService{ must.Eq(t, &ConsulSidecarService{
@ -115,6 +118,9 @@ func TestConsulSidecarService_Canonicalize(t *testing.T) {
Proxy: &ConsulProxy{ Proxy: &ConsulProxy{
LocalServiceAddress: "lsa", LocalServiceAddress: "lsa",
LocalServicePort: 80}, LocalServicePort: 80},
Meta: map[string]string{
"test-key": "test-value",
},
}, css) }, css)
}) })
} }

View File

@ -129,6 +129,7 @@ func connectSidecarRegistration(serviceID string, info structs.AllocInfo, css *s
Address: cMapping.HostIP, Address: cMapping.HostIP,
Proxy: proxy, Proxy: proxy,
Checks: checks, Checks: checks,
Meta: maps.Clone(css.Meta),
}, nil }, nil
} }

View File

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

View File

@ -2557,6 +2557,9 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
Tags: []string{"f", "g"}, Tags: []string{"f", "g"},
Port: "9000", Port: "9000",
DisableDefaultTCPCheck: true, DisableDefaultTCPCheck: true,
Meta: map[string]string{
"test-key": "test-value",
},
}, },
}, },
}, },
@ -2965,6 +2968,9 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
Tags: []string{"f", "g"}, Tags: []string{"f", "g"},
Port: "9000", Port: "9000",
DisableDefaultTCPCheck: true, DisableDefaultTCPCheck: true,
Meta: map[string]string{
"test-key": "test-value",
},
}, },
}, },
}, },
@ -3766,12 +3772,18 @@ func TestConversion_apiConnectSidecarServiceToStructs(t *testing.T) {
Proxy: &structs.ConsulProxy{ Proxy: &structs.ConsulProxy{
LocalServiceAddress: "192.168.30.1", LocalServiceAddress: "192.168.30.1",
}, },
Meta: map[string]string{
"test-key": "test-value",
},
}, apiConnectSidecarServiceToStructs(&api.ConsulSidecarService{ }, apiConnectSidecarServiceToStructs(&api.ConsulSidecarService{
Tags: []string{"foo"}, Tags: []string{"foo"},
Port: "myPort", Port: "myPort",
Proxy: &api.ConsulProxy{ Proxy: &api.ConsulProxy{
LocalServiceAddress: "192.168.30.1", LocalServiceAddress: "192.168.30.1",
}, },
Meta: map[string]string{
"test-key": "test-value",
},
})) }))
} }

View File

@ -664,6 +664,7 @@ func parseSidecarService(o *ast.ObjectItem) (*api.ConsulSidecarService, error) {
"proxy", "proxy",
"tags", "tags",
"disable_default_tcp_check", "disable_default_tcp_check",
"meta",
} }
if err := checkHCLKeys(o.Val, valid); err != nil { if err := checkHCLKeys(o.Val, valid); err != nil {

View File

@ -1346,6 +1346,31 @@ func TestParse(t *testing.T) {
}, },
false, 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", "tg-service-connect-resources.hcl",
&api.Job{ &api.Job{

View File

@ -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"
}
}
}
}
}
}

View File

@ -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,
},
},
},
},
},
},
}, },
} }

View File

@ -1101,6 +1101,9 @@ type ConsulSidecarService struct {
// DisableDefaultTCPCheck, if true, instructs Nomad to avoid setting a // DisableDefaultTCPCheck, if true, instructs Nomad to avoid setting a
// default TCP check for the sidecar service. // default TCP check for the sidecar service.
DisableDefaultTCPCheck bool 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 // HasUpstreams checks if the sidecar service has any upstreams configured
@ -1118,6 +1121,7 @@ func (s *ConsulSidecarService) Copy() *ConsulSidecarService {
Port: s.Port, Port: s.Port,
Proxy: s.Proxy.Copy(), Proxy: s.Proxy.Copy(),
DisableDefaultTCPCheck: s.DisableDefaultTCPCheck, DisableDefaultTCPCheck: s.DisableDefaultTCPCheck,
Meta: maps.Clone(s.Meta),
} }
} }
@ -1139,6 +1143,10 @@ func (s *ConsulSidecarService) Equal(o *ConsulSidecarService) bool {
return false return false
} }
if !maps.Equal(s.Meta, o.Meta) {
return false
}
return s.Proxy.Equal(o.Proxy) return s.Proxy.Equal(o.Proxy)
} }

View File

@ -390,6 +390,9 @@ func TestService_Hash(t *testing.T) {
Config: map[string]any{"foo": "bar"}, Config: map[string]any{"foo": "bar"},
}}, }},
}, },
Meta: map[string]string{
"test-key": "test-value",
},
}, },
// SidecarTask: nil // not hashed // SidecarTask: nil // not hashed
}} }}
@ -530,6 +533,9 @@ func TestConsulConnect_CopyEqual(t *testing.T) {
"foo": 1, "foo": 1,
}, },
}, },
Meta: map[string]string{
"test-key": "test-value",
},
}, },
} }
@ -833,12 +839,18 @@ func TestConsulSidecarService_Copy(t *testing.T) {
Tags: []string{"foo", "bar"}, Tags: []string{"foo", "bar"},
Port: "port1", Port: "port1",
Proxy: &ConsulProxy{LocalServiceAddress: "10.0.0.1"}, Proxy: &ConsulProxy{LocalServiceAddress: "10.0.0.1"},
Meta: map[string]string{
"test-key": "test-value",
},
} }
result := s.Copy() result := s.Copy()
require.Equal(t, &ConsulSidecarService{ require.Equal(t, &ConsulSidecarService{
Tags: []string{"foo", "bar"}, Tags: []string{"foo", "bar"},
Port: "port1", Port: "port1",
Proxy: &ConsulProxy{LocalServiceAddress: "10.0.0.1"}, Proxy: &ConsulProxy{LocalServiceAddress: "10.0.0.1"},
Meta: map[string]string{
"test-key": "test-value",
},
}, result) }, result)
}) })
} }

View File

@ -49,6 +49,8 @@ job "countdash" {
- `disable_default_tcp_check` `(bool: false)` - disable the default TCP health - `disable_default_tcp_check` `(bool: false)` - disable the default TCP health
check. check.
- `meta` <code>(map&lt;string|string&gt;: nil)</code> - Specifies arbitrary KV metadata pairs.
- `port` `(string: )` - Port label for sidecar service. - `port` `(string: )` - Port label for sidecar service.
- `proxy` <code>([proxy][]: nil)</code> - This is used to configure the - `proxy` <code>([proxy][]: nil)</code> - This is used to configure the
@ -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 ```hcl
sidecar_service { sidecar_service {
@ -77,6 +79,9 @@ The following example includes specifying upstreams.
local_bind_port = 8080 local_bind_port = 8080
} }
} }
meta {
"test-key" = "test-value"
}
} }
``` ```