Merge pull request #1368 from abronan/configurable_discovery_path

Add configurable discovery path option
This commit is contained in:
Alexandre Beslic 2015-11-11 15:32:05 -08:00
commit 4dc83cc71b
2 changed files with 20 additions and 13 deletions

View File

@ -17,7 +17,7 @@ import (
) )
const ( const (
discoveryPath = "docker/swarm/nodes" defaultDiscoveryPath = "docker/swarm/nodes"
) )
// Discovery is exported // Discovery is exported
@ -62,7 +62,14 @@ func (s *Discovery) Initialize(uris string, heartbeat time.Duration, ttl time.Du
s.heartbeat = heartbeat s.heartbeat = heartbeat
s.ttl = ttl s.ttl = ttl
s.path = path.Join(s.prefix, discoveryPath)
// Use a custom path if specified in discovery options
dpath := defaultDiscoveryPath
if discoveryOpt["kv.path"] != "" {
dpath = discoveryOpt["kv.path"]
}
s.path = path.Join(s.prefix, dpath)
var config *store.Config var config *store.Config
if discoveryOpt["kv.cacertfile"] != "" && discoveryOpt["kv.certfile"] != "" && discoveryOpt["kv.keyfile"] != "" { if discoveryOpt["kv.cacertfile"] != "" && discoveryOpt["kv.certfile"] != "" && discoveryOpt["kv.keyfile"] != "" {

View File

@ -28,7 +28,7 @@ func TestInitialize(t *testing.T) {
s := d.store.(*libkvmock.Mock) s := d.store.(*libkvmock.Mock)
assert.Len(t, s.Endpoints, 1) assert.Len(t, s.Endpoints, 1)
assert.Equal(t, s.Endpoints[0], "127.0.0.1") assert.Equal(t, s.Endpoints[0], "127.0.0.1")
assert.Equal(t, d.path, discoveryPath) assert.Equal(t, d.path, defaultDiscoveryPath)
storeMock, err = libkvmock.New([]string{"127.0.0.1:1234"}, nil) storeMock, err = libkvmock.New([]string{"127.0.0.1:1234"}, nil)
assert.NotNil(t, storeMock) assert.NotNil(t, storeMock)
@ -41,7 +41,7 @@ func TestInitialize(t *testing.T) {
s = d.store.(*libkvmock.Mock) s = d.store.(*libkvmock.Mock)
assert.Len(t, s.Endpoints, 1) assert.Len(t, s.Endpoints, 1)
assert.Equal(t, s.Endpoints[0], "127.0.0.1:1234") assert.Equal(t, s.Endpoints[0], "127.0.0.1:1234")
assert.Equal(t, d.path, "path/"+discoveryPath) assert.Equal(t, d.path, "path/"+defaultDiscoveryPath)
storeMock, err = libkvmock.New([]string{"127.0.0.1:1234", "127.0.0.2:1234", "127.0.0.3:1234"}, nil) storeMock, err = libkvmock.New([]string{"127.0.0.1:1234", "127.0.0.2:1234", "127.0.0.3:1234"}, nil)
assert.NotNil(t, storeMock) assert.NotNil(t, storeMock)
@ -57,7 +57,7 @@ func TestInitialize(t *testing.T) {
assert.Equal(t, s.Endpoints[1], "127.0.0.2:1234") assert.Equal(t, s.Endpoints[1], "127.0.0.2:1234")
assert.Equal(t, s.Endpoints[2], "127.0.0.3:1234") assert.Equal(t, s.Endpoints[2], "127.0.0.3:1234")
} }
assert.Equal(t, d.path, "path/"+discoveryPath) assert.Equal(t, d.path, "path/"+defaultDiscoveryPath)
} }
func TestInitializeWithCerts(t *testing.T) { func TestInitializeWithCerts(t *testing.T) {
@ -147,19 +147,19 @@ func TestWatch(t *testing.T) {
mockCh := make(chan []*store.KVPair) mockCh := make(chan []*store.KVPair)
// The first watch will fail on those three calls // The first watch will fail on those three calls
s.On("Exists", "path/"+discoveryPath).Return(false, errors.New("test error")) s.On("Exists", "path/"+defaultDiscoveryPath).Return(false, errors.New("test error"))
s.On("Put", "path/"+discoveryPath, mock.Anything, mock.Anything).Return(errors.New("test error")) s.On("Put", "path/"+defaultDiscoveryPath, mock.Anything, mock.Anything).Return(errors.New("test error"))
s.On("WatchTree", "path/"+discoveryPath, mock.Anything).Return(mockCh, errors.New("test error")).Once() s.On("WatchTree", "path/"+defaultDiscoveryPath, mock.Anything).Return(mockCh, errors.New("test error")).Once()
// The second one will succeed. // The second one will succeed.
s.On("WatchTree", "path/"+discoveryPath, mock.Anything).Return(mockCh, nil).Once() s.On("WatchTree", "path/"+defaultDiscoveryPath, mock.Anything).Return(mockCh, nil).Once()
expected := discovery.Entries{ expected := discovery.Entries{
&discovery.Entry{Host: "1.1.1.1", Port: "1111"}, &discovery.Entry{Host: "1.1.1.1", Port: "1111"},
&discovery.Entry{Host: "2.2.2.2", Port: "2222"}, &discovery.Entry{Host: "2.2.2.2", Port: "2222"},
} }
kvs := []*store.KVPair{ kvs := []*store.KVPair{
{Key: path.Join("path", discoveryPath, "1.1.1.1"), Value: []byte("1.1.1.1:1111")}, {Key: path.Join("path", defaultDiscoveryPath, "1.1.1.1"), Value: []byte("1.1.1.1:1111")},
{Key: path.Join("path", discoveryPath, "2.2.2.2"), Value: []byte("2.2.2.2:2222")}, {Key: path.Join("path", defaultDiscoveryPath, "2.2.2.2"), Value: []byte("2.2.2.2:2222")},
} }
stopCh := make(chan struct{}) stopCh := make(chan struct{})
@ -179,13 +179,13 @@ func TestWatch(t *testing.T) {
// Add a new entry. // Add a new entry.
expected = append(expected, &discovery.Entry{Host: "3.3.3.3", Port: "3333"}) expected = append(expected, &discovery.Entry{Host: "3.3.3.3", Port: "3333"})
kvs = append(kvs, &store.KVPair{Key: path.Join("path", discoveryPath, "3.3.3.3"), Value: []byte("3.3.3.3:3333")}) kvs = append(kvs, &store.KVPair{Key: path.Join("path", defaultDiscoveryPath, "3.3.3.3"), Value: []byte("3.3.3.3:3333")})
mockCh <- kvs mockCh <- kvs
assert.Equal(t, <-ch, expected) assert.Equal(t, <-ch, expected)
// Make sure that if an error occurs it retries. // Make sure that if an error occurs it retries.
// This third call to WatchTree will be checked later by AssertExpectations. // This third call to WatchTree will be checked later by AssertExpectations.
s.On("WatchTree", "path/"+discoveryPath, mock.Anything).Return(mockCh, nil) s.On("WatchTree", "path/"+defaultDiscoveryPath, mock.Anything).Return(mockCh, nil)
close(mockCh) close(mockCh)
// Give it enough time to call WatchTree. // Give it enough time to call WatchTree.
time.Sleep(3) time.Sleep(3)