Make opening of release details work properly when release has resources without namespace (#6088)

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>
Co-authored-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
authored and GitHub committed 2022-09-08 13:57:11 -04:00
1 parent d4bb416f99
commit ec78080d99
38 files changed
+11090 -10160

No files matched your search

+1 -111
View File
@@ -7,10 +7,8 @@ import tempy from "tempy";
import fse from "fs-extra";
import * as yaml from "js-yaml";
import { toCamelCase } from "../../common/utils/camelCase";
import { execFile } from "child_process";
import { execHelm } from "./exec";
import assert from "assert";
import type { JsonObject, JsonValue } from "type-fest";
import type { JsonValue } from "type-fest";
import { isObject, json } from "../../common/utils";
export async function listReleases(pathToKubeconfig: string, namespace?: string): Promise<Record<string, any>[]> {
@@ -77,55 +75,6 @@ export async function installChart(chart: string, values: JsonValue, name: strin
}
}
export async function upgradeRelease(name: string, chart: string, values: any, namespace: string, version: string, kubeconfigPath: string, kubectlPath: string) {
const valuesFilePath = tempy.file({ name: "values.yaml" });
await fse.writeFile(valuesFilePath, yaml.dump(values));
const args = [
"upgrade",
name,
chart,
"--version", version,
"--values", valuesFilePath,
"--namespace", namespace,
"--kubeconfig", kubeconfigPath,
];
try {
const output = await execHelm(args);
return {
log: output,
release: await getRelease(name, namespace, kubeconfigPath, kubectlPath),
};
} finally {
await fse.unlink(valuesFilePath);
}
}
export async function getRelease(name: string, namespace: string, kubeconfigPath: string, kubectlPath: string) {
const args = [
"status",
name,
"--namespace", namespace,
"--kubeconfig", kubeconfigPath,
"--output", "json",
];
const release = json.parse(await execHelm(args, {
maxBuffer: 32 * 1024 * 1024 * 1024, // 32 MiB
}));
if (!isObject(release) || Array.isArray(release)) {
return undefined;
}
release.resources = await getResources(name, namespace, kubeconfigPath, kubectlPath);
return release;
}
export async function deleteRelease(name: string, namespace: string, kubeconfigPath: string) {
return execHelm([
"delete",
@@ -180,62 +129,3 @@ export async function rollback(name: string, namespace: string, revision: number
"--kubeconfig", kubeconfigPath,
]);
}
async function getResources(name: string, namespace: string, kubeconfigPath: string, kubectlPath: string) {
const helmArgs = [
"get",
"manifest",
name,
"--namespace", namespace,
"--kubeconfig", kubeconfigPath,
];
const kubectlArgs = [
"get",
"--kubeconfig", kubeconfigPath,
"-f", "-",
"--output", "json",
// Temporary workaround for https://github.com/lensapp/lens/issues/6031
// and other potential issues where resources can't be found. Showing
// no resources is better than the app hard-locking, and at least
// the helm metadata is shown.
"--ignore-not-found",
];
try {
const helmOutput = await execHelm(helmArgs);
return new Promise<JsonObject[]>((resolve, reject) => {
let stdout = "";
let stderr = "";
const kubectl = execFile(kubectlPath, kubectlArgs);
kubectl
.on("exit", (code, signal) => {
if (typeof code === "number") {
if (code === 0) {
if (stdout === "") {
resolve([]);
} else {
const output = json.parse(stdout) as { items: JsonObject[] };
resolve(output.items);
}
} else {
reject(stderr);
}
} else {
reject(new Error(`Kubectl exited with signal ${signal}`));
}
})
.on("error", reject);
assert(kubectl.stderr && kubectl.stdout && kubectl.stdin, "For some reason the IO streams are undefined");
kubectl.stderr.on("data", output => stderr += output);
kubectl.stdout.on("data", output => stdout += output);
kubectl.stdin.end(helmOutput);
});
} catch {
return [];
}
}