From e17a501bcb7a3d2c5f4ff5c41f122fe155395dcd Mon Sep 17 00:00:00 2001 From: Paul Michali Date: Thu, 29 Jun 2017 14:05:56 +0000 Subject: [PATCH] Support IPv6 addresses for getListener() Currently, client-go requires that an IPv6 address string for hostname has square brackets surrounding, so that it can be used with address:port in an API request. This change, removes that requirement, and has getListener() add the square brackets for IPv6 addresses for hosts. If IPv4 or hostname, the name will not be modified. Decided to change here, rather than everywhere client-go is used (thinking that there may be places where we DON'T want the square brackets applied). This issue was found in kubelet, which, at startup, creates a listener for services and nodes. If an IPv6 address is used, the URI was malformed. --- .../client-go/tools/portforward/portforward.go | 2 +- .../client-go/tools/portforward/portforward_test.go | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/staging/src/k8s.io/client-go/tools/portforward/portforward.go b/staging/src/k8s.io/client-go/tools/portforward/portforward.go index bfd9c745359..24737b9a7ac 100644 --- a/staging/src/k8s.io/client-go/tools/portforward/portforward.go +++ b/staging/src/k8s.io/client-go/tools/portforward/portforward.go @@ -207,7 +207,7 @@ func (pf *PortForwarder) listenOnPortAndAddress(port *ForwardedPort, protocol st // getListener creates a listener on the interface targeted by the given hostname on the given port with // the given protocol. protocol is in net.Listen style which basically admits values like tcp, tcp4, tcp6 func (pf *PortForwarder) getListener(protocol string, hostname string, port *ForwardedPort) (net.Listener, error) { - listener, err := net.Listen(protocol, fmt.Sprintf("%s:%d", hostname, port.Local)) + listener, err := net.Listen(protocol, net.JoinHostPort(hostname, strconv.Itoa(int(port.Local)))) if err != nil { return nil, fmt.Errorf("Unable to create listener: Error %s", err) } diff --git a/staging/src/k8s.io/client-go/tools/portforward/portforward_test.go b/staging/src/k8s.io/client-go/tools/portforward/portforward_test.go index 2d642b085db..ab705849c84 100644 --- a/staging/src/k8s.io/client-go/tools/portforward/portforward_test.go +++ b/staging/src/k8s.io/client-go/tools/portforward/portforward_test.go @@ -134,13 +134,13 @@ func TestGetListener(t *testing.T) { ExpectedListenerAddress: "127.0.0.1", }, { - Hostname: "[::1]", + Hostname: "::1", Protocol: "tcp6", ShouldRaiseError: false, ExpectedListenerAddress: "::1", }, { - Hostname: "[::1]", + Hostname: "::1", Protocol: "tcp4", ShouldRaiseError: true, }, @@ -149,12 +149,6 @@ func TestGetListener(t *testing.T) { Protocol: "tcp6", ShouldRaiseError: true, }, - { - // IPv6 address must be put into brackets. This test reveals this. - Hostname: "::1", - Protocol: "tcp6", - ShouldRaiseError: true, - }, } for i, testCase := range testCases { @@ -182,7 +176,7 @@ func TestGetListener(t *testing.T) { host, port, _ := net.SplitHostPort(listener.Addr().String()) t.Logf("Asked a %s forward for: %s:%v, got listener %s:%s, expected: %s", testCase.Protocol, testCase.Hostname, 12345, host, port, expectedListenerPort) if host != testCase.ExpectedListenerAddress { - t.Errorf("Test case #%d failed: Listener does not listen on exepected address: asked %v got %v", i, testCase.ExpectedListenerAddress, host) + t.Errorf("Test case #%d failed: Listener does not listen on expected address: asked '%v' got '%v'", i, testCase.ExpectedListenerAddress, host) } if port != expectedListenerPort { t.Errorf("Test case #%d failed: Listener does not listen on exepected port: asked %v got %v", i, expectedListenerPort, port)