Move consulcatalog provider to only use health apis

Co-authored-by: Charles Zaffery <czaffery@roblox.com>
This commit is contained in:
Kevin Pollet 2022-06-29 12:04:09 +02:00 committed by GitHub
parent ba3f5b318c
commit 3c1d5e0393
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -316,31 +316,31 @@ func (p *Provider) getConsulServicesData(ctx context.Context) ([]itemData, error
}
for _, consulService := range consulServices {
address := consulService.ServiceAddress
address := consulService.Service.Address
if address == "" {
address = consulService.Address
address = consulService.Node.Address
}
namespace := consulService.Namespace
namespace := consulService.Service.Namespace
if namespace == "" {
namespace = "default"
}
status, exists := statuses[consulService.ID+consulService.ServiceID]
status, exists := statuses[consulService.Node.ID+consulService.Service.ID]
if !exists {
status = api.HealthAny
}
item := itemData{
ID: consulService.ServiceID,
Node: consulService.Node,
Datacenter: consulService.Datacenter,
ID: consulService.Service.ID,
Node: consulService.Node.Node,
Datacenter: consulService.Node.Datacenter,
Namespace: namespace,
Name: name,
Address: address,
Port: strconv.Itoa(consulService.ServicePort),
Labels: tagsToNeutralLabels(consulService.ServiceTags, p.Prefix),
Tags: consulService.ServiceTags,
Port: strconv.Itoa(consulService.Service.Port),
Labels: tagsToNeutralLabels(consulService.Service.Tags, p.Prefix),
Tags: consulService.Service.Tags,
Status: status,
}
@ -358,7 +358,7 @@ func (p *Provider) getConsulServicesData(ctx context.Context) ([]itemData, error
return data, nil
}
func (p *Provider) fetchService(ctx context.Context, name string, connectEnabled bool) ([]*api.CatalogService, map[string]string, error) {
func (p *Provider) fetchService(ctx context.Context, name string, connectEnabled bool) ([]*api.ServiceEntry, map[string]string, error) {
var tagFilter string
if !p.ExposedByDefault {
tagFilter = p.Prefix + ".enable=true"
@ -367,19 +367,12 @@ func (p *Provider) fetchService(ctx context.Context, name string, connectEnabled
opts := &api.QueryOptions{AllowStale: p.Stale, RequireConsistent: p.RequireConsistent, UseCache: p.Cache}
opts = opts.WithContext(ctx)
catalogFunc := p.client.Catalog().Service
healthFunc := p.client.Health().Service
if connectEnabled {
catalogFunc = p.client.Catalog().Connect
healthFunc = p.client.Health().Connect
}
consulServices, _, err := catalogFunc(name, tagFilter, opts)
if err != nil {
return nil, nil, err
}
healthServices, _, err := healthFunc(name, tagFilter, false, opts)
consulServices, _, err := healthFunc(name, tagFilter, false, opts)
if err != nil {
return nil, nil, err
}
@ -387,7 +380,7 @@ func (p *Provider) fetchService(ctx context.Context, name string, connectEnabled
// Index status by service and node so it can be retrieved from a CatalogService even if the health and services
// are not in sync.
statuses := make(map[string]string)
for _, health := range healthServices {
for _, health := range consulServices {
if health.Service == nil || health.Node == nil {
continue
}