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

fix: tolerate optional fields from external pepr module definitions #1732

Merged
merged 6 commits into from
Jan 29, 2025
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
11 changes: 6 additions & 5 deletions src/cli/init/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import { inspect } from "util";
import { v4 as uuidv4, v5 as uuidv5 } from "uuid";

import eslintJSON from "../../templates/.eslintrc.template.json";
import peprSnippetsJSON from "../../templates/pepr.code-snippets.json";
import prettierJSON from "../../templates/.prettierrc.json";
import samplesJSON from "../../templates/capabilities/hello-pepr.samples.json";
import { gitIgnore, helloPeprTS, packageJSON, peprTS, readmeMd } from "../../templates/data.json";
import peprSnippetsJSON from "../../templates/pepr.code-snippets.json";
import settingsJSON from "../../templates/settings.json";
import tsConfigJSON from "../../templates/tsconfig.module.json";
import { sanitizeName } from "./utils";
import { CustomLabels } from "../../lib/core/module";
import { InitOptions } from "../types";
import { V1PolicyRule as PolicyRule } from "@kubernetes/client-node";
import { OnError, RbacMode } from "./enums";
import { V1PolicyRule as PolicyRule } from "@kubernetes/client-node";
import { gitIgnore, helloPeprTS, packageJSON, peprTS, readmeMd } from "../../templates/data.json";
import { sanitizeName } from "./utils";

export const { dependencies, devDependencies, peerDependencies, scripts, version } = packageJSON;

Expand All @@ -30,7 +31,7 @@ type peprPackageJSON = {
uuid: string;
onError: OnError;
webhookTimeout: number;
customLabels: { namespace: Record<string, string> };
customLabels: CustomLabels;
alwaysIgnore: { namespaces: string[] };
includedFiles: string[];
env: object;
Expand Down
4 changes: 3 additions & 1 deletion src/lib/assets/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { it, describe, expect } from "@jest/globals";
import { createWebhookYaml } from "./index";
import { kind } from "kubernetes-fluent-client";
import { ModuleConfig } from "../core/module";

describe("createWebhookYaml", () => {
const webhookConfiguration = new kind.MutatingWebhookConfiguration();
Expand Down Expand Up @@ -37,13 +38,14 @@ describe("createWebhookYaml", () => {
},
];

const moduleConfig = {
const moduleConfig: ModuleConfig = {
onError: "reject",
webhookTimeout: 15,
uuid: "some-uuid",
alwaysIgnore: {
namespaces: ["kube-system", "pepr-system"],
},
customLabels: { namespace: { "pepr.dev": "" } },
};

it("replaces placeholders in the YAML correctly", () => {
Expand Down
44 changes: 24 additions & 20 deletions src/lib/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,41 @@ import { V1PolicyRule as PolicyRule } from "@kubernetes/client-node";
import { resolveIgnoreNamespaces } from "../assets/webhooks";

/** Custom Labels Type for package.json */
export interface CustomLabels {
namespace?: Record<string, string>;
}
/** Global configuration for the Pepr runtime. */
export type ModuleConfig = {

export type CustomLabels = { namespace: Record<string, string> } | Record<string, never>;

/** Configuration that MAY be set a Pepr module's package.json. */
export type ModuleConfigOptions = {
/** The Pepr version this module uses */
peprVersion?: string;
peprVersion: string;
/** The user-defined version of the module */
appVersion?: string;
/** A unique identifier for this Pepr module. This is automatically generated by Pepr. */
uuid: string;
appVersion: string;
/** A description of the Pepr module and what it does. */
description?: string;
description: string;
/** The webhookTimeout */
webhookTimeout?: number;
webhookTimeout: number;
/** Reject K8s resource AdmissionRequests on error. */
onError?: string;
/** Configure global exclusions that will never be processed by Pepr. */
alwaysIgnore: WebhookIgnore;
onError: string;
/** Define the log level for the in-cluster controllers */
logLevel?: string;
logLevel: string;
/** Propagate env variables to in-cluster controllers */
env?: Record<string, string>;
/** Custom Labels for Kubernetes Objects */
customLabels?: CustomLabels;
env: Record<string, string>;
/** Custom RBAC rules */
rbac?: PolicyRule[];
rbac: PolicyRule[];
/** The RBAC mode; if "scoped", generates scoped rules, otherwise uses wildcard rules. */
rbacMode?: string;
rbacMode: string;
/** Custom Labels for Kubernetes Objects */
customLabels: CustomLabels;
};

/** Global configuration for the Pepr runtime. */
export type ModuleConfig = {
/** A unique identifier for this Pepr module. This is automatically generated by Pepr. */
uuid: string;
/** Configure global exclusions that will never be processed by Pepr. */
alwaysIgnore: WebhookIgnore;
} & Partial<ModuleConfigOptions>;

export type PackageJSON = {
description: string;
pepr: ModuleConfig;
Expand Down
Loading