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.
* 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;
};
}

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(
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",
}],
}),
);

View File

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

View File

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

View File

@ -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<HelmRepo, "cacheFilePath">;
constructor(repo: HelmRepo, private dependencies: Dependencies) {
assert(repo.cacheFilePath, "CacheFilePath must be provided on the helm repo");
this.repo = repo as SetRequired<HelmRepo, "cacheFilePath">;
}
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();