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

Make listHelmReleases injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-12-22 12:47:29 -05:00
parent ad09d15385
commit a449356e2a
5 changed files with 58 additions and 45 deletions

View File

@ -61,7 +61,6 @@ import deleteHelmReleaseInjectable from "./helm/helm-service/delete-helm-release
import getHelmReleaseHistoryInjectable from "./helm/helm-service/get-helm-release-history.injectable";
import getHelmReleaseValuesInjectable from "./helm/helm-service/get-helm-release-values.injectable";
import installHelmChartInjectable from "./helm/helm-service/install-helm-chart.injectable";
import listHelmReleasesInjectable from "./helm/helm-service/list-helm-releases.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 { registerMobX } from "@ogre-tools/injectable-extension-for-mobx";
@ -143,7 +142,6 @@ export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {})
getHelmReleaseHistoryInjectable,
getHelmReleaseValuesInjectable,
installHelmChartInjectable,
listHelmReleasesInjectable,
rollbackHelmReleaseInjectable,
writeJsonFileInjectable,
readJsonFileInjectable,

View File

@ -6,45 +6,13 @@
import tempy from "tempy";
import fse from "fs-extra";
import * as yaml from "js-yaml";
import { toCamelCase } from "../../common/utils/camelCase";
import type { JsonValue } from "type-fest";
import { isObject, 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 execHelmInjectable from "./exec-helm/exec-helm.injectable";
const execHelm = asLegacyGlobalFunctionForExtensionApi(execHelmInjectable);
export async function listReleases(pathToKubeconfig: string, namespace?: string): Promise<Record<string, any>[]> {
const args = [
"ls",
"--all",
"--output", "json",
];
if (namespace) {
args.push("-n", namespace);
} else {
args.push("--all-namespaces");
}
args.push("--kubeconfig", pathToKubeconfig);
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 [];
}
return output.filter(isObject).map(toCamelCase);
}
export async function installChart(chart: string, values: JsonValue, name: string | undefined = "", namespace: string, version: string, kubeconfigPath: string) {
const valuesFilePath = tempy.file({ name: "values.yaml" });

View File

@ -5,24 +5,23 @@
import { getInjectable } from "@ogre-tools/injectable";
import type { Cluster } from "../../../common/cluster/cluster";
import loggerInjectable from "../../../common/logger.injectable";
import { listReleases } from "../helm-release-manager";
import listHelmReleasesInjectable from "../list-helm-releases.injectable";
const listHelmReleasesInjectable = getInjectable({
id: "list-helm-releases",
const listClusterHelmReleasesInjectable = getInjectable({
id: "list-cluster-helm-releases",
instantiate: (di) => {
const logger = di.inject(loggerInjectable);
const listHelmReleases = di.inject(listHelmReleasesInjectable);
return async (cluster: Cluster, namespace?: string) => {
const proxyKubeconfig = await cluster.getProxyKubeconfigPath();
logger.debug("list releases");
logger.debug(`[CLUSTER]: listing helm releases for clusterId=${cluster.id}`, { namespace });
return listReleases(proxyKubeconfig, namespace);
return listHelmReleases(proxyKubeconfig, namespace);
};
},
causesSideEffects: true,
});
export default listHelmReleasesInjectable;
export default listClusterHelmReleasesInjectable;

View File

@ -0,0 +1,48 @@
/**
* 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 execHelmInjectable from "./exec-helm/exec-helm.injectable";
import { isObject, json, toCamelCase } from "../../common/utils";
export type ListHelmReleases = (pathToKubeconfig: string, namespace?: string) => Promise<Record<string, any>[]>;
const listHelmReleasesInjectable = getInjectable({
id: "list-helm-releases",
instantiate: (di): ListHelmReleases => {
const execHelm = di.inject(execHelmInjectable);
return async (pathToKubeconfig, namespace) => {
const args = [
"ls",
"--all",
"--output", "json",
];
if (namespace) {
args.push("-n", namespace);
} else {
args.push("--all-namespaces");
}
args.push("--kubeconfig", pathToKubeconfig);
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 [];
}
return output.filter(isObject).map(toCamelCase);
};
},
});
export default listHelmReleasesInjectable;

View File

@ -5,13 +5,13 @@
import { apiPrefix } from "../../../../common/vars";
import { getRouteInjectable } from "../../../router/router.injectable";
import { clusterRoute } from "../../../router/route";
import listHelmReleasesInjectable from "../../../helm/helm-service/list-helm-releases.injectable";
import listClusterHelmReleasesInjectable from "../../../helm/helm-service/list-helm-releases.injectable";
const listReleasesRouteInjectable = getRouteInjectable({
id: "list-releases-route",
instantiate: (di) => {
const listHelmReleases = di.inject(listHelmReleasesInjectable);
const listHelmReleases = di.inject(listClusterHelmReleasesInjectable);
return clusterRoute({
method: "get",