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

Replace use of legacy global execHelm with injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-11-30 14:33:52 -05:00
parent 9fee31a872
commit 4c1e1ac1db
6 changed files with 66 additions and 34 deletions

View File

@ -2,15 +2,15 @@
* Copyright (c) OpenLens Authors. All rights reserved. * Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information. * 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; name: string;
url: string; url: string;
cacheFilePath?: string; cacheFilePath: string;
caFile?: string; caFile?: string;
certFile?: string; certFile?: string;
insecureSkipTlsVerify?: boolean; insecureSkipTlsVerify?: boolean;
keyFile?: string; keyFile?: string;
username?: string; username?: string;
password?: string; password?: string;
}; }

View File

@ -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;

View File

@ -41,10 +41,17 @@ describe("Helm Service tests", () => {
getActiveHelmRepositoriesMock.mockReturnValue( getActiveHelmRepositoriesMock.mockReturnValue(
Promise.resolve({ Promise.resolve({
callWasSuccessful: true, callWasSuccessful: true,
response: [ 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( getActiveHelmRepositoriesMock.mockReturnValue(
Promise.resolve({ Promise.resolve({
callWasSuccessful: true, callWasSuccessful: true,
response: [{ name: "bitnami", url: "bitnamiurl" }], response: [{
name: "bitnami",
url: "bitnamiurl",
cacheFilePath: "/some-cache-file-path-for-bitnami",
}],
}), }),
); );

View File

@ -4,16 +4,16 @@
*/ */
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import { computed } from "mobx"; 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({ const execHelmEnvInjectable = getInjectable({
id: "exec-helm-env", id: "exec-helm-env",
instantiate: (di) => { instantiate: (di) => {
const userStore = di.inject(userStoreInjectable); const httpsProxyConfiguration = di.inject(httpsProxyConfigurationInjectable);
return computed(() => { return computed(() => {
const { const {
HTTPS_PROXY = userStore.httpsProxy, HTTPS_PROXY = httpsProxyConfiguration.get(),
...env ...env
} = process.env; } = process.env;

View File

@ -7,16 +7,20 @@ import type { HelmRepo } from "../../common/helm/helm-repo";
import { HelmChartManager } from "./helm-chart-manager"; import { HelmChartManager } from "./helm-chart-manager";
import helmChartManagerCacheInjectable from "./helm-chart-manager-cache.injectable"; import helmChartManagerCacheInjectable from "./helm-chart-manager-cache.injectable";
import loggerInjectable from "../../common/logger.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({ const helmChartManagerInjectable = getInjectable({
id: "helm-chart-manager", id: "helm-chart-manager",
instantiate: (di, repo: HelmRepo) => { instantiate: (di, repo: HelmRepo) => new HelmChartManager({
const cache = di.inject(helmChartManagerCacheInjectable); cache: di.inject(helmChartManagerCacheInjectable),
const logger = di.inject(loggerInjectable); logger: di.inject(loggerInjectable),
execHelm: di.inject(execHelmInjectable),
return new HelmChartManager(repo, { cache, logger }); readFile: di.inject(readFileInjectable),
}, stat: di.inject(statInjectable),
}, repo),
lifecycle: lifecycleEnum.keyedSingleton({ lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di, repo: HelmRepo) => repo.name, getInstanceKey: (di, repo: HelmRepo) => repo.name,

View File

@ -3,16 +3,15 @@
* 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 fs from "fs";
import * as yaml from "js-yaml"; import * as yaml from "js-yaml";
import { iter, put, sortBySemverVersion } from "../../common/utils"; 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 { HelmRepo } from "../../common/helm/helm-repo";
import type { HelmChartManagerCache } from "./helm-chart-manager-cache.injectable"; import type { HelmChartManagerCache } from "./helm-chart-manager-cache.injectable";
import type { Logger } from "../../common/logger"; import type { Logger } from "../../common/logger";
import type { RepoHelmChartList } from "../../common/k8s-api/endpoints/helm-charts.api/request-charts.injectable"; 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 { export interface HelmCacheFile {
apiVersion: string; apiVersion: string;
@ -20,18 +19,18 @@ export interface HelmCacheFile {
} }
interface Dependencies { interface Dependencies {
cache: HelmChartManagerCache; readonly cache: HelmChartManagerCache;
logger: Logger; readonly logger: Logger;
execHelm: ExecHelm;
readFile: ReadFile;
stat: Stat;
} }
export class HelmChartManager { export class HelmChartManager {
protected readonly repo: SetRequired<HelmRepo, "cacheFilePath">; constructor(
private readonly dependencies: Dependencies,
constructor(repo: HelmRepo, private dependencies: Dependencies) { protected readonly repo: HelmRepo,
assert(repo.cacheFilePath, "CacheFilePath must be provided on the helm repo"); ) {}
this.repo = repo as SetRequired<HelmRepo, "cacheFilePath">;
}
public async chartVersions(name: string) { public async chartVersions(name: string) {
const charts = await this.charts(); const charts = await this.charts();
@ -56,7 +55,7 @@ export class HelmChartManager {
args.push("--version", version); args.push("--version", version);
} }
return execHelm(args); return this.dependencies.execHelm(args);
} }
public async getReadme(name: string, version?: string) { public async getReadme(name: string, version?: string) {
@ -68,8 +67,8 @@ export class HelmChartManager {
} }
protected async updateYamlCache() { protected async updateYamlCache() {
const cacheFile = await fs.promises.readFile(this.repo.cacheFilePath, "utf-8"); const cacheFile = await this.dependencies.readFile(this.repo.cacheFilePath);
const cacheFileStats = await fs.promises.stat(this.repo.cacheFilePath); const cacheFileStats = await this.dependencies.stat(this.repo.cacheFilePath);
const data = yaml.load(cacheFile) as string | number | HelmCacheFile; const data = yaml.load(cacheFile) as string | number | HelmCacheFile;
if (!data || typeof data !== "object" || typeof data.entries !== "object") { if (!data || typeof data !== "object" || typeof data.entries !== "object") {
@ -94,7 +93,7 @@ export class HelmChartManager {
if (!cacheEntry) { if (!cacheEntry) {
cacheEntry = await this.updateYamlCache(); cacheEntry = await this.updateYamlCache();
} else { } else {
const newStats = await fs.promises.stat(this.repo.cacheFilePath); const newStats = await this.dependencies.stat(this.repo.cacheFilePath);
if (cacheEntry.mtimeMs < newStats.mtimeMs) { if (cacheEntry.mtimeMs < newStats.mtimeMs) {
cacheEntry = await this.updateYamlCache(); cacheEntry = await this.updateYamlCache();