mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-09 02:50:04 +00:00
port-forward listen on address
adds an address flag to kubectl port-forward to allow to bind to a different ip then localhost Kubernetes-commit: 4643c6e95e0a0cf6561554fb3b9a1bc59bcead0c
This commit is contained in:
committed by
Kubernetes Publisher
parent
f3fa17a207
commit
2a9c6c4210
@@ -20,6 +20,7 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -40,23 +41,52 @@ func (d *fakeDialer) Dial(protocols ...string) (httpstream.Connection, string, e
|
||||
|
||||
func TestParsePortsAndNew(t *testing.T) {
|
||||
tests := []struct {
|
||||
input []string
|
||||
expected []ForwardedPort
|
||||
expectParseError bool
|
||||
expectNewError bool
|
||||
input []string
|
||||
addresses []string
|
||||
expectedPorts []ForwardedPort
|
||||
expectedAddresses []listenAddress
|
||||
expectPortParseError bool
|
||||
expectAddressParseError bool
|
||||
expectNewError bool
|
||||
}{
|
||||
{input: []string{}, expectNewError: true},
|
||||
{input: []string{"a"}, expectParseError: true, expectNewError: true},
|
||||
{input: []string{":a"}, expectParseError: true, expectNewError: true},
|
||||
{input: []string{"-1"}, expectParseError: true, expectNewError: true},
|
||||
{input: []string{"65536"}, expectParseError: true, expectNewError: true},
|
||||
{input: []string{"0"}, expectParseError: true, expectNewError: true},
|
||||
{input: []string{"0:0"}, expectParseError: true, expectNewError: true},
|
||||
{input: []string{"a:5000"}, expectParseError: true, expectNewError: true},
|
||||
{input: []string{"5000:a"}, expectParseError: true, expectNewError: true},
|
||||
{input: []string{"a"}, expectPortParseError: true, expectAddressParseError: false, expectNewError: true},
|
||||
{input: []string{":a"}, expectPortParseError: true, expectAddressParseError: false, expectNewError: true},
|
||||
{input: []string{"-1"}, expectPortParseError: true, expectAddressParseError: false, expectNewError: true},
|
||||
{input: []string{"65536"}, expectPortParseError: true, expectAddressParseError: false, expectNewError: true},
|
||||
{input: []string{"0"}, expectPortParseError: true, expectAddressParseError: false, expectNewError: true},
|
||||
{input: []string{"0:0"}, expectPortParseError: true, expectAddressParseError: false, expectNewError: true},
|
||||
{input: []string{"a:5000"}, expectPortParseError: true, expectAddressParseError: false, expectNewError: true},
|
||||
{input: []string{"5000:a"}, expectPortParseError: true, expectAddressParseError: false, expectNewError: true},
|
||||
{input: []string{"5000:5000"}, addresses: []string{"127.0.0.257"}, expectPortParseError: false, expectAddressParseError: true, expectNewError: true},
|
||||
{input: []string{"5000:5000"}, addresses: []string{"::g"}, expectPortParseError: false, expectAddressParseError: true, expectNewError: true},
|
||||
{input: []string{"5000:5000"}, addresses: []string{"domain.invalid"}, expectPortParseError: false, expectAddressParseError: true, expectNewError: true},
|
||||
{
|
||||
input: []string{"5000", "5000:5000", "8888:5000", "5000:8888", ":5000", "0:5000"},
|
||||
expected: []ForwardedPort{
|
||||
input: []string{"5000:5000"},
|
||||
addresses: []string{"localhost"},
|
||||
expectedPorts: []ForwardedPort{
|
||||
{5000, 5000},
|
||||
},
|
||||
expectedAddresses: []listenAddress{
|
||||
{protocol: "tcp4", address: "127.0.0.1", failureMode: "all"},
|
||||
{protocol: "tcp6", address: "::1", failureMode: "all"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: []string{"5000:5000"},
|
||||
addresses: []string{"localhost", "127.0.0.1"},
|
||||
expectedPorts: []ForwardedPort{
|
||||
{5000, 5000},
|
||||
},
|
||||
expectedAddresses: []listenAddress{
|
||||
{protocol: "tcp4", address: "127.0.0.1", failureMode: "any"},
|
||||
{protocol: "tcp6", address: "::1", failureMode: "all"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: []string{"5000", "5000:5000", "8888:5000", "5000:8888", ":5000", "0:5000"},
|
||||
addresses: []string{"127.0.0.1", "::1"},
|
||||
expectedPorts: []ForwardedPort{
|
||||
{5000, 5000},
|
||||
{5000, 5000},
|
||||
{8888, 5000},
|
||||
@@ -64,34 +94,63 @@ func TestParsePortsAndNew(t *testing.T) {
|
||||
{0, 5000},
|
||||
{0, 5000},
|
||||
},
|
||||
expectedAddresses: []listenAddress{
|
||||
{protocol: "tcp4", address: "127.0.0.1", failureMode: "any"},
|
||||
{protocol: "tcp6", address: "::1", failureMode: "any"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
parsed, err := parsePorts(test.input)
|
||||
parsedPorts, err := parsePorts(test.input)
|
||||
haveError := err != nil
|
||||
if e, a := test.expectParseError, haveError; e != a {
|
||||
if e, a := test.expectPortParseError, haveError; e != a {
|
||||
t.Fatalf("%d: parsePorts: error expected=%t, got %t: %s", i, e, a, err)
|
||||
}
|
||||
|
||||
// default to localhost
|
||||
if len(test.addresses) == 0 && len(test.expectedAddresses) == 0 {
|
||||
test.addresses = []string{"localhost"}
|
||||
test.expectedAddresses = []listenAddress{{protocol: "tcp4", address: "127.0.0.1"}, {protocol: "tcp6", address: "::1"}}
|
||||
}
|
||||
// assert address parser
|
||||
parsedAddresses, err := parseAddresses(test.addresses)
|
||||
haveError = err != nil
|
||||
if e, a := test.expectAddressParseError, haveError; e != a {
|
||||
t.Fatalf("%d: parseAddresses: error expected=%t, got %t: %s", i, e, a, err)
|
||||
}
|
||||
|
||||
dialer := &fakeDialer{}
|
||||
expectedStopChan := make(chan struct{})
|
||||
readyChan := make(chan struct{})
|
||||
pf, err := New(dialer, test.input, expectedStopChan, readyChan, os.Stdout, os.Stderr)
|
||||
|
||||
var pf *PortForwarder
|
||||
if len(test.addresses) > 0 {
|
||||
pf, err = NewOnAddresses(dialer, test.addresses, test.input, expectedStopChan, readyChan, os.Stdout, os.Stderr)
|
||||
} else {
|
||||
pf, err = New(dialer, test.input, expectedStopChan, readyChan, os.Stdout, os.Stderr)
|
||||
}
|
||||
haveError = err != nil
|
||||
if e, a := test.expectNewError, haveError; e != a {
|
||||
t.Fatalf("%d: New: error expected=%t, got %t: %s", i, e, a, err)
|
||||
}
|
||||
|
||||
if test.expectParseError || test.expectNewError {
|
||||
if test.expectPortParseError || test.expectAddressParseError || test.expectNewError {
|
||||
continue
|
||||
}
|
||||
|
||||
for pi, expectedPort := range test.expected {
|
||||
if e, a := expectedPort.Local, parsed[pi].Local; e != a {
|
||||
sort.Slice(test.expectedAddresses, func(i, j int) bool { return test.expectedAddresses[i].address < test.expectedAddresses[j].address })
|
||||
sort.Slice(parsedAddresses, func(i, j int) bool { return parsedAddresses[i].address < parsedAddresses[j].address })
|
||||
|
||||
if !reflect.DeepEqual(test.expectedAddresses, parsedAddresses) {
|
||||
t.Fatalf("%d: expectedAddresses: %v, got: %v", i, test.expectedAddresses, parsedAddresses)
|
||||
}
|
||||
|
||||
for pi, expectedPort := range test.expectedPorts {
|
||||
if e, a := expectedPort.Local, parsedPorts[pi].Local; e != a {
|
||||
t.Fatalf("%d: local expected: %d, got: %d", i, e, a)
|
||||
}
|
||||
if e, a := expectedPort.Remote, parsed[pi].Remote; e != a {
|
||||
if e, a := expectedPort.Remote, parsedPorts[pi].Remote; e != a {
|
||||
t.Fatalf("%d: remote expected: %d, got: %d", i, e, a)
|
||||
}
|
||||
}
|
||||
@@ -108,8 +167,8 @@ func TestParsePortsAndNew(t *testing.T) {
|
||||
|
||||
if ports, portErr := pf.GetPorts(); portErr != nil {
|
||||
t.Fatalf("%d: GetPorts: unable to retrieve ports: %s", i, portErr)
|
||||
} else if !reflect.DeepEqual(test.expected, ports) {
|
||||
t.Fatalf("%d: ports: expected %#v, got %#v", i, test.expected, ports)
|
||||
} else if !reflect.DeepEqual(test.expectedPorts, ports) {
|
||||
t.Fatalf("%d: ports: expected %#v, got %#v", i, test.expectedPorts, ports)
|
||||
}
|
||||
if e, a := expectedStopChan, pf.stopChan; e != a {
|
||||
t.Fatalf("%d: stopChan: expected %#v, got %#v", i, e, a)
|
||||
|
Reference in New Issue
Block a user