feat(rancher): added constraint management for rancher provider

This commit is contained in:
Youcef Yekhlef 2017-05-02 16:51:02 +02:00 committed by Fernandez Ludovic
parent b5283391dd
commit 8de107866f
4 changed files with 34 additions and 0 deletions

View file

@ -117,6 +117,7 @@ Supported backends:
- Zookeeper - Zookeeper
- Etcd - Etcd
- Consul Catalog - Consul Catalog
- Rancher
Supported filters: Supported filters:

View file

@ -234,6 +234,7 @@ func getenv(key, fallback string) string {
// Provide allows the rancher provider to provide configurations to traefik // Provide allows the rancher provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error { func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error {
p.Constraints = append(p.Constraints, constraints...)
safe.Go(func() { safe.Go(func() {
operation := func() error { operation := func() error {
@ -485,6 +486,14 @@ func (p *Provider) serviceFilter(service rancherData) bool {
return false 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 // Only filter services by Health (HealthState) and State if EnableServiceHealthFilter is true
if p.EnableServiceHealthFilter { if p.EnableServiceHealthFilter {

View file

@ -15,6 +15,9 @@ func TestRancherServiceFilter(t *testing.T) {
EnableServiceHealthFilter: true, EnableServiceHealthFilter: true,
} }
constraint, _ := types.NewConstraint("tag==ch*se")
provider.Constraints = types.Constraints{constraint}
services := []struct { services := []struct {
service rancherData service rancherData
expected bool expected bool
@ -54,6 +57,7 @@ func TestRancherServiceFilter(t *testing.T) {
{ {
service: rancherData{ service: rancherData{
Labels: map[string]string{ Labels: map[string]string{
"traefik.tags": "not-cheesy",
"traefik.port": "80", "traefik.port": "80",
"traefik.enable": "true", "traefik.enable": "true",
}, },
@ -65,6 +69,7 @@ func TestRancherServiceFilter(t *testing.T) {
{ {
service: rancherData{ service: rancherData{
Labels: map[string]string{ Labels: map[string]string{
"traefik.tags": "cheese",
"traefik.port": "80", "traefik.port": "80",
"traefik.enable": "true", "traefik.enable": "true",
}, },
@ -76,6 +81,7 @@ func TestRancherServiceFilter(t *testing.T) {
{ {
service: rancherData{ service: rancherData{
Labels: map[string]string{ Labels: map[string]string{
"traefik.tags": "cheeeeese",
"traefik.port": "80", "traefik.port": "80",
"traefik.enable": "true", "traefik.enable": "true",
}, },
@ -84,6 +90,18 @@ func TestRancherServiceFilter(t *testing.T) {
}, },
expected: true, 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 { for _, e := range services {

View file

@ -1081,6 +1081,12 @@
# Required # Required
# SecretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # SecretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Constraints
#
# Optional
#
# constraints = ["tag==api", "tag==chee*e"]
# Matching with containers having this tag: "traefik.tags=cheese,wine"
################################################################ ################################################################
# DynamoDB configuration backend # DynamoDB configuration backend