diff --git a/src/common/__tests__/kube-helpers.test.ts b/src/common/__tests__/kube-helpers.test.ts index 46e93cf57d..9b117a8465 100644 --- a/src/common/__tests__/kube-helpers.test.ts +++ b/src/common/__tests__/kube-helpers.test.ts @@ -77,7 +77,7 @@ describe("kube helpers", () => { }); }); describe("with invalid context object", () => { - it("it returns an error", () => { + it("returns an error", () => { expect(String(validateKubeConfig(kc, "invalid"))).toEqual( expect.stringContaining("No valid context object provided in kubeconfig for context 'invalid'") ); @@ -85,7 +85,7 @@ describe("kube helpers", () => { }); describe("with invalid cluster object", () => { - it("it returns an error", () => { + it("returns an error", () => { expect(String(validateKubeConfig(kc, "invalidCluster"))).toEqual( expect.stringContaining("No valid cluster object provided in kubeconfig for context 'invalidCluster'") ); @@ -93,7 +93,7 @@ describe("kube helpers", () => { }); describe("with invalid user object", () => { - it("it returns an error", () => { + it("returns an error", () => { expect(String(validateKubeConfig(kc, "invalidUser"))).toEqual( expect.stringContaining("No valid user object provided in kubeconfig for context 'invalidUser'") ); @@ -101,7 +101,7 @@ describe("kube helpers", () => { }); describe("with invalid exec command", () => { - it("it returns an error", () => { + it("returns an error", () => { expect(String(validateKubeConfig(kc, "invalidExec"))).toEqual( expect.stringContaining("User Exec command \"foo\" not found on host. Please ensure binary is found in PATH or use absolute path to binary in Kubeconfig") ); diff --git a/src/common/kube-helpers.ts b/src/common/kube-helpers.ts index f8a75624f4..56cb12ae2e 100644 --- a/src/common/kube-helpers.ts +++ b/src/common/kube-helpers.ts @@ -210,41 +210,43 @@ export function getNodeWarningConditions(node: V1Node) { * Note: This function returns an error instead of throwing it, returning `undefined` if the validation passes */ export function validateKubeConfig(config: KubeConfig, contextName: string, validationOpts: KubeConfigValidationOpts = {}): Error | undefined { - // we only receive a single context, cluster & user object here so lets validate them as this - // will be called when we add a new cluster to Lens + try { + // we only receive a single context, cluster & user object here so lets validate them as this + // will be called when we add a new cluster to Lens - const { validateUser = true, validateCluster = true, validateExec = true } = validationOpts; + const { validateUser = true, validateCluster = true, validateExec = true } = validationOpts; - const contextObject = config.getContextObject(contextName); + const contextObject = config.getContextObject(contextName); - // Validate the Context Object - if (!contextObject) { - return new Error(`No valid context object provided in kubeconfig for context '${contextName}'`); - } - - // Validate the Cluster Object - if (validateCluster && !config.getCluster(contextObject.cluster)) { - return new Error(`No valid cluster object provided in kubeconfig for context '${contextName}'`); - } - - const user = config.getUser(contextObject.user); - - // Validate the User Object - if (validateUser && !user) { - return new Error(`No valid user object provided in kubeconfig for context '${contextName}'`); - } - - // Validate exec command if present - if (validateExec && user?.exec) { - const execCommand = user.exec["command"]; - // check if the command is absolute or not - const isAbsolute = path.isAbsolute(execCommand); - - // validate the exec struct in the user object, start with the command field - if (!commandExists.sync(execCommand)) { - logger.debug(`validateKubeConfig: exec command ${String(execCommand)} in kubeconfig ${contextName} not found`); - - return new ExecValidationNotFoundError(execCommand, isAbsolute); + // Validate the Context Object + if (!contextObject) { + return new Error(`No valid context object provided in kubeconfig for context '${contextName}'`); } + + // Validate the Cluster Object + if (validateCluster && !config.getCluster(contextObject.cluster)) { + return new Error(`No valid cluster object provided in kubeconfig for context '${contextName}'`); + } + + const user = config.getUser(contextObject.user); + + // Validate the User Object + if (validateUser && !user) { + return new Error(`No valid user object provided in kubeconfig for context '${contextName}'`); + } + + // Validate exec command if present + if (validateExec && user?.exec) { + const execCommand = user.exec["command"]; + // check if the command is absolute or not + const isAbsolute = path.isAbsolute(execCommand); + + // validate the exec struct in the user object, start with the command field + if (!commandExists.sync(execCommand)) { + return new ExecValidationNotFoundError(execCommand, isAbsolute); + } + } + } catch (error) { + return error; } }