Merge pull request #438 from samber/TRAEFIK-311--support-docker-backend

feat(constraints): Supports constraints for docker backend
This commit is contained in:
Emile Vauge 2016-06-13 00:29:28 +02:00 committed by GitHub
commit 29e647763a
3 changed files with 24 additions and 4 deletions

View file

@ -203,6 +203,7 @@ Træfɪk filters services according to service attributes/tags set in your confi
Supported backends: Supported backends:
- Docker
- Consul Catalog - Consul Catalog
Supported filters: Supported filters:
@ -574,6 +575,14 @@ watch = true
# cert = "/etc/ssl/docker.crt" # cert = "/etc/ssl/docker.crt"
# key = "/etc/ssl/docker.key" # key = "/etc/ssl/docker.key"
# insecureskipverify = true # insecureskipverify = true
# Constraint on Docker tags
#
# Optional
#
# constraints = ["tag==api", "tag==he*ld"]
# Matching with containers having the label "traefik.tags" set to "api,helloworld"
# ex: $ docker run -d -P --label traefik.tags=api,helloworld emilevauge/whoami
``` ```
Labels can be used on containers to override default behaviour: Labels can be used on containers to override default behaviour:

View file

@ -177,11 +177,12 @@ func (provider *Docker) loadDockerConfig(containersInspected []dockertypes.Conta
} }
// filter containers // filter containers
filteredContainers := fun.Filter(containerFilter, containersInspected).([]dockertypes.ContainerJSON) filteredContainers := fun.Filter(provider.ContainerFilter, containersInspected).([]dockertypes.ContainerJSON)
frontends := map[string][]dockertypes.ContainerJSON{} frontends := map[string][]dockertypes.ContainerJSON{}
for _, container := range filteredContainers { for _, container := range filteredContainers {
frontends[provider.getFrontendName(container)] = append(frontends[provider.getFrontendName(container)], container) frontendName := provider.getFrontendName(container)
frontends[frontendName] = append(frontends[frontendName], container)
} }
templateObjects := struct { templateObjects := struct {
@ -201,7 +202,8 @@ func (provider *Docker) loadDockerConfig(containersInspected []dockertypes.Conta
return configuration return configuration
} }
func containerFilter(container dockertypes.ContainerJSON) bool { // ContainerFilter checks if container have to be exposed
func (provider *Docker) ContainerFilter(container dockertypes.ContainerJSON) bool {
_, err := strconv.Atoi(container.Config.Labels["traefik.port"]) _, err := strconv.Atoi(container.Config.Labels["traefik.port"])
if len(container.NetworkSettings.Ports) == 0 && err != nil { if len(container.NetworkSettings.Ports) == 0 && err != nil {
log.Debugf("Filtering container without port and no traefik.port label %s", container.Name) log.Debugf("Filtering container without port and no traefik.port label %s", container.Name)
@ -217,6 +219,14 @@ func containerFilter(container dockertypes.ContainerJSON) bool {
return false return false
} }
constraintTags := strings.Split(container.Config.Labels["traefik.tags"], ",")
if ok, failingConstraint := provider.MatchConstraints(constraintTags); !ok {
if failingConstraint != nil {
log.Debugf("Container %v pruned by '%v' constraint", container.Name, failingConstraint.String())
}
return false
}
return true return true
} }

View file

@ -621,6 +621,7 @@ func TestDockerGetLabels(t *testing.T) {
} }
func TestDockerTraefikFilter(t *testing.T) { func TestDockerTraefikFilter(t *testing.T) {
provider := Docker{}
containers := []struct { containers := []struct {
container docker.ContainerJSON container docker.ContainerJSON
expected bool expected bool
@ -792,7 +793,7 @@ func TestDockerTraefikFilter(t *testing.T) {
} }
for _, e := range containers { for _, e := range containers {
actual := containerFilter(e.container) actual := provider.ContainerFilter(e.container)
if actual != e.expected { if actual != e.expected {
t.Fatalf("expected %v for %+v, got %+v", e.expected, e, actual) t.Fatalf("expected %v for %+v, got %+v", e.expected, e, actual)
} }