From 23a7e79b35e2d044ba6ce7c97b9e0e279873cdba Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 22 Dec 2022 13:08:58 -0500 Subject: [PATCH] Make getHelmReleaseHistory injectable Signed-off-by: Sebastian Malton --- src/main/getDiForUnitTesting.ts | 2 - .../get-helm-release-history.injectable.ts | 39 +++++++++++++++++++ src/main/helm/helm-release-manager.ts | 19 --------- .../get-helm-release-history.injectable.ts | 18 ++++----- .../get-release-history-route.injectable.ts | 12 ++---- 5 files changed, 52 insertions(+), 38 deletions(-) create mode 100644 src/main/helm/get-helm-release-history.injectable.ts diff --git a/src/main/getDiForUnitTesting.ts b/src/main/getDiForUnitTesting.ts index e07e2d3aea..f5ef0b0505 100644 --- a/src/main/getDiForUnitTesting.ts +++ b/src/main/getDiForUnitTesting.ts @@ -57,7 +57,6 @@ import normalizedPlatformArchitectureInjectable from "../common/vars/normalized- import getHelmChartVersionsInjectable from "./helm/helm-service/get-helm-chart-versions.injectable"; import getHelmChartValuesInjectable from "./helm/helm-service/get-helm-chart-values.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 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"; @@ -135,7 +134,6 @@ export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {}) getHelmChartVersionsInjectable, getHelmChartValuesInjectable, listHelmChartsInjectable, - getHelmReleaseHistoryInjectable, rollbackHelmReleaseInjectable, writeJsonFileInjectable, readJsonFileInjectable, diff --git a/src/main/helm/get-helm-release-history.injectable.ts b/src/main/helm/get-helm-release-history.injectable.ts new file mode 100644 index 0000000000..5f8f4e5a6e --- /dev/null +++ b/src/main/helm/get-helm-release-history.injectable.ts @@ -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; + +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; diff --git a/src/main/helm/helm-release-manager.ts b/src/main/helm/helm-release-manager.ts index 2feb81691c..e0cac1c1e2 100644 --- a/src/main/helm/helm-release-manager.ts +++ b/src/main/helm/helm-release-manager.ts @@ -3,30 +3,11 @@ * 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 execHelmInjectable from "./exec-helm/exec-helm.injectable"; const execHelm = asLegacyGlobalFunctionForExtensionApi(execHelmInjectable); - -export async function getHistory(name: string, namespace: string, kubeconfigPath: string): Promise { - 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 { const result = await execHelm([ "rollback", diff --git a/src/main/helm/helm-service/get-helm-release-history.injectable.ts b/src/main/helm/helm-service/get-helm-release-history.injectable.ts index a642c2e265..fe4e3448ac 100644 --- a/src/main/helm/helm-service/get-helm-release-history.injectable.ts +++ b/src/main/helm/helm-service/get-helm-release-history.injectable.ts @@ -4,25 +4,25 @@ */ import { getInjectable } from "@ogre-tools/injectable"; import type { Cluster } from "../../../common/cluster/cluster"; -import { getHistory } from "../helm-release-manager"; 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({ - id: "get-helm-release-history", +const getClusterHelmReleaseHistoryInjectable = getInjectable({ + id: "get-cluster-helm-release-history", instantiate: (di) => { 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(); - 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; diff --git a/src/main/routes/helm/releases/get-release-history-route.injectable.ts b/src/main/routes/helm/releases/get-release-history-route.injectable.ts index 142adfc0d5..2ba1349de0 100644 --- a/src/main/routes/helm/releases/get-release-history-route.injectable.ts +++ b/src/main/routes/helm/releases/get-release-history-route.injectable.ts @@ -5,23 +5,19 @@ import { apiPrefix } from "../../../../common/vars"; import { getRouteInjectable } from "../../../router/router.injectable"; 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({ id: "get-release-history-route", instantiate: (di) => { - const getHelmReleaseHistory = di.inject(getHelmReleaseHistoryInjectable); + const getHelmReleaseHistory = di.inject(getClusterHelmReleaseHistoryInjectable); return clusterRoute({ method: "get", - path: `${apiPrefix}/v2/releases/{namespace}/{release}/history`, + path: `${apiPrefix}/v2/releases/{namespace}/{name}/history`, })(async ({ cluster, params }) => ({ - response: await getHelmReleaseHistory( - cluster, - params.release, - params.namespace, - ), + response: await getHelmReleaseHistory(cluster, params), })); }, });