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

Make getHelmReleaseHistory injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-12-22 13:08:58 -05:00
parent 52c93f3a29
commit 23a7e79b35
5 changed files with 52 additions and 38 deletions

View File

@ -57,7 +57,6 @@ import normalizedPlatformArchitectureInjectable from "../common/vars/normalized-
import getHelmChartVersionsInjectable from "./helm/helm-service/get-helm-chart-versions.injectable"; import getHelmChartVersionsInjectable from "./helm/helm-service/get-helm-chart-versions.injectable";
import getHelmChartValuesInjectable from "./helm/helm-service/get-helm-chart-values.injectable"; import getHelmChartValuesInjectable from "./helm/helm-service/get-helm-chart-values.injectable";
import listHelmChartsInjectable from "./helm/helm-service/list-helm-charts.injectable"; import listHelmChartsInjectable from "./helm/helm-service/list-helm-charts.injectable";
import getHelmReleaseHistoryInjectable from "./helm/helm-service/get-helm-release-history.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";
@ -135,7 +134,6 @@ export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {})
getHelmChartVersionsInjectable, getHelmChartVersionsInjectable,
getHelmChartValuesInjectable, getHelmChartValuesInjectable,
listHelmChartsInjectable, listHelmChartsInjectable,
getHelmReleaseHistoryInjectable,
rollbackHelmReleaseInjectable, rollbackHelmReleaseInjectable,
writeJsonFileInjectable, writeJsonFileInjectable,
readJsonFileInjectable, readJsonFileInjectable,

View File

@ -0,0 +1,39 @@
/**
* 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 type { HelmReleaseRevision } from "../../common/k8s-api/endpoints/helm-releases.api/request-history.injectable";
import execHelmInjectable from "./exec-helm/exec-helm.injectable";
export interface GetHelmReleaseHistoryData {
name: string;
namespace: string;
}
export type GetHelmReleaseHistory = (kubeconfigPath: string, data: GetHelmReleaseHistoryData) => Promise<HelmReleaseRevision[]>;
const getHelmReleaseHistoryInjectable = getInjectable({
id: "get-helm-release-history",
instantiate: (di): GetHelmReleaseHistory => {
const execHelm = di.inject(execHelmInjectable);
return async (kubeconfigPath, { name, namespace }) => {
const result = await execHelm([
"history",
name,
"--output", "json",
"--namespace", namespace,
"--kubeconfig", kubeconfigPath,
]);
if (result.callWasSuccessful) {
return JSON.parse(result.response);
}
throw result.error;
};
},
});
export default getHelmReleaseHistoryInjectable;

View File

@ -3,30 +3,11 @@
* 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 type { JsonValue } from "type-fest";
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";
import execHelmInjectable from "./exec-helm/exec-helm.injectable"; import execHelmInjectable from "./exec-helm/exec-helm.injectable";
const execHelm = asLegacyGlobalFunctionForExtensionApi(execHelmInjectable); const execHelm = asLegacyGlobalFunctionForExtensionApi(execHelmInjectable);
export async function getHistory(name: string, namespace: string, kubeconfigPath: string): Promise<JsonValue> {
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): Promise<void> { export async function rollback(name: string, namespace: string, revision: number, kubeconfigPath: string): Promise<void> {
const result = await execHelm([ const result = await execHelm([
"rollback", "rollback",

View File

@ -4,25 +4,25 @@
*/ */
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import type { Cluster } from "../../../common/cluster/cluster"; import type { Cluster } from "../../../common/cluster/cluster";
import { getHistory } from "../helm-release-manager";
import loggerInjectable from "../../../common/logger.injectable"; import loggerInjectable from "../../../common/logger.injectable";
import type { GetHelmReleaseHistoryData } from "../get-helm-release-history.injectable";
import getHelmReleaseHistoryInjectable from "../get-helm-release-history.injectable";
const getHelmReleaseHistoryInjectable = getInjectable({ const getClusterHelmReleaseHistoryInjectable = getInjectable({
id: "get-helm-release-history", id: "get-cluster-helm-release-history",
instantiate: (di) => { instantiate: (di) => {
const logger = di.inject(loggerInjectable); const logger = di.inject(loggerInjectable);
const getHelmReleaseHistory = di.inject(getHelmReleaseHistoryInjectable);
return async (cluster: Cluster, releaseName: string, namespace: string) => { return async (cluster: Cluster, data: GetHelmReleaseHistoryData) => {
const proxyKubeconfig = await cluster.getProxyKubeconfigPath(); const proxyKubeconfig = await cluster.getProxyKubeconfigPath();
logger.debug("Fetch release history"); logger.debug(`[CLUSTER]: Fetch release history for clusterId=${cluster.id}`, data);
return getHistory(releaseName, namespace, proxyKubeconfig); return getHelmReleaseHistory(proxyKubeconfig, data);
}; };
}, },
causesSideEffects: true,
}); });
export default getHelmReleaseHistoryInjectable; export default getClusterHelmReleaseHistoryInjectable;

View File

@ -5,23 +5,19 @@
import { apiPrefix } from "../../../../common/vars"; import { apiPrefix } from "../../../../common/vars";
import { getRouteInjectable } from "../../../router/router.injectable"; import { getRouteInjectable } from "../../../router/router.injectable";
import { clusterRoute } from "../../../router/route"; import { clusterRoute } from "../../../router/route";
import getHelmReleaseHistoryInjectable from "../../../helm/helm-service/get-helm-release-history.injectable"; import getClusterHelmReleaseHistoryInjectable from "../../../helm/helm-service/get-helm-release-history.injectable";
const getReleaseRouteHistoryInjectable = getRouteInjectable({ const getReleaseRouteHistoryInjectable = getRouteInjectable({
id: "get-release-history-route", id: "get-release-history-route",
instantiate: (di) => { instantiate: (di) => {
const getHelmReleaseHistory = di.inject(getHelmReleaseHistoryInjectable); const getHelmReleaseHistory = di.inject(getClusterHelmReleaseHistoryInjectable);
return clusterRoute({ return clusterRoute({
method: "get", method: "get",
path: `${apiPrefix}/v2/releases/{namespace}/{release}/history`, path: `${apiPrefix}/v2/releases/{namespace}/{name}/history`,
})(async ({ cluster, params }) => ({ })(async ({ cluster, params }) => ({
response: await getHelmReleaseHistory( response: await getHelmReleaseHistory(cluster, params),
cluster,
params.release,
params.namespace,
),
})); }));
}, },
}); });