Skip to content

Commit 258b83f

Browse files
authored
k8s: add websocket support to exec (#6496)
Signed-off-by: Mark Konrad <[email protected]>
1 parent d4b9c2c commit 258b83f

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

internal/k8s/exec.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"io"
88

99
corev1 "k8s.io/api/core/v1"
10+
"k8s.io/apimachinery/pkg/util/httpstream"
1011
"k8s.io/client-go/tools/remotecommand"
1112
"k8s.io/kubectl/pkg/scheme"
1213

1314
"github.com/tilt-dev/tilt/internal/container"
1415
)
1516

16-
func (k *K8sClient) Exec(_ context.Context, podID PodID, cName container.Name, n Namespace, cmd []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
17+
func (k *K8sClient) Exec(ctx context.Context, podID PodID, cName container.Name, n Namespace, cmd []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
1718
req := k.core.RESTClient().Post().
1819
Resource("pods").
1920
Namespace(n.String()).
@@ -28,12 +29,20 @@ func (k *K8sClient) Exec(_ context.Context, podID PodID, cName container.Name, n
2829
Stderr: stderr != nil,
2930
}, scheme.ParameterCodec)
3031

31-
exec, err := remotecommand.NewSPDYExecutor(k.restConfig, "POST", req.URL())
32+
spdyExec, err := remotecommand.NewSPDYExecutor(k.restConfig, "POST", req.URL())
3233
if err != nil {
33-
return fmt.Errorf("establishing connection: %w", err)
34+
return fmt.Errorf("failed to create spdy executor: %w", err)
3435
}
36+
websocketExec, err := remotecommand.NewWebSocketExecutor(k.restConfig, "GET", req.URL().String())
37+
if err != nil {
38+
return fmt.Errorf("failed to create websocket executor: %w", err)
39+
}
40+
41+
exec, _ := remotecommand.NewFallbackExecutor(websocketExec, spdyExec, func(err error) bool {
42+
return httpstream.IsUpgradeFailure(err) || httpstream.IsHTTPSProxyError(err)
43+
})
3544

36-
err = exec.Stream(remotecommand.StreamOptions{
45+
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
3746
Stdin: stdin,
3847
Stdout: stdout,
3948
Stderr: stderr,

0 commit comments

Comments
 (0)