diff --git a/src/main/helm/helm-chart-manager.ts b/src/main/helm/helm-chart-manager.ts index e99554dcd0..2a3547e145 100644 --- a/src/main/helm/helm-chart-manager.ts +++ b/src/main/helm/helm-chart-manager.ts @@ -24,10 +24,15 @@ import v8 from "v8"; import * as yaml from "js-yaml"; import type { HelmRepo } from "./helm-repo-manager"; import logger from "../logger"; -import { promiseExec } from "../promise-exec"; +import { promiseExecFile } from "../promise-exec"; import { helmCli } from "./helm-cli"; import type { RepoHelmChartList } from "../../common/k8s-api/endpoints/helm-charts.api"; -import { sortCharts } from "../../common/utils"; +import { iter, sortCharts } from "../../common/utils"; + +interface ChartCacheEntry { + data: Buffer, + mtimeMs: number, +} export interface HelmCacheFile { apiVersion: string; @@ -35,7 +40,7 @@ export interface HelmCacheFile { } export class HelmChartManager { - static #cache = new Map(); + static #cache = new Map(); private constructor(protected repo: HelmRepo) {} @@ -59,16 +64,17 @@ export class HelmChartManager { } } - private async executeCommand(action: string, name: string, version?: string) { + private async executeCommand(args: string[], name: string, version?: string) { const helm = await helmCli.binaryPath(); - const cmd = [`"${helm}" ${action} ${this.repo.name}/${name}`]; + + args.push(`${this.repo.name}/${name}`); if (version) { - cmd.push("--version", version); + args.push("--version", version); } try { - const { stdout } = await promiseExec(cmd.join(" ")); + const { stdout } = await promiseExecFile(helm, args); return stdout; } catch (error) { @@ -77,32 +83,36 @@ export class HelmChartManager { } public async getReadme(name: string, version?: string) { - return this.executeCommand("show readme", name, version); + return this.executeCommand(["show", "readme"], name, version); } public async getValues(name: string, version?: string) { - return this.executeCommand("show values", name, version); + return this.executeCommand(["show", "values"], name, version); } - protected async cachedYaml(): Promise { - if (!HelmChartManager.#cache.has(this.repo.name)) { - const cacheFile = await fs.promises.readFile(this.repo.cacheFilePath, "utf-8"); - const data = yaml.load(cacheFile) as string | number | HelmCacheFile; + protected async updateYamlCache() { + const cacheFile = await fs.promises.readFile(this.repo.cacheFilePath, "utf-8"); + const cacheFileStats = await fs.promises.stat(this.repo.cacheFilePath); + const data = yaml.load(cacheFile) as string | number | HelmCacheFile; - if (typeof data !== "object" || !data) { - return {}; - } + if (typeof data !== "object" || !data) { + return; + } - /** - * Do some initial preprocessing on the data, so as to avoid needing to do it later - * 1. Set the repo name - * 2. Normalize the created date - * 3. Filter out deprecated items - */ + const { entries } = data; - const normalized = Object.fromEntries( - Object.entries(data.entries) - .map(([name, charts]) => [ + /** + * Do some initial preprocessing on the data, so as to avoid needing to do it later + * 1. Set the repo name + * 2. Normalize the created date + * 3. Filter out deprecated items + */ + + const normalized = Object.fromEntries( + iter.filter( + iter.map( + Object.entries(entries), + ([name, charts]) => [ name, sortCharts( charts.map(chart => ({ @@ -111,13 +121,30 @@ export class HelmChartManager { repo: this.repo.name, })), ), - ] as const) - .filter(([, charts]) => !charts.every(chart => chart.deprecated)) - ); + ] as const + ), + ([, charts]) => !charts.every(chart => chart.deprecated), + ) + ); - HelmChartManager.#cache.set(this.repo.name, v8.serialize(normalized)); + HelmChartManager.#cache.set(this.repo.name, { + data: v8.serialize(normalized), + mtimeMs: cacheFileStats.mtimeMs, + }); + } + + protected async cachedYaml(): Promise { + if (!HelmChartManager.#cache.has(this.repo.name)) { + await this.updateYamlCache(); + } else { + const newStats = await fs.promises.stat(this.repo.cacheFilePath); + const cacheEntry = HelmChartManager.#cache.get(this.repo.name); + + if (cacheEntry.mtimeMs < newStats.mtimeMs) { + await this.updateYamlCache(); + } } - return v8.deserialize(HelmChartManager.#cache.get(this.repo.name)); + return v8.deserialize(HelmChartManager.#cache.get(this.repo.name).data); } }