Skip to content

client: include response body in ProtocolSwitch error#1984

Open
SebTardif wants to merge 1 commit into
kube-rs:mainfrom
SebTardif:fix-upgrade-error-body
Open

client: include response body in ProtocolSwitch error#1984
SebTardif wants to merge 1 commit into
kube-rs:mainfrom
SebTardif:fix-upgrade-error-body

Conversation

@SebTardif
Copy link
Copy Markdown
Contributor

What this PR does

Fixes #1405.

When exec, portforward, or attach fails because the Kubernetes API server returns a non-101 response (e.g. 403 Forbidden, 404 Pod Not Found), the HTTP response body, which contains a full metav1.Status JSON with message, reason, and details, was discarded. Users saw only:

failed to switch protocol: 403 Forbidden

...instead of the actual error:

pods "my-pod" is forbidden: User "system:serviceaccount:default:my-sa" cannot create resource "pods/exec"

Changes:

  • Changed ProtocolSwitch(StatusCode) to ProtocolSwitch { status, body } struct variant
  • Made verify_response async to read the response body on failure
  • Updated the error Display format to include the body text
  • Updated the call site in mod.rs

Closes #1405

AI Disclosure

Developed with AI assistance (Grok by xAI) in a human-in-the-loop workflow. All code reviewed and verified by the human author.

Signed-off-by: Sebastien Tardif sebtardif@ncf.ca

When a WebSocket upgrade fails with a non-101 status (e.g. 403 Forbidden),
the Kubernetes API server returns a JSON metav1.Status body with the actual
error message, reason, and details. Previously this body was discarded,
leaving users with only "failed to switch protocol: 403 Forbidden".

Change ProtocolSwitch from a tuple variant carrying just the StatusCode to
a struct variant carrying both the status and the response body string.
Make verify_response async so it can read the body before returning the
error.

Closes kube-rs#1405

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 21, 2026

Codecov Report

❌ Patch coverage is 54.54545% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.7%. Comparing base (67e4671) to head (b868a0f).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
kube-client/src/client/upgrade.rs 37.5% 5 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #1984     +/-   ##
=======================================
+ Coverage   75.6%   75.7%   +0.2%     
=======================================
  Files         89      89             
  Lines       8666    8751     +85     
=======================================
+ Hits        6544    6622     +78     
- Misses      2122    2129      +7     
Files with missing lines Coverage Δ
kube-client/src/client/mod.rs 83.0% <100.0%> (+5.4%) ⬆️
kube-client/src/client/upgrade.rs 77.1% <37.5%> (-5.4%) ⬇️

... and 9 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Websocket upgrade error messages are discarded

1 participant