Skip to content

Commit 73888b6

Browse files
mnenciagbartolini
andauthored
fix(exec): use FallbackExecutor to restore OpenShift compatibility (#10933)
Switch `execCommandOnce` from `NewWebSocketExecutor` to `NewFallbackExecutor`, which tries WebSocket first and falls back to SPDY when the handshake is rejected. OpenShift rejects WebSocket exec upgrades with 403 Forbidden, causing every exec call through this path to fail. Closes #10931 Signed-off-by: Marco Nenciarini <[email protected]> Signed-off-by: Gabriele Bartolini <[email protected]> Co-authored-by: Gabriele Bartolini <[email protected]>
1 parent b4a50c3 commit 73888b6

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

‎pkg/utils/exec.go‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"k8s.io/client-go/kubernetes/scheme"
4040
"k8s.io/client-go/rest"
4141
"k8s.io/client-go/tools/remotecommand"
42+
"k8s.io/streaming/pkg/httpstream"
4243
)
4344

4445
const (
@@ -172,6 +173,17 @@ func ExecCommand(
172173
return stdout, stderr, execErr
173174
}
174175

176+
// shouldFallbackToSPDY reports whether a failed WebSocket streaming attempt
177+
// should be retried over SPDY. This mirrors kubectl's own fallback predicate:
178+
// a WebSocket upgrade failure (e.g. an API server or container runtime that
179+
// does not support the V5 WebSocket subprotocol, as on some OpenShift
180+
// versions) is wrapped as an httpstream.UpgradeFailureError, and an HTTPS
181+
// proxy dial failure is detected separately. Both indicate the connection was
182+
// never established over WebSocket and is safe to retry over SPDY.
183+
func shouldFallbackToSPDY(err error) bool {
184+
return httpstream.IsUpgradeFailure(err) || httpstream.IsHTTPSProxyError(err)
185+
}
186+
175187
// execCommandOnce performs a single kubectl exec operation without retries
176188
func execCommandOnce(
177189
ctx context.Context,
@@ -202,7 +214,15 @@ func execCommandOnce(
202214
Stderr: true,
203215
}, scheme.ParameterCodec)
204216

205-
executor, err := remotecommand.NewWebSocketExecutor(&newConfig, "POST", req.URL().String())
217+
wsExecutor, err := remotecommand.NewWebSocketExecutor(&newConfig, "POST", req.URL().String())
218+
if err != nil {
219+
return "", "", err
220+
}
221+
spdyExecutor, err := remotecommand.NewSPDYExecutor(&newConfig, "POST", req.URL())
222+
if err != nil {
223+
return "", "", err
224+
}
225+
executor, err := remotecommand.NewFallbackExecutor(wsExecutor, spdyExecutor, shouldFallbackToSPDY)
206226
if err != nil {
207227
return "", "", err
208228
}

‎pkg/utils/exec_test.go‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ package utils
2121

2222
import (
2323
"errors"
24+
"fmt"
2425

2526
apierrors "k8s.io/apimachinery/pkg/api/errors"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2628
"k8s.io/apimachinery/pkg/runtime/schema"
29+
"k8s.io/streaming/pkg/httpstream"
2730

2831
. "github.com/onsi/ginkgo/v2"
2932
. "github.com/onsi/gomega"
@@ -160,3 +163,44 @@ var _ = Describe("isRetryableExecError", func() {
160163
})
161164
})
162165
})
166+
167+
var _ = Describe("shouldFallbackToSPDY", func() {
168+
It("should return false for a nil error", func() {
169+
Expect(shouldFallbackToSPDY(nil)).To(BeFalse())
170+
})
171+
172+
It("should return true for a WebSocket upgrade failure (bad handshake)", func() {
173+
err := &httpstream.UpgradeFailureError{Cause: errors.New("websocket: bad handshake")}
174+
Expect(shouldFallbackToSPDY(err)).To(BeTrue())
175+
})
176+
177+
It("should return true when the upgrade failure is wrapped", func() {
178+
err := fmt.Errorf("error streaming: %w",
179+
&httpstream.UpgradeFailureError{Cause: errors.New("websocket: bad handshake")})
180+
Expect(shouldFallbackToSPDY(err)).To(BeTrue())
181+
})
182+
183+
It("should return true even when the handshake cause is a decoded Kubernetes status", func() {
184+
// The WebSocket round tripper replaces the bad-handshake cause with a
185+
// StatusError when the server returns a decodable metav1.Status, so the
186+
// error string no longer mentions "bad handshake". The fallback must
187+
// still trigger off the UpgradeFailureError wrapper.
188+
statusErr := &apierrors.StatusError{ErrStatus: metav1.Status{
189+
Message: "the server does not allow this method on the requested resource",
190+
Reason: metav1.StatusReasonBadRequest,
191+
}}
192+
err := &httpstream.UpgradeFailureError{Cause: statusErr}
193+
Expect(err.Error()).NotTo(ContainSubstring("bad handshake"))
194+
Expect(shouldFallbackToSPDY(err)).To(BeTrue())
195+
})
196+
197+
It("should return true for an HTTPS proxy dial error", func() {
198+
err := errors.New("proxy: unknown scheme: https")
199+
Expect(shouldFallbackToSPDY(err)).To(BeTrue())
200+
})
201+
202+
It("should return false for an unrelated error", func() {
203+
err := errors.New("command terminated with exit code 1")
204+
Expect(shouldFallbackToSPDY(err)).To(BeFalse())
205+
})
206+
})

0 commit comments

Comments
 (0)