Check for endpoints while detecting Consul service changes

This commit is contained in:
Kondrashov Ilia 2018-02-22 09:48:03 +01:00 committed by Traefiker Bot
parent 58d4481118
commit 0c0949679f
2 changed files with 41 additions and 8 deletions

View file

@ -38,10 +38,11 @@ type Provider struct {
// Service represent a Consul service. // Service represent a Consul service.
type Service struct { type Service struct {
Name string Name string
Tags []string Tags []string
Nodes []string Nodes []string
Ports []int Addresses []string
Ports []int
} }
type serviceUpdate struct { type serviceUpdate struct {
@ -188,6 +189,7 @@ func (p *Provider) watchCatalogServices(stopCh <-chan struct{}, watchCh chan<- m
nodesID := getServiceIds(nodes) nodesID := getServiceIds(nodes)
ports := getServicePorts(nodes) ports := getServicePorts(nodes)
addresses := getServiceAddresses(nodes)
if service, ok := current[key]; ok { if service, ok := current[key]; ok {
service.Tags = value service.Tags = value
@ -195,10 +197,11 @@ func (p *Provider) watchCatalogServices(stopCh <-chan struct{}, watchCh chan<- m
service.Ports = ports service.Ports = ports
} else { } else {
service := Service{ service := Service{
Name: key, Name: key,
Tags: value, Tags: value,
Nodes: nodesID, Nodes: nodesID,
Ports: ports, Addresses: addresses,
Ports: ports,
} }
current[key] = service current[key] = service
} }
@ -338,6 +341,10 @@ func hasServiceChanged(current map[string]Service, previous map[string]Service)
if len(addedTagsKeys) > 0 || len(removedTagsKeys) > 0 { if len(addedTagsKeys) > 0 || len(removedTagsKeys) > 0 {
return true return true
} }
addedAddressesKeys, removedAddressesKeys := getChangedStringKeys(value.Addresses, prevValue.Addresses)
if len(addedAddressesKeys) > 0 || len(removedAddressesKeys) > 0 {
return true
}
addedPortsKeys, removedPortsKeys := getChangedIntKeys(value.Ports, prevValue.Ports) addedPortsKeys, removedPortsKeys := getChangedIntKeys(value.Ports, prevValue.Ports)
if len(addedPortsKeys) > 0 || len(removedPortsKeys) > 0 { if len(addedPortsKeys) > 0 || len(removedPortsKeys) > 0 {
return true return true
@ -383,6 +390,14 @@ func getServicePorts(services []*api.CatalogService) []int {
return servicePorts return servicePorts
} }
func getServiceAddresses(services []*api.CatalogService) []string {
var serviceAddresses []string
for _, service := range services {
serviceAddresses = append(serviceAddresses, service.ServiceAddress)
}
return serviceAddresses
}
func (p *Provider) healthyNodes(service string) (catalogUpdate, error) { func (p *Provider) healthyNodes(service string) (catalogUpdate, error) {
health := p.client.Health() health := p.client.Health()
data, _, err := health.Service(service, "", true, &api.QueryOptions{}) data, _, err := health.Service(service, "", true, &api.QueryOptions{})

View file

@ -637,6 +637,24 @@ func TestHasServiceChanged(t *testing.T) {
}, },
expected: true, expected: true,
}, },
{
desc: "Change detected on addresses",
current: map[string]Service{
"foo-service": {
Name: "foo",
Nodes: []string{"node1"},
Addresses: []string{"127.0.0.1"},
},
},
previous: map[string]Service{
"foo-service": {
Name: "foo",
Nodes: []string{"node1"},
Addresses: []string{"127.0.0.2"},
},
},
expected: true,
},
{ {
desc: "No Change detected", desc: "No Change detected",
current: map[string]Service{ current: map[string]Service{