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

chore: add typing to templates.ts #1602

Merged
merged 8 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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 docs/030_user-guide/120_customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Below are the available configurations through `package.json`.
| Field | Description | Example Values |
|------------------|----------------------------------------|---------------------------------|
| `uuid` | Unique identifier for the module | `hub-operator` |
| `onError` | Behavior of the webhook failure policy | `reject`, `ignore` |
| `onError` | Behavior of the webhook failure policy | `audit`, `ignore`, `reject` |
| `webhookTimeout` | Webhook timeout in seconds | `1` - `30` |
| `customLabels` | Custom labels for namespaces | `{namespace: {}}` |
| `alwaysIgnore` | Conditions to always ignore | `{namespaces: []}` |
Expand Down
9 changes: 9 additions & 0 deletions src/cli/init/enums.ts
samayer12 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export enum RbacMode {
SCOPED = "scoped",
ADMIN = "admin",
}
export enum OnError {
AUDIT = "audit",
IGNORE = "ignore",
REJECT = "reject",
}
34 changes: 32 additions & 2 deletions src/cli/init/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,38 @@ import settingsJSON from "../../templates/settings.json";
import tsConfigJSON from "../../templates/tsconfig.module.json";
import { sanitizeName } from "./utils";
import { InitOptions } from "../types";
import { V1PolicyRule as PolicyRule } from "@kubernetes/client-node";
import { OnError, RbacMode } from "./enums";

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

export function genPkgJSON(opts: InitOptions, pgkVerOverride?: string) {
type peprPackageJSON = {
data: {
name: string;
version: string;
description: string;
keywords: string[];
engines: { node: string };
pepr: {
samayer12 marked this conversation as resolved.
Show resolved Hide resolved
uuid: string;
onError: OnError;
webhookTimeout: number;
customLabels: { namespace: Record<string, string> };
alwaysIgnore: { namespaces: string[] };
includedFiles: string[];
env: object;
rbac: PolicyRule[];
rbacMode: RbacMode;
};
scripts: { "k3d-setup": string };
dependencies: { pepr: string; undici: string };
devDependencies: { typescript: string };
};
path: string;
print: string;
};

export function genPkgJSON(opts: InitOptions, pgkVerOverride?: string): peprPackageJSON {
// Generate a random UUID for the module based on the module name
const uuid = uuidv5(opts.name, uuidv4());
// Generate a name for the module based on the module name
Expand Down Expand Up @@ -52,6 +80,8 @@ export function genPkgJSON(opts: InitOptions, pgkVerOverride?: string) {
},
includedFiles: [],
env: pgkVerOverride ? testEnv : {},
rbac: [],
rbacMode: RbacMode.SCOPED,
samayer12 marked this conversation as resolved.
Show resolved Hide resolved
},
scripts: {
"k3d-setup": scripts["test:journey:k3d"],
Expand All @@ -72,7 +102,7 @@ export function genPkgJSON(opts: InitOptions, pgkVerOverride?: string) {
};
}

export function genPeprTS() {
export function genPeprTS(): { path: string; data: string } {
return {
path: "pepr.ts",
data: peprTS,
Expand Down
5 changes: 3 additions & 2 deletions src/cli/init/walkthrough.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
setName,
setErrorBehavior,
} from "./walkthrough";
import { OnError } from "./enums";

let consoleLog: jest.Spied<typeof console.log>;
let consoleError: jest.Spied<typeof console.error>;
Expand Down Expand Up @@ -41,7 +42,7 @@ describe("when processing input", () => {
const expected: PromptOptions = {
name: "My Test Module",
description: "A test module for Pepr",
errorBehavior: "reject",
errorBehavior: OnError.REJECT,
};

// Set values for the flag(s) under test by making a subset of (expected)
Expand Down Expand Up @@ -78,7 +79,7 @@ describe("when processing input", () => {
it("should prompt for errorBehavior when given invalid input", async () => {
const expected = { errorBehavior: "audit" };
prompts.inject(["audit"]);
const result = await setErrorBehavior("not-valid" as "reject"); // Type-Coercion forces invalid input
const result = await setErrorBehavior("not-valid" as OnError); // Type-Coercion forces invalid input
expect(result).toStrictEqual(expected);
});
});
Expand Down
15 changes: 7 additions & 8 deletions src/cli/init/walkthrough.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import { promises as fs } from "fs";
import prompt, { Answers, PromptObject } from "prompts";

import { ErrorList, Errors } from "../../lib/errors";
import { ErrorList } from "../../lib/errors";
import { eslint, gitignore, prettier, readme, tsConfig } from "./templates";
import { sanitizeName } from "./utils";
import { OnError } from "./enums";

export type PromptOptions = {
name: string;
description: string;
errorBehavior: "audit" | "ignore" | "reject";
errorBehavior: OnError;
};

export type PartialPromptOptions = Partial<PromptOptions>;
Expand Down Expand Up @@ -70,30 +71,28 @@ async function setDescription(description?: string): Promise<Answers<string>> {
return prompt([askDescription]);
}

export async function setErrorBehavior(
errorBehavior?: "audit" | "ignore" | "reject",
): Promise<Answers<string>> {
export async function setErrorBehavior(errorBehavior?: OnError): Promise<Answers<string>> {
const askErrorBehavior: PromptObject = {
type: "select",
name: "errorBehavior",
message: "How do you want Pepr to handle errors encountered during K8s operations?",
choices: [
{
title: "Reject the operation",
value: Errors.reject,
value: OnError.REJECT,
description:
"In the event that Pepr is down or other module errors occur, the operation will not be allowed to continue. (Recommended for production.)",
},
{
title: "Ignore",
value: Errors.ignore,
value: OnError.IGNORE,
description:
"In the event that Pepr is down or other module errors occur, an entry will be generated in the Pepr Controller Log and the operation will be allowed to continue. (Recommended for development, not for production.)",
selected: true,
},
{
title: "Log an audit event",
value: Errors.audit,
value: OnError.AUDIT,
description:
"Pepr will continue processing and generate an entry in the Pepr Controller log as well as an audit event in the cluster.",
},
Expand Down
6 changes: 6 additions & 0 deletions src/lib/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ export enum Event {
CREATE_OR_UPDATE = "CREATEORUPDATE",
ANY = "*",
}

export enum Errors {
AUDIT = "audit",
IGNORE = "ignore",
reject = "reject",
}
Loading