1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Remove last use of legacy global execHelm

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-11-30 14:41:47 -05:00
parent 4c1e1ac1db
commit 1c277a96fb
2 changed files with 49 additions and 58 deletions

View File

@ -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<string> {
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;
}
}

View File

@ -7,9 +7,12 @@ import tempy from "tempy";
import fse from "fs-extra"; import fse from "fs-extra";
import * as yaml from "js-yaml"; import * as yaml from "js-yaml";
import { toCamelCase } from "../../common/utils/camelCase"; import { toCamelCase } from "../../common/utils/camelCase";
import { execHelm } from "./exec";
import type { JsonValue } from "type-fest"; import type { JsonValue } from "type-fest";
import { isObject, json } from "../../common/utils"; 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<Record<string, any>[]> { export async function listReleases(pathToKubeconfig: string, namespace?: string): Promise<Record<string, any>[]> {
const args = [ const args = [
@ -26,7 +29,13 @@ export async function listReleases(pathToKubeconfig: string, namespace?: string)
args.push("--kubeconfig", pathToKubeconfig); 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) { if (!Array.isArray(output) || output.length == 0) {
return []; return [];
@ -60,7 +69,13 @@ export async function installChart(chart: string, values: JsonValue, name: strin
} }
try { 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(); const releaseName = output.split("\n")[0].split(" ")[1].trim();
return { 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) { export async function deleteRelease(name: string, namespace: string, kubeconfigPath: string): Promise<string> {
return execHelm([ const result = await execHelm([
"delete", "delete",
name, name,
"--namespace", namespace, "--namespace", namespace,
"--kubeconfig", kubeconfigPath, "--kubeconfig", kubeconfigPath,
]); ]);
if (result.callWasSuccessful) {
return result.response;
}
throw result.error;
} }
interface GetValuesOptions { interface GetValuesOptions {
@ -90,7 +111,7 @@ interface GetValuesOptions {
kubeconfigPath: string; 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<string> {
const args = [ const args = [
"get", "get",
"values", "values",
@ -107,25 +128,41 @@ export async function getValues(name: string, { namespace, all = false, kubeconf
"--kubeconfig", kubeconfigPath, "--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) { export async function getHistory(name: string, namespace: string, kubeconfigPath: string): Promise<JsonValue> {
return json.parse(await execHelm([ const result = await execHelm([
"history", "history",
name, name,
"--output", "json", "--output", "json",
"--namespace", namespace, "--namespace", namespace,
"--kubeconfig", kubeconfigPath, "--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) { export async function rollback(name: string, namespace: string, revision: number, kubeconfigPath: string): Promise<void> {
await execHelm([ const result = await execHelm([
"rollback", "rollback",
name, name,
`${revision}`, `${revision}`,
"--namespace", namespace, "--namespace", namespace,
"--kubeconfig", kubeconfigPath, "--kubeconfig", kubeconfigPath,
]); ]);
if (!result.callWasSuccessful) {
throw result.error;
}
} }