Skip to content

Commit

Permalink
fix: tolerate optional fields from external pepr module definitions (#…
Browse files Browse the repository at this point in the history
…1732)

## Description

This PR relates to an issue where customLabels were not being applied to
a Helm chart after running `pepr build`.

## Related Issue

Relates to #1713

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Other (security config, docs update, etc)

## Checklist before merging
- [ ] Unit,
[Journey](https://github.com/defenseunicorns/pepr/tree/main/journey),
[E2E Tests](https://github.com/defenseunicorns/pepr-excellent-examples),
[docs](https://github.com/defenseunicorns/pepr/tree/main/docs),
[adr](https://github.com/defenseunicorns/pepr/tree/main/adr) added or
updated as needed
- [x] [Contributor Guide
Steps](https://docs.pepr.dev/main/contribute/#submitting-a-pull-request)
followed
  • Loading branch information
samayer12 authored Jan 29, 2025
1 parent 2f52362 commit 2e03a46
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
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

0 comments on commit 2e03a46

Please sign in to comment.