-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow setting of Consul ServiceMeta tags from config file #11084
Conversation
Still very interested in seeing this through, any chance it could be reviewed? |
Hey there @unRob ! I'm really very sorry this one has waited so long, but I'm pretty eager to get it merged in as long as you are. There's just a few things I'd like to confirm/address before we do. I'll be leaving a review shortly with some comments, but I'll be watching this closely for updates, and please feel free to tag me just in case :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for bearing with us on this. A few comments and things I'd like to see answered/addressed, but I'm eager to get this merged. I agree that the approach of deserializing the JSON where you have seems to be the easiest place and I don't see it as too problematic.
I know this took a while to be reviewed but I'm committed to helping you see this to the finish line, as long as you're still willing :)
pass: false, | ||
}, | ||
{ | ||
conf: map[string]string{"service_meta": "[{\"key\":\"value\"}]"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come the config must be a list? Since we're only taking the first of these values, I'd expect to be able to configure this as a set of JSON strings, e.g.
conf: map[string]string{"service_meta": "{\"key\":\"value\", \"bar\":\"baz\"}"},
metaTagList := []map[string]string{} | ||
err := json.Unmarshal([]byte(metaTagsJSON), &metaTagList) | ||
if err != nil { | ||
return nil, errors.New("service tags must be a dictionary of string keys and values") | ||
} | ||
metaTags = metaTagList[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned elsewhere, I'm a little confused why we're unmarshaling this into a list first and then taking the first element. Could this be changed so that we don't expect a list?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't recall why I made this choice in the context of the codebase I originally submitted this PR to, but agree it does not make any sense at all as in config, these values—as part of storage
or service_registration
— will be maps. Vaguely remember having trouble understanding if I should be using HCL blocks or objects.
I think my original intention was to allow something like the following config snippet
storage "consul" {
service_meta = {some_key = "some value"}
}
It's been a while, and I eventually ended up hacking around this shortcoming, so don't really have an informed opinion. Fixing.
if logger.IsDebug() { | ||
logger.Debug("config service_meta set", "service_meta", metaTags) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if logger.IsDebug() { | |
logger.Debug("config service_meta set", "service_meta", metaTags) | |
} | |
c.logger.Debug("config service_meta set", "service_meta", metaTags) |
@@ -85,6 +85,9 @@ at Consul's service discovery layer. | |||
- `service_tags` `(string: "")` – Specifies a comma-separated list of case-sensitive | |||
tags to attach to the service registration in Consul. | |||
|
|||
- `service_meta` `(map[string]string: {})` – Specifies a key-value list of meta tags to | |||
attach to the service registration in Consul. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think an example here would be great
website/content/docs/configuration/service-registration/consul.mdx
Outdated
Show resolved
Hide resolved
@@ -229,6 +232,22 @@ func (c *serviceRegistration) merge(conf map[string]string) error { | |||
c.logger.Debug("config service_tags set", "service_tags", tags) | |||
} | |||
|
|||
// Get user-defined meta tags to attach to the registered service name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The formatting looks a little strange here -- could you give this a make fmt
/align this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I used github's UI to resolve the refactor of NewServiceRegistration
/ serverRegistration.merge
and did not really pay attention, while I had hope, I was not really expecting someone to bring this PR back from the dead 😂 . Fixing!
metaTagList := []map[string]string{} | ||
err := json.Unmarshal([]byte(metaTagsJSON), &metaTagList) | ||
if err != nil { | ||
return nil, errors.New("service tags must be a dictionary of string keys and values") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the code has changed around this, and this should just be
return errors.New("service tags must be a dictionary of string keys and values")
Oh, one last thing, we would require a changelog for this. I'm happy to do it myself if you don't want to fiddle, but it'd be a file in the
(Delete the - preceding the three backticks, I couldn't get it to format properly otherwise. The other changelogs in that directory should show you the right formatting though) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Just a few more things before we can get this over the finish line. Thanks for the quick response so far!
if err := json.Unmarshal([]byte(metaTagsJSON), &metaTags); err != nil { | ||
return errors.New("service tags must be a dictionary of string keys and values") | ||
} | ||
metaTags["external-source"] = metaExternalSource |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should do this irrespective of if ok
, since otherwise there will be a behaviour change as part of this PR. Previously, the tag would have been set, but now, it won't be set unless I provide my own meta tags.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To put it another way: if I don't provide my own service_meta
, it should still be:
map[string]string{
"external-source": metaExternalSource,
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest moving this line until after the if ok
, that way the behaviour is preserved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, fixed!
@@ -492,6 +492,43 @@ func TestConsul_serviceTags(t *testing.T) { | |||
} | |||
} | |||
|
|||
func TestConsul_serviceMeta(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: We should have a godoc describing what this test does and is about
@@ -492,6 +492,43 @@ func TestConsul_serviceTags(t *testing.T) { | |||
} | |||
} | |||
|
|||
func TestConsul_serviceMeta(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func TestConsul_serviceMeta(t *testing.T) { | |
func TestConsul_ServiceMeta(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love to see a test in some respect here that validates that if I don't provide my own service_meta
, that it's still set to what it was before ("external-source": metaExternalSource
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One way we could do this is by improving TestConsul_ServiceMeta
to test the resultant map for the three configurations, but I'm open to whatever you feel is easiest :)
probably a bad idea, let's see how it works scaffold tests
Co-authored-by: Violet Hynes <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I appreciate your patience for bearing with us on this, but thanks for being so understanding and great to work with. Approved! I'll look to get this merged as soon as all the CI passes etc.
Hi there!
I'd love to be able to set
ServiceMeta
on the Consul service registered by Vault by specifying the desired tags in the config file, and I'm submitting this PR to that effect.I'm not happy about unmarshalling json during
NewServiceRegistration
but considered that a simpler change than the couple of alternatives I came up from a quick glance at the codebase. That is to say, I'd love feedback on the approach (and perhaps suggestions on a proper testing strategy should folks feel this approach works).fixes #9121
see also #10233