Commit e17a501b authored by Paul Michali's avatar Paul Michali

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.
parent 74f19437
...@@ -207,7 +207,7 @@ func (pf *PortForwarder) listenOnPortAndAddress(port *ForwardedPort, protocol st ...@@ -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 // 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 // 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) { 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 { if err != nil {
return nil, fmt.Errorf("Unable to create listener: Error %s", err) return nil, fmt.Errorf("Unable to create listener: Error %s", err)
} }
......
...@@ -134,13 +134,13 @@ func TestGetListener(t *testing.T) { ...@@ -134,13 +134,13 @@ func TestGetListener(t *testing.T) {
ExpectedListenerAddress: "127.0.0.1", ExpectedListenerAddress: "127.0.0.1",
}, },
{ {
Hostname: "[::1]", Hostname: "::1",
Protocol: "tcp6", Protocol: "tcp6",
ShouldRaiseError: false, ShouldRaiseError: false,
ExpectedListenerAddress: "::1", ExpectedListenerAddress: "::1",
}, },
{ {
Hostname: "[::1]", Hostname: "::1",
Protocol: "tcp4", Protocol: "tcp4",
ShouldRaiseError: true, ShouldRaiseError: true,
}, },
...@@ -149,12 +149,6 @@ func TestGetListener(t *testing.T) { ...@@ -149,12 +149,6 @@ func TestGetListener(t *testing.T) {
Protocol: "tcp6", Protocol: "tcp6",
ShouldRaiseError: true, ShouldRaiseError: true,
}, },
{
// IPv6 address must be put into brackets. This test reveals this.
Hostname: "::1",
Protocol: "tcp6",
ShouldRaiseError: true,
},
} }
for i, testCase := range testCases { for i, testCase := range testCases {
...@@ -182,7 +176,7 @@ func TestGetListener(t *testing.T) { ...@@ -182,7 +176,7 @@ func TestGetListener(t *testing.T) {
host, port, _ := net.SplitHostPort(listener.Addr().String()) 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) 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 { 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 { if port != expectedListenerPort {
t.Errorf("Test case #%d failed: Listener does not listen on exepected port: asked %v got %v", i, expectedListenerPort, port) t.Errorf("Test case #%d failed: Listener does not listen on exepected port: asked %v got %v", i, expectedListenerPort, port)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment