Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(helm): cache tar file #145

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/weak-falcons-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kosko/helm": patch
---

Cache tar file instead of extracted
28 changes: 12 additions & 16 deletions packages/helm/src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import getCacheDir from "cachedir";
import { createHash } from "node:crypto";
import { dirname, join } from "node:path";
import { env } from "node:process";
import { readdir } from "node:fs/promises";
import yaml from "js-yaml";
import { fileExists, move } from "./fs";

Expand Down Expand Up @@ -266,17 +267,11 @@ async function isLocalChart(
return chartManifestExists(options.chart);
}

function getChartBaseName(chart: string): string {
const index = chart.lastIndexOf("/");

return index === -1 ? chart : chart.substring(index + 1);
}

async function getChartMetadata(
chart: string
): Promise<Record<string, unknown> | undefined> {
try {
const content = await readFile(getChartManifestPath(chart), "utf8");
const { stdout: content } = await runHelm(["show", "chart", chart]);
const metadata = yaml.load(content);

if (isRecord(metadata)) return metadata;
Expand Down Expand Up @@ -305,7 +300,7 @@ function getPullArgs(options: PullOptions): string[] {

async function moveChartToCacheDir(src: string, dest: string): Promise<void> {
// Skip if the chart already exists in the cache directory
if (await chartManifestExists(dest)) return;
if (await fileExists(dest)) return;

await mkdir(dirname(dest), { recursive: true });

Expand All @@ -323,11 +318,10 @@ async function moveChartToCacheDir(src: string, dest: string): Promise<void> {
return;
}

// Windows throws EPERM error when the target already exists. In this case,
// we will try to check if the `Chart.yaml` exists in the target directory.
// Windows throws EPERM error when the target already exists.
//
// https://github.com/nodejs/node/issues/29481
if (code === "EPERM" && (await chartManifestExists(dest))) {
if (code === "EPERM") {
return;
}

Expand Down Expand Up @@ -373,15 +367,17 @@ async function pullChart(options: PullOptions): Promise<string | undefined> {
await runHelm([
"pull",
...getPullArgs(options),
"--untar",
"--untardir",
"--destination",
tmpDir.path
]);

const chartDir = join(tmpDir.path, getChartBaseName(options.chart));
// Get the chart path
const files = await readdir(tmpDir.path);
if (files.length === 0) return;
const chartPath = join(tmpDir.path, files[0]);

// Get chart version
const chartMeta = await getChartMetadata(chartDir);
const chartMeta = await getChartMetadata(chartPath);
const chartVersion = chartMeta?.version;

if (typeof chartVersion !== "string") return;
Expand All @@ -394,7 +390,7 @@ async function pullChart(options: PullOptions): Promise<string | undefined> {
const dest = join(cacheDir, chartHash);

// Move the chart to the cache directory
await moveChartToCacheDir(chartDir, dest);
await moveChartToCacheDir(chartPath, dest);

// Write index file
await writeFile(indexPath, chartHash);
Expand Down
Loading