mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Merge pull request #73676 from martin-helmich/bugfix/expose-forwarded-local-port
client-go: Dynamically assigned local port number not retrievable when port-forwarding
This commit is contained in:
commit
38a325250f
@ -205,8 +205,9 @@ func (pf *PortForwarder) forward() error {
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
listenSuccess := false
|
listenSuccess := false
|
||||||
for _, port := range pf.ports {
|
for i := range pf.ports {
|
||||||
err = pf.listenOnPort(&port)
|
port := &pf.ports[i]
|
||||||
|
err = pf.listenOnPort(port)
|
||||||
switch {
|
switch {
|
||||||
case err == nil:
|
case err == nil:
|
||||||
listenSuccess = true
|
listenSuccess = true
|
||||||
|
@ -18,11 +18,13 @@ package portforward
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/httpstream"
|
"k8s.io/apimachinery/pkg/util/httpstream"
|
||||||
)
|
)
|
||||||
@ -39,6 +41,37 @@ func (d *fakeDialer) Dial(protocols ...string) (httpstream.Connection, string, e
|
|||||||
return d.conn, d.negotiatedProtocol, d.err
|
return d.conn, d.negotiatedProtocol, d.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type fakeConnection struct {
|
||||||
|
closed bool
|
||||||
|
closeChan chan bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFakeConnection() httpstream.Connection {
|
||||||
|
return &fakeConnection{
|
||||||
|
closeChan: make(chan bool),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *fakeConnection) CreateStream(headers http.Header) (httpstream.Stream, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *fakeConnection) Close() error {
|
||||||
|
if !c.closed {
|
||||||
|
c.closed = true
|
||||||
|
close(c.closeChan)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *fakeConnection) CloseChan() <-chan bool {
|
||||||
|
return c.closeChan
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *fakeConnection) SetIdleTimeout(timeout time.Duration) {
|
||||||
|
// no-op
|
||||||
|
}
|
||||||
|
|
||||||
func TestParsePortsAndNew(t *testing.T) {
|
func TestParsePortsAndNew(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
input []string
|
input []string
|
||||||
@ -310,3 +343,46 @@ func TestGetListener(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetPortsReturnsDynamicallyAssignedLocalPort(t *testing.T) {
|
||||||
|
dialer := &fakeDialer{
|
||||||
|
conn: newFakeConnection(),
|
||||||
|
}
|
||||||
|
|
||||||
|
stopChan := make(chan struct{})
|
||||||
|
readyChan := make(chan struct{})
|
||||||
|
errChan := make(chan error)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
close(stopChan)
|
||||||
|
|
||||||
|
forwardErr := <-errChan
|
||||||
|
if forwardErr != nil {
|
||||||
|
t.Fatalf("ForwardPorts returned error: %s", forwardErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
pf, err := New(dialer, []string{":5000"}, stopChan, readyChan, os.Stdout, os.Stderr)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error while calling New: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
errChan <- pf.ForwardPorts()
|
||||||
|
close(errChan)
|
||||||
|
}()
|
||||||
|
|
||||||
|
<-pf.Ready
|
||||||
|
|
||||||
|
ports, err := pf.GetPorts()
|
||||||
|
|
||||||
|
if len(ports) != 1 {
|
||||||
|
t.Fatalf("expected 1 port, got %d", len(ports))
|
||||||
|
}
|
||||||
|
|
||||||
|
port := ports[0]
|
||||||
|
if port.Local == 0 {
|
||||||
|
t.Fatalf("local port is 0, expected != 0")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user