feat(constraints): Support constraint for Marathon provider

This commit is contained in:
tuier 2016-09-20 09:44:32 +01:00
parent 12c1131b0c
commit 928675a847
2 changed files with 28 additions and 7 deletions

View file

@ -153,12 +153,12 @@ func (provider *Marathon) loadMarathonConfig() *types.Configuration {
//filter tasks //filter tasks
filteredTasks := fun.Filter(func(task marathon.Task) bool { filteredTasks := fun.Filter(func(task marathon.Task) bool {
return taskFilter(task, applications, provider.ExposedByDefault) return provider.taskFilter(task, applications, provider.ExposedByDefault)
}, tasks.Tasks).([]marathon.Task) }, tasks.Tasks).([]marathon.Task)
//filter apps //filter apps
filteredApps := fun.Filter(func(app marathon.Application) bool { filteredApps := fun.Filter(func(app marathon.Application) bool {
return applicationFilter(app, filteredTasks) return provider.applicationFilter(app, filteredTasks)
}, applications.Apps).([]marathon.Application) }, applications.Apps).([]marathon.Application)
templateObjects := struct { templateObjects := struct {
@ -178,7 +178,7 @@ func (provider *Marathon) loadMarathonConfig() *types.Configuration {
return configuration return configuration
} }
func taskFilter(task marathon.Task, applications *marathon.Applications, exposedByDefaultFlag bool) bool { func (provider *Marathon) taskFilter(task marathon.Task, applications *marathon.Applications, exposedByDefaultFlag bool) bool {
if len(task.Ports) == 0 { if len(task.Ports) == 0 {
log.Debug("Filtering marathon task without port %s", task.AppID) log.Debug("Filtering marathon task without port %s", task.AppID)
return false return false
@ -188,6 +188,15 @@ func taskFilter(task marathon.Task, applications *marathon.Applications, exposed
log.Errorf("Unable to get marathon application from task %s", task.AppID) log.Errorf("Unable to get marathon application from task %s", task.AppID)
return false return false
} }
if label, err := provider.getLabel(application, "traefik.tags"); err == nil {
constraintTags := strings.Split(label, ",")
if ok, failingConstraint := provider.MatchConstraints(constraintTags); !ok {
if failingConstraint != nil {
log.Debugf("Application %v pruned by '%v' constraint", application.ID, failingConstraint.String())
}
return false
}
}
if !isApplicationEnabled(application, exposedByDefaultFlag) { if !isApplicationEnabled(application, exposedByDefaultFlag) {
log.Debugf("Filtering disabled marathon task %s", task.AppID) log.Debugf("Filtering disabled marathon task %s", task.AppID)
@ -248,7 +257,15 @@ func taskFilter(task marathon.Task, applications *marathon.Applications, exposed
return true return true
} }
func applicationFilter(app marathon.Application, filteredTasks []marathon.Task) bool { func (provider *Marathon) applicationFilter(app marathon.Application, filteredTasks []marathon.Task) bool {
constraintTags := strings.Split((*app.Labels)["traefik.tags"], ",")
if ok, failingConstraint := provider.MatchConstraints(constraintTags); !ok {
if failingConstraint != nil {
log.Debugf("Application %v pruned by '%v' constraint", app.ID, failingConstraint.String())
}
return false
}
return fun.Exists(func(task marathon.Task) bool { return fun.Exists(func(task marathon.Task) bool {
return task.AppID == app.ID return task.AppID == app.ID
}, filteredTasks) }, filteredTasks)

View file

@ -674,8 +674,9 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
} }
provider := &Marathon{}
for _, c := range cases { for _, c := range cases {
actual := taskFilter(c.task, c.applications, c.exposedByDefault) actual := provider.taskFilter(c.task, c.applications, c.exposedByDefault)
if actual != c.expected { if actual != c.expected {
t.Fatalf("expected %v, got %v", c.expected, actual) t.Fatalf("expected %v, got %v", c.expected, actual)
} }
@ -689,7 +690,9 @@ func TestMarathonApplicationFilter(t *testing.T) {
expected bool expected bool
}{ }{
{ {
application: marathon.Application{}, application: marathon.Application{
Labels: &map[string]string{},
},
filteredTasks: []marathon.Task{}, filteredTasks: []marathon.Task{},
expected: false, expected: false,
}, },
@ -727,8 +730,9 @@ func TestMarathonApplicationFilter(t *testing.T) {
}, },
} }
provider := &Marathon{}
for _, c := range cases { for _, c := range cases {
actual := applicationFilter(c.application, c.filteredTasks) actual := provider.applicationFilter(c.application, c.filteredTasks)
if actual != c.expected { if actual != c.expected {
t.Fatalf("expected %v, got %v", c.expected, actual) t.Fatalf("expected %v, got %v", c.expected, actual)
} }