Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve error message when k8s token expired #6382

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/src/plugins/kubernetes/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { NodeJSErrnoException } from "../../exceptions.js"
import { InternalError, isErrnoException } from "../../exceptions.js"
import type { ErrorEvent } from "ws"
import dns from "node:dns"
import { trim } from "lodash-es"

/**
* The flag {@code forceRetry} can be used to avoid {@link shouldRetry} helper call in case if the error code
Expand Down Expand Up @@ -121,6 +122,12 @@ export function toKubernetesError(err: unknown, context: string): KubernetesErro
errorType = "WebsocketError"
originalMessage = err.message
// The ErrorEvent does not expose the status code other than as part of the error.message
} else if (err instanceof Error && err.name === "Error" && err.cause === undefined) {
// exec auth getCredential function of kubernetes client throws plain error
// see also https://github.com/kubernetes-client/javascript/blob/release-1.x/src/exec_auth.ts
// TODO: fix the client to throw a more recognizable error
errorType = "Error"
originalMessage = trim(err.message)
} else {
// In all other cases, we don't know what this is, so let's just throw an InternalError
throw InternalError.wrapError(err, `toKubernetesError encountered an unknown error during ${context}`)
Expand Down
23 changes: 23 additions & 0 deletions core/test/unit/src/plugins/kubernetes/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { ErrorEvent, WebSocket } from "ws"
import { shouldRetry, toKubernetesError } from "../../../../../src/plugins/kubernetes/retry.js"
import { expect } from "chai"
import dedent from "dedent"
import { expectError } from "../../../../helpers.js"

const testKubeOp = "test"
const websocketError: ErrorEvent = {
Expand All @@ -18,6 +19,8 @@ const websocketError: ErrorEvent = {
type: "error",
target: true as unknown as WebSocket,
}
const plainError = new Error("failed to refresh token")
const syntaxError = new SyntaxError("invalid syntax")

describe("toKubernetesError", () => {
it("should handle WebsocketError", () => {
Expand All @@ -33,6 +36,26 @@ describe("toKubernetesError", () => {
expect(err.apiMessage).to.be.undefined
expect(err.type).to.equal("kubernetes")
})

it("should handle plain error gracefully", () => {
const err = toKubernetesError(plainError, testKubeOp)

expect(err).to.be.instanceof(KubernetesError)
expect(err.message).to.equal(dedent`
Error while performing Kubernetes API operation test: Error

failed to refresh token
`)
expect(err.responseStatusCode).to.be.undefined
expect(err.apiMessage).to.be.undefined
expect(err.type).to.equal("kubernetes")
})

it("should crash on other errors like TypeError and SyntaxError", async () => {
await expectError(async () => toKubernetesError(syntaxError, testKubeOp), {
type: "crash",
})
})
})

describe("shouldRetry", () => {
Expand Down