Skip to content

Instantly share code, notes, and snippets.

@zarkone
Created October 27, 2020 08:28
Show Gist options
  • Save zarkone/d1868aa59a9ea2cec3c86982c0e42307 to your computer and use it in GitHub Desktop.
Save zarkone/d1868aa59a9ea2cec3c86982c0e42307 to your computer and use it in GitHub Desktop.
Helm3 workaround for pulumi, see https://github.com/pulumi/pulumi-kubernetes/issues/1335. Doens't support arrays and requires manual escaping / marshaling.
import * as child_process from "child_process";
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
const SPAWN_PROCESS_BUFFER_SIZE = 104857600;
export const repos = {
stable: "https://kubernetes-charts.storage.googleapis.com",
prometheus: "https://prometheus-community.github.io/helm-charts",
jetstack: "https://charts.jetstack.io",
ingress: "https://kubernetes.github.io/ingress-nginx",
banzaicloud: "https://kubernetes-charts.banzaicloud.com"
};
export interface Helm3Args {
repos?: { [keyof: string]: string },
fetchOpts: {
repo: string
}
version: string,
values?: pulumi.Inputs,
namespace?: pulumi.Input<string>,
chart: string
}
function jsonToHelmSetValues(values: { [keyof: string]: any }): string[] {
// TODO: arrays
return Object.keys(values).map(key => {
const value = values[key];
if (value instanceof Object) {
const nextValues = jsonToHelmSetValues(value);
return nextValues.map(v => `${key}.${v.toString()}`)
} else {
return [`${key}=${value.toString()}`];
}
}).flat();
}
export function helm3(name: string, args: Helm3Args, opts?: pulumi.ComponentResourceOptions) {
const setArgs =
jsonToHelmSetValues(args.values || {})
.map(value => `--set ${value}`)
.join(" ");
const helmArgs = [
`--version ${args.version}`,
`--namespace ${args.namespace || 'default'}`,
`--repo ${args.fetchOpts.repo}`,
setArgs
].join(" ");
const helmCmd = `helm template ${name} ${args.chart} ${helmArgs}`;
console.log(helmCmd)
const rendered = child_process.execSync(helmCmd, {
maxBuffer: SPAWN_PROCESS_BUFFER_SIZE
}).toString();
return new k8s.yaml.ConfigGroup(name, {
yaml: rendered,
}, opts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment