Skip to content

Commit

Permalink
feat(helm): Add more options
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Jun 2, 2024
1 parent aa4e625 commit ba07165
Show file tree
Hide file tree
Showing 11 changed files with 683 additions and 66 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-books-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kosko/helm": minor
---

Add options: `insecureSkipTlsVerify`, `passCredentials`, `isUpgrade`, `kubeVersion`, `postRenderer`, `postRendererArgs`.
2 changes: 2 additions & 0 deletions packages/helm/src/__fixtures__/crd/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: crd
version: 0.0.0
308 changes: 308 additions & 0 deletions packages/helm/src/__fixtures__/crd/crds/traefik.io_ingressroutes.yaml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions packages/helm/src/__fixtures__/kube-version/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: kube-version
version: 0.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: Pod
metadata:
name: foo
annotations:
kube-version: "{{ .Capabilities.KubeVersion }}"
2 changes: 2 additions & 0 deletions packages/helm/src/__fixtures__/tests/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: test
version: 0.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Pod
metadata:
name: test-connection
annotations:
"helm.sh/hook": test
spec:
containers:
- name: wget
image: busybox
command: ["wget"]
restartPolicy: Never
2 changes: 2 additions & 0 deletions packages/helm/src/__fixtures__/upgrade/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: upgrade
version: 0.0.0
7 changes: 7 additions & 0 deletions packages/helm/src/__fixtures__/upgrade/templates/pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Pod
metadata:
name: foo
annotations:
is-install: "{{ .Release.IsInstall }}"
is-upgrade: "{{ .Release.IsUpgrade }}"
227 changes: 211 additions & 16 deletions packages/helm/src/__tests__/load.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ChartOptions, loadChart } from "../load";
/// <reference types="jest-extended" />
import { loadChart } from "../load";
import { join } from "node:path";
import { Manifest } from "@kosko/yaml";
import { spawn } from "@kosko/exec-utils";
import tmp from "tmp-promise";
import { readdir } from "node:fs/promises";
import { Pod } from "kubernetes-models/v1/Pod";

jest.mock("@kosko/exec-utils", () => {
const actual = jest.requireActual("@kosko/exec-utils");
Expand Down Expand Up @@ -70,29 +73,140 @@ test("values are specified", async () => {
});

describe("includeCrds option", () => {
const baseOptions: ChartOptions = {
chart: "traefik",
repo: "https://helm.traefik.io/traefik",
version: "v10.6.1"
};
test("should not include CRDs when includeCrds is not set", async () => {
const result = loadChart({
chart: join(FIXTURE_DIR, "crd")
});

function pickCrds(manifests: readonly Manifest[]): Manifest[] {
return manifests.filter((m) => m.kind === "CustomResourceDefinition");
}
await expect(result()).resolves.toBeEmpty();
});

test("should not include CRDs when includeCrds is not set", async () => {
const result = loadChart(baseOptions);
test("should not include CRDs when includeCrds is false", async () => {
const result = loadChart({
chart: join(FIXTURE_DIR, "crd"),
includeCrds: false
});

expect(pickCrds(await result())).toHaveLength(0);
await expect(result()).resolves.toBeEmpty();
});

test("should include CRDs when includeCrds is set", async () => {
test("should include CRDs when includeCrds is true", async () => {
const result = loadChart({
...baseOptions,
chart: join(FIXTURE_DIR, "crd"),
includeCrds: true
});

expect(pickCrds(await result())).not.toHaveLength(0);
await expect(result()).resolves.not.toBeEmpty();
});
});

describe("skipTests option", () => {
test("should include tests by default", async () => {
const result = loadChart({
chart: join(FIXTURE_DIR, "tests")
});

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

test("should include tests when skipTests is false", async () => {
const result = loadChart({
chart: join(FIXTURE_DIR, "tests"),
skipTests: false
});

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

test("should exclude tests when skipTests is true", async () => {
const result = loadChart({
chart: join(FIXTURE_DIR, "tests"),
skipTests: true
});

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

describe("isUpgrade option", () => {
test("should not set is-upgrade by default", async () => {
const result = loadChart({
chart: join(FIXTURE_DIR, "upgrade")
});
const manifests = await result();

expect(manifests).toEqual([
new Pod({
metadata: {
name: "foo",
annotations: {
"is-upgrade": "false",
"is-install": "true"
}
}
})
]);
});

test("should not set is-upgrade if isUpgrade is false", async () => {
const result = loadChart({
chart: join(FIXTURE_DIR, "upgrade"),
isUpgrade: false
});
const manifests = await result();

expect(manifests).toEqual([
new Pod({
metadata: {
name: "foo",
annotations: {
"is-upgrade": "false",
"is-install": "true"
}
}
})
]);
});

test("should set is-upgrade if isUpgrade is true", async () => {
const result = loadChart({
chart: join(FIXTURE_DIR, "upgrade"),
isUpgrade: true
});
const manifests = await result();

expect(manifests).toEqual([
new Pod({
metadata: {
name: "foo",
annotations: {
"is-upgrade": "true",
"is-install": "false"
}
}
})
]);
});
});

describe("kubeVersion is specified", () => {
test("should set kube-version when kubeVersion is specified", async () => {
const result = loadChart({
chart: join(FIXTURE_DIR, "kube-version"),
kubeVersion: "1.22.0"
});
const manifests = await result();

expect(manifests).toEqual([
new Pod({
metadata: {
name: "foo",
annotations: {
"kube-version": "v1.22.0"
}
}
})
]);
});
});

Expand All @@ -117,3 +231,84 @@ test("OCI chart", async () => {

await expect(result()).resolves.toMatchSnapshot();
});

describe("when cache is disabled", () => {
let tmpDir: tmp.DirectoryResult;

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

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

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

await expect(result()).toResolve();

// Cache directory should be empty
await expect(readdir(tmpDir.path)).resolves.toBeEmpty();
});
});

describe("when cache directory is specified", () => {
let tmpDir: tmp.DirectoryResult;

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

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

test("should store cache in the specified directory", 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()).toResolve();

// Cache directory should not be empty
await expect(readdir(tmpDir.path)).resolves.not.toBeEmpty();
});
});

describe("when KOSKO_HELM_CACHE_DIR is set", () => {
let tmpDir: tmp.DirectoryResult;
let origEnv: string | undefined;

beforeEach(async () => {
tmpDir = await tmp.dir({ unsafeCleanup: true });
origEnv = process.env.KOSKO_HELM_CACHE_DIR;
process.env.KOSKO_HELM_CACHE_DIR = tmpDir.path;
});

afterEach(async () => {
await tmpDir.cleanup();
process.env.KOSKO_HELM_CACHE_DIR = origEnv;
});

test("should store cache in the specified directory", async () => {
const result = loadChart({
chart: "prometheus",
repo: "https://prometheus-community.github.io/helm-charts",
version: "13.6.0"
});

await expect(result()).toResolve();

// Cache directory should not be empty
await expect(readdir(tmpDir.path)).resolves.not.toBeEmpty();
});
});
Loading

0 comments on commit ba07165

Please sign in to comment.