From 36338b492898f2b59abcf675f52871026a50a80c Mon Sep 17 00:00:00 2001 From: Emile Vauge Date: Fri, 15 Apr 2016 18:59:51 +0200 Subject: [PATCH] add backoff to marathon provider Signed-off-by: Emile Vauge --- examples/compose-marathon.yml | 15 +++++++-- examples/compose-traefik.yml | 3 +- examples/whoami.json | 2 +- provider/marathon.go | 61 +++++++++++++++++++---------------- 4 files changed, 48 insertions(+), 33 deletions(-) diff --git a/examples/compose-marathon.yml b/examples/compose-marathon.yml index 722f2f91d..e0be6a038 100644 --- a/examples/compose-marathon.yml +++ b/examples/compose-marathon.yml @@ -6,7 +6,7 @@ zk: ZK_ID: 1 master: - image: mesosphere/mesos-master:0.26.0-0.2.145.ubuntu1404 + image: mesosphere/mesos-master:0.28.1-2.0.20.ubuntu1404 net: host environment: MESOS_ZK: zk://127.0.0.1:2181/mesos @@ -17,7 +17,7 @@ master: MESOS_WORK_DIR: /var/lib/mesos slave: - image: mesosphere/mesos-slave:0.26.0-0.2.145.ubuntu1404 + image: mesosphere/mesos-slave:0.28.1-2.0.20.ubuntu1404 net: host pid: host privileged: true @@ -34,10 +34,19 @@ slave: - /lib/x86_64-linux-gnu/libsystemd-journal.so.0:/lib/x86_64-linux-gnu/libsystemd-journal.so.0 marathon: - image: mesosphere/marathon:v0.13.0 + image: mesosphere/marathon:v1.1.1 net: host environment: MARATHON_MASTER: zk://127.0.0.1:2181/mesos MARATHON_ZK: zk://127.0.0.1:2181/marathon MARATHON_HOSTNAME: 127.0.0.1 command: --event_subscriber http_callback + +traefik: + image: traefik + command: -c /dev/null --web --logLevel=DEBUG --marathon --marathon.domain marathon.localhost --marathon.endpoint http://172.17.0.1:8080 --marathon.watch + ports: + - "8000:80" + - "8081:8080" + volumes: + - /var/run/docker.sock:/var/run/docker.sock \ No newline at end of file diff --git a/examples/compose-traefik.yml b/examples/compose-traefik.yml index 1a15e57ea..ac468878f 100644 --- a/examples/compose-traefik.yml +++ b/examples/compose-traefik.yml @@ -1,12 +1,11 @@ traefik: image: traefik - command: --web --docker --docker.domain=docker.localhost --logLevel=DEBUG + command: -c /dev/null --web --docker --docker.domain=docker.localhost --logLevel=DEBUG ports: - "80:80" - "8080:8080" volumes: - /var/run/docker.sock:/var/run/docker.sock - - /dev/null:/traefik.toml whoami1: image: emilevauge/whoami diff --git a/examples/whoami.json b/examples/whoami.json index daebbfa26..6b4cc719b 100644 --- a/examples/whoami.json +++ b/examples/whoami.json @@ -26,6 +26,6 @@ "labels": { "traefik.weight": "1", "traefik.protocole": "http", - "traefik.frontend.rule" : "Headers:Host,test.localhost" + "traefik.frontend.rule" : "Host:test.marathon.localhost" } } diff --git a/provider/marathon.go b/provider/marathon.go index 1df13547e..1f8b07599 100644 --- a/provider/marathon.go +++ b/provider/marathon.go @@ -10,10 +10,12 @@ import ( "crypto/tls" "github.com/BurntSushi/ty/fun" log "github.com/Sirupsen/logrus" + "github.com/cenkalti/backoff" "github.com/containous/traefik/safe" "github.com/containous/traefik/types" "github.com/gambol99/go-marathon" "net/http" + "time" ) // Marathon holds configuration of the Marathon provider. @@ -41,29 +43,31 @@ type lightMarathonClient interface { // Provide allows the provider to provide configurations to traefik // using the given configuration channel. func (provider *Marathon) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool) error { - config := marathon.NewDefaultConfig() - config.URL = provider.Endpoint - config.EventsTransport = marathon.EventsTransportSSE - if provider.Basic != nil { - config.HTTPBasicAuthUser = provider.Basic.HTTPBasicAuthUser - config.HTTPBasicPassword = provider.Basic.HTTPBasicPassword - } - config.HTTPClient = &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: provider.TLS, - }, - } - client, err := marathon.NewClient(config) - if err != nil { - log.Errorf("Failed to create a client for marathon, error: %s", err) - return err - } - provider.marathonClient = client - update := make(marathon.EventsChannel, 5) - if provider.Watch { - if err := client.AddEventsListener(update, marathon.EVENTS_APPLICATIONS); err != nil { - log.Errorf("Failed to register for events, %s", err) - } else { + operation := func() error { + config := marathon.NewDefaultConfig() + config.URL = provider.Endpoint + config.EventsTransport = marathon.EventsTransportSSE + if provider.Basic != nil { + config.HTTPBasicAuthUser = provider.Basic.HTTPBasicAuthUser + config.HTTPBasicPassword = provider.Basic.HTTPBasicPassword + } + config.HTTPClient = &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: provider.TLS, + }, + } + client, err := marathon.NewClient(config) + if err != nil { + log.Errorf("Failed to create a client for marathon, error: %s", err) + return err + } + provider.marathonClient = client + update := make(marathon.EventsChannel, 5) + if provider.Watch { + if err := client.AddEventsListener(update, marathon.EVENTS_APPLICATIONS); err != nil { + log.Errorf("Failed to register for events, %s", err) + return err + } pool.Go(func(stop chan bool) { for { select { @@ -82,12 +86,15 @@ func (provider *Marathon) Provide(configurationChan chan<- types.ConfigMessage, } }) } + return nil } - configuration := provider.loadMarathonConfig() - configurationChan <- types.ConfigMessage{ - ProviderName: "marathon", - Configuration: configuration, + notify := func(err error, time time.Duration) { + log.Errorf("Marathon connection error %+v, retrying in %s", err, time) + } + err := backoff.RetryNotify(operation, backoff.NewExponentialBackOff(), notify) + if err != nil { + log.Fatalf("Cannot connect to Marathon server %+v", err) } return nil }