From 4dc5b34122f904585b876fc038c20d6c813ca63e Mon Sep 17 00:00:00 2001 From: emile Date: Thu, 10 Sep 2015 16:14:08 +0200 Subject: [PATCH] Weights --- ROADMAP.md | 3 ++- configuration.go | 1 + docker.go | 8 ++++++++ docker.tmpl | 1 + marathon.go | 8 ++++++++ marathon.tmpl | 7 +++++++ træfik.go | 2 +- træfik.toml | 21 ++++++++++++--------- 8 files changed, 40 insertions(+), 11 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index ad7d8a0c5..11601e918 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,6 @@ * Godoc * Weights * Licence -* GraceTimeout * Filter no exposed port apps * Filter træfik.enabled=false apps * Add træfik.indlude all/enabled policy @@ -14,3 +13,5 @@ * Smart configuration diff * README + +* ~~GraceTimeout~~ \ No newline at end of file diff --git a/configuration.go b/configuration.go index fda2f8150..f9960280a 100644 --- a/configuration.go +++ b/configuration.go @@ -24,6 +24,7 @@ type Backend struct { type Server struct { Url string + Weight int } type Rule struct { diff --git a/docker.go b/docker.go index ab1a18256..67ef12d7b 100644 --- a/docker.go +++ b/docker.go @@ -37,6 +37,14 @@ var DockerFuncMap = template.FuncMap{ } return "" }, + "getWeight": func(container docker.Container) string { + for key, value := range container.Config.Labels { + if (key == "træfik.weight") { + return value + } + } + return "0" + }, "replace": func(s1 string, s2 string, s3 string) string { return strings.Replace(s3, s1, s2, -1) }, diff --git a/docker.tmpl b/docker.tmpl index 022e8f49c..28ff42b98 100644 --- a/docker.tmpl +++ b/docker.tmpl @@ -1,6 +1,7 @@ [backends]{{range .Containers}} [backends.backend-{{getBackend .}}.servers.server-{{.Name | replace "/" "" | replace "." "-"}}] url = "http://{{.NetworkSettings.IPAddress}}:{{getPort .}}" + weight = {{getWeight .}} {{end}} [routes]{{range $host, $containers := .Hosts}} diff --git a/marathon.go b/marathon.go index a03ed3321..bf3ade064 100644 --- a/marathon.go +++ b/marathon.go @@ -34,6 +34,14 @@ var MarathonFuncMap = template.FuncMap{ } return strings.Replace(application.ID, "/", "", 1) }, + "getWeight": func(application marathon.Application) string { + for key, value := range application.Labels { + if (key == "træfik.weight") { + return value + } + } + return "0" + }, "replace": func(s1 string, s2 string, s3 string) string { return strings.Replace(s3, s1, s2, -1) }, diff --git a/marathon.tmpl b/marathon.tmpl index e58998bf2..a00c02699 100644 --- a/marathon.tmpl +++ b/marathon.tmpl @@ -1,6 +1,13 @@ +{{$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}} [routes]{{range .Applications}} diff --git a/træfik.go b/træfik.go index b763f01e1..e19fa2346 100644 --- a/træfik.go +++ b/træfik.go @@ -137,7 +137,7 @@ func LoadConfig(configuration *Configuration) *mux.Router { for serverName, server := range configuration.Backends[route.Backend].Servers { log.Println("Creating server", serverName) url, _ := url.Parse(server.Url) - rb.UpsertServer(url) + rb.UpsertServer(url, roundrobin.Weight(server.Weight)) } backends[route.Backend]=lb }else { diff --git a/træfik.toml b/træfik.toml index 906770c03..8020cfd82 100644 --- a/træfik.toml +++ b/træfik.toml @@ -1,35 +1,38 @@ port = ":8001" graceTimeOut = 10 -#[docker] -#endpoint = "unix:///var/run/docker.sock" -#watch = true -#domain = "toto.fr" -#filename = "docker.tmpl" +# [docker] +# endpoint = "unix:///var/run/docker.sock" +# watch = true +# domain = "toto.fr" +# filename = "docker.tmpl" [marathon] endpoint = "http://127.0.0.1:8080" +networkInterface = "eth0" watch = true domain = "toto.fr" filename = "marathon.tmpl" -networkInterface = "eth0" [web] address = ":8010" -#[file] -#filename = "rules.toml" -#watch = true +# [file] +# filename = "rules.toml" +# watch = true [backends] [backends.backend1] [backends.backend1.servers.server1] url = "http://172.17.0.2:80" + weight = 10 [backends.backend1.servers.server2] url = "http://172.17.0.3:80" + weight = 1 [backends.backend2] [backends.backend2.servers.server1] url = "http://172.17.0.2:80" + weight = 100 [routes] [routes.route1]