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 } 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.