Skip to content

Commit 6e72397

Browse files
unRobVioletHynes
andauthored
Allow setting of Consul ServiceMeta tags from config file (#11084)
* Allow setting of Consul ServiceMeta tags from config file probably a bad idea, let's see how it works scaffold tests * kick circleci * Add links to consul docs Co-authored-by: Violet Hynes <[email protected]> * add changelog note * use relative developer docs links * address feedback * please linter --------- Co-authored-by: Violet Hynes <[email protected]>
1 parent 4975e69 commit 6e72397

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

changelog/11084.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
serviceregistration: Added support for Consul ServiceMeta tags from config file from the new `service_meta` config field.
3+
```

serviceregistration/consul/consul_service_registration.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package consul
55

66
import (
77
"context"
8+
"encoding/json"
89
"errors"
910
"fmt"
1011
"math/rand"
@@ -73,6 +74,7 @@ type serviceRegistration struct {
7374
redirectPort int64
7475
serviceName string
7576
serviceTags []string
77+
serviceMeta map[string]string
7678
serviceAddress *string
7779
disableRegistration bool
7880
checkTimeout time.Duration
@@ -229,6 +231,18 @@ func (c *serviceRegistration) merge(conf map[string]string) error {
229231
c.logger.Debug("config service_tags set", "service_tags", tags)
230232
}
231233

234+
// Get user-defined meta tags to attach to the registered service name
235+
metaTags := map[string]string{}
236+
if metaTagsJSON, ok := conf["service_meta"]; ok {
237+
if err := json.Unmarshal([]byte(metaTagsJSON), &metaTags); err != nil {
238+
return errors.New("service tags must be a dictionary of string keys and values")
239+
}
240+
}
241+
metaTags["external-source"] = metaExternalSource
242+
if c.logger.IsDebug() {
243+
c.logger.Debug("config service_meta set", "service_meta", metaTags)
244+
}
245+
232246
// Get the service-specific address to override the use of the HA redirect address
233247
var serviceAddr *string
234248
serviceAddrStr, ok := conf["service_address"]
@@ -294,6 +308,7 @@ func (c *serviceRegistration) merge(conf map[string]string) error {
294308
c.config = consulConf
295309
c.serviceName = service
296310
c.serviceTags = strutil.ParseDedupAndSortStrings(tags, ",")
311+
c.serviceMeta = metaTags
297312
c.serviceAddress = serviceAddr
298313
c.checkTimeout = checkTimeout
299314
c.disableRegistration = disableRegistration
@@ -586,12 +601,10 @@ func (c *serviceRegistration) reconcileConsul() (serviceID string, err error) {
586601
ID: serviceID,
587602
Name: c.serviceName,
588603
Tags: tags,
604+
Meta: c.serviceMeta,
589605
Port: int(c.redirectPort),
590606
Address: serviceAddress,
591607
EnableTagOverride: false,
592-
Meta: map[string]string{
593-
"external-source": metaExternalSource,
594-
},
595608
}
596609

597610
checkStatus := api.HealthCritical

serviceregistration/consul/consul_service_registration_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,66 @@ func TestConsul_serviceTags(t *testing.T) {
492492
}
493493
}
494494

495+
// TestConsul_ServiceMeta tests whether consul service meta registration works
496+
func TestConsul_ServiceMeta(t *testing.T) {
497+
tests := []struct {
498+
conf map[string]string
499+
pass bool
500+
expect map[string]string
501+
}{
502+
{
503+
conf: map[string]string{},
504+
pass: true,
505+
expect: map[string]string{"external-source": "vault"},
506+
},
507+
{
508+
conf: map[string]string{"service_meta": "true"},
509+
pass: false,
510+
expect: map[string]string{"external-source": "vault"},
511+
},
512+
{
513+
conf: map[string]string{"service_meta": "{\"key\":\"value\"}"},
514+
pass: true,
515+
expect: map[string]string{"key": "value", "external-source": "vault"},
516+
},
517+
{
518+
conf: map[string]string{"service_meta": "{\"external-source\":\"something-else\"}"},
519+
pass: true,
520+
expect: map[string]string{"external-source": "vault"},
521+
},
522+
}
523+
524+
for _, test := range tests {
525+
logger := logging.NewVaultLogger(log.Debug)
526+
527+
shutdownCh := make(chan struct{})
528+
defer func() {
529+
close(shutdownCh)
530+
}()
531+
sr, err := NewServiceRegistration(test.conf, logger, sr.State{})
532+
if !test.pass {
533+
if err == nil {
534+
t.Fatal("Expected Consul to fail with error")
535+
}
536+
continue
537+
}
538+
539+
if err != nil && test.pass {
540+
t.Fatalf("Expected Consul to initialize: %v", err)
541+
}
542+
543+
c, ok := sr.(*serviceRegistration)
544+
if !ok {
545+
t.Fatalf("Expected serviceRegistration")
546+
}
547+
548+
if !reflect.DeepEqual(c.serviceMeta, test.expect) {
549+
t.Fatalf("Did not produce expected meta: wanted: %v, got %v", test.expect, c.serviceMeta)
550+
}
551+
552+
}
553+
}
554+
495555
func TestConsul_setRedirectAddr(t *testing.T) {
496556
tests := []struct {
497557
addr string

website/content/docs/configuration/service-registration/consul.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ at Consul's service discovery layer.
8585
- `service_tags` `(string: "")` – Specifies a comma-separated list of case-sensitive
8686
tags to attach to the service registration in Consul.
8787

88+
- `service_meta` `(map[string]string: {})` – Specifies a key-value list of meta tags to
89+
attach to the service registration in Consul. See [ServiceMeta](/consul/api-docs/catalog#servicemeta) in the Consul docs for more information.
90+
8891
- `service_address` `(string: nil)` – Specifies a service-specific address to
8992
set on the service registration in Consul. If unset, Vault will use what it
9093
knows to be the HA redirect address - which is usually desirable. Setting

website/content/docs/configuration/storage/consul.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ and [`cluster_addr`][cluster-addr] ([example][listener-example]).
9292
- `service_tags` `(string: "")` – Specifies a comma-separated list of tags to
9393
attach to the service registration in Consul.
9494

95+
- `service_meta` `(map[string]string: {})` – Specifies a key-value list of meta tags to
96+
attach to the service registration in Consul. See [ServiceMeta](/consul/api-docs/catalog#servicemeta) in the Consul docs for more information.
97+
9598
- `service_address` `(string: nil)` – Specifies a service-specific address to
9699
set on the service registration in Consul. If unset, Vault will use what it
97100
knows to be the HA redirect address - which is usually desirable. Setting

0 commit comments

Comments
 (0)