mirror of https://github.com/k3s-io/k3s
Merge pull request #67513 from novas0x2a/expose-port
export a method to expose which ports were forwardedpull/58/head
commit
4a627e9008
|
@ -340,3 +340,20 @@ func (pf *PortForwarder) Close() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetPorts will return the ports that were forwarded; this can be used to
|
||||
// retrieve the locally-bound port in cases where the input was port 0. This
|
||||
// function will signal an error if the Ready channel is nil or if the
|
||||
// listeners are not ready yet; this function will succeed after the Ready
|
||||
// channel has been closed.
|
||||
func (pf *PortForwarder) GetPorts() ([]ForwardedPort, error) {
|
||||
if pf.Ready == nil {
|
||||
return nil, fmt.Errorf("no Ready channel provided")
|
||||
}
|
||||
select {
|
||||
case <-pf.Ready:
|
||||
return pf.ports, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("listeners not ready")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -99,8 +99,17 @@ func TestParsePortsAndNew(t *testing.T) {
|
|||
if dialer.dialed {
|
||||
t.Fatalf("%d: expected not dialed", i)
|
||||
}
|
||||
if e, a := test.expected, pf.ports; !reflect.DeepEqual(e, a) {
|
||||
t.Fatalf("%d: ports: expected %#v, got %#v", i, e, a)
|
||||
if _, portErr := pf.GetPorts(); portErr == nil {
|
||||
t.Fatalf("%d: GetPorts: error expected but got nil", i)
|
||||
}
|
||||
|
||||
// mock-signal the Ready channel
|
||||
close(readyChan)
|
||||
|
||||
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)
|
||||
}
|
||||
if e, a := expectedStopChan, pf.stopChan; e != a {
|
||||
t.Fatalf("%d: stopChan: expected %#v, got %#v", i, e, a)
|
||||
|
|
Loading…
Reference in New Issue