Add exposedByDefault option to marathon section

This commit is contained in:
Nikita Borzykh 2016-03-21 12:37:02 +03:00
parent 7716d3377a
commit c313950891
3 changed files with 111 additions and 33 deletions

1
cmd.go
View file

@ -126,6 +126,7 @@ func init() {
traefikCmd.PersistentFlags().StringVar(&arguments.Marathon.Filename, "marathon.filename", "", "Override default configuration template. For advanced users :)") traefikCmd.PersistentFlags().StringVar(&arguments.Marathon.Filename, "marathon.filename", "", "Override default configuration template. For advanced users :)")
traefikCmd.PersistentFlags().StringVar(&arguments.Marathon.Endpoint, "marathon.endpoint", "http://127.0.0.1:8080", "Marathon server endpoint. You can also specify multiple endpoint for Marathon") traefikCmd.PersistentFlags().StringVar(&arguments.Marathon.Endpoint, "marathon.endpoint", "http://127.0.0.1:8080", "Marathon server endpoint. You can also specify multiple endpoint for Marathon")
traefikCmd.PersistentFlags().StringVar(&arguments.Marathon.Domain, "marathon.domain", "", "Default domain used") traefikCmd.PersistentFlags().StringVar(&arguments.Marathon.Domain, "marathon.domain", "", "Default domain used")
traefikCmd.PersistentFlags().BoolVar(&arguments.Marathon.ExposedByDefault, "marathon.exposedByDefault", true, "Expose Marathon apps by default")
traefikCmd.PersistentFlags().BoolVar(&arguments.consul, "consul", false, "Enable Consul backend") traefikCmd.PersistentFlags().BoolVar(&arguments.consul, "consul", false, "Enable Consul backend")
traefikCmd.PersistentFlags().BoolVar(&arguments.Consul.Watch, "consul.watch", true, "Watch provider") traefikCmd.PersistentFlags().BoolVar(&arguments.Consul.Watch, "consul.watch", true, "Watch provider")

View file

@ -20,6 +20,7 @@ type Marathon struct {
BaseProvider `mapstructure:",squash"` BaseProvider `mapstructure:",squash"`
Endpoint string Endpoint string
Domain string Domain string
ExposedByDefault bool
Basic *MarathonBasic Basic *MarathonBasic
TLS *tls.Config TLS *tls.Config
marathonClient marathon.Marathon marathonClient marathon.Marathon
@ -115,7 +116,7 @@ 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) return taskFilter(task, applications, provider.ExposedByDefault)
}, tasks.Tasks).([]marathon.Task) }, tasks.Tasks).([]marathon.Task)
//filter apps //filter apps
@ -140,7 +141,7 @@ func (provider *Marathon) loadMarathonConfig() *types.Configuration {
return configuration return configuration
} }
func taskFilter(task marathon.Task, applications *marathon.Applications) bool { func 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
@ -150,7 +151,8 @@ func taskFilter(task marathon.Task, applications *marathon.Applications) bool {
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 application.Labels["traefik.enable"] == "false" {
if !isApplicationEnabled(application, exposedByDefaultFlag) {
log.Debugf("Filtering disabled marathon task %s", task.AppID) log.Debugf("Filtering disabled marathon task %s", task.AppID)
return false return false
} }
@ -227,6 +229,10 @@ func getApplication(task marathon.Task, apps []marathon.Application) (marathon.A
return marathon.Application{}, errors.New("Application not found: " + task.AppID) return marathon.Application{}, errors.New("Application not found: " + task.AppID)
} }
func isApplicationEnabled(application marathon.Application, exposedByDefault bool) bool {
return exposedByDefault && application.Labels["traefik.enable"] != "false" || application.Labels["traefik.enable"] == "true"
}
func (provider *Marathon) getLabel(application marathon.Application, label string) (string, error) { func (provider *Marathon) getLabel(application marathon.Application, label string) (string, error) {
for key, value := range application.Labels { for key, value := range application.Labels {
if key == label { if key == label {

View file

@ -111,6 +111,7 @@ func TestMarathonLoadConfig(t *testing.T) {
fakeClient := newFakeClient(c.applicationsError, c.applications, c.tasksError, c.tasks) fakeClient := newFakeClient(c.applicationsError, c.applications, c.tasksError, c.tasks)
provider := &Marathon{ provider := &Marathon{
Domain: "docker.localhost", Domain: "docker.localhost",
ExposedByDefault: true,
marathonClient: fakeClient, marathonClient: fakeClient,
} }
actualConfig := provider.loadMarathonConfig() actualConfig := provider.loadMarathonConfig()
@ -135,11 +136,13 @@ func TestMarathonTaskFilter(t *testing.T) {
task marathon.Task task marathon.Task
applications *marathon.Applications applications *marathon.Applications
expected bool expected bool
exposedByDefault bool
}{ }{
{ {
task: marathon.Task{}, task: marathon.Task{},
applications: &marathon.Applications{}, applications: &marathon.Applications{},
expected: false, expected: false,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -148,6 +151,7 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
applications: &marathon.Applications{}, applications: &marathon.Applications{},
expected: false, expected: false,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -162,6 +166,7 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
}, },
expected: false, expected: false,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -177,6 +182,7 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
}, },
expected: false, expected: false,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -195,6 +201,7 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
}, },
expected: false, expected: false,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -213,6 +220,7 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
}, },
expected: true, expected: true,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -231,6 +239,7 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
}, },
expected: false, expected: false,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -249,6 +258,7 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
}, },
expected: true, expected: true,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -267,6 +277,7 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
}, },
expected: false, expected: false,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -286,6 +297,7 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
}, },
expected: false, expected: false,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -304,6 +316,7 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
}, },
expected: false, expected: false,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -327,6 +340,7 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
}, },
expected: false, expected: false,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -353,6 +367,7 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
}, },
expected: false, expected: false,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -368,6 +383,7 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
}, },
expected: true, expected: true,
exposedByDefault: true,
}, },
{ {
task: marathon.Task{ task: marathon.Task{
@ -391,11 +407,66 @@ func TestMarathonTaskFilter(t *testing.T) {
}, },
}, },
expected: true, expected: true,
exposedByDefault: true,
},
{
task: marathon.Task{
AppID: "disable-default-expose",
Ports: []int{80},
},
applications: &marathon.Applications{
Apps: []marathon.Application{
{
ID: "disable-default-expose",
Ports: []int{80},
},
},
},
expected: false,
exposedByDefault: false,
},
{
task: marathon.Task{
AppID: "disable-default-expose-disable-in-label",
Ports: []int{80},
},
applications: &marathon.Applications{
Apps: []marathon.Application{
{
ID: "disable-default-expose-disable-in-label",
Ports: []int{80},
Labels: map[string]string{
"traefik.enable": "false",
},
},
},
},
expected: false,
exposedByDefault: false,
},
{
task: marathon.Task{
AppID: "disable-default-expose-enable-in-label",
Ports: []int{80},
},
applications: &marathon.Applications{
Apps: []marathon.Application{
{
ID: "disable-default-expose-enable-in-label",
Ports: []int{80},
Labels: map[string]string{
"traefik.enable": "true",
},
},
},
},
expected: true,
exposedByDefault: false,
}, },
} }
for _, c := range cases { for _, c := range cases {
actual := taskFilter(c.task, c.applications) actual := 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)
} }