Merge pull request #1196 from klausenbusk/url

kv: Ignore backend servers with no url
This commit is contained in:
Emile Vauge 2017-04-06 11:14:57 +02:00 committed by GitHub
commit e1aa16ae70
2 changed files with 33 additions and 8 deletions

View file

@ -162,6 +162,13 @@ func (provider *Kv) list(keys ...string) []string {
func (provider *Kv) listServers(backend string) []string {
serverNames := provider.list(backend, "/servers/")
return fun.Filter(func(serverName string) bool {
key := fmt.Sprint(serverName, "/url")
if _, err := provider.kvclient.Get(key); err != nil {
if err != store.ErrKeyNotFound {
log.Errorf("Failed to retrieve value for key %s: %s", key, err)
}
return false
}
return provider.checkConstraints(serverName, "/tags")
}, serverNames).([]string)
}

View file

@ -96,7 +96,9 @@ func TestKvList(t *testing.T) {
// Error case
provider := &Kv{
kvclient: &Mock{
Error: true,
Error: KvError{
List: store.ErrKeyNotFound,
},
},
}
actual := provider.list("anything")
@ -187,7 +189,9 @@ func TestKvGet(t *testing.T) {
// Error case
provider := &Kv{
kvclient: &Mock{
Error: true,
Error: KvError{
Get: store.ErrKeyNotFound,
},
},
}
actual := provider.get("", "anything")
@ -284,9 +288,15 @@ func TestKvWatchTree(t *testing.T) {
}
}
// Override Get/List to return a error
type KvError struct {
Get error
List error
}
// Extremely limited mock store so we can test initialization
type Mock struct {
Error bool
Error KvError
KVPairs []*store.KVPair
WatchTreeMethod func() <-chan []*store.KVPair
}
@ -296,15 +306,15 @@ func (s *Mock) Put(key string, value []byte, opts *store.WriteOptions) error {
}
func (s *Mock) Get(key string) (*store.KVPair, error) {
if s.Error {
return nil, errors.New("Error")
if err := s.Error.Get; err != nil {
return nil, err
}
for _, kvPair := range s.KVPairs {
if kvPair.Key == key {
return kvPair, nil
}
}
return nil, nil
return nil, store.ErrKeyNotFound
}
func (s *Mock) Delete(key string) error {
@ -333,8 +343,8 @@ func (s *Mock) NewLock(key string, options *store.LockOptions) (store.Locker, er
// List mock
func (s *Mock) List(prefix string) ([]*store.KVPair, error) {
if s.Error {
return nil, errors.New("Error")
if err := s.Error.List; err != nil {
return nil, err
}
kv := []*store.KVPair{}
for _, kvPair := range s.KVPairs {
@ -410,6 +420,14 @@ func TestKVLoadConfig(t *testing.T) {
Key: "traefik/backends/backend.with.dot.too/servers/server.with.dot/weight",
Value: []byte("0"),
},
{
Key: "traefik/backends/backend.with.dot.too/servers/server.with.dot.without.url",
Value: []byte(""),
},
{
Key: "traefik/backends/backend.with.dot.too/servers/server.with.dot.without.url/weight",
Value: []byte("0"),
},
},
},
}