From 0a89cccdc0dd3af71350cb62612bb619f372e809 Mon Sep 17 00:00:00 2001 From: Jean Prat Date: Mon, 5 Aug 2019 17:40:04 +0200 Subject: [PATCH] warning should not be a fail status --- cmd/configuration.go | 1 + docs/configuration/backends/consulcatalog.md | 9 +++++++++ provider/consulcatalog/consul_catalog.go | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/cmd/configuration.go b/cmd/configuration.go index b180e1b6f..7f9b47e61 100644 --- a/cmd/configuration.go +++ b/cmd/configuration.go @@ -111,6 +111,7 @@ func NewTraefikDefaultPointersConfiguration() *TraefikConfiguration { defaultConsulCatalog.Prefix = "traefik" defaultConsulCatalog.FrontEndRule = "Host:{{.ServiceName}}.{{.Domain}}" defaultConsulCatalog.Stale = false + defaultConsulCatalog.StrictChecks = true // default Etcd var defaultEtcd etcd.Provider diff --git a/docs/configuration/backends/consulcatalog.md b/docs/configuration/backends/consulcatalog.md index e6a169caa..02cf870a2 100644 --- a/docs/configuration/backends/consulcatalog.md +++ b/docs/configuration/backends/consulcatalog.md @@ -37,6 +37,15 @@ stale = false # domain = "consul.localhost" +# Keep a Consul node only if all checks status are passing +# If true, only the Consul nodes with checks status 'passing' will be kept. +# if false, only the Consul nodes with checks status 'passing' or 'warning' will be kept. +# +# Optional +# Default: true +# +strictChecks = true + # Prefix for Consul catalog tags. # # Optional diff --git a/provider/consulcatalog/consul_catalog.go b/provider/consulcatalog/consul_catalog.go index f9fd796b3..849d5a429 100644 --- a/provider/consulcatalog/consul_catalog.go +++ b/provider/consulcatalog/consul_catalog.go @@ -34,6 +34,7 @@ type Provider struct { Stale bool `description:"Use stale consistency for catalog reads" export:"true"` ExposedByDefault bool `description:"Expose Consul services by default" export:"true"` Prefix string `description:"Prefix used for Consul catalog tags" export:"true"` + StrictChecks bool `description:"Keep a Consul node only if all checks status are passing" export:"true"` FrontEndRule string `description:"Frontend rule used for Consul services" export:"true"` TLS *types.ClientTLS `description:"Enable TLS support" export:"true"` client *api.Client @@ -301,6 +302,8 @@ func (p *Provider) watchHealthState(stopCh <-chan struct{}, watchCh chan<- map[s _, failing := currentFailing[key] if healthy.Status == "passing" && !failing { current[key] = append(current[key], healthy.Node) + } else if !p.StrictChecks && healthy.Status == "warning" && !failing { + current[key] = append(current[key], healthy.Node) } else if strings.HasPrefix(healthy.CheckID, "_service_maintenance") || strings.HasPrefix(healthy.CheckID, "_node_maintenance") { maintenance = append(maintenance, healthy.CheckID) } else { @@ -489,7 +492,8 @@ func getServiceAddresses(services []*api.CatalogService) []string { func (p *Provider) healthyNodes(service string) (catalogUpdate, error) { health := p.client.Health() - data, _, err := health.Service(service, "", true, &api.QueryOptions{AllowStale: p.Stale}) + // You can't filter with assigning passingOnly here, nodeFilter will do this later + data, _, err := health.Service(service, "", false, &api.QueryOptions{AllowStale: p.Stale}) if err != nil { log.WithError(err).Errorf("Failed to fetch details of %s", service) return catalogUpdate{}, err @@ -533,7 +537,8 @@ func (p *Provider) nodeFilter(service string, node *api.ServiceEntry) bool { log.Debugf("Service %v pruned by '%v' constraint", service, failingConstraint.String()) return false } - return true + + return p.hasPassingChecks(node) } func (p *Provider) isServiceEnabled(node *api.ServiceEntry) bool { @@ -567,6 +572,11 @@ func (p *Provider) getConstraintTags(tags []string) []string { return values } +func (p *Provider) hasPassingChecks(node *api.ServiceEntry) bool { + status := node.Checks.AggregatedStatus() + return status == "passing" || !p.StrictChecks && status == "warning" +} + func (p *Provider) generateFrontends(service *serviceUpdate) []*serviceUpdate { frontends := make([]*serviceUpdate, 0) // to support .frontend.xxx