diff --git a/README.md b/README.md index 62a5f538a..84600bf31 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ ![Træfɪk](http://traefik.github.io/traefik.logo.svg "Træfɪk") ___ -[![Circle CI](https://img.shields.io/circleci/project/EmileVauge/traefik.svg)](https://circleci.com/gh/EmileVauge/traefik) +[![Circle CI](https://circleci.com/gh/emilevauge/traefik.svg?style=shield&circle-token=:circle-token)](https://circleci.com/gh/emilevauge/traefik) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/EmileVauge/traefik/blob/master/LICENSE.md) -[![Join the chat at https://gitter.im/EmileVauge/traefik](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/EmileVauge/traefik?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![Join the chat at https://traefik.herokuapp.com](https://img.shields.io/badge/style-register-green.svg?style=social&label=Slack)](https://traefik.herokuapp.com) Træfɪk is a modern HTTP reverse proxy and load balancer made to deploy microservices with ease. diff --git a/configuration.go b/configuration.go index 8507ab4c9..3778a17f5 100644 --- a/configuration.go +++ b/configuration.go @@ -103,3 +103,10 @@ func NewLoadBalancerMethod(loadBalancer *LoadBalancer) (LoadBalancerMethod, erro } var ErrInvalidLoadBalancerMethod = errors.New("Invalid method, using default") + +type configMessage struct { + providerName string + configuration *Configuration +} + +type configs map[string]*Configuration diff --git a/docker.go b/docker.go index aefe0efdb..45a29b8c6 100644 --- a/docker.go +++ b/docker.go @@ -8,6 +8,7 @@ import ( "text/template" "time" + "fmt" "github.com/BurntSushi/toml" "github.com/BurntSushi/ty/fun" log "github.com/Sirupsen/logrus" @@ -73,18 +74,14 @@ func (provider *DockerProvider) Provide(configurationChan chan<- configMessage) func (provider *DockerProvider) loadDockerConfig(dockerClient *docker.Client) *Configuration { var DockerFuncMap = template.FuncMap{ "getBackend": func(container docker.Container) string { - for key, value := range container.Config.Labels { - if key == "traefik.backend" { - return value - } + if label, err := provider.getLabel(container, "traefik.backend"); err == nil { + return label } - return getHost(container) + return provider.getEscapedName(container.Name) }, "getPort": func(container docker.Container) string { - for key, value := range container.Config.Labels { - if key == "traefik.port" { - return value - } + if label, err := provider.getLabel(container, "traefik.port"); err == nil { + return label } for key := range container.NetworkSettings.Ports { return key.Port() @@ -92,30 +89,33 @@ func (provider *DockerProvider) loadDockerConfig(dockerClient *docker.Client) *C return "" }, "getWeight": func(container docker.Container) string { - for key, value := range container.Config.Labels { - if key == "traefik.weight" { - return value - } + if label, err := provider.getLabel(container, "traefik.weight"); err == nil { + return label } return "0" }, "getDomain": func(container docker.Container) string { - for key, value := range container.Config.Labels { - if key == "traefik.domain" { - return value - } + if label, err := provider.getLabel(container, "traefik.domain"); err == nil { + return label } return provider.Domain }, + "getProtocol": func(container docker.Container) string { + if label, err := provider.getLabel(container, "traefik.protocol"); err == nil { + return label + } + return "http" + }, + "getFrontendValue": provider.GetFrontendValue, + "getFrontendRule": provider.GetFrontendRule, "replace": func(s1 string, s2 string, s3 string) string { return strings.Replace(s3, s1, s2, -1) }, - "getHost": getHost, } configuration := new(Configuration) containerList, _ := dockerClient.ListContainers(docker.ListContainersOptions{}) containersInspected := []docker.Container{} - hosts := map[string][]docker.Container{} + frontends := map[string][]docker.Container{} // get inspect containers for _, container := range containerList { @@ -138,20 +138,26 @@ func (provider *DockerProvider) loadDockerConfig(dockerClient *docker.Client) *C log.Debugf("Filtering disabled container %s", container.Name) return false } + + if _, err := provider.getLabels(container, []string{"traefik.frontend.rule", "traefik.frontend.value"}); err != nil { + log.Debugf("Filtering bad labeled container %s", container.Name) + return false + } + return true }, containersInspected).([]docker.Container) for _, container := range filteredContainers { - hosts[getHost(container)] = append(hosts[getHost(container)], container) + frontends[provider.getFrontendName(container)] = append(frontends[provider.getFrontendName(container)], container) } templateObjects := struct { Containers []docker.Container - Hosts map[string][]docker.Container + Frontends map[string][]docker.Container Domain string }{ filteredContainers, - hosts, + frontends, provider.Domain, } tmpl := template.New(provider.Filename).Funcs(DockerFuncMap) @@ -162,7 +168,7 @@ func (provider *DockerProvider) loadDockerConfig(dockerClient *docker.Client) *C return nil } } else { - buf, err := Asset("providerTemplates/docker.tmpl") + buf, err := Asset("templates/docker.tmpl") if err != nil { log.Error("Error reading file", err) } @@ -181,17 +187,53 @@ func (provider *DockerProvider) loadDockerConfig(dockerClient *docker.Client) *C } if _, err := toml.Decode(buffer.String(), configuration); err != nil { - log.Error("Error creating docker configuration", err) + log.Error("Error creating docker configuration ", err) return nil } return configuration } -func getHost(container docker.Container) string { +func (provider *DockerProvider) getFrontendName(container docker.Container) string { + // Replace '.' with '-' in quoted keys because of this issue https://github.com/BurntSushi/toml/issues/78 + frontendName := fmt.Sprintf("%s-%s", provider.GetFrontendRule(container), provider.GetFrontendValue(container)) + return strings.Replace(frontendName, ".", "-", -1) +} + +func (provider *DockerProvider) getEscapedName(name string) string { + return strings.Replace(name, "/", "", -1) +} + +func (provider *DockerProvider) getLabel(container docker.Container, label string) (string, error) { for key, value := range container.Config.Labels { - if key == "traefik.host" { - return value + if key == label { + return value, nil } } - return strings.Replace(strings.Replace(container.Name, "/", "", -1), ".", "-", -1) + return "", errors.New("Label not found:" + label) +} + +func (provider *DockerProvider) getLabels(container docker.Container, labels []string) (map[string]string, error) { + foundLabels := map[string]string{} + for _, label := range labels { + if foundLabel, err := provider.getLabel(container, label); err != nil { + return nil, errors.New("Label not found: " + label) + } else { + foundLabels[label] = foundLabel + } + } + return foundLabels, nil +} + +func (provider *DockerProvider) GetFrontendValue(container docker.Container) string { + if label, err := provider.getLabel(container, "traefik.frontend.value"); err == nil { + return label + } + return provider.getEscapedName(container.Name) + "." + provider.Domain +} + +func (provider *DockerProvider) GetFrontendRule(container docker.Container) string { + if label, err := provider.getLabel(container, "traefik.frontend.rule"); err == nil { + return label + } + return "Host" } diff --git a/docs/index.md b/docs/index.md index ffdd05d7b..743215fc4 100644 --- a/docs/index.md +++ b/docs/index.md @@ -25,7 +25,7 @@ It supports several backends ([Docker :whale:](https://www.docker.com/), [Mesos/ Basically, Træfɪk is a http router, which sends traffic from frontends to http backends, following rules you have configured. -### Frontends +### Frontends Frontends can be defined using the following rules: @@ -407,11 +407,14 @@ Labels can be used on containers to override default behaviour: - `traefik.backend=foo`: assign the container to `foo` backend - `traefik.port=80`: register this port. Useful when the container exposes multiples ports. +- `traefik.protocol=https`: override the default `http` protocol - `traefik.weight=10`: assign this weight to the container - `traefik.enable=false`: disable this container in Træfɪk -- `traefik.host=bar`: override the default routing from `{containerName}.{domain}` to `bar.{domain}` +- `traefik.frontend.rule=Host`: override the default frontend rule (Default: Host). See [frontends](#frontends). +- `traefik.frontend.value=test.example.com`: override the default frontend value (Default: `{containerName}.{domain}`) See [frontends](#frontends). * `traefik.domain=traefik.localhost`: override the default domain + ## Marathon backend Træfɪk can be configured to use Marathon as a backend configuration: @@ -466,10 +469,11 @@ Labels can be used on containers to override default behaviour: - `traefik.backend=foo`: assign the application to `foo` backend - `traefik.port=80`: register this port. Useful when the application exposes multiples ports. +- `traefik.protocol=https`: override the default `http` protocol - `traefik.weight=10`: assign this weight to the application - `traefik.enable=false`: disable this application in Træfɪk -- `traefik.host=bar`: override the default routing from `{appName}.{domain}` to `bar.{domain}` -- `traefik.prefixes=pf1,pf2`: use `PathPrefix(es)` instead of hostname for routing, use `filename="providerTemplates/marathon-prefix.tmpl"` with this option +- `traefik.frontend.rule=Host`: override the default frontend rule (Default: Host). See [frontends](#frontends). +- `traefik.frontend.value=test.example.com`: override the default frontend value (Default: `{appName}.{domain}`) See [frontends](#frontends). * `traefik.domain=traefik.localhost`: override the default domain ## Consul backend diff --git a/generate.go b/generate.go index 4757a70bd..f7b6e4ec3 100644 --- a/generate.go +++ b/generate.go @@ -4,6 +4,6 @@ Copyright //go:generate go get github.com/jteeuwen/go-bindata/... //go:generate rm -vf gen.go -//go:generate go-bindata -o gen.go static/... templates/... providerTemplates/... +//go:generate go-bindata -o gen.go static/... templates/... package main diff --git a/kv.go b/kv.go index 4d3f7dd82..44ab3401b 100644 --- a/kv.go +++ b/kv.go @@ -88,6 +88,7 @@ func (provider *KvProvider) provide(configurationChan chan<- configMessage) erro []string{provider.Endpoint}, &store.Config{ ConnectionTimeout: 30 * time.Second, + Bucket: "traefik", }, ) if err != nil { @@ -166,7 +167,7 @@ func (provider *KvProvider) loadConfig() *Configuration { return nil } } else { - buf, err := Asset("providerTemplates/kv.tmpl") + buf, err := Asset("templates/kv.tmpl") if err != nil { log.Error("Error reading file", err) } diff --git a/marathon.go b/marathon.go index 46b197007..f79558d0a 100644 --- a/marathon.go +++ b/marathon.go @@ -6,6 +6,7 @@ import ( "strings" "text/template" + "errors" "github.com/BurntSushi/toml" "github.com/BurntSushi/ty/fun" log "github.com/Sirupsen/logrus" @@ -62,41 +63,39 @@ func (provider *MarathonProvider) loadMarathonConfig() *Configuration { } return "" }, - "getHost": func(application marathon.Application) string { - for key, value := range application.Labels { - if key == "traefik.host" { - return value - } + "getWeight": func(task marathon.Task, applications []marathon.Application) string { + application, errApp := getApplication(task, applications) + if errApp != nil { + log.Errorf("Unable to get marathon application from task %s", task.AppID) + return "0" } - return strings.Replace(application.ID, "/", "", 1) - }, - "getWeight": func(application marathon.Application) string { - for key, value := range application.Labels { - if key == "traefik.weight" { - return value - } + if label, err := provider.getLabel(application, "traefik.weight"); err == nil { + return label } return "0" }, "getDomain": func(application marathon.Application) string { - for key, value := range application.Labels { - if key == "traefik.domain" { - return value - } + if label, err := provider.getLabel(application, "traefik.domain"); err == nil { + return label } return provider.Domain }, - "getPrefixes": func(application marathon.Application) ([]string, error) { - for key, value := range application.Labels { - if key == "traefik.prefixes" { - return strings.Split(value, ","), nil - } - } - return []string{}, nil - }, "replace": func(s1 string, s2 string, s3 string) string { return strings.Replace(s3, s1, s2, -1) }, + "getProtocol": func(task marathon.Task, applications []marathon.Application) string { + application, errApp := getApplication(task, applications) + if errApp != nil { + log.Errorf("Unable to get marathon application from task %s", task.AppID) + return "http" + } + if label, err := provider.getLabel(application, "traefik.protocol"); err == nil { + return label + } + return "http" + }, + "getFrontendValue": provider.GetFrontendValue, + "getFrontendRule": provider.GetFrontendRule, } configuration := new(Configuration) @@ -118,8 +117,8 @@ func (provider *MarathonProvider) loadMarathonConfig() *Configuration { log.Debug("Filtering marathon task without port", task.AppID) return false } - application := getApplication(task, applications.Apps) - if application == nil { + application, errApp := getApplication(task, applications.Apps) + if errApp != nil { log.Errorf("Unable to get marathon application from task %s", task.AppID) return false } @@ -167,7 +166,7 @@ func (provider *MarathonProvider) loadMarathonConfig() *Configuration { return nil } } else { - buf, err := Asset("providerTemplates/marathon.tmpl") + buf, err := Asset("templates/marathon.tmpl") if err != nil { log.Error("Error reading file", err) } @@ -194,11 +193,38 @@ func (provider *MarathonProvider) loadMarathonConfig() *Configuration { return configuration } -func getApplication(task marathon.Task, apps []marathon.Application) *marathon.Application { +func getApplication(task marathon.Task, apps []marathon.Application) (marathon.Application, error) { for _, application := range apps { if application.ID == task.AppID { - return &application + return application, nil } } - return nil + return marathon.Application{}, errors.New("Application not found: " + task.AppID) +} + +func (provider *MarathonProvider) getLabel(application marathon.Application, label string) (string, error) { + for key, value := range application.Labels { + if key == label { + return value, nil + } + } + return "", errors.New("Label not found:" + label) +} + +func (provider *MarathonProvider) getEscapedName(name string) string { + return strings.Replace(name, "/", "", -1) +} + +func (provider *MarathonProvider) GetFrontendValue(application marathon.Application) string { + if label, err := provider.getLabel(application, "traefik.frontend.value"); err == nil { + return label + } + return provider.getEscapedName(application.ID) + "." + provider.Domain +} + +func (provider *MarathonProvider) GetFrontendRule(application marathon.Application) string { + if label, err := provider.getLabel(application, "traefik.frontend.rule"); err == nil { + return label + } + return "Host" } diff --git a/middlewares/websocketproxy.go b/middlewares/websocketproxy.go index abb82688f..7d497016d 100644 --- a/middlewares/websocketproxy.go +++ b/middlewares/websocketproxy.go @@ -73,15 +73,16 @@ func NewProxy(target *url.URL) *WebsocketProxy { // ServeHTTP implements the http.Handler that proxies WebSocket connections. func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { if w.Backend == nil { - log.Println("websocketproxy: backend function is not defined") - http.Error(rw, "internal server error (code: 1)", http.StatusInternalServerError) + log.Errorf("Websocketproxy: backend function is not defined") + http.Error(rw, "Backend not found", http.StatusInternalServerError) + http.NotFound(rw, req) return } backendURL := w.Backend(req) if backendURL == nil { - log.Println("websocketproxy: backend URL is nil") - http.Error(rw, "internal server error (code: 2)", http.StatusInternalServerError) + log.Errorf("Websocketproxy: backend URL is nil") + http.Error(rw, "Backend URL is nil", http.StatusInternalServerError) return } @@ -131,7 +132,8 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { // http://tools.ietf.org/html/draft-ietf-hybi-websocket-multiplexing-01 connBackend, resp, err := dialer.Dial(backendURL.String(), nil) if err != nil { - log.Printf("websocketproxy: couldn't dial to remote backend url %s, %s, %+v", backendURL.String(), err, resp) + log.Errorf("Websocketproxy: couldn't dial to remote backend url %s, %s, %+v", backendURL.String(), err, resp) + http.Error(rw, "Remote backend unreachable", http.StatusBadGateway) return } defer connBackend.Close() @@ -152,7 +154,8 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { // Also pass the header that we gathered from the Dial handshake. connPub, err := upgrader.Upgrade(rw, req, upgradeHeader) if err != nil { - log.Printf("websocketproxy: couldn't upgrade %s\n", err) + log.Errorf("Websocketproxy: couldn't upgrade %s", err) + http.NotFound(rw, req) return } defer connPub.Close() diff --git a/providerTemplates/docker.tmpl b/providerTemplates/docker.tmpl deleted file mode 100644 index 9dfd6eac4..000000000 --- a/providerTemplates/docker.tmpl +++ /dev/null @@ -1,14 +0,0 @@ -[backends]{{range .Containers}} - [backends.backend-{{getBackend .}}.servers.server-{{.Name | replace "/" "" | replace "." "-"}}] - url = "http://{{.NetworkSettings.IPAddress}}:{{getPort .}}" - weight = {{getWeight .}} -{{end}} - -[frontends]{{range $host, $containers := .Hosts}} - [frontends.frontend-{{$host}}] - {{$container := index $containers 0}} - backend = "backend-{{getBackend $container}}" - [frontends.frontend-{{$host}}.routes.route-host-{{$host}}] - rule = "Host" - value = "{{$host}}.{{getDomain $container}}" -{{end}} \ No newline at end of file diff --git a/providerTemplates/marathon-prefix.tmpl b/providerTemplates/marathon-prefix.tmpl deleted file mode 100644 index 1a331626a..000000000 --- a/providerTemplates/marathon-prefix.tmpl +++ /dev/null @@ -1,27 +0,0 @@ -{{$apps := .Applications}} -[backends]{{range .Tasks}} - [backends.backend{{.AppID | replace "/" "-"}}.servers.server-{{.ID | replace "." "-"}}] - url = "http://{{.Host}}:{{getPort .}}" - {{$appID := .AppID}} - {{range $apps}} - {{if eq $appID .ID}} - weight = {{getWeight .}} - {{end}} - {{end}} -{{end}} - -[frontends]{{ range $app := .Applications}} - {{range $prefix := getPrefixes .}} - [frontends.frontend{{$app.ID | replace "/" "-"}}{{$prefix | replace "/" "-"}}] - backend = "backend{{$app.ID | replace "/" "-"}}" - [frontends.frontend-{{getHost $app | replace "/" "-"}}{{$prefix | replace "/" "-"}}.routes.route-prefix{{$prefix | replace "/" "-"}}] - rule = "PathPrefix" - value = "{{.}}" - {{else}} - [frontends.frontend{{.ID | replace "/" "-"}}] - backend = "backend{{.ID | replace "/" "-"}}" - [frontends.frontend-{{getHost $app | replace "/" "-"}}.routes.route-host-{{getHost $app | replace "/" "-"}}] - rule = "Host" - value = "{{getHost $app | replace "/" "-"}}.{{getDomain .}}" - {{end}} -{{end}} diff --git a/providerTemplates/marathon.tmpl b/providerTemplates/marathon.tmpl deleted file mode 100644 index 10f1fa249..000000000 --- a/providerTemplates/marathon.tmpl +++ /dev/null @@ -1,19 +0,0 @@ -{{$apps := .Applications}} -[backends]{{range .Tasks}} - [backends.backend{{.AppID | replace "/" "-"}}.servers.server-{{.ID | replace "." "-"}}] - url = "http://{{.Host}}:{{getPort .}}" - {{$appID := .AppID}} - {{range $apps}} - {{if eq $appID .ID}} - weight = {{getWeight .}} - {{end}} - {{end}} -{{end}} - -[frontends]{{range .Applications}} - [frontends.frontend{{.ID | replace "/" "-"}}] - backend = "backend{{.ID | replace "/" "-"}}" - [frontends.frontend-{{getHost . | replace "/" "-"}}.routes.route-host-{{getHost . | replace "/" "-"}}] - rule = "Host" - value = "{{getHost . | replace "/" "-"}}.{{getDomain .}}" -{{end}} \ No newline at end of file diff --git a/templates/docker.tmpl b/templates/docker.tmpl new file mode 100644 index 000000000..4aba4e124 --- /dev/null +++ b/templates/docker.tmpl @@ -0,0 +1,13 @@ +[backends]{{range .Containers}} + [backends.backend-{{getBackend .}}.servers.server-{{.Name | replace "/" "" | replace "." "-"}}] + url = "{{getProtocol .}}://{{.NetworkSettings.IPAddress}}:{{getPort .}}" + weight = {{getWeight .}} +{{end}} + +[frontends]{{range $frontend, $containers := .Frontends}} + [frontends."frontend-{{$frontend}}"]{{$container := index $containers 0}} + backend = "backend-{{getBackend $container}}" + [frontends."frontend-{{$frontend}}".routes."route-frontend-{{$frontend}}"] + rule = "{{getFrontendRule $container}}" + value = "{{getFrontendValue $container}}" +{{end}} diff --git a/providerTemplates/kv.tmpl b/templates/kv.tmpl similarity index 100% rename from providerTemplates/kv.tmpl rename to templates/kv.tmpl diff --git a/templates/marathon.tmpl b/templates/marathon.tmpl new file mode 100644 index 000000000..e79d792c9 --- /dev/null +++ b/templates/marathon.tmpl @@ -0,0 +1,14 @@ +{{$apps := .Applications}} +[backends]{{range .Tasks}} + [backends.backend{{.AppID | replace "/" "-"}}.servers.server-{{.ID | replace "." "-"}}] + url = "{{getProtocol . $apps}}://{{.Host}}:{{getPort .}}" + weight = {{getWeight . $apps}} +{{end}} + +[frontends]{{range .Applications}} + [frontends.frontend{{.ID | replace "/" "-"}}] + backend = "backend{{.ID | replace "/" "-"}}" + [frontends.frontend-{{.ID | replace "/" ""}}.routes.route-host-{{.ID | replace "/" ""}}] + rule = "{{getFrontendRule .}}" + value = "{{getFrontendValue .}}" +{{end}} \ No newline at end of file diff --git a/tests/whoami.json b/tests/whoami.json new file mode 100644 index 000000000..741d399d8 --- /dev/null +++ b/tests/whoami.json @@ -0,0 +1,39 @@ +{ + "id": "whoami", + "cpus": 0.1, + "mem": 64.0, + "instances": 3, + "container": { + "type": "DOCKER", + "docker": { + "image": "emilevauge/whoami", + "network": "BRIDGE", + "portMappings": [ + { "containerPort": 80, "hostPort": 0, "protocol": "tcp" } + ], + "parameters": [{ + "key": "log-driver", + "value": "gelf" + }, { + "key": "log-opt", + "value": "gelf-address=udp://172.17.42.1:12201" + }] + } + }, + "healthChecks": [ + { + "protocol": "HTTP", + "portIndex": 0, + "path": "/", + "gracePeriodSeconds": 5, + "intervalSeconds": 20, + "maxConsecutiveFailures": 3 + } + ], + "labels": { + "traefik.weight": "1", + "traefik.protocole": "https", + "traefik.frontend.rule": "Path", + "traefik.frontend.value": "/test" + } +} diff --git a/traefik.go b/traefik.go index a33b23fe1..63b83e70b 100644 --- a/traefik.go +++ b/traefik.go @@ -23,7 +23,6 @@ import ( "github.com/mailgun/oxy/forward" "github.com/mailgun/oxy/roundrobin" "github.com/thoas/stats" - "github.com/unrolled/render" "gopkg.in/alecthomas/kingpin.v2" "runtime" ) @@ -36,20 +35,8 @@ var ( currentConfigurations = make(configs) metrics = stats.New() oxyLogger = &OxyLogger{} - templatesRenderer = render.New(render.Options{ - Directory: "templates", - Asset: Asset, - AssetNames: AssetNames, - }) ) -type configMessage struct { - providerName string - configuration *Configuration -} - -type configs map[string]*Configuration - func main() { runtime.GOMAXPROCS(runtime.NumCPU()) kingpin.Version(Version + " built on the " + BuildDate) diff --git a/web.go b/web.go index ed2d4f70f..1d4db0617 100644 --- a/web.go +++ b/web.go @@ -9,6 +9,7 @@ import ( log "github.com/Sirupsen/logrus" "github.com/elazarl/go-bindata-assetfs" "github.com/gorilla/mux" + "github.com/unrolled/render" ) type WebProvider struct { @@ -16,6 +17,12 @@ type WebProvider struct { CertFile, KeyFile string } +var ( + templatesRenderer = render.New(render.Options{ + Directory: "nowhere", + }) +) + func (provider *WebProvider) Provide(configurationChan chan<- configMessage) error { systemRouter := mux.NewRouter()