Skip to content

Commit

Permalink
Merge pull request #1527 from yyekhlef/master
Browse files Browse the repository at this point in the history
feat(rancher): added constraint management for rancher provider
  • Loading branch information
ldez committed May 3, 2017
2 parents b528339 + 8de1078 commit b67a27d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Supported backends:
- Zookeeper
- Etcd
- Consul Catalog
- Rancher

Supported filters:

Expand Down
9 changes: 9 additions & 0 deletions provider/rancher/rancher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {

Expand Down
18 changes: 18 additions & 0 deletions provider/rancher/rancher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
},
Expand All @@ -65,6 +69,7 @@ func TestRancherServiceFilter(t *testing.T) {
{
service: rancherData{
Labels: map[string]string{
"traefik.tags": "cheese",
"traefik.port": "80",
"traefik.enable": "true",
},
Expand All @@ -76,6 +81,7 @@ func TestRancherServiceFilter(t *testing.T) {
{
service: rancherData{
Labels: map[string]string{
"traefik.tags": "cheeeeese",
"traefik.port": "80",
"traefik.enable": "true",
},
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions traefik.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b67a27d

Please sign in to comment.