mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Make installHelmChart injectable
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
a449356e2a
commit
c69eb16248
@ -60,7 +60,6 @@ import listHelmChartsInjectable from "./helm/helm-service/list-helm-charts.injec
|
|||||||
import deleteHelmReleaseInjectable from "./helm/helm-service/delete-helm-release.injectable";
|
import deleteHelmReleaseInjectable from "./helm/helm-service/delete-helm-release.injectable";
|
||||||
import getHelmReleaseHistoryInjectable from "./helm/helm-service/get-helm-release-history.injectable";
|
import getHelmReleaseHistoryInjectable from "./helm/helm-service/get-helm-release-history.injectable";
|
||||||
import getHelmReleaseValuesInjectable from "./helm/helm-service/get-helm-release-values.injectable";
|
import getHelmReleaseValuesInjectable from "./helm/helm-service/get-helm-release-values.injectable";
|
||||||
import installHelmChartInjectable from "./helm/helm-service/install-helm-chart.injectable";
|
|
||||||
import rollbackHelmReleaseInjectable from "./helm/helm-service/rollback-helm-release.injectable";
|
import rollbackHelmReleaseInjectable from "./helm/helm-service/rollback-helm-release.injectable";
|
||||||
import waitUntilBundledExtensionsAreLoadedInjectable from "./start-main-application/lens-window/application-window/wait-until-bundled-extensions-are-loaded.injectable";
|
import waitUntilBundledExtensionsAreLoadedInjectable from "./start-main-application/lens-window/application-window/wait-until-bundled-extensions-are-loaded.injectable";
|
||||||
import { registerMobX } from "@ogre-tools/injectable-extension-for-mobx";
|
import { registerMobX } from "@ogre-tools/injectable-extension-for-mobx";
|
||||||
@ -141,7 +140,6 @@ export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {})
|
|||||||
deleteHelmReleaseInjectable,
|
deleteHelmReleaseInjectable,
|
||||||
getHelmReleaseHistoryInjectable,
|
getHelmReleaseHistoryInjectable,
|
||||||
getHelmReleaseValuesInjectable,
|
getHelmReleaseValuesInjectable,
|
||||||
installHelmChartInjectable,
|
|
||||||
rollbackHelmReleaseInjectable,
|
rollbackHelmReleaseInjectable,
|
||||||
writeJsonFileInjectable,
|
writeJsonFileInjectable,
|
||||||
readJsonFileInjectable,
|
readJsonFileInjectable,
|
||||||
|
|||||||
@ -3,9 +3,6 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import tempy from "tempy";
|
|
||||||
import fse from "fs-extra";
|
|
||||||
import * as yaml from "js-yaml";
|
|
||||||
import type { JsonValue } from "type-fest";
|
import type { JsonValue } from "type-fest";
|
||||||
import { json } from "../../common/utils";
|
import { json } from "../../common/utils";
|
||||||
import { asLegacyGlobalFunctionForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/as-legacy-global-function-for-extension-api";
|
import { asLegacyGlobalFunctionForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/as-legacy-global-function-for-extension-api";
|
||||||
@ -13,51 +10,6 @@ import execHelmInjectable from "./exec-helm/exec-helm.injectable";
|
|||||||
|
|
||||||
const execHelm = asLegacyGlobalFunctionForExtensionApi(execHelmInjectable);
|
const execHelm = asLegacyGlobalFunctionForExtensionApi(execHelmInjectable);
|
||||||
|
|
||||||
export async function installChart(chart: string, values: JsonValue, name: string | undefined = "", namespace: string, version: string, kubeconfigPath: string) {
|
|
||||||
const valuesFilePath = tempy.file({ name: "values.yaml" });
|
|
||||||
|
|
||||||
await fse.writeFile(valuesFilePath, yaml.dump(values));
|
|
||||||
|
|
||||||
const args = ["install"];
|
|
||||||
|
|
||||||
if (name) {
|
|
||||||
args.push(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
args.push(
|
|
||||||
chart,
|
|
||||||
"--version", version,
|
|
||||||
"--values", valuesFilePath,
|
|
||||||
"--namespace", namespace,
|
|
||||||
"--kubeconfig", kubeconfigPath,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!name) {
|
|
||||||
args.push("--generate-name");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
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 {
|
|
||||||
log: output,
|
|
||||||
release: {
|
|
||||||
name: releaseName,
|
|
||||||
namespace,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
} finally {
|
|
||||||
await fse.unlink(valuesFilePath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function deleteRelease(name: string, namespace: string, kubeconfigPath: string): Promise<string> {
|
export async function deleteRelease(name: string, namespace: string, kubeconfigPath: string): Promise<string> {
|
||||||
const result = await execHelm([
|
const result = await execHelm([
|
||||||
"delete",
|
"delete",
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import type { JsonObject } from "type-fest";
|
import type { JsonObject } from "type-fest";
|
||||||
import type { Cluster } from "../../../common/cluster/cluster";
|
import type { Cluster } from "../../../common/cluster/cluster";
|
||||||
import { installChart } from "../helm-release-manager";
|
import installHelmChartInjectable from "../install-helm-chart.injectable";
|
||||||
|
|
||||||
export interface InstallChartArgs {
|
export interface InstallChartArgs {
|
||||||
chart: string;
|
chart: string;
|
||||||
@ -15,16 +15,21 @@ export interface InstallChartArgs {
|
|||||||
version: string;
|
version: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const installHelmChartInjectable = getInjectable({
|
const installClusterHelmChartInjectable = getInjectable({
|
||||||
id: "install-helm-chart",
|
id: "install-cluster-helm-chart",
|
||||||
|
|
||||||
instantiate: () => async (cluster: Cluster, data: InstallChartArgs) => {
|
instantiate: (di) => {
|
||||||
const proxyKubeconfig = await cluster.getProxyKubeconfigPath();
|
const installHelmChart = di.inject(installHelmChartInjectable);
|
||||||
|
|
||||||
return installChart(data.chart, data.values, data.name, data.namespace, data.version, proxyKubeconfig);
|
return async (cluster: Cluster, data: InstallChartArgs) => {
|
||||||
|
const proxyKubeconfig = await cluster.getProxyKubeconfigPath();
|
||||||
|
|
||||||
|
return installHelmChart({
|
||||||
|
...data,
|
||||||
|
kubeconfigPath: proxyKubeconfig,
|
||||||
|
});
|
||||||
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
causesSideEffects: true,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default installHelmChartInjectable;
|
export default installClusterHelmChartInjectable;
|
||||||
|
|||||||
93
src/main/helm/install-helm-chart.injectable.ts
Normal file
93
src/main/helm/install-helm-chart.injectable.ts
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import { dump } from "js-yaml";
|
||||||
|
import tempy from "tempy";
|
||||||
|
import type { JsonValue } from "type-fest";
|
||||||
|
import removePathInjectable from "../../common/fs/remove.injectable";
|
||||||
|
import writeFileInjectable from "../../common/fs/write-file.injectable";
|
||||||
|
import execHelmInjectable from "./exec-helm/exec-helm.injectable";
|
||||||
|
|
||||||
|
export interface InstallHelmChartData {
|
||||||
|
chart: string;
|
||||||
|
values: JsonValue;
|
||||||
|
name: string;
|
||||||
|
namespace: string;
|
||||||
|
version: string;
|
||||||
|
kubeconfigPath: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface InstallHelmChartResult {
|
||||||
|
log: string;
|
||||||
|
release: {
|
||||||
|
name: string;
|
||||||
|
namespace: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export type InstallHelmChart = (data: InstallHelmChartData) => Promise<InstallHelmChartResult>;
|
||||||
|
|
||||||
|
const installHelmChartInjectable = getInjectable({
|
||||||
|
id: "install-helm-chart",
|
||||||
|
instantiate: (di): InstallHelmChart => {
|
||||||
|
const writeFile = di.inject(writeFileInjectable);
|
||||||
|
const removePath = di.inject(removePathInjectable);
|
||||||
|
const execHelm = di.inject(execHelmInjectable);
|
||||||
|
|
||||||
|
return async ({
|
||||||
|
chart,
|
||||||
|
kubeconfigPath,
|
||||||
|
name,
|
||||||
|
namespace,
|
||||||
|
values,
|
||||||
|
version,
|
||||||
|
}) => {
|
||||||
|
const valuesFilePath = tempy.file({ name: "values.yaml" });
|
||||||
|
|
||||||
|
await writeFile(valuesFilePath, dump(values));
|
||||||
|
|
||||||
|
const args = ["install"];
|
||||||
|
|
||||||
|
if (name) {
|
||||||
|
args.push(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
args.push(
|
||||||
|
chart,
|
||||||
|
"--version", version,
|
||||||
|
"--values", valuesFilePath,
|
||||||
|
"--namespace", namespace,
|
||||||
|
"--kubeconfig", kubeconfigPath,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!name) {
|
||||||
|
args.push("--generate-name");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
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 {
|
||||||
|
log: output,
|
||||||
|
release: {
|
||||||
|
name: releaseName,
|
||||||
|
namespace,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} finally {
|
||||||
|
await removePath(valuesFilePath);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default installHelmChartInjectable;
|
||||||
@ -7,7 +7,7 @@ import { getRouteInjectable } from "../../../router/router.injectable";
|
|||||||
import Joi from "joi";
|
import Joi from "joi";
|
||||||
import { payloadValidatedClusterRoute } from "../../../router/route";
|
import { payloadValidatedClusterRoute } from "../../../router/route";
|
||||||
import type { InstallChartArgs } from "../../../helm/helm-service/install-helm-chart.injectable";
|
import type { InstallChartArgs } from "../../../helm/helm-service/install-helm-chart.injectable";
|
||||||
import installHelmChartInjectable from "../../../helm/helm-service/install-helm-chart.injectable";
|
import installClusterHelmChartInjectable from "../../../helm/helm-service/install-helm-chart.injectable";
|
||||||
|
|
||||||
const installChartArgsValidator = Joi.object<InstallChartArgs, true, InstallChartArgs>({
|
const installChartArgsValidator = Joi.object<InstallChartArgs, true, InstallChartArgs>({
|
||||||
chart: Joi
|
chart: Joi
|
||||||
@ -31,7 +31,7 @@ const installChartRouteInjectable = getRouteInjectable({
|
|||||||
id: "install-chart-route",
|
id: "install-chart-route",
|
||||||
|
|
||||||
instantiate: (di) => {
|
instantiate: (di) => {
|
||||||
const installHelmChart = di.inject(installHelmChartInjectable);
|
const installHelmChart = di.inject(installClusterHelmChartInjectable);
|
||||||
|
|
||||||
return payloadValidatedClusterRoute({
|
return payloadValidatedClusterRoute({
|
||||||
method: "post",
|
method: "post",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user