Skip to content

fix(trpc): improve kubernetes api error parsing and admission webhook handling - #398

Open
Norway-02 wants to merge 1 commit into
volcano-sh:mainfrom
Norway-02:fix/trpc-k8s-error-parsing
Open

Norway-02 wants to merge 1 commit into
volcano-sh:mainfrom
Norway-02:fix/trpc-k8s-error-parsing

Conversation

@Norway-02

Copy link
Copy Markdown

What type of PR is this?

/kind bug

What this PR does / why we need it:

This PR enhances Kubernetes API error handling and admission webhook message parsing in packages/trpc/server/utils/k8s-errors.ts:

  1. Plain-Text Webhook & Response Body Parsing:

    • parseK8sStatusBody previously attempted JSON.parse on all string response bodies and returned null upon failure. Kubernetes admission webhook rejections (such as Volcano validatequeue or validatejob webhooks) and non-JSON server error responses are returned as plain-text strings. Because parsing failed, the webhook formatter formatAdmissionWebhookMessage was completely bypassed.
    • Non-JSON string bodies are now safely preserved as { message: trimmed } so webhook constraint parsers (deserved[...] <= capability[...], deserved[...] >= guarantee[...]) can process and format them into plain, actionable language.
  2. Prevent Leaking Raw Debug Dumps into User Toasts:

    • @kubernetes/client-node's ApiException class generates a multi-line debugging string for error.message (HTTP-Code: ...\nMessage: ...\nBody: ...\nHeaders: ...). Falling back to error.message leaked this entire HTTP header and body dump directly into dashboard UI toasts.
    • The fallback now checks if the error is an ApiException or starts with HTTP-Code:, returning clean, localized status messages instead.
  3. Robust Code & Body Extraction:

    • Safely extracts error codes from code, statusCode, and response.statusCode.
    • Safely checks both error.body and error.response.body.
    • Added user-friendly messages for standard HTTP status codes: 401 (Unauthorized), 403 (Forbidden), 404 (Not Found), 409 (Conflict), 500 (Internal Server Error), and 503 (Service Unavailable).
  4. Unit Test Suite:

    • Added packages/trpc/server/utils/k8s-errors.test.ts covering JSON bodies, plain-text webhook rejections, constraint formatters, 404/409 handling, and standard HTTP error cases.

Which issue(s) this PR fixes:

N/A

Special notes for your reviewer:

  • Zero breaking API changes; only improves error parsing and formatting for user-facing toasts and mutations.
  • All unit tests pass with npx tsx --test packages/trpc/server/utils/k8s-errors.test.ts.

Does this PR introduce a user-facing change?

Fix raw HTTP header debug dump leakage and improve plain-text admission webhook error formatting for Kubernetes API requests.

…k handling

- Parse plain-text string bodies in parseK8sStatusBody so admission webhook rejection messages (e.g. validatequeue constraint errors) are properly extracted instead of failing JSON parsing.
- Avoid leaking raw multi-line ApiException debug dumps (HTTP-Code/Body/Headers) to user-facing UI toasts when status messages are missing.
- Check both error.body and error.response.body as well as status codes gracefully.
- Provide clean human-readable fallbacks for HTTP 401, 403, 500, and 503.
- Add unit test suite validating parseK8sStatusBody, constraint formatters, and ApiException handling.

Signed-off-by: Norway-02 <anshulkhetade02@gmail.com>
@volcano-sh-bot

Copy link
Copy Markdown
Contributor

@Norway-02: The label(s) kind/bug cannot be applied, because the repository doesn't have them.

Details

In response to this:

What type of PR is this?

/kind bug

What this PR does / why we need it:

This PR enhances Kubernetes API error handling and admission webhook message parsing in packages/trpc/server/utils/k8s-errors.ts:

  1. Plain-Text Webhook & Response Body Parsing:
  • parseK8sStatusBody previously attempted JSON.parse on all string response bodies and returned null upon failure. Kubernetes admission webhook rejections (such as Volcano validatequeue or validatejob webhooks) and non-JSON server error responses are returned as plain-text strings. Because parsing failed, the webhook formatter formatAdmissionWebhookMessage was completely bypassed.
  • Non-JSON string bodies are now safely preserved as { message: trimmed } so webhook constraint parsers (deserved[...] <= capability[...], deserved[...] >= guarantee[...]) can process and format them into plain, actionable language.
  1. Prevent Leaking Raw Debug Dumps into User Toasts:
  • @kubernetes/client-node's ApiException class generates a multi-line debugging string for error.message (HTTP-Code: ...\nMessage: ...\nBody: ...\nHeaders: ...). Falling back to error.message leaked this entire HTTP header and body dump directly into dashboard UI toasts.
  • The fallback now checks if the error is an ApiException or starts with HTTP-Code:, returning clean, localized status messages instead.
  1. Robust Code & Body Extraction:
  • Safely extracts error codes from code, statusCode, and response.statusCode.
  • Safely checks both error.body and error.response.body.
  • Added user-friendly messages for standard HTTP status codes: 401 (Unauthorized), 403 (Forbidden), 404 (Not Found), 409 (Conflict), 500 (Internal Server Error), and 503 (Service Unavailable).
  1. Unit Test Suite:
  • Added packages/trpc/server/utils/k8s-errors.test.ts covering JSON bodies, plain-text webhook rejections, constraint formatters, 404/409 handling, and standard HTTP error cases.

Which issue(s) this PR fixes:

N/A

Special notes for your reviewer:

  • Zero breaking API changes; only improves error parsing and formatting for user-facing toasts and mutations.
  • All unit tests pass with npx tsx --test packages/trpc/server/utils/k8s-errors.test.ts.

Does this PR introduce a user-facing change?

Fix raw HTTP header debug dump leakage and improve plain-text admission webhook error formatting for Kubernetes API requests.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@volcano-sh-bot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign william-wang for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Norway-02

Copy link
Copy Markdown
Author

cc @de6p @JesseStutler @kevin-wangzefeng for review when you have a moment. Thanks!

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants