From 61def880dbd072e8d520f2e2e8a1a68431257c89 Mon Sep 17 00:00:00 2001 From: Emile Vauge Date: Wed, 27 Apr 2016 22:28:02 +0200 Subject: [PATCH] Fix etcd backend with prefix / Signed-off-by: Emile Vauge --- examples/compose-etcd.yml | 4 ++++ examples/etcd-config.sh | 25 +++++++++++++++++++++++++ provider/kv.go | 4 ++-- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 examples/compose-etcd.yml create mode 100755 examples/etcd-config.sh diff --git a/examples/compose-etcd.yml b/examples/compose-etcd.yml new file mode 100644 index 000000000..62df465d3 --- /dev/null +++ b/examples/compose-etcd.yml @@ -0,0 +1,4 @@ +etcd: + image: gcr.io/google_containers/etcd:2.2.1 + net: host + command: ['/usr/local/bin/etcd', '--addr=127.0.0.1:4001', '--bind-addr=0.0.0.0:4001', '--data-dir=/var/etcd/data'] \ No newline at end of file diff --git a/examples/etcd-config.sh b/examples/etcd-config.sh new file mode 100755 index 000000000..648ad970b --- /dev/null +++ b/examples/etcd-config.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +# backend 1 +curl -i -H "Accept: application/json" -X PUT -d value="NetworkErrorRatio() > 0.5" http://localhost:4001/v2/keys/traefik/backends/backend1/circuitbreaker/expression +curl -i -H "Accept: application/json" -X PUT -d value="http://172.17.0.2:80" http://localhost:4001/v2/keys/traefik/backends/backend1/servers/server1/url +curl -i -H "Accept: application/json" -X PUT -d value="10" http://localhost:4001/v2/keys/traefik/backends/backend1/servers/server1/weight +curl -i -H "Accept: application/json" -X PUT -d value="http://172.17.0.3:80" http://localhost:4001/v2/keys/traefik/backends/backend1/servers/server2/url +curl -i -H "Accept: application/json" -X PUT -d value="1" http://localhost:4001/v2/keys/traefik/backends/backend1/servers/server2/weight + +# backend 2 +curl -i -H "Accept: application/json" -X PUT -d value="drr" http://localhost:4001/v2/keys/traefik/backends/backend2/loadbalancer/method +curl -i -H "Accept: application/json" -X PUT -d value="http://172.17.0.4:80" http://localhost:4001/v2/keys/traefik/backends/backend2/servers/server1/url +curl -i -H "Accept: application/json" -X PUT -d value="1" http://localhost:4001/v2/keys/traefik/backends/backend2/servers/server1/weight +curl -i -H "Accept: application/json" -X PUT -d value="http://172.17.0.5:80" http://localhost:4001/v2/keys/traefik/backends/backend2/servers/server2/url +curl -i -H "Accept: application/json" -X PUT -d value="2" http://localhost:4001/v2/keys/traefik/backends/backend2/servers/server2/weight + +# frontend 1 +curl -i -H "Accept: application/json" -X PUT -d value="backend2" http://localhost:4001/v2/keys/traefik/frontends/frontend1/backend +curl -i -H "Accept: application/json" -X PUT -d value="http" http://localhost:4001/v2/keys/traefik/frontends/frontend1/entrypoints +curl -i -H "Accept: application/json" -X PUT -d value="Host:test.localhost" http://localhost:4001/v2/keys/traefik/frontends/frontend1/routes/test_1/rule + +# frontend 2 +curl -i -H "Accept: application/json" -X PUT -d value="backend1" http://localhost:4001/v2/keys/traefik/frontends/frontend2/backend +curl -i -H "Accept: application/json" -X PUT -d value="http" http://localhost:4001/v2/keys/traefik/frontends/frontend2/entrypoints +curl -i -H "Accept: application/json" -X PUT -d value="Path:/test" http://localhost:4001/v2/keys/traefik/frontends/frontend2/routes/test_2/rule diff --git a/provider/kv.go b/provider/kv.go index ebf55bce6..bd7a38193 100644 --- a/provider/kv.go +++ b/provider/kv.go @@ -170,7 +170,7 @@ func (provider *Kv) list(keys ...string) []string { } directoryKeys := make(map[string]string) for _, key := range keysPairs { - directory := strings.Split(strings.TrimPrefix(key.Key, strings.TrimPrefix(joinedKeys, "/")), "/")[0] + directory := strings.Split(strings.TrimPrefix(key.Key, joinedKeys), "/")[0] directoryKeys[directory] = joinedKeys + directory } return fun.Values(directoryKeys).([]string) @@ -178,7 +178,7 @@ func (provider *Kv) list(keys ...string) []string { func (provider *Kv) get(defaultValue string, keys ...string) string { joinedKeys := strings.Join(keys, "") - keyPair, err := provider.kvclient.Get(joinedKeys) + keyPair, err := provider.kvclient.Get(strings.TrimPrefix(joinedKeys, "/")) if err != nil { log.Warnf("Error getting key %s %s, setting default %s", joinedKeys, err, defaultValue) return defaultValue