Skip to content

Commit

Permalink
Merge pull request #146 from yz89122/fix/helm/cache-empty-index-file
Browse files Browse the repository at this point in the history
fix(helm): cache empty index file
  • Loading branch information
tommy351 authored Jun 21, 2024
2 parents b273bed + 451cf52 commit 637e4f2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-roses-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kosko/helm": patch
---

Fix loadHelm might read empty index file in case of race condition
37 changes: 36 additions & 1 deletion packages/helm/src/__tests__/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { loadChart } from "../load";
import { join } from "node:path";
import { spawn } from "@kosko/exec-utils";
import tmp from "tmp-promise";
import { readdir } from "node:fs/promises";
import { readdir, writeFile } from "node:fs/promises";
import { Pod } from "kubernetes-models/v1/Pod";
import { Manifest } from "@kosko/yaml";

Expand Down Expand Up @@ -484,3 +484,38 @@ describe("cache reuse", () => {
expect(getPullCalls()).toHaveLength(2);
});
});

describe("when index file is empty", () => {
let tmpDir: tmp.DirectoryResult;

beforeEach(async () => {
tmpDir = await tmp.dir({ unsafeCleanup: true });
});

afterEach(async () => {
await tmpDir.cleanup();
});

test("should not use cache", async () => {
const run = async () => {
const result = loadChart({
chart: "prometheus",
repo: "https://prometheus-community.github.io/helm-charts",
version: "13.6.0",
cache: { dir: tmpDir.path }
});

await expect(result()).resolves.not.toBeEmpty();
};

await run();

const filenames = await readdir(tmpDir.path);
const indexFilename = filenames.find((filename) =>
filename.startsWith("index")
);
await writeFile(join(tmpDir.path, indexFilename!), "");

await run();
});
});
5 changes: 4 additions & 1 deletion packages/helm/src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,10 @@ async function pullChart(options: PullOptions): Promise<string | undefined> {
// Read the index file to get the cache path
try {
const content = await readFile(indexPath, "utf8");
return join(cacheDir, content);
if (content) {
// The content read by current process might be empty due to race condition.
return join(cacheDir, content);
}
} catch (err) {
if (getErrorCode(err) !== "ENOENT") throw err;
}
Expand Down

0 comments on commit 637e4f2

Please sign in to comment.