Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/test-all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: |
helm plugin install https://github.com/jkroepke/helm-secrets --version v3.4.1

- name: Install pugin
- name: Install plugin
run: make install-plugin

- name: Start local kubernetes cluster
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
tar xvf helm.tar.gz linux-amd64/helm
echo "$PWD/linux-amd64" >> $GITHUB_PATH

- name: Install pugin
- name: Install plugin
run: make install-plugin

- name: Run tests
Expand Down
5 changes: 4 additions & 1 deletion src/args/__tests__/parse-args.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertEquals } from "https://deno.land/std@0.86.0/testing/asserts.ts"
import { parseArgs } from "../parse-helm-deno-args.ts"

Deno.test("Should parse --deno-log-level flag", () => {
Deno.test("Should parse --deno-* flags", () => {
const { options, helmArgs } = parseArgs([
"upgrade",
"--install",
Expand All @@ -12,10 +12,13 @@ Deno.test("Should parse --deno-log-level flag", () => {
"--deno-log-level",
"debug",
"--deno-keep-tmp-chart",
"--deno-import-map",
"test/import_map.json",
])

assertEquals(options.logLevel, "debug")
assertEquals(options.keepTmpChart, true)
assertEquals(options.importMap, "test/import_map.json")
assertEquals(helmArgs, [
"upgrade",
"--install",
Expand Down
11 changes: 10 additions & 1 deletion src/args/parse-helm-deno-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import {
PartialOption,
BinaryFlag,
} from "https://deno.land/x/args@2.0.7/flag-types.ts"
import { Choice } from "https://deno.land/x/args@2.0.7/value-types.ts"
import { Choice, Text } from "https://deno.land/x/args@2.0.7/value-types.ts"

type LogLevel = "info" | "debug"
export interface HelmDenoOptions {
readonly logLevel: LogLevel
readonly keepTmpChart: boolean
readonly importMap: string
}

const parser = args
Expand All @@ -34,6 +35,13 @@ const parser = args
describe: "Keep downloaded chart in temporary directory",
})
)
.with(
PartialOption("deno-import-map", {
type: Text,
default: "",
describe: "Path to import_map.json",
})
)

function toEnum<T>(value: string): T {
// deno-lint-ignore no-explicit-any
Expand All @@ -59,6 +67,7 @@ export function parseArgs(args: readonly string[]): ParseArgsResult {
options: {
logLevel: toEnum(res.value?.["deno-log-level"]) || "info",
keepTmpChart: !!res.value?.["deno-keep-tmp-chart"],
importMap: res.value?.["deno-import-map"],
},
helmArgs: res.remaining().rawArgs(),
}
Expand Down
14 changes: 14 additions & 0 deletions src/deno/import-chart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
async function main() {
const ctx = JSON.parse(Deno.args[0])
const denoResources = await import(ctx.chartPath).then((chart) => {
return chart.default(ctx.chartContext)
})
console.log(JSON.stringify(denoResources))
}

if (import.meta.main) {
main().catch((err) => {
console.error(err)
Deno.exit(1)
})
}
54 changes: 50 additions & 4 deletions src/deno/render-chart.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ChartContext } from "../std/mod.ts"
import { HelmDenoOptions } from "../args/parse-helm-deno-args.ts"
import * as yaml from "https://deno.land/std@0.86.0/encoding/yaml.ts"
import * as fs from "https://deno.land/std@0.86.0/fs/mod.ts"
import * as path from "https://deno.land/std@0.86.0/path/mod.ts"
Expand All @@ -15,7 +16,8 @@ function stringifyResource(manifest: any): string {

export async function renderDenoChart(
chartContext: ChartContext,
chartPath: string
chartPath: string,
denoOptions: HelmDenoOptions
): Promise<void> {
const templateFolderPath = path.join(chartPath, "templates")
await fs.ensureDir(templateFolderPath)
Expand All @@ -25,14 +27,58 @@ export async function renderDenoChart(
if (!isDenoChart) {
return
}
const pluginFolderPath = Deno.env.get("HELM_PLUGIN_DIR") || ""

const denoResources = await import(denoTemplateFilePath).then((chart) => {
return chart.default(chartContext)
const deno = path.join(pluginFolderPath, "bin/deno")
const importer = path.join(pluginFolderPath, "src/deno/import-chart.ts")

const hasImportMap = await fs.exists(denoOptions.importMap)
const importMapArgs =
denoOptions.importMap && hasImportMap
? ["--importmap", denoOptions.importMap]
: []

const cmd = Deno.run({
cmd: [
deno,
"run",
"--unstable",
"--allow-net",
"--allow-read",
"--allow-write",
"--allow-run",
"--allow-env",
"--quiet",
...importMapArgs,
importer,
JSON.stringify({
chartPath: denoTemplateFilePath,
chartContext: chartContext,
}),
],
stdout: "piped",
stderr: "piped",
})

const [status, output, error] = await Promise.all([
cmd.status(),
cmd.output(),
cmd.stderrOutput(),
])
cmd.close()

if (!status.success) {
return Promise.reject(new TextDecoder().decode(error))
}

const denoResources = JSON.parse(new TextDecoder().decode(output))
const templates = denoResources.map(stringifyResource).join("\n---\n")
await Deno.writeTextFile(
path.join(templateFolderPath, `rendered-deno-templates.yaml`),
path.join(templateFolderPath, `import-rendered-templates.yaml`),
'{{ .Files.Get "rendered-deno-templates.yaml" }}'
)
await Deno.writeTextFile(
path.join(chartPath, `rendered-deno-templates.yaml`),
templates
)
}
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function copyChart(chartPath: string, destination: string) {
}
await withErrorMsg(
fs.copy(chartPath, destination, { overwrite: true }),
"Cloud not copy chart directory"
"Could not copy chart directory"
)
}

Expand Down Expand Up @@ -124,7 +124,7 @@ async function main() {
const chartContext = await getChartContext(releaseName, workdir, args)
debug(`Chart context:\n${JSON.stringify(chartContext, null, 2)}`)

await renderDenoChart(chartContext, workdir)
await renderDenoChart(chartContext, workdir, options)
debug("Deno templates were successfuly rendered")

const helmExecuteArgs = [
Expand All @@ -144,9 +144,13 @@ async function main() {
: str.replaceAll(`file://${workdir}`, "<chart-root>")
}

// Replace paths in stacktrace with readable value
// Replace paths in error or error stacktrace with readable value
if (err?.stack) {
err.stack = replaceChartPath(err.stack)
} else if (err?.message) {
err.message = replaceChartPath(err.message)
} else if (typeof err === "string") {
throw replaceChartPath(err)
}

throw err
Expand Down