Add entrypoints to providers

This commit is contained in:
emile 2016-02-01 16:08:58 +01:00
parent 81cb00573f
commit a8cc26fd91
No known key found for this signature in database
GPG key ID: D808B4C167352E59
10 changed files with 63 additions and 19 deletions

2
cmd.go
View file

@ -4,12 +4,12 @@ Copyright
package main package main
import ( import (
"encoding/json"
fmtlog "log" fmtlog "log"
"os" "os"
"strings" "strings"
"time" "time"
"encoding/json"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/emilevauge/traefik/middlewares" "github.com/emilevauge/traefik/middlewares"
"github.com/emilevauge/traefik/provider" "github.com/emilevauge/traefik/provider"

View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
fmtlog "log" fmtlog "log"
"regexp"
"strings" "strings"
"time" "time"
@ -11,7 +12,6 @@ import (
"github.com/emilevauge/traefik/types" "github.com/emilevauge/traefik/types"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/spf13/viper" "github.com/spf13/viper"
"regexp"
) )
// GlobalConfiguration holds global configuration (with providers, etc.). // GlobalConfiguration holds global configuration (with providers, etc.).

View file

@ -1,11 +1,11 @@
package main package main
import ( import (
"fmt"
"net/http" "net/http"
"os/exec" "os/exec"
"time" "time"
"fmt"
checker "github.com/vdemeester/shakers" checker "github.com/vdemeester/shakers"
check "gopkg.in/check.v1" check "gopkg.in/check.v1"
) )

View file

@ -107,6 +107,7 @@ func (provider *Docker) loadDockerConfig(containersInspected []docker.Container)
"getDomain": provider.getDomain, "getDomain": provider.getDomain,
"getProtocol": provider.getProtocol, "getProtocol": provider.getProtocol,
"getPassHostHeader": provider.getPassHostHeader, "getPassHostHeader": provider.getPassHostHeader,
"getEntryPoints": provider.getEntryPoints,
"getFrontendValue": provider.getFrontendValue, "getFrontendValue": provider.getFrontendValue,
"getFrontendRule": provider.getFrontendRule, "getFrontendRule": provider.getFrontendRule,
"replace": replace, "replace": replace,
@ -234,6 +235,13 @@ func (provider *Docker) getPassHostHeader(container docker.Container) string {
return "false" 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) { func getLabel(container docker.Container, label string) (string, error) {
for key, value := range container.Config.Labels { for key, value := range container.Config.Labels {
if key == label { if key == label {

View file

@ -73,9 +73,10 @@ func (provider *Kv) loadConfig() *types.Configuration {
provider.Prefix, provider.Prefix,
} }
var KvFuncMap = template.FuncMap{ var KvFuncMap = template.FuncMap{
"List": provider.list, "List": provider.list,
"Get": provider.get, "Get": provider.get,
"Last": provider.last, "SplitGet": provider.splitGet,
"Last": provider.last,
} }
configuration, err := provider.getConfiguration("templates/kv.tmpl", KvFuncMap, templateObjects) configuration, err := provider.getConfiguration("templates/kv.tmpl", KvFuncMap, templateObjects)
@ -89,7 +90,7 @@ func (provider *Kv) list(keys ...string) []string {
joinedKeys := strings.Join(keys, "") joinedKeys := strings.Join(keys, "")
keysPairs, err := provider.kvclient.List(joinedKeys) keysPairs, err := provider.kvclient.List(joinedKeys)
if err != nil { if err != nil {
log.Error("Error getting keys: ", joinedKeys, err) log.Errorf("Error getting keys %s %s ", joinedKeys, err)
return nil return nil
} }
directoryKeys := make(map[string]string) directoryKeys := make(map[string]string)
@ -100,18 +101,32 @@ func (provider *Kv) list(keys ...string) []string {
return fun.Values(directoryKeys).([]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, "") joinedKeys := strings.Join(keys, "")
keyPair, err := provider.kvclient.Get(joinedKeys) keyPair, err := provider.kvclient.Get(joinedKeys)
if err != nil { if err != nil {
log.Error("Error getting key: ", joinedKeys, err) log.Warnf("Error getting key %s %s, setting default %s", joinedKeys, err, defaultValue)
return "" return defaultValue
} else if keyPair == nil { } else if keyPair == nil {
return "" log.Warnf("Error getting key %s, setting default %s", joinedKeys, defaultValue)
return defaultValue
} }
return string(keyPair.Value) 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 { func (provider *Kv) last(key string) string {
splittedKey := strings.Split(key, "/") splittedKey := strings.Split(key, "/")
return splittedKey[len(splittedKey)-1] return splittedKey[len(splittedKey)-1]

View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"net/url" "net/url"
"strconv" "strconv"
"strings"
"text/template" "text/template"
"github.com/BurntSushi/ty/fun" "github.com/BurntSushi/ty/fun"
@ -86,6 +87,7 @@ func (provider *Marathon) loadMarathonConfig() *types.Configuration {
"getDomain": provider.getDomain, "getDomain": provider.getDomain,
"getProtocol": provider.getProtocol, "getProtocol": provider.getProtocol,
"getPassHostHeader": provider.getPassHostHeader, "getPassHostHeader": provider.getPassHostHeader,
"getEntryPoints": provider.getEntryPoints,
"getFrontendValue": provider.getFrontendValue, "getFrontendValue": provider.getFrontendValue,
"getFrontendRule": provider.getFrontendRule, "getFrontendRule": provider.getFrontendRule,
"replace": replace, "replace": replace,
@ -286,6 +288,13 @@ func (provider *Marathon) getPassHostHeader(application marathon.Application) st
return "false" 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 // getFrontendValue returns the frontend value for the specified application, using
// it's label. It returns a default one if the label is not present. // it's label. It returns a default one if the label is not present.
func (provider *Marathon) getFrontendValue(application marathon.Application) string { func (provider *Marathon) getFrontendValue(application marathon.Application) string {

View file

@ -8,6 +8,9 @@
[frontends."frontend-{{$frontend}}"]{{$container := index $containers 0}} [frontends."frontend-{{$frontend}}"]{{$container := index $containers 0}}
backend = "backend-{{getBackend $container}}" backend = "backend-{{getBackend $container}}"
passHostHeader = {{getPassHostHeader $container}} passHostHeader = {{getPassHostHeader $container}}
entryPoints = [{{range getEntryPoints $container}}
"{{.}}",
{{end}}]
[frontends."frontend-{{$frontend}}".routes."route-frontend-{{$frontend}}"] [frontends."frontend-{{$frontend}}".routes."route-frontend-{{$frontend}}"]
rule = "{{getFrontendRule $container}}" rule = "{{getFrontendRule $container}}"
value = "{{getFrontendValue $container}}" value = "{{getFrontendValue $container}}"

View file

@ -5,13 +5,13 @@
{{$backend := .}} {{$backend := .}}
{{$servers := List $backend "/servers/" }} {{$servers := List $backend "/servers/" }}
{{$circuitBreaker := Get . "/circuitbreaker/" "expression"}} {{$circuitBreaker := Get "" . "/circuitbreaker/" "expression"}}
{{with $circuitBreaker}} {{with $circuitBreaker}}
[backends.{{Last $backend}}.circuitBreaker] [backends.{{Last $backend}}.circuitBreaker]
expression = "{{$circuitBreaker}}" expression = "{{$circuitBreaker}}"
{{end}} {{end}}
{{$loadBalancer := Get . "/loadbalancer/" "method"}} {{$loadBalancer := Get "" . "/loadbalancer/" "method"}}
{{with $loadBalancer}} {{with $loadBalancer}}
[backends.{{Last $backend}}.loadBalancer] [backends.{{Last $backend}}.loadBalancer]
method = "{{$loadBalancer}}" method = "{{$loadBalancer}}"
@ -19,20 +19,24 @@
{{range $servers}} {{range $servers}}
[backends.{{Last $backend}}.servers.{{Last .}}] [backends.{{Last $backend}}.servers.{{Last .}}]
url = "{{Get . "/url"}}" url = "{{Get "" . "/url"}}"
weight = {{Get . "/weight"}} weight = {{Get "" . "/weight"}}
{{end}} {{end}}
{{end}} {{end}}
[frontends]{{range $frontends}} [frontends]{{range $frontends}}
{{$frontend := Last .}} {{$frontend := Last .}}
{{$entryPoints := SplitGet . "/entrypoints"}}
[frontends.{{$frontend}}] [frontends.{{$frontend}}]
backend = "{{Get . "/backend"}}" backend = "{{Get "" . "/backend"}}"
passHostHeader = {{Get . "/passHostHeader"}} passHostHeader = {{Get "false" . "/passHostHeader"}}
entryPoints = [{{range $entryPoints}}
"{{.}}",
{{end}}]
{{$routes := List . "/routes/"}} {{$routes := List . "/routes/"}}
{{range $routes}} {{range $routes}}
[frontends.{{$frontend}}.routes.{{Last .}}] [frontends.{{$frontend}}.routes.{{Last .}}]
rule = "{{Get . "/rule"}}" rule = "{{Get "" . "/rule"}}"
value = "{{Get . "/value"}}" value = "{{Get "" . "/value"}}"
{{end}} {{end}}
{{end}} {{end}}

View file

@ -9,6 +9,9 @@
[frontends.frontend{{.ID | replace "/" "-"}}] [frontends.frontend{{.ID | replace "/" "-"}}]
backend = "backend{{getBackend .}}" backend = "backend{{getBackend .}}"
passHostHeader = {{getPassHostHeader .}} passHostHeader = {{getPassHostHeader .}}
entryPoints = [{{range getEntryPoints .}}
"{{.}}",
{{end}}]
[frontends.frontend{{.ID | replace "/" "-"}}.routes.route-host{{.ID | replace "/" "-"}}] [frontends.frontend{{.ID | replace "/" "-"}}.routes.route-host{{.ID | replace "/" "-"}}]
rule = "{{getFrontendRule .}}" rule = "{{getFrontendRule .}}"
value = "{{getFrontendValue .}}" value = "{{getFrontendValue .}}"

View file

@ -16,10 +16,12 @@ curl -i -H "Accept: application/json" -X PUT -d "2" ht
# frontend 1 # 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 "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 "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 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 # 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 "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 "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 curl -i -H "Accept: application/json" -X PUT -d "/test" http://localhost:8500/v1/kv/traefik/frontends/frontend2/routes/test_2/value