From f7c9214e29a81679916b953572173393d5e766e5 Mon Sep 17 00:00:00 2001 From: Dong Chen Date: Tue, 1 Mar 2016 15:13:08 -0800 Subject: [PATCH 1/2] Handle IPv6 entries. Signed-off-by: Dong Chen --- pkg/discovery/discovery_test.go | 8 +++++++- pkg/discovery/entry.go | 7 ++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/discovery/discovery_test.go b/pkg/discovery/discovery_test.go index ed0964dc14..6084f3ef0d 100644 --- a/pkg/discovery/discovery_test.go +++ b/pkg/discovery/discovery_test.go @@ -19,6 +19,11 @@ func (s *DiscoverySuite) TestNewEntry(c *check.C) { c.Assert(entry.Equals(&Entry{Host: "127.0.0.1", Port: "2375"}), check.Equals, true) c.Assert(entry.String(), check.Equals, "127.0.0.1:2375") + entry, err = NewEntry("[2001:db8:0:f101::2]:2375") + c.Assert(err, check.IsNil) + c.Assert(entry.Equals(&Entry{Host: "2001:db8:0:f101::2", Port: "2375"}), check.Equals, true) + c.Assert(entry.String(), check.Equals, "[2001:db8:0:f101::2]:2375") + _, err = NewEntry("127.0.0.1") c.Assert(err, check.NotNil) } @@ -50,11 +55,12 @@ func (s *DiscoverySuite) TestCreateEntries(c *check.C) { c.Assert(entries, check.DeepEquals, Entries{}) c.Assert(err, check.IsNil) - entries, err = CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", ""}) + entries, err = CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", "[2001:db8:0:f101::2]:2375", ""}) c.Assert(err, check.IsNil) expected := Entries{ &Entry{Host: "127.0.0.1", Port: "2375"}, &Entry{Host: "127.0.0.2", Port: "2375"}, + &Entry{Host: "2001:db8:0:f101::2", Port: "2375"}, } c.Assert(entries.Equals(expected), check.Equals, true) diff --git a/pkg/discovery/entry.go b/pkg/discovery/entry.go index e9cee26ee1..ce23bbf89b 100644 --- a/pkg/discovery/entry.go +++ b/pkg/discovery/entry.go @@ -1,9 +1,6 @@ package discovery -import ( - "fmt" - "net" -) +import "net" // NewEntry creates a new entry. func NewEntry(url string) (*Entry, error) { @@ -27,7 +24,7 @@ func (e *Entry) Equals(cmp *Entry) bool { // String returns the string form of an entry. func (e *Entry) String() string { - return fmt.Sprintf("%s:%s", e.Host, e.Port) + return net.JoinHostPort(e.Host, e.Port) } // Entries is a list of *Entry with some helpers. From 7554e882dfe3a3eb9ec61457e20a42de528db725 Mon Sep 17 00:00:00 2001 From: Dong Chen Date: Tue, 1 Mar 2016 17:27:30 -0800 Subject: [PATCH 2/2] Use net.JoinHostPort to handle address format. Signed-off-by: Dong Chen --- pkg/discovery/backends.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/discovery/backends.go b/pkg/discovery/backends.go index f150115a1d..65364c9ae8 100644 --- a/pkg/discovery/backends.go +++ b/pkg/discovery/backends.go @@ -89,7 +89,7 @@ func ParseAdvertise(advertise string) (string, error) { return "", fmt.Errorf("couldnt find a valid ip-address in interface %s", advertise) } - addr = fmt.Sprintf("%s:%s", addr, port) + addr = net.JoinHostPort(addr, port) return addr, nil }