diff --git a/etcdserver/cluster.go b/etcdserver/cluster.go index 793b63bad..bd8e00c14 100644 --- a/etcdserver/cluster.go +++ b/etcdserver/cluster.go @@ -99,10 +99,10 @@ func (c Cluster) IDs() []int64 { return ids } -// Endpoints returns a list of all peer addresses. Each address is prefixed +// PeerURLs returns a list of all peer addresses. Each address is prefixed // with the scheme (currently "http://"). The returned list is sorted in // ascending lexicographical order. -func (c Cluster) Endpoints() []string { +func (c Cluster) PeerURLs() []string { endpoints := make([]string, 0) for _, p := range c { for _, addr := range p.PeerURLs { @@ -112,3 +112,17 @@ func (c Cluster) Endpoints() []string { sort.Strings(endpoints) return endpoints } + +// ClientURLs returns a list of all client addresses. Each address is prefixed +// with the scheme (currently "http://"). The returned list is sorted in +// ascending lexicographical order. +func (c Cluster) ClientURLs() []string { + urls := make([]string, 0) + for _, p := range c { + for _, url := range p.ClientURLs { + urls = append(urls, addScheme(url)) + } + } + sort.Strings(urls) + return urls +} diff --git a/etcdserver/cluster_test.go b/etcdserver/cluster_test.go index 7ddf6d461..ebae4fbcf 100644 --- a/etcdserver/cluster_test.go +++ b/etcdserver/cluster_test.go @@ -1,6 +1,7 @@ package etcdserver import ( + "reflect" "testing" ) @@ -141,3 +142,121 @@ func TestClusterAddBad(t *testing.T) { } } } + +func TestClusterPeerURLs(t *testing.T) { + tests := []struct { + mems []Member + wurls []string + }{ + // single peer with a single address + { + mems: []Member{ + {ID: 1, PeerURLs: []string{"192.0.2.1"}}, + }, + wurls: []string{"http://192.0.2.1"}, + }, + + // single peer with a single address with a port + { + mems: []Member{ + {ID: 1, PeerURLs: []string{"192.0.2.1:8001"}}, + }, + wurls: []string{"http://192.0.2.1:8001"}, + }, + + // several members explicitly unsorted + { + mems: []Member{ + {ID: 2, PeerURLs: []string{"192.0.2.3", "192.0.2.4"}}, + {ID: 3, PeerURLs: []string{"192.0.2.5", "192.0.2.6"}}, + {ID: 1, PeerURLs: []string{"192.0.2.1", "192.0.2.2"}}, + }, + wurls: []string{"http://192.0.2.1", "http://192.0.2.2", "http://192.0.2.3", "http://192.0.2.4", "http://192.0.2.5", "http://192.0.2.6"}, + }, + + // no members + { + mems: []Member{}, + wurls: []string{}, + }, + + // peer with no peer urls + { + mems: []Member{ + {ID: 3, PeerURLs: []string{}}, + }, + wurls: []string{}, + }, + } + + for i, tt := range tests { + c := Cluster{} + if err := c.AddSlice(tt.mems); err != nil { + t.Errorf("AddSlice error: %v", err) + continue + } + urls := c.PeerURLs() + if !reflect.DeepEqual(urls, tt.wurls) { + t.Errorf("#%d: PeerURLs = %v, want %v", i, urls, tt.wurls) + } + } +} + +func TestClusterClientURLs(t *testing.T) { + tests := []struct { + mems []Member + wurls []string + }{ + // single peer with a single address + { + mems: []Member{ + {ID: 1, ClientURLs: []string{"192.0.2.1"}}, + }, + wurls: []string{"http://192.0.2.1"}, + }, + + // single peer with a single address with a port + { + mems: []Member{ + {ID: 1, ClientURLs: []string{"192.0.2.1:8001"}}, + }, + wurls: []string{"http://192.0.2.1:8001"}, + }, + + // several members explicitly unsorted + { + mems: []Member{ + {ID: 2, ClientURLs: []string{"192.0.2.3", "192.0.2.4"}}, + {ID: 3, ClientURLs: []string{"192.0.2.5", "192.0.2.6"}}, + {ID: 1, ClientURLs: []string{"192.0.2.1", "192.0.2.2"}}, + }, + wurls: []string{"http://192.0.2.1", "http://192.0.2.2", "http://192.0.2.3", "http://192.0.2.4", "http://192.0.2.5", "http://192.0.2.6"}, + }, + + // no members + { + mems: []Member{}, + wurls: []string{}, + }, + + // peer with no client urls + { + mems: []Member{ + {ID: 3, ClientURLs: []string{}}, + }, + wurls: []string{}, + }, + } + + for i, tt := range tests { + c := Cluster{} + if err := c.AddSlice(tt.mems); err != nil { + t.Errorf("AddSlice error: %v", err) + continue + } + urls := c.ClientURLs() + if !reflect.DeepEqual(urls, tt.wurls) { + t.Errorf("#%d: ClientURLs = %v, want %v", i, urls, tt.wurls) + } + } +} diff --git a/etcdserver/etcdhttp/http.go b/etcdserver/etcdhttp/http.go index db9e4249f..e250e026d 100644 --- a/etcdserver/etcdhttp/http.go +++ b/etcdserver/etcdhttp/http.go @@ -115,7 +115,7 @@ func (h serverHandler) serveMachines(w http.ResponseWriter, r *http.Request) { if !allowMethod(w, r.Method, "GET", "HEAD") { return } - endpoints := h.clusterStore.Get().Endpoints() + endpoints := h.clusterStore.Get().ClientURLs() w.Write([]byte(strings.Join(endpoints, ", "))) } diff --git a/etcdserver/etcdhttp/http_test.go b/etcdserver/etcdhttp/http_test.go index 7e60e53bb..440ed6ee4 100644 --- a/etcdserver/etcdhttp/http_test.go +++ b/etcdserver/etcdhttp/http_test.go @@ -612,9 +612,9 @@ func TestV2MachinesEndpoint(t *testing.T) { func TestServeMachines(t *testing.T) { cluster := &fakeCluster{ members: []etcdserver.Member{ - {ID: 0xBEEF0, PeerURLs: []string{"localhost:8080"}}, - {ID: 0xBEEF1, PeerURLs: []string{"localhost:8081"}}, - {ID: 0xBEEF2, PeerURLs: []string{"localhost:8082"}}, + {ID: 0xBEEF0, ClientURLs: []string{"localhost:8080"}}, + {ID: 0xBEEF1, ClientURLs: []string{"localhost:8081"}}, + {ID: 0xBEEF2, ClientURLs: []string{"localhost:8082"}}, }, } @@ -634,68 +634,6 @@ func TestServeMachines(t *testing.T) { } } -func TestClusterGetEndpoints(t *testing.T) { - tests := []struct { - clusterStore etcdserver.ClusterStore - endpoints []string - }{ - // single peer with a single address - { - clusterStore: &fakeCluster{ - members: []etcdserver.Member{ - {ID: 1, PeerURLs: []string{"192.0.2.1"}}, - }, - }, - endpoints: []string{"http://192.0.2.1"}, - }, - - // single peer with a single address with a port - { - clusterStore: &fakeCluster{ - members: []etcdserver.Member{ - {ID: 1, PeerURLs: []string{"192.0.2.1:8001"}}, - }, - }, - endpoints: []string{"http://192.0.2.1:8001"}, - }, - - // several members explicitly unsorted - { - clusterStore: &fakeCluster{ - members: []etcdserver.Member{ - {ID: 2, PeerURLs: []string{"192.0.2.3", "192.0.2.4"}}, - {ID: 3, PeerURLs: []string{"192.0.2.5", "192.0.2.6"}}, - {ID: 1, PeerURLs: []string{"192.0.2.1", "192.0.2.2"}}, - }, - }, - endpoints: []string{"http://192.0.2.1", "http://192.0.2.2", "http://192.0.2.3", "http://192.0.2.4", "http://192.0.2.5", "http://192.0.2.6"}, - }, - - // no members - { - clusterStore: &fakeCluster{members: []etcdserver.Member{}}, - endpoints: []string{}, - }, - - // peer with no endpoints - { - clusterStore: &fakeCluster{ - members: []etcdserver.Member{ - {ID: 3, PeerURLs: []string{}}, - }, - }, - endpoints: []string{}, - }, - } - - for i, tt := range tests { - endpoints := tt.clusterStore.Get().Endpoints() - if !reflect.DeepEqual(tt.endpoints, endpoints) { - t.Errorf("#%d: members.Endpoints() incorrect: want=%#v got=%#v", i, tt.endpoints, endpoints) - } - } -} - func TestAllowMethod(t *testing.T) { tests := []struct { m string diff --git a/main.go b/main.go index 94c12130c..0e7ca68ff 100644 --- a/main.go +++ b/main.go @@ -247,7 +247,7 @@ func startProxy() { log.Fatal(err) } - ph, err := proxy.NewHandler(pt, (*cluster).Endpoints()) + ph, err := proxy.NewHandler(pt, (*cluster).PeerURLs()) if err != nil { log.Fatal(err) }