diff --git a/cmd.go b/cmd.go index b39a1e6f1..b97406fa3 100644 --- a/cmd.go +++ b/cmd.go @@ -4,12 +4,12 @@ Copyright package main import ( + "encoding/json" fmtlog "log" "os" "strings" "time" - "encoding/json" log "github.com/Sirupsen/logrus" "github.com/emilevauge/traefik/middlewares" "github.com/emilevauge/traefik/provider" diff --git a/configuration.go b/configuration.go index e02a1fbb5..facd3e8f4 100644 --- a/configuration.go +++ b/configuration.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" fmtlog "log" + "regexp" "strings" "time" @@ -11,7 +12,6 @@ import ( "github.com/emilevauge/traefik/types" "github.com/mitchellh/mapstructure" "github.com/spf13/viper" - "regexp" ) // GlobalConfiguration holds global configuration (with providers, etc.). diff --git a/integration/marathon_test.go b/integration/marathon_test.go index e13b25bbf..ea45381f4 100644 --- a/integration/marathon_test.go +++ b/integration/marathon_test.go @@ -1,11 +1,11 @@ package main import ( + "fmt" "net/http" "os/exec" "time" - "fmt" checker "github.com/vdemeester/shakers" check "gopkg.in/check.v1" ) diff --git a/provider/docker.go b/provider/docker.go index e4e6a1f5b..68ab5f958 100644 --- a/provider/docker.go +++ b/provider/docker.go @@ -107,6 +107,7 @@ func (provider *Docker) loadDockerConfig(containersInspected []docker.Container) "getDomain": provider.getDomain, "getProtocol": provider.getProtocol, "getPassHostHeader": provider.getPassHostHeader, + "getEntryPoints": provider.getEntryPoints, "getFrontendValue": provider.getFrontendValue, "getFrontendRule": provider.getFrontendRule, "replace": replace, @@ -234,6 +235,13 @@ func (provider *Docker) getPassHostHeader(container docker.Container) string { return "false" } +func (provider *Docker) getEntryPoints(container docker.Container) []string { + if entryPoints, err := getLabel(container, "traefik.frontend.entryPoints"); err == nil { + return strings.Split(entryPoints, ",") + } + return []string{} +} + func getLabel(container docker.Container, label string) (string, error) { for key, value := range container.Config.Labels { if key == label { diff --git a/provider/kv.go b/provider/kv.go index 4fc5cb143..c8c674721 100644 --- a/provider/kv.go +++ b/provider/kv.go @@ -73,9 +73,10 @@ func (provider *Kv) loadConfig() *types.Configuration { provider.Prefix, } var KvFuncMap = template.FuncMap{ - "List": provider.list, - "Get": provider.get, - "Last": provider.last, + "List": provider.list, + "Get": provider.get, + "SplitGet": provider.splitGet, + "Last": provider.last, } configuration, err := provider.getConfiguration("templates/kv.tmpl", KvFuncMap, templateObjects) @@ -89,7 +90,7 @@ func (provider *Kv) list(keys ...string) []string { joinedKeys := strings.Join(keys, "") keysPairs, err := provider.kvclient.List(joinedKeys) if err != nil { - log.Error("Error getting keys: ", joinedKeys, err) + log.Errorf("Error getting keys %s %s ", joinedKeys, err) return nil } directoryKeys := make(map[string]string) @@ -100,18 +101,32 @@ func (provider *Kv) list(keys ...string) []string { return fun.Values(directoryKeys).([]string) } -func (provider *Kv) get(keys ...string) string { +func (provider *Kv) get(defaultValue string, keys ...string) string { joinedKeys := strings.Join(keys, "") keyPair, err := provider.kvclient.Get(joinedKeys) if err != nil { - log.Error("Error getting key: ", joinedKeys, err) - return "" + log.Warnf("Error getting key %s %s, setting default %s", joinedKeys, err, defaultValue) + return defaultValue } else if keyPair == nil { - return "" + log.Warnf("Error getting key %s, setting default %s", joinedKeys, defaultValue) + return defaultValue } return string(keyPair.Value) } +func (provider *Kv) splitGet(keys ...string) []string { + joinedKeys := strings.Join(keys, "") + keyPair, err := provider.kvclient.Get(joinedKeys) + if err != nil { + log.Warnf("Error getting key %s %s, setting default empty", joinedKeys, err) + return []string{} + } else if keyPair == nil { + log.Warnf("Error getting key %s, setting default %empty", joinedKeys) + return []string{} + } + return strings.Split(string(keyPair.Value), ",") +} + func (provider *Kv) last(key string) string { splittedKey := strings.Split(key, "/") return splittedKey[len(splittedKey)-1] diff --git a/provider/marathon.go b/provider/marathon.go index 7cc04c483..26982e460 100644 --- a/provider/marathon.go +++ b/provider/marathon.go @@ -4,6 +4,7 @@ import ( "errors" "net/url" "strconv" + "strings" "text/template" "github.com/BurntSushi/ty/fun" @@ -86,6 +87,7 @@ func (provider *Marathon) loadMarathonConfig() *types.Configuration { "getDomain": provider.getDomain, "getProtocol": provider.getProtocol, "getPassHostHeader": provider.getPassHostHeader, + "getEntryPoints": provider.getEntryPoints, "getFrontendValue": provider.getFrontendValue, "getFrontendRule": provider.getFrontendRule, "replace": replace, @@ -286,6 +288,13 @@ func (provider *Marathon) getPassHostHeader(application marathon.Application) st return "false" } +func (provider *Marathon) getEntryPoints(application marathon.Application) []string { + if entryPoints, err := provider.getLabel(application, "traefik.frontend.entryPoints"); err == nil { + return strings.Split(entryPoints, ",") + } + return []string{} +} + // getFrontendValue returns the frontend value for the specified application, using // it's label. It returns a default one if the label is not present. func (provider *Marathon) getFrontendValue(application marathon.Application) string { diff --git a/templates/docker.tmpl b/templates/docker.tmpl index 89b79f90a..afbee282c 100644 --- a/templates/docker.tmpl +++ b/templates/docker.tmpl @@ -8,6 +8,9 @@ [frontends."frontend-{{$frontend}}"]{{$container := index $containers 0}} backend = "backend-{{getBackend $container}}" passHostHeader = {{getPassHostHeader $container}} + entryPoints = [{{range getEntryPoints $container}} + "{{.}}", + {{end}}] [frontends."frontend-{{$frontend}}".routes."route-frontend-{{$frontend}}"] rule = "{{getFrontendRule $container}}" value = "{{getFrontendValue $container}}" diff --git a/templates/kv.tmpl b/templates/kv.tmpl index f5bb470ac..9fd770d24 100644 --- a/templates/kv.tmpl +++ b/templates/kv.tmpl @@ -5,13 +5,13 @@ {{$backend := .}} {{$servers := List $backend "/servers/" }} -{{$circuitBreaker := Get . "/circuitbreaker/" "expression"}} +{{$circuitBreaker := Get "" . "/circuitbreaker/" "expression"}} {{with $circuitBreaker}} [backends.{{Last $backend}}.circuitBreaker] expression = "{{$circuitBreaker}}" {{end}} -{{$loadBalancer := Get . "/loadbalancer/" "method"}} +{{$loadBalancer := Get "" . "/loadbalancer/" "method"}} {{with $loadBalancer}} [backends.{{Last $backend}}.loadBalancer] method = "{{$loadBalancer}}" @@ -19,20 +19,24 @@ {{range $servers}} [backends.{{Last $backend}}.servers.{{Last .}}] - url = "{{Get . "/url"}}" - weight = {{Get . "/weight"}} + url = "{{Get "" . "/url"}}" + weight = {{Get "" . "/weight"}} {{end}} {{end}} [frontends]{{range $frontends}} {{$frontend := Last .}} + {{$entryPoints := SplitGet . "/entrypoints"}} [frontends.{{$frontend}}] - backend = "{{Get . "/backend"}}" - passHostHeader = {{Get . "/passHostHeader"}} + backend = "{{Get "" . "/backend"}}" + passHostHeader = {{Get "false" . "/passHostHeader"}} + entryPoints = [{{range $entryPoints}} + "{{.}}", + {{end}}] {{$routes := List . "/routes/"}} {{range $routes}} [frontends.{{$frontend}}.routes.{{Last .}}] - rule = "{{Get . "/rule"}}" - value = "{{Get . "/value"}}" + rule = "{{Get "" . "/rule"}}" + value = "{{Get "" . "/value"}}" {{end}} {{end}} diff --git a/templates/marathon.tmpl b/templates/marathon.tmpl index ae31e9f8f..5558924fd 100644 --- a/templates/marathon.tmpl +++ b/templates/marathon.tmpl @@ -9,6 +9,9 @@ [frontends.frontend{{.ID | replace "/" "-"}}] backend = "backend{{getBackend .}}" passHostHeader = {{getPassHostHeader .}} + entryPoints = [{{range getEntryPoints .}} + "{{.}}", + {{end}}] [frontends.frontend{{.ID | replace "/" "-"}}.routes.route-host{{.ID | replace "/" "-"}}] rule = "{{getFrontendRule .}}" value = "{{getFrontendValue .}}" diff --git a/tests/consul-config.sh b/tests/consul-config.sh index e567f3319..58952031e 100755 --- a/tests/consul-config.sh +++ b/tests/consul-config.sh @@ -16,10 +16,12 @@ curl -i -H "Accept: application/json" -X PUT -d "2" ht # frontend 1 curl -i -H "Accept: application/json" -X PUT -d "backend2" http://localhost:8500/v1/kv/traefik/frontends/frontend1/backend +curl -i -H "Accept: application/json" -X PUT -d "http" http://localhost:8500/v1/kv/traefik/frontends/frontend1/entrypoints curl -i -H "Accept: application/json" -X PUT -d "Host" http://localhost:8500/v1/kv/traefik/frontends/frontend1/routes/test_1/rule curl -i -H "Accept: application/json" -X PUT -d "test.localhost" http://localhost:8500/v1/kv/traefik/frontends/frontend1/routes/test_1/value # frontend 2 curl -i -H "Accept: application/json" -X PUT -d "backend1" http://localhost:8500/v1/kv/traefik/frontends/frontend2/backend +curl -i -H "Accept: application/json" -X PUT -d "http,https" http://localhost:8500/v1/kv/traefik/frontends/frontend2/entrypoints curl -i -H "Accept: application/json" -X PUT -d "Path" http://localhost:8500/v1/kv/traefik/frontends/frontend2/routes/test_2/rule curl -i -H "Accept: application/json" -X PUT -d "/test" http://localhost:8500/v1/kv/traefik/frontends/frontend2/routes/test_2/value