From 8de107866fe9455656d7da5d9c01899fc7a95786 Mon Sep 17 00:00:00 2001 From: Youcef Yekhlef Date: Tue, 2 May 2017 16:51:02 +0200 Subject: [PATCH] feat(rancher): added constraint management for rancher provider --- docs/toml.md | 1 + provider/rancher/rancher.go | 9 +++++++++ provider/rancher/rancher_test.go | 18 ++++++++++++++++++ traefik.sample.toml | 6 ++++++ 4 files changed, 34 insertions(+) diff --git a/docs/toml.md b/docs/toml.md index 7a8cc95d1..e5dbff173 100644 --- a/docs/toml.md +++ b/docs/toml.md @@ -117,6 +117,7 @@ Supported backends: - Zookeeper - Etcd - Consul Catalog +- Rancher Supported filters: diff --git a/provider/rancher/rancher.go b/provider/rancher/rancher.go index e02b2c7cd..88474ee9f 100644 --- a/provider/rancher/rancher.go +++ b/provider/rancher/rancher.go @@ -234,6 +234,7 @@ func getenv(key, fallback string) string { // Provide allows the rancher provider to provide configurations to traefik // using the given configuration channel. func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error { + p.Constraints = append(p.Constraints, constraints...) safe.Go(func() { operation := func() error { @@ -485,6 +486,14 @@ func (p *Provider) serviceFilter(service rancherData) bool { return false } + constraintTags := strings.Split(service.Labels["traefik.tags"], ",") + if ok, failingConstraint := p.MatchConstraints(constraintTags); !ok { + if failingConstraint != nil { + log.Debugf("Filtering service %s with constraint %s", service.Name, failingConstraint.String()) + } + return false + } + // Only filter services by Health (HealthState) and State if EnableServiceHealthFilter is true if p.EnableServiceHealthFilter { diff --git a/provider/rancher/rancher_test.go b/provider/rancher/rancher_test.go index 84662d7eb..a2eee64d8 100644 --- a/provider/rancher/rancher_test.go +++ b/provider/rancher/rancher_test.go @@ -15,6 +15,9 @@ func TestRancherServiceFilter(t *testing.T) { EnableServiceHealthFilter: true, } + constraint, _ := types.NewConstraint("tag==ch*se") + provider.Constraints = types.Constraints{constraint} + services := []struct { service rancherData expected bool @@ -54,6 +57,7 @@ func TestRancherServiceFilter(t *testing.T) { { service: rancherData{ Labels: map[string]string{ + "traefik.tags": "not-cheesy", "traefik.port": "80", "traefik.enable": "true", }, @@ -65,6 +69,7 @@ func TestRancherServiceFilter(t *testing.T) { { service: rancherData{ Labels: map[string]string{ + "traefik.tags": "cheese", "traefik.port": "80", "traefik.enable": "true", }, @@ -76,6 +81,7 @@ func TestRancherServiceFilter(t *testing.T) { { service: rancherData{ Labels: map[string]string{ + "traefik.tags": "cheeeeese", "traefik.port": "80", "traefik.enable": "true", }, @@ -84,6 +90,18 @@ func TestRancherServiceFilter(t *testing.T) { }, expected: true, }, + { + service: rancherData{ + Labels: map[string]string{ + "traefik.tags": "chose", + "traefik.port": "80", + "traefik.enable": "true", + }, + Health: "healthy", + State: "active", + }, + expected: true, + }, } for _, e := range services { diff --git a/traefik.sample.toml b/traefik.sample.toml index 6e4ec501b..3beeb018e 100644 --- a/traefik.sample.toml +++ b/traefik.sample.toml @@ -1081,6 +1081,12 @@ # Required # SecretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +# Constraints +# +# Optional +# +# constraints = ["tag==api", "tag==chee*e"] +# Matching with containers having this tag: "traefik.tags=cheese,wine" ################################################################ # DynamoDB configuration backend