diff --git a/src/common/helm/helm-repo.ts b/src/common/helm/helm-repo.ts index 9405d59bed..cb4af3449b 100644 --- a/src/common/helm/helm-repo.ts +++ b/src/common/helm/helm-repo.ts @@ -2,15 +2,15 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -export type HelmRepo = { + +export interface HelmRepo { name: string; url: string; - cacheFilePath?: string; + cacheFilePath: string; caFile?: string; certFile?: string; insecureSkipTlsVerify?: boolean; keyFile?: string; username?: string; password?: string; -}; +} diff --git a/src/common/user-store/https-proxy.injectable.ts b/src/common/user-store/https-proxy.injectable.ts new file mode 100644 index 0000000000..30569d4e77 --- /dev/null +++ b/src/common/user-store/https-proxy.injectable.ts @@ -0,0 +1,18 @@ +/** + * 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 { computed } from "mobx"; +import userStoreInjectable from "./user-store.injectable"; + +const httpsProxyConfigurationInjectable = getInjectable({ + id: "https-proxy-configuration", + instantiate: (di) => { + const userStore = di.inject(userStoreInjectable); + + return computed(() => userStore.httpsProxy); + }, +}); + +export default httpsProxyConfigurationInjectable; diff --git a/src/main/helm/__tests__/helm-service.test.ts b/src/main/helm/__tests__/helm-service.test.ts index 982a361f30..78d00b939c 100644 --- a/src/main/helm/__tests__/helm-service.test.ts +++ b/src/main/helm/__tests__/helm-service.test.ts @@ -41,10 +41,17 @@ describe("Helm Service tests", () => { getActiveHelmRepositoriesMock.mockReturnValue( Promise.resolve({ callWasSuccessful: true, - response: [ - { name: "stable", url: "stableurl" }, - { name: "experiment", url: "experimenturl" }, + { + name: "stable", + url: "stableurl", + cacheFilePath: "/some-cache-file-path-for-stable", + }, + { + name: "experiment", + url: "experimenturl", + cacheFilePath: "/some-cache-file-path-for-experiment", + }, ], }), ); @@ -152,7 +159,11 @@ describe("Helm Service tests", () => { getActiveHelmRepositoriesMock.mockReturnValue( Promise.resolve({ callWasSuccessful: true, - response: [{ name: "bitnami", url: "bitnamiurl" }], + response: [{ + name: "bitnami", + url: "bitnamiurl", + cacheFilePath: "/some-cache-file-path-for-bitnami", + }], }), ); diff --git a/src/main/helm/exec-helm/exec-env.injectable.ts b/src/main/helm/exec-helm/exec-env.injectable.ts index f562dc2d4b..3a72d0c17c 100644 --- a/src/main/helm/exec-helm/exec-env.injectable.ts +++ b/src/main/helm/exec-helm/exec-env.injectable.ts @@ -4,16 +4,16 @@ */ import { getInjectable } from "@ogre-tools/injectable"; import { computed } from "mobx"; -import userStoreInjectable from "../../../common/user-store/user-store.injectable"; +import httpsProxyConfigurationInjectable from "../../../common/user-store/https-proxy.injectable"; const execHelmEnvInjectable = getInjectable({ id: "exec-helm-env", instantiate: (di) => { - const userStore = di.inject(userStoreInjectable); + const httpsProxyConfiguration = di.inject(httpsProxyConfigurationInjectable); return computed(() => { const { - HTTPS_PROXY = userStore.httpsProxy, + HTTPS_PROXY = httpsProxyConfiguration.get(), ...env } = process.env; diff --git a/src/main/helm/helm-chart-manager.injectable.ts b/src/main/helm/helm-chart-manager.injectable.ts index 367c47ae3b..3bcfa446a4 100644 --- a/src/main/helm/helm-chart-manager.injectable.ts +++ b/src/main/helm/helm-chart-manager.injectable.ts @@ -7,16 +7,20 @@ import type { HelmRepo } from "../../common/helm/helm-repo"; import { HelmChartManager } from "./helm-chart-manager"; import helmChartManagerCacheInjectable from "./helm-chart-manager-cache.injectable"; import loggerInjectable from "../../common/logger.injectable"; +import execHelmInjectable from "./exec-helm/exec-helm.injectable"; +import readFileInjectable from "../../common/fs/read-file.injectable"; +import statInjectable from "../../common/fs/stat/stat.injectable"; const helmChartManagerInjectable = getInjectable({ id: "helm-chart-manager", - instantiate: (di, repo: HelmRepo) => { - const cache = di.inject(helmChartManagerCacheInjectable); - const logger = di.inject(loggerInjectable); - - return new HelmChartManager(repo, { cache, logger }); - }, + instantiate: (di, repo: HelmRepo) => new HelmChartManager({ + cache: di.inject(helmChartManagerCacheInjectable), + logger: di.inject(loggerInjectable), + execHelm: di.inject(execHelmInjectable), + readFile: di.inject(readFileInjectable), + stat: di.inject(statInjectable), + }, repo), lifecycle: lifecycleEnum.keyedSingleton({ getInstanceKey: (di, repo: HelmRepo) => repo.name, diff --git a/src/main/helm/helm-chart-manager.ts b/src/main/helm/helm-chart-manager.ts index dfc80a78e4..03b5d09dd8 100644 --- a/src/main/helm/helm-chart-manager.ts +++ b/src/main/helm/helm-chart-manager.ts @@ -3,16 +3,15 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ -import fs from "fs"; import * as yaml from "js-yaml"; import { iter, put, sortBySemverVersion } from "../../common/utils"; -import { execHelm } from "./exec"; -import type { SetRequired } from "type-fest"; -import { assert } from "console"; import type { HelmRepo } from "../../common/helm/helm-repo"; import type { HelmChartManagerCache } from "./helm-chart-manager-cache.injectable"; import type { Logger } from "../../common/logger"; import type { RepoHelmChartList } from "../../common/k8s-api/endpoints/helm-charts.api/request-charts.injectable"; +import type { ExecHelm } from "./exec-helm/exec-helm.injectable"; +import type { ReadFile } from "../../common/fs/read-file.injectable"; +import type { Stat } from "../../common/fs/stat/stat.injectable"; export interface HelmCacheFile { apiVersion: string; @@ -20,18 +19,18 @@ export interface HelmCacheFile { } interface Dependencies { - cache: HelmChartManagerCache; - logger: Logger; + readonly cache: HelmChartManagerCache; + readonly logger: Logger; + execHelm: ExecHelm; + readFile: ReadFile; + stat: Stat; } export class HelmChartManager { - protected readonly repo: SetRequired; - - constructor(repo: HelmRepo, private dependencies: Dependencies) { - assert(repo.cacheFilePath, "CacheFilePath must be provided on the helm repo"); - - this.repo = repo as SetRequired; - } + constructor( + private readonly dependencies: Dependencies, + protected readonly repo: HelmRepo, + ) {} public async chartVersions(name: string) { const charts = await this.charts(); @@ -56,7 +55,7 @@ export class HelmChartManager { args.push("--version", version); } - return execHelm(args); + return this.dependencies.execHelm(args); } public async getReadme(name: string, version?: string) { @@ -68,8 +67,8 @@ export class HelmChartManager { } protected async updateYamlCache() { - const cacheFile = await fs.promises.readFile(this.repo.cacheFilePath, "utf-8"); - const cacheFileStats = await fs.promises.stat(this.repo.cacheFilePath); + const cacheFile = await this.dependencies.readFile(this.repo.cacheFilePath); + const cacheFileStats = await this.dependencies.stat(this.repo.cacheFilePath); const data = yaml.load(cacheFile) as string | number | HelmCacheFile; if (!data || typeof data !== "object" || typeof data.entries !== "object") { @@ -94,7 +93,7 @@ export class HelmChartManager { if (!cacheEntry) { cacheEntry = await this.updateYamlCache(); } else { - const newStats = await fs.promises.stat(this.repo.cacheFilePath); + const newStats = await this.dependencies.stat(this.repo.cacheFilePath); if (cacheEntry.mtimeMs < newStats.mtimeMs) { cacheEntry = await this.updateYamlCache();