Skip to content

Commit

Permalink
Use the config client_idle_timeout instead of a hardcoded value to al…
Browse files Browse the repository at this point in the history
…low for long-running fort forard / exec sessions.
  • Loading branch information
creack committed Nov 17, 2024
1 parent ae27046 commit 9c212cf
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
2 changes: 0 additions & 2 deletions lib/kube/proxy/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ const (

// DefaultStreamCreationTimeout
DefaultStreamCreationTimeout = 30 * time.Second

IdleTimeout = 15 * time.Minute
)

// These constants are for remote command execution and port forwarding and are
Expand Down
3 changes: 2 additions & 1 deletion lib/kube/proxy/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,7 @@ func (f *Forwarder) exec(authCtx *authContext, w http.ResponseWriter, req *http.
httpResponseWriter: w,
context: ctx,
pingPeriod: f.cfg.ConnPingPeriod,
idleTimeout: sess.clientIdleTimeout,
onResize: func(remotecommand.TerminalSize) {},
}

Expand Down Expand Up @@ -1811,6 +1812,7 @@ func (f *Forwarder) portForward(authCtx *authContext, w http.ResponseWriter, req
onPortForward: onPortForward,
targetDialer: dialer,
pingPeriod: f.cfg.ConnPingPeriod,
idleTimeout: sess.clientIdleTimeout,
}
f.log.Debugf("Starting %v.", request)
err = runPortForwarding(request)
Expand Down Expand Up @@ -2186,7 +2188,6 @@ func (f *Forwarder) getSPDYExecutor(sess *clusterSession, req *http.Request) (re
}

func (f *Forwarder) getPortForwardDialer(sess *clusterSession, req *http.Request) (httpstream.Dialer, error) {

wsDialer, err := f.getWebsocketDialer(sess, req)
if err != nil {
return nil, trace.Wrap(err)
Expand Down
7 changes: 5 additions & 2 deletions lib/kube/proxy/portforward_spdy.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type portForwardRequest struct {
context context.Context
targetDialer httpstream.Dialer
pingPeriod time.Duration
idleTimeout time.Duration
}

func (p portForwardRequest) String() string {
Expand Down Expand Up @@ -102,8 +103,10 @@ func runPortForwardingHTTPStreams(req portForwardRequest) error {
targetConn: targetConn,
}
defer h.Close()
h.Debugf("Setting port forwarding streaming connection idle timeout to %v", IdleTimeout)
conn.SetIdleTimeout(IdleTimeout)
if req.idleTimeout != 0 {
h.Debugf("Setting port forwarding streaming connection idle timeout to %s.", req.idleTimeout)
conn.SetIdleTimeout(req.idleTimeout)
}
h.run()
return nil
}
Expand Down
13 changes: 10 additions & 3 deletions lib/kube/proxy/portforward_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ func runPortForwardingWebSocket(req portForwardRequest) error {
Channels: channels,
},
})
conn.SetIdleTimeout(IdleTimeout)

if req.idleTimeout != 0 {
conn.SetIdleTimeout(req.idleTimeout)
}

// Upgrade the request and create the virtual streams.
_, streams, err := conn.Open(
Expand Down Expand Up @@ -352,8 +355,12 @@ func runPortForwardingTunneledHTTPStreams(req portForwardRequest) error {
targetConn: targetConn,
}
defer h.Close()
h.Debugf("Setting port forwarding streaming connection idle timeout to %v", IdleTimeout)
spdyConn.SetIdleTimeout(IdleTimeout)

if req.idleTimeout != 0 {
h.Debugf("Setting port forwarding streaming connection idle timeout to %s.", req.idleTimeout)
spdyConn.SetIdleTimeout(req.idleTimeout)
}

h.run()
return nil
}
5 changes: 4 additions & 1 deletion lib/kube/proxy/remotecommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type remoteCommandRequest struct {
onResize resizeCallback
context context.Context
pingPeriod time.Duration
idleTimeout time.Duration
}

func (req remoteCommandRequest) eventPodMeta(ctx context.Context, creds kubeCreds) apievents.KubernetesPodMetadata {
Expand Down Expand Up @@ -156,7 +157,9 @@ func createSPDYStreams(req remoteCommandRequest) (*remoteCommandProxy, error) {
return nil, trace.ConnectionProblem(trace.BadParameter("missing connection"), "missing connection")
}

conn.SetIdleTimeout(IdleTimeout)
if req.idleTimeout != 0 {
conn.SetIdleTimeout(req.idleTimeout)
}

var handler protocolHandler
switch protocol {
Expand Down
6 changes: 5 additions & 1 deletion lib/kube/proxy/remotecommand_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ func createWebSocketStreams(req remoteCommandRequest) (*remoteCommandProxy, erro
Channels: channels,
},
})
conn.SetIdleTimeout(IdleTimeout)

if req.idleTimeout != 0 {
conn.SetIdleTimeout(req.idleTimeout)
}

negotiatedProtocol, streams, err := conn.Open(
responsewriter.GetOriginal(req.httpResponseWriter),
req.httpRequest,
Expand Down

0 comments on commit 9c212cf

Please sign in to comment.