From 8a2897e141f0fea20beb9bdebfc6bb287c31aad2 Mon Sep 17 00:00:00 2001 From: Alexandre Beslic Date: Mon, 2 Nov 2015 06:10:50 -0800 Subject: [PATCH] Add configurable discovery This PR allows to configure the discovery path using the --discovery-opt flag (with "kv.path=path/to/nodes"). We can point to "docker/nodes" and use the docker discovery. If docker instances are advertising to the cluster using the `--cluster-advertise` flag, the swarm join command becomes unnecessary. Signed-off-by: Alexandre Beslic --- discovery/kv/kv.go | 11 +++++++++-- discovery/kv/kv_test.go | 22 +++++++++++----------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/discovery/kv/kv.go b/discovery/kv/kv.go index d2a62bb31c..fb578e9957 100644 --- a/discovery/kv/kv.go +++ b/discovery/kv/kv.go @@ -17,7 +17,7 @@ import ( ) const ( - discoveryPath = "docker/swarm/nodes" + defaultDiscoveryPath = "docker/swarm/nodes" ) // Discovery is exported @@ -62,7 +62,14 @@ func (s *Discovery) Initialize(uris string, heartbeat time.Duration, ttl time.Du s.heartbeat = heartbeat 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 if discoveryOpt["kv.cacertfile"] != "" && discoveryOpt["kv.certfile"] != "" && discoveryOpt["kv.keyfile"] != "" { diff --git a/discovery/kv/kv_test.go b/discovery/kv/kv_test.go index 3512af8199..045559c276 100644 --- a/discovery/kv/kv_test.go +++ b/discovery/kv/kv_test.go @@ -28,7 +28,7 @@ func TestInitialize(t *testing.T) { s := d.store.(*libkvmock.Mock) assert.Len(t, s.Endpoints, 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) assert.NotNil(t, storeMock) @@ -41,7 +41,7 @@ func TestInitialize(t *testing.T) { s = d.store.(*libkvmock.Mock) assert.Len(t, s.Endpoints, 1) 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) 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[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) { @@ -147,19 +147,19 @@ func TestWatch(t *testing.T) { mockCh := make(chan []*store.KVPair) // The first watch will fail on those three calls - s.On("Exists", "path/"+discoveryPath).Return(false, errors.New("test error")) - s.On("Put", "path/"+discoveryPath, 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("Exists", "path/"+defaultDiscoveryPath).Return(false, errors.New("test error")) + s.On("Put", "path/"+defaultDiscoveryPath, mock.Anything, mock.Anything).Return(errors.New("test error")) + s.On("WatchTree", "path/"+defaultDiscoveryPath, mock.Anything).Return(mockCh, errors.New("test error")).Once() // 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{ &discovery.Entry{Host: "1.1.1.1", Port: "1111"}, &discovery.Entry{Host: "2.2.2.2", Port: "2222"}, } kvs := []*store.KVPair{ - {Key: path.Join("path", discoveryPath, "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, "1.1.1.1"), Value: []byte("1.1.1.1:1111")}, + {Key: path.Join("path", defaultDiscoveryPath, "2.2.2.2"), Value: []byte("2.2.2.2:2222")}, } stopCh := make(chan struct{}) @@ -179,13 +179,13 @@ func TestWatch(t *testing.T) { // Add a new entry. 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 assert.Equal(t, <-ch, expected) // Make sure that if an error occurs it retries. // 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) // Give it enough time to call WatchTree. time.Sleep(3)