mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Update helm cache if files have been modified (#3973)
This commit is contained in:
parent
46653386b9
commit
38276bbbff
@ -111,5 +111,5 @@ export function sortCharts(charts: RawHelmChart[]) {
|
|||||||
|
|
||||||
return chartsWithVersion
|
return chartsWithVersion
|
||||||
.sort(sortCompareChartVersions)
|
.sort(sortCompareChartVersions)
|
||||||
.map(chart => (delete chart.__version, chart));
|
.map(chart => (delete chart.__version, chart as RawHelmChart));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,10 +24,15 @@ import v8 from "v8";
|
|||||||
import * as yaml from "js-yaml";
|
import * as yaml from "js-yaml";
|
||||||
import type { HelmRepo } from "./helm-repo-manager";
|
import type { HelmRepo } from "./helm-repo-manager";
|
||||||
import logger from "../logger";
|
import logger from "../logger";
|
||||||
import { promiseExec } from "../promise-exec";
|
import { promiseExecFile } from "../promise-exec";
|
||||||
import { helmCli } from "./helm-cli";
|
import { helmCli } from "./helm-cli";
|
||||||
import type { RepoHelmChartList } from "../../common/k8s-api/endpoints/helm-charts.api";
|
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 {
|
export interface HelmCacheFile {
|
||||||
apiVersion: string;
|
apiVersion: string;
|
||||||
@ -35,7 +40,7 @@ export interface HelmCacheFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class HelmChartManager {
|
export class HelmChartManager {
|
||||||
static #cache = new Map<string, Buffer>();
|
static #cache = new Map<string, ChartCacheEntry>();
|
||||||
|
|
||||||
private constructor(protected repo: HelmRepo) {}
|
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 helm = await helmCli.binaryPath();
|
||||||
const cmd = [`"${helm}" ${action} ${this.repo.name}/${name}`];
|
|
||||||
|
args.push(`${this.repo.name}/${name}`);
|
||||||
|
|
||||||
if (version) {
|
if (version) {
|
||||||
cmd.push("--version", version);
|
args.push("--version", version);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { stdout } = await promiseExec(cmd.join(" "));
|
const { stdout } = await promiseExecFile(helm, args);
|
||||||
|
|
||||||
return stdout;
|
return stdout;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -77,47 +83,69 @@ export class HelmChartManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async getReadme(name: string, version?: string) {
|
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) {
|
public async getValues(name: string, version?: string) {
|
||||||
return this.executeCommand("show values", name, version);
|
return this.executeCommand(["show", "values"], name, version);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (!data || typeof data !== "object" || typeof data.entries !== "object") {
|
||||||
|
throw Object.assign(new TypeError("Helm Cache file does not parse correctly"), { file: this.repo.cacheFilePath, data });
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalized = normalizeHelmCharts(this.repo.name, data.entries);
|
||||||
|
|
||||||
|
HelmChartManager.#cache.set(this.repo.name, {
|
||||||
|
data: v8.serialize(normalized),
|
||||||
|
mtimeMs: cacheFileStats.mtimeMs,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async cachedYaml(): Promise<RepoHelmChartList> {
|
protected async cachedYaml(): Promise<RepoHelmChartList> {
|
||||||
if (!HelmChartManager.#cache.has(this.repo.name)) {
|
if (!HelmChartManager.#cache.has(this.repo.name)) {
|
||||||
const cacheFile = await fs.promises.readFile(this.repo.cacheFilePath, "utf-8");
|
await this.updateYamlCache();
|
||||||
const data = yaml.load(cacheFile) as string | number | HelmCacheFile;
|
} else {
|
||||||
|
const newStats = await fs.promises.stat(this.repo.cacheFilePath);
|
||||||
|
const cacheEntry = HelmChartManager.#cache.get(this.repo.name);
|
||||||
|
|
||||||
if (typeof data !== "object" || !data) {
|
if (cacheEntry.mtimeMs < newStats.mtimeMs) {
|
||||||
return {};
|
await this.updateYamlCache();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return v8.deserialize(HelmChartManager.#cache.get(this.repo.name).data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Do some initial preprocessing on the data, so as to avoid needing to do it later
|
* Do some initial preprocessing on the data, so as to avoid needing to do it later
|
||||||
* 1. Set the repo name
|
* 1. Set the repo name
|
||||||
* 2. Normalize the created date
|
* 2. Normalize the created date
|
||||||
* 3. Filter out deprecated items
|
* 3. Filter out charts that only have deprecated entries
|
||||||
*/
|
*/
|
||||||
|
function normalizeHelmCharts(repoName: string, entries: RepoHelmChartList): RepoHelmChartList {
|
||||||
const normalized = Object.fromEntries(
|
return Object.fromEntries(
|
||||||
Object.entries(data.entries)
|
iter.filter(
|
||||||
.map(([name, charts]) => [
|
iter.map(
|
||||||
|
Object.entries(entries),
|
||||||
|
([name, charts]) => [
|
||||||
name,
|
name,
|
||||||
sortCharts(
|
sortCharts(
|
||||||
charts.map(chart => ({
|
charts.map(chart => ({
|
||||||
...chart,
|
...chart,
|
||||||
created: Date.parse(chart.created).toString(),
|
created: Date.parse(chart.created).toString(),
|
||||||
repo: this.repo.name,
|
repo: repoName,
|
||||||
})),
|
})),
|
||||||
),
|
),
|
||||||
] as const)
|
] as const
|
||||||
.filter(([, charts]) => !charts.every(chart => chart.deprecated))
|
),
|
||||||
|
([, charts]) => !charts.every(chart => chart.deprecated),
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
HelmChartManager.#cache.set(this.repo.name, v8.serialize(normalized));
|
|
||||||
}
|
|
||||||
|
|
||||||
return v8.deserialize(HelmChartManager.#cache.get(this.repo.name));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user