diff --git a/src/main/helm/exec.ts b/src/main/helm/exec.ts deleted file mode 100644 index fa96846089..0000000000 --- a/src/main/helm/exec.ts +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ - -import { promiseExecFile } from "../../common/utils/promise-exec"; -import type { ObjectEncodingOptions } from "fs"; -import type { ExecFileOptions, ExecFileOptionsWithStringEncoding } from "child_process"; -import { isChildProcessError } from "../../common/utils"; -import { getLegacyGlobalDiForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api"; -import helmBinaryPathInjectable from "./helm-binary-path.injectable"; -import userStoreInjectable from "../../common/user-store/user-store.injectable"; - -/** - * ExecFile the bundled helm CLI - * @returns STDOUT - */ -export async function execHelm(args: string[], { encoding, ...rest }: ObjectEncodingOptions & ExecFileOptions = {}): Promise { - const options: ExecFileOptionsWithStringEncoding = { - encoding: encoding ?? "utf-8", - ...rest, - }; - const di = getLegacyGlobalDiForExtensionApi(); - const helmBinaryPath = di.inject(helmBinaryPathInjectable); - const userStore = di.inject(userStoreInjectable); - - try { - const opts = { ...options }; - - opts.env ??= { ...process.env }; - - if (!opts.env.HTTPS_PROXY && userStore.httpsProxy) { - opts.env.HTTPS_PROXY = userStore.httpsProxy; - } - - const { stdout } = await promiseExecFile(helmBinaryPath, args, opts); - - return stdout; - } catch (error) { - if (isChildProcessError(error, "string")) { - throw error.stderr || error; - } - - throw error; - } -} diff --git a/src/main/helm/helm-release-manager.ts b/src/main/helm/helm-release-manager.ts index 29bbbdd830..0d98b6ec44 100644 --- a/src/main/helm/helm-release-manager.ts +++ b/src/main/helm/helm-release-manager.ts @@ -7,9 +7,12 @@ import tempy from "tempy"; import fse from "fs-extra"; import * as yaml from "js-yaml"; import { toCamelCase } from "../../common/utils/camelCase"; -import { execHelm } from "./exec"; import type { JsonValue } from "type-fest"; import { isObject, json } from "../../common/utils"; +import { asLegacyGlobalFunctionForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/as-legacy-global-function-for-extension-api"; +import execHelmInjectable from "./exec-helm/exec-helm.injectable"; + +const execHelm = asLegacyGlobalFunctionForExtensionApi(execHelmInjectable); export async function listReleases(pathToKubeconfig: string, namespace?: string): Promise[]> { const args = [ @@ -26,7 +29,13 @@ export async function listReleases(pathToKubeconfig: string, namespace?: string) args.push("--kubeconfig", pathToKubeconfig); - const output = json.parse(await execHelm(args)); + const result = await execHelm(args); + + if (!result.callWasSuccessful) { + throw result.error; + } + + const output = json.parse(result.response); if (!Array.isArray(output) || output.length == 0) { return []; @@ -60,7 +69,13 @@ export async function installChart(chart: string, values: JsonValue, name: strin } try { - const output = await execHelm(args); + const result = await execHelm(args); + + if (!result.callWasSuccessful) { + throw result.error; + } + + const output = result.response; const releaseName = output.split("\n")[0].split(" ")[1].trim(); return { @@ -75,13 +90,19 @@ export async function installChart(chart: string, values: JsonValue, name: strin } } -export async function deleteRelease(name: string, namespace: string, kubeconfigPath: string) { - return execHelm([ +export async function deleteRelease(name: string, namespace: string, kubeconfigPath: string): Promise { + const result = await execHelm([ "delete", name, "--namespace", namespace, "--kubeconfig", kubeconfigPath, ]); + + if (result.callWasSuccessful) { + return result.response; + } + + throw result.error; } interface GetValuesOptions { @@ -90,7 +111,7 @@ interface GetValuesOptions { kubeconfigPath: string; } -export async function getValues(name: string, { namespace, all = false, kubeconfigPath }: GetValuesOptions) { +export async function getValues(name: string, { namespace, all = false, kubeconfigPath }: GetValuesOptions): Promise { const args = [ "get", "values", @@ -107,25 +128,41 @@ export async function getValues(name: string, { namespace, all = false, kubeconf "--kubeconfig", kubeconfigPath, ); - return execHelm(args); + const result = await execHelm(args); + + if (result.callWasSuccessful) { + return result.response; + } + + throw result.error; } -export async function getHistory(name: string, namespace: string, kubeconfigPath: string) { - return json.parse(await execHelm([ +export async function getHistory(name: string, namespace: string, kubeconfigPath: string): Promise { + const result = await execHelm([ "history", name, "--output", "json", "--namespace", namespace, "--kubeconfig", kubeconfigPath, - ])); + ]); + + if (result.callWasSuccessful) { + return json.parse(result.response); + } + + throw result.error; } -export async function rollback(name: string, namespace: string, revision: number, kubeconfigPath: string) { - await execHelm([ +export async function rollback(name: string, namespace: string, revision: number, kubeconfigPath: string): Promise { + const result = await execHelm([ "rollback", name, `${revision}`, "--namespace", namespace, "--kubeconfig", kubeconfigPath, ]); + + if (!result.callWasSuccessful) { + throw result.error; + } }