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

feat: merge package env vars #963

Merged
merged 5 commits into from
Jul 17, 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
75 changes: 71 additions & 4 deletions journey/pepr-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { execSync } from "child_process";
import { promises as fs } from "fs";
import { resolve } from "path";
import { V1ObjectMeta, KubernetesObject } from '@kubernetes/client-node';
import yaml from "js-yaml";
import { cwd } from "./entrypoint.test";

export function peprBuild() {
Expand All @@ -23,6 +24,72 @@ export function peprBuild() {
await fs.access(resolve(cwd, "dist", "zarf.yaml"));
await validateZarfYaml();
});

it("should correct merge in the package.json env vars into the values.yaml helm chart file", async () => {
interface ValuesJSON {
admission: {
env: Record<string, string>[] | undefined;
};
watcher: {
env: Record<string, string>[] | undefined;
};
}

const expectedWatcherEnv = [
{
"name": "PEPR_WATCH_MODE",
"value": "true"
},
{
"name": "PEPR_PRETTY_LOG",
"value": "false"
},
{
"name": "LOG_LEVEL",
"value": "info"
},
{
"name": "MY_CUSTOM_VAR",
"value": "example-value"
},
{
"name": "ZARF_VAR",
"value": "###ZARF_VAR_THING###"
}
];

const expectedAdmissionEnv = [
{
"name": "PEPR_WATCH_MODE",
"value": "false"
},
{
"name": "PEPR_PRETTY_LOG",
"value": "false"
},
{
"name": "LOG_LEVEL",
"value": "info"
},
{
"name": "MY_CUSTOM_VAR",
"value": "example-value"
},
{
"name": "ZARF_VAR",
"value": "###ZARF_VAR_THING###"
}
]

try {
const valuesYaml = await fs.readFile(resolve(cwd, "dist", "static-test-chart", "values.yaml"), "utf8");
const valuesJSON = yaml.load(valuesYaml) as ValuesJSON;
expect(valuesJSON.admission.env).toEqual(expectedAdmissionEnv);
expect(valuesJSON.watcher!.env).toEqual(expectedWatcherEnv);
} catch (error) {
expect(error).toBeUndefined();
}
})
}

async function validateHelmChart() {
Expand Down Expand Up @@ -102,9 +169,9 @@ function parseYAMLToJSON(yamlContent: string): KubernetesObject[] | null {

function sortKubernetesObjects(objects: KubernetesObject[]): KubernetesObject[] {
return objects.sort((a, b) => {
if (a?.kind !== b?.kind) {
return (a?.kind ?? '').localeCompare(b?.kind ?? '');
}
return ((a && a.metadata && (a.metadata as V1ObjectMeta)?.name) ?? '').localeCompare((b && b.metadata && (b.metadata as V1ObjectMeta)?.name) ?? '');
if (a?.kind !== b?.kind) {
return (a?.kind ?? '').localeCompare(b?.kind ?? '');
}
return ((a && a.metadata && (a.metadata as V1ObjectMeta)?.name) ?? '').localeCompare((b && b.metadata && (b.metadata as V1ObjectMeta)?.name) ?? '');
});
}
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@
"@types/node": "18.x.x",
"@types/node-forge": "1.3.11",
"@types/prompts": "2.4.9",
"fast-check": "^3.19.0",
"@types/uuid": "10.0.0",
"fast-check": "^3.19.0",
"jest": "29.7.0",
"js-yaml": "^4.1.0",
"nock": "13.5.4",
"ts-jest": "29.2.2"
},
Expand Down
31 changes: 14 additions & 17 deletions src/lib/assets/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@
import { dumpYaml } from "@kubernetes/client-node";
import crypto from "crypto";
import { promises as fs } from "fs";

import { Assets } from ".";
import { apiTokenSecret, service, tlsSecret, watcherService } from "./networking";
import { deployment, moduleSecret, namespace, watcher } from "./pods";
import { clusterRole, clusterRoleBinding, serviceAccount, storeRole, storeRoleBinding } from "./rbac";
import { webhookConfig } from "./webhooks";
import { mergePkgJSONEnv, envMapToArray } from "../helpers";

// Helm Chart overrides file (values.yaml) generated from assets
export async function overridesFile({ hash, name, image, config, apiToken }: Assets, path: string) {
const pkgJSONAdmissionEnv = {
PEPR_WATCH_MODE: "false",
PEPR_PRETTY_LOG: "false",
LOG_LEVEL: "info",
};
const pkgJSONWatchEnv = {
PEPR_WATCH_MODE: "true",
PEPR_PRETTY_LOG: "false",
LOG_LEVEL: "info",
};

const overrides = {
secrets: {
apiToken: Buffer.from(apiToken).toString("base64"),
Expand All @@ -29,11 +40,7 @@ export async function overridesFile({ hash, name, image, config, apiToken }: Ass
terminationGracePeriodSeconds: 5,
failurePolicy: config.onError === "reject" ? "Fail" : "Ignore",
webhookTimeout: config.webhookTimeout,
env: [
{ name: "PEPR_WATCH_MODE", value: "false" },
{ name: "PEPR_PRETTY_LOG", value: "false" },
{ name: "LOG_LEVEL", value: "info" },
],
env: envMapToArray(mergePkgJSONEnv(pkgJSONAdmissionEnv, config.env)),
image,
annotations: {
"pepr.dev/description": `${config.description}` || "",
Expand Down Expand Up @@ -77,11 +84,7 @@ export async function overridesFile({ hash, name, image, config, apiToken }: Ass
},
watcher: {
terminationGracePeriodSeconds: 5,
env: [
{ name: "PEPR_WATCH_MODE", value: "true" },
{ name: "PEPR_PRETTY_LOG", value: "false" },
{ name: "LOG_LEVEL", value: "info" },
],
env: envMapToArray(mergePkgJSONEnv(pkgJSONWatchEnv, config.env)),
image,
annotations: {
"pepr.dev/description": `${config.description}` || "",
Expand Down Expand Up @@ -124,12 +127,6 @@ export async function overridesFile({ hash, name, image, config, apiToken }: Ass
podAnnotations: {},
},
};
if (process.env.PEPR_MODE === "dev") {
overrides.admission.env.push({ name: "ZARF_VAR", value: "###ZARF_VAR_THING###" });
overrides.watcher.env.push({ name: "ZARF_VAR", value: "###ZARF_VAR_THING###" });
overrides.admission.env.push({ name: "MY_CUSTOM_VAR", value: "example-value" });
overrides.watcher.env.push({ name: "MY_CUSTOM_VAR", value: "example-value" });
}

await fs.writeFile(path, dumpYaml(overrides, { noRefs: true, forceQuotes: true }));
}
Expand Down
131 changes: 131 additions & 0 deletions src/lib/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
validateHash,
ValidationError,
validateCapabilityNames,
mergePkgJSONEnv,
envMapToArray,
} from "./helpers";
import { sanitizeResourceName } from "../sdk/sdk";
import * as fc from "fast-check";
Expand Down Expand Up @@ -1190,3 +1192,132 @@ describe("validateHash", () => {
expect(() => validateHash(validHash)).not.toThrow();
});
});

describe("mergePkgJSONEnv", () => {
const pkgJSON = {
FAV_COLOR: "blue",
};
const admissionEnv = { PEPR_WATCH_MODE: "false" };
const watcherEnv = { PEPR_WATCH_MODE: "true" };
test("should merge env variables from package.json", () => {
const result = mergePkgJSONEnv(admissionEnv, pkgJSON);
expect(result).toEqual({
FAV_COLOR: "blue",
PEPR_WATCH_MODE: "false",
});
});

test("should not be able to override PEPR_WATCH_MODE", () => {
const pkgJSON = {
PEPR_WATCH_MODE: "false",
};
const result = mergePkgJSONEnv(watcherEnv, pkgJSON);
expect(result).toEqual({
PEPR_WATCH_MODE: "true",
});
});

test("if conflicts arrise, package.json should be used", () => {
const pkgJSON = {
FAV_COLOR: "red",
};
const admissionEnv = { FAV_COLOR: "blue" };
const result = mergePkgJSONEnv(admissionEnv, pkgJSON);
expect(result).toEqual({
FAV_COLOR: "red",
});
});

test("if there is no package.json env, return other env", () => {
const admissionEnv = { FAV_COLOR: "blue" };
const result = mergePkgJSONEnv(admissionEnv, undefined);
expect(result).toEqual({
FAV_COLOR: "blue",
});
});
});

describe("envMapToArr", () => {
test("should return an array of key value pairs", () => {
const envMap = [
{
FAV_COLOR: "blue",
PEPR_WATCH_MODE: "true",
},
{
FAV_COLOR: "yellow",
PEPR_WATCH_MODE: "false",
},
{
FAV_COLOR: "orange",
PEPR_WATCH_MODE: "true",
},
{
FAV_COLOR: "green",
PEPR_WATCH_MODE: "false",
},
{
FAV_COLOR: "red",
PEPR_WATCH_MODE: "true",
},
];

const expectation = [
[
{
name: "FAV_COLOR",
value: "blue",
},
{
name: "PEPR_WATCH_MODE",
value: "true",
},
],
[
{
name: "FAV_COLOR",
value: "yellow",
},
{
name: "PEPR_WATCH_MODE",
value: "false",
},
],
[
{
name: "FAV_COLOR",
value: "orange",
},
{
name: "PEPR_WATCH_MODE",
value: "true",
},
],
[
{
name: "FAV_COLOR",
value: "green",
},
{
name: "PEPR_WATCH_MODE",
value: "false",
},
],
[
{
name: "FAV_COLOR",
value: "red",
},
{
name: "PEPR_WATCH_MODE",
value: "true",
},
],
];

envMap.forEach((env, index) => {
const result = envMapToArray(env);
expect(result).toEqual(expectation[index]);
});
});
});
17 changes: 17 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,24 @@ import { K8s, KubernetesObject, kind } from "kubernetes-fluent-client";
import Log from "./logger";
import { Binding, CapabilityExport } from "./types";
import { sanitizeResourceName } from "../sdk/sdk";
import { mergeDeepRight } from "ramda";

export function mergePkgJSONEnv(env: Record<string, string>, pkgJSONEnv?: Record<string, string>) {
if (!pkgJSONEnv) {
return env;
}
// Cannot override watch mode because it is critical to deployments
Object.keys(pkgJSONEnv).forEach(key => {
if (key === "PEPR_WATCH_MODE") {
delete pkgJSONEnv[key];
}
});
return mergeDeepRight(env, pkgJSONEnv);
}

export function envMapToArray(env: Record<string, string>) {
return Object.entries(env).map(([key, value]) => ({ name: key, value }));
}
export class ValidationError extends Error {}

export function validateCapabilityNames(capabilities: CapabilityExport[] | undefined): void {
Expand Down
Loading