mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-21 20:59:17 +00:00
portforward: return error on lost connection to pod
Currently, when the remote connection is unexpected closed, forward() prints an error message saying "lost connection to pod" via runtime.HandleError, but then it returns nil for the error. This prevents the caller from being able to handle this error differently. This commit changes forward() to return the "lost connection to pod" error so that it can be handled by the caller. Making this change enables kubectl port-forward to exit with code 1, instead of 0, which is the expected behavior for a command that has failed. Kubernetes-commit: a9f04103854893056237a09250ad3335867b0391
This commit is contained in:
parent
5ea7f43b5d
commit
d0842249d3
@ -37,6 +37,8 @@ import (
|
||||
// TODO move to API machinery and re-unify with kubelet/server/portfoward
|
||||
const PortForwardProtocolV1Name = "portforward.k8s.io"
|
||||
|
||||
var ErrLostConnectionToPod = errors.New("lost connection to pod")
|
||||
|
||||
// PortForwarder knows how to listen for local connections and forward them to
|
||||
// a remote pod via an upgraded HTTP request.
|
||||
type PortForwarder struct {
|
||||
@ -230,7 +232,7 @@ func (pf *PortForwarder) forward() error {
|
||||
select {
|
||||
case <-pf.stopChan:
|
||||
case <-pf.streamConn.CloseChan():
|
||||
runtime.HandleError(errors.New("lost connection to pod"))
|
||||
return ErrLostConnectionToPod
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -567,3 +567,33 @@ func TestWaitForConnectionExitsOnStreamConnClosed(t *testing.T) {
|
||||
port := ForwardedPort{}
|
||||
pf.waitForConnection(&listener, port)
|
||||
}
|
||||
|
||||
func TestForwardPortsReturnsErrorWhenConnectionIsLost(t *testing.T) {
|
||||
dialer := &fakeDialer{
|
||||
conn: newFakeConnection(),
|
||||
}
|
||||
|
||||
stopChan := make(chan struct{})
|
||||
readyChan := make(chan struct{})
|
||||
errChan := make(chan error)
|
||||
|
||||
pf, err := New(dialer, []string{":5000"}, stopChan, readyChan, os.Stdout, os.Stderr)
|
||||
if err != nil {
|
||||
t.Fatalf("faile to create new PortForwarder: %s", err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
errChan <- pf.ForwardPorts()
|
||||
}()
|
||||
|
||||
<-pf.Ready
|
||||
|
||||
pf.streamConn.Close()
|
||||
|
||||
err = <-errChan
|
||||
if err == nil {
|
||||
t.Fatalf("unexpected non-error from pf.ForwardPorts()")
|
||||
} else if err != ErrLostConnectionToPod {
|
||||
t.Fatalf("unexpected error from pf.ForwardPorts(): %s", err)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user