pepr 0.44.0 → 0.45.0-nightly.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/README.md +1 -1
  2. package/dist/cli/build.d.ts +3 -3
  3. package/dist/cli/build.d.ts.map +1 -1
  4. package/dist/cli/build.helpers.d.ts +13 -7
  5. package/dist/cli/build.helpers.d.ts.map +1 -1
  6. package/dist/cli/deploy.d.ts.map +1 -1
  7. package/dist/cli/init/templates.d.ts +7 -8
  8. package/dist/cli/init/templates.d.ts.map +1 -1
  9. package/dist/cli.js +120 -100
  10. package/dist/controller.js +1 -1
  11. package/dist/lib/assets/assets.d.ts +2 -2
  12. package/dist/lib/assets/assets.d.ts.map +1 -1
  13. package/dist/lib/assets/index.d.ts +1 -1
  14. package/dist/lib/assets/index.d.ts.map +1 -1
  15. package/dist/lib/assets/pods.d.ts +1 -1
  16. package/dist/lib/assets/pods.d.ts.map +1 -1
  17. package/dist/lib/assets/rbac.d.ts.map +1 -1
  18. package/dist/lib/assets/webhooks.d.ts +2 -0
  19. package/dist/lib/assets/webhooks.d.ts.map +1 -1
  20. package/dist/lib/assets/yaml/overridesFile.d.ts +2 -4
  21. package/dist/lib/assets/yaml/overridesFile.d.ts.map +1 -1
  22. package/dist/lib/controller/index.d.ts +7 -2
  23. package/dist/lib/controller/index.d.ts.map +1 -1
  24. package/dist/lib/core/envChecks.d.ts +4 -0
  25. package/dist/lib/core/envChecks.d.ts.map +1 -0
  26. package/dist/lib/core/module.d.ts +1 -48
  27. package/dist/lib/core/module.d.ts.map +1 -1
  28. package/dist/lib/core/storage.d.ts.map +1 -1
  29. package/dist/lib/processors/mutate-processor.d.ts +1 -1
  30. package/dist/lib/processors/mutate-processor.d.ts.map +1 -1
  31. package/dist/lib/processors/validate-processor.d.ts +1 -1
  32. package/dist/lib/processors/validate-processor.d.ts.map +1 -1
  33. package/dist/lib/processors/watch-processor.d.ts.map +1 -1
  34. package/dist/lib/telemetry/metrics.d.ts.map +1 -1
  35. package/dist/lib/types.d.ts +45 -0
  36. package/dist/lib/types.d.ts.map +1 -1
  37. package/dist/lib.js +2008 -1990
  38. package/dist/lib.js.map +4 -4
  39. package/package.json +16 -13
  40. package/src/cli/build.helpers.ts +28 -13
  41. package/src/cli/build.ts +65 -65
  42. package/src/cli/deploy.ts +32 -26
  43. package/src/cli/init/index.ts +2 -2
  44. package/src/cli/init/templates.ts +6 -5
  45. package/src/cli/init/walkthrough.ts +1 -1
  46. package/src/lib/assets/assets.ts +2 -2
  47. package/src/lib/assets/index.ts +1 -2
  48. package/src/lib/assets/pods.ts +1 -1
  49. package/src/lib/assets/rbac.ts +1 -2
  50. package/src/lib/assets/webhooks.ts +1 -1
  51. package/src/lib/assets/yaml/overridesFile.ts +4 -6
  52. package/src/lib/controller/index.ts +19 -11
  53. package/src/lib/core/capability.ts +1 -1
  54. package/src/lib/core/envChecks.ts +6 -0
  55. package/src/lib/core/module.ts +20 -69
  56. package/src/lib/core/schedule.ts +2 -2
  57. package/src/lib/core/storage.ts +2 -1
  58. package/src/lib/processors/mutate-processor.ts +2 -2
  59. package/src/lib/processors/validate-processor.ts +1 -1
  60. package/src/lib/processors/watch-processor.ts +5 -3
  61. package/src/lib/telemetry/metrics.ts +4 -6
  62. package/src/lib/types.ts +48 -0
  63. package/src/sdk/heredoc.ts +2 -2
@@ -2,69 +2,14 @@
2
2
  // SPDX-FileCopyrightText: 2023-Present The Pepr Authors
3
3
  import { clone } from "ramda";
4
4
  import { Capability } from "./capability";
5
- import { Controller } from "../controller";
5
+ import { Controller, ControllerHooks } from "../controller";
6
6
  import { ValidateError } from "../errors";
7
- import { MutateResponse, ValidateResponse, WebhookIgnore } from "../k8s";
8
- import { CapabilityExport, AdmissionRequest } from "../types";
7
+ import { CapabilityExport } from "../types";
9
8
  import { setupWatch } from "../processors/watch-processor";
10
9
  import { Log } from "../../lib";
11
- import { V1PolicyRule as PolicyRule } from "@kubernetes/client-node";
12
10
  import { resolveIgnoreNamespaces } from "../assets/webhooks";
13
-
14
- /** Custom Labels Type for package.json */
15
- export interface CustomLabels {
16
- namespace?: Record<string, string>;
17
- }
18
- /** Global configuration for the Pepr runtime. */
19
- export type ModuleConfig = {
20
- /** The Pepr version this module uses */
21
- peprVersion?: string;
22
- /** The user-defined version of the module */
23
- appVersion?: string;
24
- /** A unique identifier for this Pepr module. This is automatically generated by Pepr. */
25
- uuid: string;
26
- /** A description of the Pepr module and what it does. */
27
- description?: string;
28
- /** The webhookTimeout */
29
- webhookTimeout?: number;
30
- /** Reject K8s resource AdmissionRequests on error. */
31
- onError?: string;
32
- /** Configure global exclusions that will never be processed by Pepr. */
33
- alwaysIgnore: WebhookIgnore;
34
- /** Define the log level for the in-cluster controllers */
35
- logLevel?: string;
36
- /** Propagate env variables to in-cluster controllers */
37
- env?: Record<string, string>;
38
- /** Custom Labels for Kubernetes Objects */
39
- customLabels?: CustomLabels;
40
- /** Custom RBAC rules */
41
- rbac?: PolicyRule[];
42
- /** The RBAC mode; if "scoped", generates scoped rules, otherwise uses wildcard rules. */
43
- rbacMode?: string;
44
- };
45
-
46
- export type PackageJSON = {
47
- description: string;
48
- pepr: ModuleConfig;
49
- };
50
-
51
- export type PeprModuleOptions = {
52
- deferStart?: boolean;
53
-
54
- /** A user-defined callback to pre-process or intercept a Pepr request from K8s immediately before it is processed */
55
- beforeHook?: (req: AdmissionRequest) => void;
56
-
57
- /** A user-defined callback to post-process or intercept a Pepr response just before it is returned to K8s */
58
- afterHook?: (res: MutateResponse | ValidateResponse) => void;
59
- };
60
-
61
- // Track if this is a watch mode controller
62
- export const isWatchMode = (): boolean => process.env.PEPR_WATCH_MODE === "true";
63
-
64
- // Track if Pepr is running in build mode
65
- export const isBuildMode = (): boolean => process.env.PEPR_MODE === "build";
66
-
67
- export const isDevMode = (): boolean => process.env.PEPR_MODE === "dev";
11
+ import { isBuildMode, isDevMode, isWatchMode } from "./envChecks";
12
+ import { PackageJSON, PeprModuleOptions, ModuleConfig } from "../types";
68
13
 
69
14
  export class PeprModule {
70
15
  #controller!: Controller;
@@ -110,17 +55,23 @@ export class PeprModule {
110
55
  return;
111
56
  }
112
57
 
113
- this.#controller = new Controller(config, capabilities, opts.beforeHook, opts.afterHook, () => {
114
- // Wait for the controller to be ready before setting up watches
115
- if (isWatchMode() || isDevMode()) {
116
- try {
117
- setupWatch(capabilities, resolveIgnoreNamespaces(pepr?.alwaysIgnore?.namespaces));
118
- } catch (e) {
119
- Log.error(e, "Error setting up watch");
120
- process.exit(1);
58
+ const controllerHooks: ControllerHooks = {
59
+ beforeHook: opts.beforeHook,
60
+ afterHook: opts.afterHook,
61
+ onReady: (): void => {
62
+ // Wait for the controller to be ready before setting up watches
63
+ if (isWatchMode() || isDevMode()) {
64
+ try {
65
+ setupWatch(capabilities, resolveIgnoreNamespaces(pepr?.alwaysIgnore?.namespaces));
66
+ } catch (e) {
67
+ Log.error(e, "Error setting up watch");
68
+ process.exit(1);
69
+ }
121
70
  }
122
- }
123
- });
71
+ },
72
+ };
73
+
74
+ this.#controller = new Controller(config, capabilities, controllerHooks);
124
75
 
125
76
  // Stop processing if deferStart is set to true
126
77
  if (opts.deferStart) {
@@ -91,7 +91,7 @@ export class OnSchedule implements Schedule {
91
91
  lastTimestamp: new Date(),
92
92
  name: this.name,
93
93
  };
94
- this.store && this.store.setItem(this.name, JSON.stringify(schedule));
94
+ if (this.store) this.store.setItem(this.name, JSON.stringify(schedule));
95
95
  }
96
96
 
97
97
  /**
@@ -170,6 +170,6 @@ export class OnSchedule implements Schedule {
170
170
  clearInterval(this.intervalId);
171
171
  this.intervalId = null;
172
172
  }
173
- this.store && this.store.removeItem(this.name);
173
+ if (this.store) this.store.removeItem(this.name);
174
174
  }
175
175
  }
@@ -110,11 +110,12 @@ export class Storage implements PeprStore {
110
110
  };
111
111
 
112
112
  clear = (): void => {
113
- Object.keys(this.#store).length > 0 &&
113
+ if (Object.keys(this.#store).length > 0) {
114
114
  this.#dispatchUpdate(
115
115
  "remove",
116
116
  Object.keys(this.#store).map(key => pointer.escape(key)),
117
117
  );
118
+ }
118
119
  };
119
120
 
120
121
  removeItem = (key: string): void => {
@@ -10,7 +10,7 @@ import { shouldSkipRequest } from "../filter/filter";
10
10
  import { MutateResponse } from "../k8s";
11
11
  import { AdmissionRequest, Binding } from "../types";
12
12
  import Log from "../telemetry/logger";
13
- import { ModuleConfig } from "../core/module";
13
+ import { ModuleConfig } from "../types";
14
14
  import { PeprMutateRequest } from "../mutate-request";
15
15
  import { base64Encode, convertFromBase64Map, convertToBase64Map } from "../utils";
16
16
  import { OnError } from "../../cli/init/enums";
@@ -55,7 +55,7 @@ export function logMutateErrorMessage(e: Error): string {
55
55
  } else {
56
56
  throw new Error("An error occurred in the mutate action.");
57
57
  }
58
- } catch (e) {
58
+ } catch {
59
59
  return "An error occurred with the mutate action.";
60
60
  }
61
61
  }
@@ -9,7 +9,7 @@ import { AdmissionRequest, Binding } from "../types";
9
9
  import Log from "../telemetry/logger";
10
10
  import { convertFromBase64Map } from "../utils";
11
11
  import { PeprValidateRequest } from "../validate-request";
12
- import { ModuleConfig } from "../core/module";
12
+ import { ModuleConfig } from "../types";
13
13
  import { resolveIgnoreNamespaces } from "../assets/webhooks";
14
14
  import { MeasureWebhookTimeout } from "../telemetry/webhookTimeouts";
15
15
  import { WebhookType } from "../enums";
@@ -137,9 +137,11 @@ async function runBinding(
137
137
 
138
138
  // [ true, void, undefined ] SHOULD remove finalizer
139
139
  // [ false ] should NOT remove finalizer
140
- shouldRemoveFinalizer === false
141
- ? Log.debug({ obj: kubernetesObject }, `Skipping removal of finalizer '${peprFinal}' from '${resource}'`)
142
- : await removeFinalizer(binding, kubernetesObject);
140
+ if (shouldRemoveFinalizer === false) {
141
+ Log.debug({ obj: kubernetesObject }, `Skipping removal of finalizer '${peprFinal}' from '${resource}'`);
142
+ } else {
143
+ await removeFinalizer(binding, kubernetesObject);
144
+ }
143
145
  }
144
146
  };
145
147
 
@@ -66,9 +66,7 @@ export class MetricsCollector {
66
66
  #addMetric = <T extends Counter<string> | Gauge<string> | Summary<string>>(
67
67
  collection: Map<string, T>,
68
68
  MetricType: new (args: MetricArgs) => T,
69
- name: string,
70
- help: string,
71
- labelNames?: string[],
69
+ { name, help, labelNames }: Omit<MetricArgs, "registers">,
72
70
  ): void => {
73
71
  if (collection.has(this.#getMetricName(name))) {
74
72
  Log.debug(`Metric for ${name} already exists`, loggingPrefix);
@@ -86,15 +84,15 @@ export class MetricsCollector {
86
84
  };
87
85
 
88
86
  addCounter = (name: string, help: string): void => {
89
- this.#addMetric(this.#counters, promClient.Counter, name, help, []);
87
+ this.#addMetric(this.#counters, promClient.Counter, { name, help, labelNames: [] });
90
88
  };
91
89
 
92
90
  addSummary = (name: string, help: string): void => {
93
- this.#addMetric(this.#summaries, promClient.Summary, name, help, []);
91
+ this.#addMetric(this.#summaries, promClient.Summary, { name, help, labelNames: [] });
94
92
  };
95
93
 
96
94
  addGauge = (name: string, help: string, labelNames?: string[]): void => {
97
- this.#addMetric(this.#gauges, promClient.Gauge, name, help, labelNames);
95
+ this.#addMetric(this.#gauges, promClient.Gauge, { name, help, labelNames });
98
96
  };
99
97
 
100
98
  incCounter = (name: string): void => {
package/src/lib/types.ts CHANGED
@@ -8,6 +8,7 @@ import { Logger } from "pino";
8
8
  import { PeprMutateRequest } from "./mutate-request";
9
9
  import { PeprValidateRequest } from "./validate-request";
10
10
  import { V1PolicyRule as PolicyRule } from "@kubernetes/client-node";
11
+ import { WebhookIgnore, MutateResponse, ValidateResponse } from "./k8s";
11
12
 
12
13
  /**
13
14
  * Specifically for deploying images with a private registry
@@ -356,4 +357,51 @@ export interface GroupVersionResource {
356
357
  // DeepPartial utility type for deep optional properties
357
358
  export type DeepPartial<T> = {
358
359
  [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
360
+ }; /** Custom Labels Type for package.json */
361
+
362
+ export type CustomLabels = { namespace: Record<string, string> } | Record<string, never>;
363
+ /** Configuration that MAY be set a Pepr module's package.json. */
364
+ export type ModuleConfigOptions = {
365
+ /** The Pepr version this module uses */
366
+ peprVersion: string;
367
+ /** The user-defined version of the module */
368
+ appVersion: string;
369
+ /** A description of the Pepr module and what it does. */
370
+ description: string;
371
+ /** The webhookTimeout */
372
+ webhookTimeout: number;
373
+ /** Reject K8s resource AdmissionRequests on error. */
374
+ onError: string;
375
+ /** Define the log level for the in-cluster controllers */
376
+ logLevel: string;
377
+ /** Propagate env variables to in-cluster controllers */
378
+ env: Record<string, string>;
379
+ /** Custom RBAC rules */
380
+ rbac: PolicyRule[];
381
+ /** The RBAC mode; if "scoped", generates scoped rules, otherwise uses wildcard rules. */
382
+ rbacMode: string;
383
+ /** Custom Labels for Kubernetes Objects */
384
+ customLabels: CustomLabels;
385
+ };
386
+ /** Global configuration for the Pepr runtime. */
387
+ export type ModuleConfig = {
388
+ /** A unique identifier for this Pepr module. This is automatically generated by Pepr. */
389
+ uuid: string;
390
+ /** Configure global exclusions that will never be processed by Pepr. */
391
+ alwaysIgnore: WebhookIgnore;
392
+ } & Partial<ModuleConfigOptions>;
393
+
394
+ export type PackageJSON = {
395
+ description: string;
396
+ pepr: ModuleConfig;
359
397
  };
398
+
399
+ export type PeprModuleOptions = {
400
+ deferStart?: boolean;
401
+
402
+ /** A user-defined callback to pre-process or intercept a Pepr request from K8s immediately before it is processed */
403
+ beforeHook?: (req: AdmissionRequest) => void;
404
+
405
+ /** A user-defined callback to post-process or intercept a Pepr response just before it is returned to K8s */
406
+ afterHook?: (res: MutateResponse | ValidateResponse) => void;
407
+ }; // Track if this is a watch mode controller
@@ -14,8 +14,8 @@ export function heredoc(strings: TemplateStringsArray, ...values: string[]): str
14
14
  const asLines = zipped.join("").split(/[\r\n]+/);
15
15
 
16
16
  // strip whitespace-only first & last lines
17
- asLines[0].trim().length === 0 ? asLines.shift() : null;
18
- asLines.slice(-1)[0].trim().length === 0 ? asLines.pop() : null;
17
+ if (asLines[0].trim().length === 0) asLines.shift();
18
+ if (asLines.slice(-1)[0].trim().length === 0) asLines.pop();
19
19
 
20
20
  // find smallest indent
21
21
  const indent = asLines.reduce((acc, cur) => {