2018-01-04 14:56:03 +00:00
|
|
|
package consulcatalog
|
2016-02-02 17:03:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
2016-05-20 15:17:38 +00:00
|
|
|
"github.com/BurntSushi/ty/fun"
|
2016-09-19 17:08:39 +00:00
|
|
|
"github.com/cenk/backoff"
|
|
|
|
"github.com/containous/traefik/job"
|
2016-09-23 16:27:01 +00:00
|
|
|
"github.com/containous/traefik/log"
|
2017-04-17 10:50:02 +00:00
|
|
|
"github.com/containous/traefik/provider"
|
2017-12-04 19:02:15 +00:00
|
|
|
"github.com/containous/traefik/provider/label"
|
2016-03-31 16:57:08 +00:00
|
|
|
"github.com/containous/traefik/safe"
|
2016-02-24 15:43:39 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2016-02-02 17:03:40 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultWatchWaitTime is the duration to wait when polling consul
|
|
|
|
DefaultWatchWaitTime = 15 * time.Second
|
|
|
|
)
|
|
|
|
|
2018-01-04 14:56:03 +00:00
|
|
|
var _ provider.Provider = (*Provider)(nil)
|
2016-08-16 17:13:18 +00:00
|
|
|
|
2018-01-04 14:56:03 +00:00
|
|
|
// Provider holds configurations of the Consul catalog provider.
|
|
|
|
type Provider struct {
|
2017-10-02 08:32:02 +00:00
|
|
|
provider.BaseProvider `mapstructure:",squash" export:"true"`
|
2017-04-17 10:50:02 +00:00
|
|
|
Endpoint string `description:"Consul server endpoint"`
|
|
|
|
Domain string `description:"Default domain used"`
|
2017-10-02 08:32:02 +00:00
|
|
|
ExposedByDefault bool `description:"Expose Consul services by default" export:"true"`
|
|
|
|
Prefix string `description:"Prefix used for Consul catalog tags" export:"true"`
|
|
|
|
FrontEndRule string `description:"Frontend rule used for Consul services" export:"true"`
|
2017-04-17 10:50:02 +00:00
|
|
|
client *api.Client
|
2017-05-08 17:46:53 +00:00
|
|
|
frontEndRuleTemplate *template.Template
|
2016-04-12 07:49:37 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
// Service represent a Consul service.
|
|
|
|
type Service struct {
|
|
|
|
Name string
|
|
|
|
Tags []string
|
|
|
|
Nodes []string
|
2017-12-15 21:16:48 +00:00
|
|
|
Ports []int
|
2017-12-04 19:02:15 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 07:49:37 +00:00
|
|
|
type serviceUpdate struct {
|
|
|
|
ServiceName string
|
|
|
|
Attributes []string
|
2016-02-02 17:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type catalogUpdate struct {
|
2016-04-12 07:49:37 +00:00
|
|
|
Service *serviceUpdate
|
2016-02-02 17:03:40 +00:00
|
|
|
Nodes []*api.ServiceEntry
|
|
|
|
}
|
|
|
|
|
2016-06-20 17:13:22 +00:00
|
|
|
type nodeSorter []*api.ServiceEntry
|
|
|
|
|
|
|
|
func (a nodeSorter) Len() int {
|
|
|
|
return len(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a nodeSorter) Swap(i int, j int) {
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a nodeSorter) Less(i int, j int) bool {
|
2017-12-04 19:02:15 +00:00
|
|
|
lEntry := a[i]
|
|
|
|
rEntry := a[j]
|
2016-06-20 17:13:22 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
ls := strings.ToLower(lEntry.Service.Service)
|
|
|
|
lr := strings.ToLower(rEntry.Service.Service)
|
2016-06-20 17:13:22 +00:00
|
|
|
|
|
|
|
if ls != lr {
|
|
|
|
return ls < lr
|
|
|
|
}
|
2017-12-04 19:02:15 +00:00
|
|
|
if lEntry.Service.Address != rEntry.Service.Address {
|
|
|
|
return lEntry.Service.Address < rEntry.Service.Address
|
2016-06-20 17:13:22 +00:00
|
|
|
}
|
2017-12-04 19:02:15 +00:00
|
|
|
if lEntry.Node.Address != rEntry.Node.Address {
|
|
|
|
return lEntry.Node.Address < rEntry.Node.Address
|
2016-06-20 17:13:22 +00:00
|
|
|
}
|
2017-12-04 19:02:15 +00:00
|
|
|
return lEntry.Service.Port < rEntry.Service.Port
|
2016-06-20 17:13:22 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
// Provide allows the consul catalog provider to provide configurations to traefik
|
|
|
|
// using the given configuration channel.
|
2018-01-04 14:56:03 +00:00
|
|
|
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error {
|
2017-12-04 19:02:15 +00:00
|
|
|
config := api.DefaultConfig()
|
|
|
|
config.Address = p.Endpoint
|
|
|
|
client, err := api.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.client = client
|
|
|
|
p.Constraints = append(p.Constraints, constraints...)
|
2017-12-31 23:02:18 +00:00
|
|
|
p.setupFrontEndRuleTemplate()
|
2017-12-04 19:02:15 +00:00
|
|
|
|
|
|
|
pool.Go(func(stop chan bool) {
|
|
|
|
notify := func(err error, time time.Duration) {
|
|
|
|
log.Errorf("Consul connection error %+v, retrying in %s", err, time)
|
|
|
|
}
|
|
|
|
operation := func() error {
|
|
|
|
return p.watch(configurationChan, stop)
|
|
|
|
}
|
|
|
|
errRetry := backoff.RetryNotify(safe.OperationWithRecover(operation), job.NewBackOff(backoff.NewExponentialBackOff()), notify)
|
|
|
|
if errRetry != nil {
|
|
|
|
log.Errorf("Cannot connect to consul server %+v", errRetry)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
2017-11-13 11:14:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 14:56:03 +00:00
|
|
|
func (p *Provider) watch(configurationChan chan<- types.ConfigMessage, stop chan bool) error {
|
2017-12-04 19:02:15 +00:00
|
|
|
stopCh := make(chan struct{})
|
|
|
|
watchCh := make(chan map[string][]string)
|
|
|
|
errorCh := make(chan error)
|
2016-02-02 17:03:40 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
p.watchHealthState(stopCh, watchCh, errorCh)
|
|
|
|
p.watchCatalogServices(stopCh, watchCh, errorCh)
|
2017-06-18 09:38:35 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
defer close(stopCh)
|
|
|
|
defer close(watchCh)
|
2017-06-18 09:38:35 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return nil
|
|
|
|
case index, ok := <-watchCh:
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Consul service list nil")
|
|
|
|
}
|
|
|
|
log.Debug("List of services changed")
|
|
|
|
nodes, err := p.getNodes(index)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
configuration := p.buildConfiguration(nodes)
|
|
|
|
configurationChan <- types.ConfigMessage{
|
|
|
|
ProviderName: "consul_catalog",
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
|
|
|
case err := <-errorCh:
|
|
|
|
return err
|
2017-09-08 18:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 14:56:03 +00:00
|
|
|
func (p *Provider) watchCatalogServices(stopCh <-chan struct{}, watchCh chan<- map[string][]string, errorCh chan<- error) {
|
2017-12-04 19:02:15 +00:00
|
|
|
catalog := p.client.Catalog()
|
2017-09-08 18:50:04 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
safe.Go(func() {
|
|
|
|
// variable to hold previous state
|
|
|
|
var flashback map[string]Service
|
2017-09-08 18:50:04 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
options := &api.QueryOptions{WaitTime: DefaultWatchWaitTime}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
data, meta, err := catalog.Services(options)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to list services: %v", err)
|
|
|
|
errorCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.WaitIndex == meta.LastIndex {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
options.WaitIndex = meta.LastIndex
|
|
|
|
|
|
|
|
if data != nil {
|
|
|
|
current := make(map[string]Service)
|
|
|
|
for key, value := range data {
|
|
|
|
nodes, _, err := catalog.Service(key, "", &api.QueryOptions{})
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to get detail of service %s: %v", key, err)
|
|
|
|
errorCh <- err
|
|
|
|
return
|
|
|
|
}
|
2017-12-15 21:16:48 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
nodesID := getServiceIds(nodes)
|
2017-12-15 21:16:48 +00:00
|
|
|
ports := getServicePorts(nodes)
|
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
if service, ok := current[key]; ok {
|
|
|
|
service.Tags = value
|
|
|
|
service.Nodes = nodesID
|
2017-12-15 21:16:48 +00:00
|
|
|
service.Ports = ports
|
2017-12-04 19:02:15 +00:00
|
|
|
} else {
|
|
|
|
service := Service{
|
|
|
|
Name: key,
|
|
|
|
Tags: value,
|
|
|
|
Nodes: nodesID,
|
2017-12-15 21:16:48 +00:00
|
|
|
Ports: ports,
|
2017-12-04 19:02:15 +00:00
|
|
|
}
|
|
|
|
current[key] = service
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 21:16:48 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
// A critical note is that the return of a blocking request is no guarantee of a change.
|
|
|
|
// It is possible that there was an idempotent write that does not affect the result of the query.
|
|
|
|
// Thus it is required to do extra check for changes...
|
|
|
|
if hasChanged(current, flashback) {
|
|
|
|
watchCh <- data
|
|
|
|
flashback = current
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2017-09-08 18:50:04 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 14:56:03 +00:00
|
|
|
func (p *Provider) watchHealthState(stopCh <-chan struct{}, watchCh chan<- map[string][]string, errorCh chan<- error) {
|
2017-03-06 23:46:37 +00:00
|
|
|
health := p.client.Health()
|
2017-06-18 09:38:35 +00:00
|
|
|
catalog := p.client.Catalog()
|
2016-02-02 17:03:40 +00:00
|
|
|
|
2016-03-31 16:57:08 +00:00
|
|
|
safe.Go(func() {
|
2017-06-18 09:38:35 +00:00
|
|
|
// variable to hold previous state
|
2017-09-08 18:50:04 +00:00
|
|
|
var flashback []string
|
2016-02-02 17:03:40 +00:00
|
|
|
|
2017-06-18 09:38:35 +00:00
|
|
|
options := &api.QueryOptions{WaitTime: DefaultWatchWaitTime}
|
2016-02-02 17:03:40 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-03-06 23:46:37 +00:00
|
|
|
// Listening to changes that leads to `passing` state or degrades from it.
|
2017-09-08 18:50:04 +00:00
|
|
|
healthyState, meta, err := health.State("passing", options)
|
2017-03-06 23:46:37 +00:00
|
|
|
if err != nil {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.WithError(err).Error("Failed to retrieve health checks")
|
2017-10-16 14:58:03 +00:00
|
|
|
errorCh <- err
|
2017-03-06 23:46:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-08 18:50:04 +00:00
|
|
|
var current []string
|
|
|
|
if healthyState != nil {
|
|
|
|
for _, healthy := range healthyState {
|
|
|
|
current = append(current, healthy.ServiceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-02 17:03:40 +00:00
|
|
|
// If LastIndex didn't change then it means `Get` returned
|
|
|
|
// because of the WaitTime and the key didn't changed.
|
2017-06-18 09:38:35 +00:00
|
|
|
if options.WaitIndex == meta.LastIndex {
|
2016-02-02 17:03:40 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-06-18 09:38:35 +00:00
|
|
|
|
|
|
|
options.WaitIndex = meta.LastIndex
|
|
|
|
|
|
|
|
// The response should be unified with watchCatalogServices
|
|
|
|
data, _, err := catalog.Services(&api.QueryOptions{})
|
|
|
|
if err != nil {
|
2017-12-04 19:02:15 +00:00
|
|
|
log.Errorf("Failed to list services: %v", err)
|
2017-10-16 14:58:03 +00:00
|
|
|
errorCh <- err
|
2017-06-18 09:38:35 +00:00
|
|
|
return
|
|
|
|
}
|
2016-02-02 17:03:40 +00:00
|
|
|
|
|
|
|
if data != nil {
|
2017-06-18 09:38:35 +00:00
|
|
|
// A critical note is that the return of a blocking request is no guarantee of a change.
|
|
|
|
// It is possible that there was an idempotent write that does not affect the result of the query.
|
|
|
|
// Thus it is required to do extra check for changes...
|
2017-11-13 11:14:02 +00:00
|
|
|
addedKeys, removedKeys := getChangedStringKeys(current, flashback)
|
2017-06-18 09:38:35 +00:00
|
|
|
|
|
|
|
if len(addedKeys) > 0 {
|
|
|
|
log.WithField("DiscoveredServices", addedKeys).Debug("Health State change detected.")
|
|
|
|
watchCh <- data
|
2017-09-08 18:50:04 +00:00
|
|
|
flashback = current
|
2017-06-18 09:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(removedKeys) > 0 {
|
|
|
|
log.WithField("MissingServices", removedKeys).Debug("Health State change detected.")
|
|
|
|
watchCh <- data
|
2017-09-08 18:50:04 +00:00
|
|
|
flashback = current
|
2017-06-18 09:38:35 +00:00
|
|
|
}
|
2016-02-02 17:03:40 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-31 16:57:08 +00:00
|
|
|
})
|
2017-06-18 09:38:35 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 14:56:03 +00:00
|
|
|
func (p *Provider) getNodes(index map[string][]string) ([]catalogUpdate, error) {
|
2017-12-04 19:02:15 +00:00
|
|
|
visited := make(map[string]bool)
|
|
|
|
|
|
|
|
var nodes []catalogUpdate
|
|
|
|
for service := range index {
|
|
|
|
name := strings.ToLower(service)
|
|
|
|
if !strings.Contains(name, " ") && !visited[name] {
|
|
|
|
visited[name] = true
|
|
|
|
log.WithField("service", name).Debug("Fetching service")
|
|
|
|
healthy, err := p.healthyNodes(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// healthy.Nodes can be empty if constraints do not match, without throwing error
|
|
|
|
if healthy.Service != nil && len(healthy.Nodes) > 0 {
|
|
|
|
nodes = append(nodes, healthy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes, nil
|
2017-09-08 18:50:04 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
func hasChanged(current map[string]Service, previous map[string]Service) bool {
|
2017-12-15 21:16:48 +00:00
|
|
|
if len(current) != len(previous) {
|
|
|
|
return true
|
|
|
|
}
|
2017-12-04 19:02:15 +00:00
|
|
|
addedServiceKeys, removedServiceKeys := getChangedServiceKeys(current, previous)
|
2017-12-15 21:16:48 +00:00
|
|
|
return len(removedServiceKeys) > 0 || len(addedServiceKeys) > 0 || hasServiceChanged(current, previous)
|
2017-12-04 19:02:15 +00:00
|
|
|
}
|
2017-06-18 09:38:35 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
func getChangedServiceKeys(current map[string]Service, previous map[string]Service) ([]string, []string) {
|
|
|
|
currKeySet := fun.Set(fun.Keys(current).([]string)).(map[string]bool)
|
|
|
|
prevKeySet := fun.Set(fun.Keys(previous).([]string)).(map[string]bool)
|
2017-06-18 09:38:35 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
addedKeys := fun.Difference(currKeySet, prevKeySet).(map[string]bool)
|
|
|
|
removedKeys := fun.Difference(prevKeySet, currKeySet).(map[string]bool)
|
2016-02-02 17:03:40 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
return fun.Keys(addedKeys).([]string), fun.Keys(removedKeys).([]string)
|
|
|
|
}
|
2017-06-18 09:38:35 +00:00
|
|
|
|
2017-12-15 21:16:48 +00:00
|
|
|
func hasServiceChanged(current map[string]Service, previous map[string]Service) bool {
|
2017-12-04 19:02:15 +00:00
|
|
|
for key, value := range current {
|
|
|
|
if prevValue, ok := previous[key]; ok {
|
|
|
|
addedNodesKeys, removedNodesKeys := getChangedStringKeys(value.Nodes, prevValue.Nodes)
|
2017-12-15 21:16:48 +00:00
|
|
|
if len(addedNodesKeys) > 0 || len(removedNodesKeys) > 0 {
|
|
|
|
return true
|
2017-06-18 09:38:35 +00:00
|
|
|
}
|
2017-12-04 19:02:15 +00:00
|
|
|
addedTagsKeys, removedTagsKeys := getChangedStringKeys(value.Tags, prevValue.Tags)
|
2017-12-15 21:16:48 +00:00
|
|
|
if len(addedTagsKeys) > 0 || len(removedTagsKeys) > 0 {
|
|
|
|
return true
|
2017-06-18 09:38:35 +00:00
|
|
|
}
|
2017-12-15 21:16:48 +00:00
|
|
|
addedPortsKeys, removedPortsKeys := getChangedIntKeys(value.Ports, prevValue.Ports)
|
|
|
|
if len(addedPortsKeys) > 0 || len(removedPortsKeys) > 0 {
|
|
|
|
return true
|
2017-06-18 09:38:35 +00:00
|
|
|
}
|
2017-12-04 19:02:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-15 21:16:48 +00:00
|
|
|
return false
|
2017-12-04 19:02:15 +00:00
|
|
|
}
|
2017-06-18 09:38:35 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
func getChangedStringKeys(currState []string, prevState []string) ([]string, []string) {
|
|
|
|
currKeySet := fun.Set(currState).(map[string]bool)
|
|
|
|
prevKeySet := fun.Set(prevState).(map[string]bool)
|
2017-06-18 09:38:35 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
addedKeys := fun.Difference(currKeySet, prevKeySet).(map[string]bool)
|
|
|
|
removedKeys := fun.Difference(prevKeySet, currKeySet).(map[string]bool)
|
2017-06-18 09:38:35 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
return fun.Keys(addedKeys).([]string), fun.Keys(removedKeys).([]string)
|
2016-02-02 17:03:40 +00:00
|
|
|
}
|
2017-11-13 11:14:02 +00:00
|
|
|
|
2017-12-15 21:16:48 +00:00
|
|
|
func getChangedIntKeys(currState []int, prevState []int) ([]int, []int) {
|
|
|
|
currKeySet := fun.Set(currState).(map[int]bool)
|
|
|
|
prevKeySet := fun.Set(prevState).(map[int]bool)
|
|
|
|
|
|
|
|
addedKeys := fun.Difference(currKeySet, prevKeySet).(map[int]bool)
|
|
|
|
removedKeys := fun.Difference(prevKeySet, currKeySet).(map[int]bool)
|
|
|
|
|
|
|
|
return fun.Keys(addedKeys).([]int), fun.Keys(removedKeys).([]int)
|
2016-02-02 17:03:40 +00:00
|
|
|
}
|
2017-11-13 11:14:02 +00:00
|
|
|
|
2017-09-08 18:50:04 +00:00
|
|
|
func getServiceIds(services []*api.CatalogService) []string {
|
|
|
|
var serviceIds []string
|
|
|
|
for _, service := range services {
|
2017-12-15 15:00:14 +00:00
|
|
|
serviceIds = append(serviceIds, service.ID)
|
2017-09-08 18:50:04 +00:00
|
|
|
}
|
|
|
|
return serviceIds
|
|
|
|
}
|
2016-02-02 17:03:40 +00:00
|
|
|
|
2017-12-15 19:52:03 +00:00
|
|
|
func getServicePorts(services []*api.CatalogService) []int {
|
|
|
|
var servicePorts []int
|
|
|
|
for _, service := range services {
|
|
|
|
servicePorts = append(servicePorts, service.ServicePort)
|
|
|
|
}
|
|
|
|
return servicePorts
|
|
|
|
}
|
|
|
|
|
2018-01-04 14:56:03 +00:00
|
|
|
func (p *Provider) healthyNodes(service string) (catalogUpdate, error) {
|
2017-04-17 10:50:02 +00:00
|
|
|
health := p.client.Health()
|
2018-01-01 02:10:17 +00:00
|
|
|
data, _, err := health.Service(service, "", true, &api.QueryOptions{})
|
2016-02-02 17:03:40 +00:00
|
|
|
if err != nil {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.WithError(err).Errorf("Failed to fetch details of %s", service)
|
2016-02-02 17:03:40 +00:00
|
|
|
return catalogUpdate{}, err
|
|
|
|
}
|
2018-01-01 02:10:17 +00:00
|
|
|
|
2016-05-20 15:17:38 +00:00
|
|
|
nodes := fun.Filter(func(node *api.ServiceEntry) bool {
|
2017-08-25 15:32:03 +00:00
|
|
|
return p.nodeFilter(service, node)
|
2016-05-20 15:17:38 +00:00
|
|
|
}, data).([]*api.ServiceEntry)
|
|
|
|
|
2017-12-15 19:52:03 +00:00
|
|
|
// Merge tags of nodes matching constraints, in a single slice.
|
2016-05-20 15:17:38 +00:00
|
|
|
tags := fun.Foldl(func(node *api.ServiceEntry, set []string) []string {
|
|
|
|
return fun.Keys(fun.Union(
|
|
|
|
fun.Set(set),
|
|
|
|
fun.Set(node.Service.Tags),
|
|
|
|
).(map[string]bool)).([]string)
|
|
|
|
}, []string{}, nodes).([]string)
|
2016-04-12 07:49:37 +00:00
|
|
|
|
2016-02-02 17:03:40 +00:00
|
|
|
return catalogUpdate{
|
2016-04-12 07:49:37 +00:00
|
|
|
Service: &serviceUpdate{
|
|
|
|
ServiceName: service,
|
|
|
|
Attributes: tags,
|
|
|
|
},
|
2016-05-30 13:05:58 +00:00
|
|
|
Nodes: nodes,
|
2016-02-02 17:03:40 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-04 14:56:03 +00:00
|
|
|
func (p *Provider) nodeFilter(service string, node *api.ServiceEntry) bool {
|
2017-08-25 15:32:03 +00:00
|
|
|
// Filter disabled application.
|
|
|
|
if !p.isServiceEnabled(node) {
|
|
|
|
log.Debugf("Filtering disabled Consul service %s", service)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by constraints.
|
|
|
|
constraintTags := p.getConstraintTags(node.Service.Tags)
|
|
|
|
ok, failingConstraint := p.MatchConstraints(constraintTags)
|
|
|
|
if !ok && failingConstraint != nil {
|
|
|
|
log.Debugf("Service %v pruned by '%v' constraint", service, failingConstraint.String())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-01-04 14:56:03 +00:00
|
|
|
func (p *Provider) isServiceEnabled(node *api.ServiceEntry) bool {
|
2017-12-31 23:02:18 +00:00
|
|
|
return p.getBoolAttribute(label.SuffixEnable, node.Service.Tags, p.ExposedByDefault)
|
2016-02-02 17:03:40 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 14:56:03 +00:00
|
|
|
func (p *Provider) getConstraintTags(tags []string) []string {
|
2017-12-04 19:02:15 +00:00
|
|
|
var values []string
|
2016-05-30 13:05:58 +00:00
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
prefix := p.getPrefixedName("tags=")
|
2016-05-30 13:05:58 +00:00
|
|
|
for _, tag := range tags {
|
2017-08-26 10:12:44 +00:00
|
|
|
// We look for a Consul tag named 'traefik.tags' (unless different 'prefix' is configured)
|
2017-12-04 19:02:15 +00:00
|
|
|
if strings.HasPrefix(strings.ToLower(tag), prefix) {
|
2017-05-08 17:46:53 +00:00
|
|
|
// If 'traefik.tags=' tag is found, take the tag value and split by ',' adding the result to the list to be returned
|
2017-12-04 19:02:15 +00:00
|
|
|
splitedTags := label.SplitAndTrimString(tag[len(prefix):], ",")
|
|
|
|
values = append(values, splitedTags...)
|
2016-02-02 17:03:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:02:15 +00:00
|
|
|
return values
|
2016-02-02 17:03:40 +00:00
|
|
|
}
|