Do not filter tags unless req.TagFilter is set

Send empty array [] instead of [""] in DNS requests when TagFilter is not set

Do not change case sensitivity of services anymore in `getServiceNodes()` since
cache keys are now case insensitive
This commit is contained in:
Pierre Souchay 2021-02-10 10:34:09 +01:00
parent 6f91085869
commit 5bee19ee16
2 changed files with 7 additions and 11 deletions

View File

@ -1171,12 +1171,16 @@ func (d *DNSServer) trimDNSResponse(cfg *dnsConfig, network string, req, resp *d
// lookupServiceNodes returns nodes with a given service. // lookupServiceNodes returns nodes with a given service.
func (d *DNSServer) lookupServiceNodes(cfg *dnsConfig, lookup serviceLookup) (structs.IndexedCheckServiceNodes, error) { func (d *DNSServer) lookupServiceNodes(cfg *dnsConfig, lookup serviceLookup) (structs.IndexedCheckServiceNodes, error) {
serviceTags := []string{}
if lookup.Tag != "" {
serviceTags = []string{lookup.Tag}
}
args := structs.ServiceSpecificRequest{ args := structs.ServiceSpecificRequest{
Connect: lookup.Connect, Connect: lookup.Connect,
Ingress: lookup.Ingress, Ingress: lookup.Ingress,
Datacenter: lookup.Datacenter, Datacenter: lookup.Datacenter,
ServiceName: lookup.Service, ServiceName: lookup.Service,
ServiceTags: []string{lookup.Tag}, ServiceTags: serviceTags,
TagFilter: lookup.Tag != "", TagFilter: lookup.Tag != "",
QueryOptions: structs.QueryOptions{ QueryOptions: structs.QueryOptions{
Token: d.agent.tokens.UserToken(), Token: d.agent.tokens.UserToken(),

View File

@ -50,7 +50,6 @@ func (c *Client) getServiceNodes(
) (structs.IndexedCheckServiceNodes, cache.ResultMeta, error) { ) (structs.IndexedCheckServiceNodes, cache.ResultMeta, error) {
var out structs.IndexedCheckServiceNodes var out structs.IndexedCheckServiceNodes
req.ServiceName = strings.ToLower(req.ServiceName)
if !req.QueryOptions.UseCache { if !req.QueryOptions.UseCache {
err := c.NetRPC.RPC("Health.ServiceNodes", &req, &out) err := c.NetRPC.RPC("Health.ServiceNodes", &req, &out)
return out, cache.ResultMeta{}, err return out, cache.ResultMeta{}, err
@ -75,19 +74,12 @@ func (c *Client) getServiceNodes(
} }
func filterTags(out *structs.IndexedCheckServiceNodes, req structs.ServiceSpecificRequest) structs.IndexedCheckServiceNodes { func filterTags(out *structs.IndexedCheckServiceNodes, req structs.ServiceSpecificRequest) structs.IndexedCheckServiceNodes {
if len(req.ServiceTags) == 0 || len(out.Nodes) == 0 { if !req.TagFilter || len(req.ServiceTags) == 0 || len(out.Nodes) == 0 {
return *out return *out
} }
tags := make([]string, 0, len(req.ServiceTags)) tags := make([]string, 0, len(req.ServiceTags))
for _, r := range req.ServiceTags { for _, r := range req.ServiceTags {
// DNS has the bad habit to setting [""] for ServiceTags tags = append(tags, strings.ToLower(r))
if r != "" {
tags = append(tags, strings.ToLower(r))
}
}
// No need to filter
if len(tags) == 0 {
return *out
} }
results := make(structs.CheckServiceNodes, 0, len(out.Nodes)) results := make(structs.CheckServiceNodes, 0, len(out.Nodes))
for _, service := range out.Nodes { for _, service := range out.Nodes {