From aa4ed088bbf72f7677da81dcaa1fe1b702f2c5f7 Mon Sep 17 00:00:00 2001 From: Emile Vauge Date: Thu, 11 May 2017 19:09:06 +0200 Subject: [PATCH] Unexport Kvclient & StoreType from kv Provider Signed-off-by: Emile Vauge --- provider/boltdb/boltdb.go | 4 ++-- provider/consul/consul.go | 4 ++-- provider/etcd/etcd.go | 4 ++-- provider/kv/kv.go | 34 ++++++++++++++++++++++------------ provider/kv/kv_test.go | 28 ++++++++++++++-------------- provider/zk/zk.go | 4 ++-- 6 files changed, 44 insertions(+), 34 deletions(-) diff --git a/provider/boltdb/boltdb.go b/provider/boltdb/boltdb.go index d2a92079c..190bea638 100644 --- a/provider/boltdb/boltdb.go +++ b/provider/boltdb/boltdb.go @@ -25,13 +25,13 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s if err != nil { return fmt.Errorf("Failed to Connect to KV store: %v", err) } - p.Kvclient = store + p.SetKVClient(store) return p.Provider.Provide(configurationChan, pool, constraints) } // CreateStore creates the KV store func (p *Provider) CreateStore() (store.Store, error) { - p.StoreType = store.BOLTDB + p.SetStoreType(store.BOLTDB) boltdb.Register() return p.Provider.CreateStore() } diff --git a/provider/consul/consul.go b/provider/consul/consul.go index 5de063c68..d7a31f28d 100644 --- a/provider/consul/consul.go +++ b/provider/consul/consul.go @@ -25,13 +25,13 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s if err != nil { return fmt.Errorf("Failed to Connect to KV store: %v", err) } - p.Kvclient = store + p.SetKVClient(store) return p.Provider.Provide(configurationChan, pool, constraints) } // CreateStore creates the KV store func (p *Provider) CreateStore() (store.Store, error) { - p.StoreType = store.CONSUL + p.SetStoreType(store.CONSUL) consul.Register() return p.Provider.CreateStore() } diff --git a/provider/etcd/etcd.go b/provider/etcd/etcd.go index a4b8bb3e5..488959e3a 100644 --- a/provider/etcd/etcd.go +++ b/provider/etcd/etcd.go @@ -25,13 +25,13 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s if err != nil { return fmt.Errorf("Failed to Connect to KV store: %v", err) } - p.Kvclient = store + p.SetKVClient(store) return p.Provider.Provide(configurationChan, pool, constraints) } // CreateStore creates the KV store func (p *Provider) CreateStore() (store.Store, error) { - p.StoreType = store.ETCD + p.SetStoreType(store.ETCD) etcd.Register() return p.Provider.CreateStore() } diff --git a/provider/kv/kv.go b/provider/kv/kv.go index fc234fa4b..9503c0f8e 100644 --- a/provider/kv/kv.go +++ b/provider/kv/kv.go @@ -26,8 +26,8 @@ type Provider struct { TLS *provider.ClientTLS `description:"Enable TLS support"` Username string `description:"KV Username"` Password string `description:"KV Password"` - StoreType store.Backend - Kvclient store.Store + storeType store.Backend + kvclient store.Store } // CreateStore create the K/V store @@ -47,15 +47,25 @@ func (p *Provider) CreateStore() (store.Store, error) { } } return libkv.NewStore( - p.StoreType, + p.storeType, strings.Split(p.Endpoint, ","), storeConfig, ) } +// SetStoreType storeType setter +func (p *Provider) SetStoreType(storeType store.Backend) { + p.storeType = storeType +} + +// SetKVClient kvclient setter +func (p *Provider) SetKVClient(kvClient store.Store) { + p.kvclient = kvClient +} + func (p *Provider) watchKv(configurationChan chan<- types.ConfigMessage, prefix string, stop chan bool) error { operation := func() error { - events, err := p.Kvclient.WatchTree(p.Prefix, make(chan struct{})) + events, err := p.kvclient.WatchTree(p.Prefix, make(chan struct{})) if err != nil { return fmt.Errorf("Failed to KV WatchTree: %v", err) } @@ -70,7 +80,7 @@ func (p *Provider) watchKv(configurationChan chan<- types.ConfigMessage, prefix configuration := p.loadConfig() if configuration != nil { configurationChan <- types.ConfigMessage{ - ProviderName: string(p.StoreType), + ProviderName: string(p.storeType), Configuration: configuration, } } @@ -92,7 +102,7 @@ func (p *Provider) watchKv(configurationChan chan<- types.ConfigMessage, prefix func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error { p.Constraints = append(p.Constraints, constraints...) operation := func() error { - if _, err := p.Kvclient.Exists("qmslkjdfmqlskdjfmqlksjazçueznbvbwzlkajzebvkwjdcqmlsfj"); err != nil { + if _, err := p.kvclient.Exists("qmslkjdfmqlskdjfmqlksjazçueznbvbwzlkajzebvkwjdcqmlsfj"); err != nil { return fmt.Errorf("Failed to test KV store connection: %v", err) } if p.Watch { @@ -105,7 +115,7 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s } configuration := p.loadConfig() configurationChan <- types.ConfigMessage{ - ProviderName: string(p.StoreType), + ProviderName: string(p.storeType), Configuration: configuration, } return nil @@ -152,7 +162,7 @@ func (p *Provider) loadConfig() *types.Configuration { func (p *Provider) list(keys ...string) []string { joinedKeys := strings.Join(keys, "") - keysPairs, err := p.Kvclient.List(joinedKeys) + keysPairs, err := p.kvclient.List(joinedKeys) if err != nil { log.Debugf("Cannot get keys %s %s ", joinedKeys, err) return nil @@ -169,7 +179,7 @@ func (p *Provider) listServers(backend string) []string { serverNames := p.list(backend, "/servers/") return fun.Filter(func(serverName string) bool { key := fmt.Sprint(serverName, "/url") - if _, err := p.Kvclient.Get(key); err != nil { + if _, err := p.kvclient.Get(key); err != nil { if err != store.ErrKeyNotFound { log.Errorf("Failed to retrieve value for key %s: %s", key, err) } @@ -181,7 +191,7 @@ func (p *Provider) listServers(backend string) []string { func (p *Provider) get(defaultValue string, keys ...string) string { joinedKeys := strings.Join(keys, "") - keyPair, err := p.Kvclient.Get(strings.TrimPrefix(joinedKeys, "/")) + keyPair, err := p.kvclient.Get(strings.TrimPrefix(joinedKeys, "/")) if err != nil { log.Debugf("Cannot get key %s %s, setting default %s", joinedKeys, err, defaultValue) return defaultValue @@ -194,7 +204,7 @@ func (p *Provider) get(defaultValue string, keys ...string) string { func (p *Provider) splitGet(keys ...string) []string { joinedKeys := strings.Join(keys, "") - keyPair, err := p.Kvclient.Get(joinedKeys) + keyPair, err := p.kvclient.Get(joinedKeys) if err != nil { log.Debugf("Cannot get key %s %s, setting default empty", joinedKeys, err) return []string{} @@ -212,7 +222,7 @@ func (p *Provider) last(key string) string { func (p *Provider) checkConstraints(keys ...string) bool { joinedKeys := strings.Join(keys, "") - keyPair, err := p.Kvclient.Get(joinedKeys) + keyPair, err := p.kvclient.Get(joinedKeys) value := "" if err == nil && keyPair != nil && keyPair.Value != nil { diff --git a/provider/kv/kv_test.go b/provider/kv/kv_test.go index 72a5ed6fd..39ba6b0bb 100644 --- a/provider/kv/kv_test.go +++ b/provider/kv/kv_test.go @@ -20,21 +20,21 @@ func TestKvList(t *testing.T) { }{ { provider: &Provider{ - Kvclient: &Mock{}, + kvclient: &Mock{}, }, keys: []string{}, expected: []string{}, }, { provider: &Provider{ - Kvclient: &Mock{}, + kvclient: &Mock{}, }, keys: []string{"traefik"}, expected: []string{}, }, { provider: &Provider{ - Kvclient: &Mock{ + kvclient: &Mock{ KVPairs: []*store.KVPair{ { Key: "foo", @@ -48,7 +48,7 @@ func TestKvList(t *testing.T) { }, { provider: &Provider{ - Kvclient: &Mock{ + kvclient: &Mock{ KVPairs: []*store.KVPair{ { Key: "foo", @@ -62,7 +62,7 @@ func TestKvList(t *testing.T) { }, { provider: &Provider{ - Kvclient: &Mock{ + kvclient: &Mock{ KVPairs: []*store.KVPair{ { Key: "foo/baz/1", @@ -95,7 +95,7 @@ func TestKvList(t *testing.T) { // Error case provider := &Provider{ - Kvclient: &Mock{ + kvclient: &Mock{ Error: KvError{ List: store.ErrKeyNotFound, }, @@ -115,21 +115,21 @@ func TestKvGet(t *testing.T) { }{ { provider: &Provider{ - Kvclient: &Mock{}, + kvclient: &Mock{}, }, keys: []string{}, expected: "", }, { provider: &Provider{ - Kvclient: &Mock{}, + kvclient: &Mock{}, }, keys: []string{"traefik"}, expected: "", }, { provider: &Provider{ - Kvclient: &Mock{ + kvclient: &Mock{ KVPairs: []*store.KVPair{ { Key: "foo", @@ -143,7 +143,7 @@ func TestKvGet(t *testing.T) { }, { provider: &Provider{ - Kvclient: &Mock{ + kvclient: &Mock{ KVPairs: []*store.KVPair{ { Key: "foo", @@ -157,7 +157,7 @@ func TestKvGet(t *testing.T) { }, { provider: &Provider{ - Kvclient: &Mock{ + kvclient: &Mock{ KVPairs: []*store.KVPair{ { Key: "foo/baz/1", @@ -188,7 +188,7 @@ func TestKvGet(t *testing.T) { // Error case provider := &Provider{ - Kvclient: &Mock{ + kvclient: &Mock{ Error: KvError{ Get: store.ErrKeyNotFound, }, @@ -249,7 +249,7 @@ func TestKvWatchTree(t *testing.T) { returnedChans := make(chan chan []*store.KVPair) provider := &KvMock{ Provider{ - Kvclient: &Mock{ + kvclient: &Mock{ WatchTreeMethod: func() <-chan []*store.KVPair { c := make(chan []*store.KVPair, 10) returnedChans <- c @@ -378,7 +378,7 @@ func (s *Mock) Close() { func TestKVLoadConfig(t *testing.T) { provider := &Provider{ Prefix: "traefik", - Kvclient: &Mock{ + kvclient: &Mock{ KVPairs: []*store.KVPair{ { Key: "traefik/frontends/frontend.with.dot", diff --git a/provider/zk/zk.go b/provider/zk/zk.go index 3adb82dc2..2089d44f5 100644 --- a/provider/zk/zk.go +++ b/provider/zk/zk.go @@ -25,13 +25,13 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s if err != nil { return fmt.Errorf("Failed to Connect to KV store: %v", err) } - p.Kvclient = store + p.SetKVClient(store) return p.Provider.Provide(configurationChan, pool, constraints) } // CreateStore creates the KV store func (p *Provider) CreateStore() (store.Store, error) { - p.StoreType = store.ZK + p.SetStoreType(store.ZK) zookeeper.Register() return p.Provider.CreateStore() }