pepr 0.45.0 → 0.45.1-nightly.0

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 +15 -7
  2. package/dist/cli/build.d.ts +1 -1
  3. package/dist/cli/build.d.ts.map +1 -1
  4. package/dist/cli/build.helpers.d.ts +1 -1
  5. package/dist/cli/init/templates.d.ts +5 -2
  6. package/dist/cli/init/templates.d.ts.map +1 -1
  7. package/dist/cli.js +31 -37
  8. package/dist/controller.js +1 -1
  9. package/dist/lib/assets/assets.d.ts +3 -3
  10. package/dist/lib/assets/assets.d.ts.map +1 -1
  11. package/dist/lib/assets/index.d.ts +1 -1
  12. package/dist/lib/assets/index.d.ts.map +1 -1
  13. package/dist/lib/assets/networking.d.ts +1 -1
  14. package/dist/lib/assets/networking.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/yaml/overridesFile.d.ts +3 -3
  18. package/dist/lib/assets/yaml/overridesFile.d.ts.map +1 -1
  19. package/dist/lib/controller/index.d.ts +1 -1
  20. package/dist/lib/controller/index.d.ts.map +1 -1
  21. package/dist/lib/core/envChecks.d.ts +4 -0
  22. package/dist/lib/core/envChecks.d.ts.map +1 -0
  23. package/dist/lib/core/module.d.ts +1 -51
  24. package/dist/lib/core/module.d.ts.map +1 -1
  25. package/dist/lib/processors/mutate-processor.d.ts +1 -1
  26. package/dist/lib/processors/mutate-processor.d.ts.map +1 -1
  27. package/dist/lib/processors/validate-processor.d.ts +1 -1
  28. package/dist/lib/processors/validate-processor.d.ts.map +1 -1
  29. package/dist/lib/processors/watch-processor.d.ts +22 -1
  30. package/dist/lib/processors/watch-processor.d.ts.map +1 -1
  31. package/dist/lib/telemetry/metrics.d.ts +2 -3
  32. package/dist/lib/telemetry/metrics.d.ts.map +1 -1
  33. package/dist/lib/types.d.ts +45 -0
  34. package/dist/lib/types.d.ts.map +1 -1
  35. package/dist/lib.js +2019 -2019
  36. package/dist/lib.js.map +4 -4
  37. package/dist/sdk/sdk.d.ts +6 -1
  38. package/dist/sdk/sdk.d.ts.map +1 -1
  39. package/package.json +11 -8
  40. package/src/cli/build.helpers.ts +1 -1
  41. package/src/cli/build.ts +3 -11
  42. package/src/cli/dev.ts +1 -1
  43. package/src/cli/init/templates.ts +1 -1
  44. package/src/lib/assets/assets.ts +8 -8
  45. package/src/lib/assets/deploy.ts +4 -4
  46. package/src/lib/assets/destroy.ts +1 -1
  47. package/src/lib/assets/helm.ts +4 -4
  48. package/src/lib/assets/index.ts +2 -3
  49. package/src/lib/assets/networking.ts +3 -3
  50. package/src/lib/assets/pods.ts +5 -5
  51. package/src/lib/assets/webhooks.ts +5 -5
  52. package/src/lib/assets/yaml/generateAllYaml.ts +3 -3
  53. package/src/lib/assets/yaml/overridesFile.ts +4 -4
  54. package/src/lib/controller/index.ts +20 -19
  55. package/src/lib/core/capability.ts +1 -1
  56. package/src/lib/core/envChecks.ts +6 -0
  57. package/src/lib/core/module.ts +3 -62
  58. package/src/lib/processors/mutate-processor.ts +1 -1
  59. package/src/lib/processors/validate-processor.ts +1 -1
  60. package/src/lib/processors/watch-processor.ts +58 -33
  61. package/src/lib/telemetry/metrics.ts +4 -4
  62. package/src/lib/types.ts +48 -0
  63. package/src/sdk/sdk.ts +8 -4
@@ -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";
@@ -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";
@@ -4,11 +4,12 @@ import Log from "../telemetry/logger";
4
4
  import { Binding } from "../types";
5
5
  import { Capability } from "../core/capability";
6
6
  import { Event } from "../enums";
7
- import { K8s, KubernetesObject, WatchCfg, WatchEvent } from "kubernetes-fluent-client";
7
+ import { K8s, KubernetesObject, WatchCfg, WatchEvent, GenericClass } from "kubernetes-fluent-client";
8
8
  import { Queue } from "../core/queue";
9
- import { WatchPhase } from "kubernetes-fluent-client/dist/fluent/types";
9
+ import { WatchPhase, WatcherType } from "kubernetes-fluent-client/dist/fluent/types";
10
+ import { KubernetesListObject } from "kubernetes-fluent-client/dist/types";
10
11
  import { filterNoMatchReason } from "../filter/filter";
11
- import { metricsCollector } from "../telemetry/metrics";
12
+ import { metricsCollector, MetricsCollectorInstance } from "../telemetry/metrics";
12
13
  import { removeFinalizer } from "../finalizer";
13
14
 
14
15
  // stores Queue instances
@@ -157,36 +158,8 @@ async function runBinding(
157
158
  }
158
159
  }, watchCfg);
159
160
 
160
- // If failure continues, log and exit
161
- watcher.events.on(WatchEvent.GIVE_UP, err => {
162
- Log.error(err, "Watch failed after 5 attempts, giving up");
163
- process.exit(1);
164
- });
165
-
166
- watcher.events.on(WatchEvent.CONNECT, url => logEvent(WatchEvent.CONNECT, url));
167
-
168
- watcher.events.on(WatchEvent.DATA_ERROR, err => logEvent(WatchEvent.DATA_ERROR, err.message));
169
- watcher.events.on(WatchEvent.RECONNECT, retryCount =>
170
- logEvent(WatchEvent.RECONNECT, `Reconnecting after ${retryCount} attempt${retryCount === 1 ? "" : "s"}`),
171
- );
172
- watcher.events.on(WatchEvent.RECONNECT_PENDING, () => logEvent(WatchEvent.RECONNECT_PENDING));
173
- watcher.events.on(WatchEvent.GIVE_UP, err => logEvent(WatchEvent.GIVE_UP, err.message));
174
- watcher.events.on(WatchEvent.ABORT, err => logEvent(WatchEvent.ABORT, err.message));
175
- watcher.events.on(WatchEvent.OLD_RESOURCE_VERSION, err => logEvent(WatchEvent.OLD_RESOURCE_VERSION, err));
176
- watcher.events.on(WatchEvent.NETWORK_ERROR, err => logEvent(WatchEvent.NETWORK_ERROR, err.message));
177
- watcher.events.on(WatchEvent.LIST_ERROR, err => logEvent(WatchEvent.LIST_ERROR, err.message));
178
- watcher.events.on(WatchEvent.LIST, list => logEvent(WatchEvent.LIST, JSON.stringify(list, undefined, 2)));
179
- watcher.events.on(WatchEvent.CACHE_MISS, windowName => {
180
- metricsCollector.incCacheMiss(windowName);
181
- });
182
-
183
- watcher.events.on(WatchEvent.INIT_CACHE_MISS, windowName => {
184
- metricsCollector.initCacheMissWindow(windowName);
185
- });
186
-
187
- watcher.events.on(WatchEvent.INC_RESYNC_FAILURE_COUNT, retryCount => {
188
- metricsCollector.incRetryCount(retryCount);
189
- });
161
+ // Register event handlers
162
+ registerWatchEventHandlers(watcher, logEvent, metricsCollector);
190
163
 
191
164
  // Start the watch
192
165
  try {
@@ -205,3 +178,55 @@ export function logEvent(event: WatchEvent, message: string = "", obj?: Kubernet
205
178
  Log.debug(logMessage);
206
179
  }
207
180
  }
181
+
182
+ export type WatchEventArgs<K extends WatchEvent, T extends GenericClass> = {
183
+ [WatchEvent.LIST]: KubernetesListObject<InstanceType<T>>;
184
+ [WatchEvent.RECONNECT]: number;
185
+ [WatchEvent.CACHE_MISS]: string;
186
+ [WatchEvent.INIT_CACHE_MISS]: string;
187
+ [WatchEvent.GIVE_UP]: Error;
188
+ [WatchEvent.ABORT]: Error;
189
+ [WatchEvent.OLD_RESOURCE_VERSION]: string;
190
+ [WatchEvent.NETWORK_ERROR]: Error;
191
+ [WatchEvent.LIST_ERROR]: Error;
192
+ [WatchEvent.DATA_ERROR]: Error;
193
+ [WatchEvent.CONNECT]: string;
194
+ [WatchEvent.RECONNECT_PENDING]: undefined;
195
+ [WatchEvent.DATA]: undefined;
196
+ [WatchEvent.INC_RESYNC_FAILURE_COUNT]: number;
197
+ }[K];
198
+
199
+ export type LogEventFunction = (event: WatchEvent, message?: string) => void;
200
+ export function registerWatchEventHandlers(
201
+ watcher: WatcherType<GenericClass>,
202
+ logEvent: LogEventFunction,
203
+ metricsCollector: MetricsCollectorInstance,
204
+ ): void {
205
+ const eventHandlers: {
206
+ [K in WatchEvent]?: (arg: WatchEventArgs<K, GenericClass>) => void;
207
+ } = {
208
+ [WatchEvent.DATA]: () => null,
209
+ [WatchEvent.GIVE_UP]: err => {
210
+ // If failure continues, log and exit
211
+ logEvent(WatchEvent.GIVE_UP, err.message);
212
+ process.exit(1);
213
+ },
214
+ [WatchEvent.CONNECT]: url => logEvent(WatchEvent.CONNECT, url),
215
+ [WatchEvent.DATA_ERROR]: err => logEvent(WatchEvent.DATA_ERROR, err.message),
216
+ [WatchEvent.RECONNECT]: retryCount =>
217
+ logEvent(WatchEvent.RECONNECT, `Reconnecting after ${retryCount} attempt${retryCount === 1 ? "" : "s"}`),
218
+ [WatchEvent.RECONNECT_PENDING]: () => logEvent(WatchEvent.RECONNECT_PENDING),
219
+ [WatchEvent.ABORT]: err => logEvent(WatchEvent.ABORT, err.message),
220
+ [WatchEvent.OLD_RESOURCE_VERSION]: errMessage => logEvent(WatchEvent.OLD_RESOURCE_VERSION, errMessage),
221
+ [WatchEvent.NETWORK_ERROR]: err => logEvent(WatchEvent.NETWORK_ERROR, err.message),
222
+ [WatchEvent.LIST_ERROR]: err => logEvent(WatchEvent.LIST_ERROR, err.message),
223
+ [WatchEvent.LIST]: list => logEvent(WatchEvent.LIST, JSON.stringify(list, undefined, 2)),
224
+ [WatchEvent.CACHE_MISS]: windowName => metricsCollector.incCacheMiss(windowName),
225
+ [WatchEvent.INIT_CACHE_MISS]: windowName => metricsCollector.initCacheMissWindow(windowName),
226
+ [WatchEvent.INC_RESYNC_FAILURE_COUNT]: retryCount => metricsCollector.incRetryCount(retryCount),
227
+ };
228
+
229
+ Object.entries(eventHandlers).forEach(([event, handler]) => {
230
+ watcher.events.on(event, handler);
231
+ });
232
+ }
@@ -9,7 +9,7 @@ import Log from "./logger";
9
9
 
10
10
  const loggingPrefix = "MetricsCollector";
11
11
 
12
- type MetricsCollectorInstance = InstanceType<typeof MetricsCollector>;
12
+ export type MetricsCollectorInstance = InstanceType<typeof MetricsCollector>;
13
13
  interface MetricNames {
14
14
  errors: string;
15
15
  alerts: string;
@@ -54,7 +54,7 @@ export class MetricsCollector {
54
54
  this.#registry = new Registry();
55
55
  this.#prefix = prefix;
56
56
  this.addCounter(this.#metricNames.errors, "Mutation/Validate errors encountered");
57
- this.addCounter(this.#metricNames.alerts, "Mutation/Validate bad api token received");
57
+ this.addCounter(this.#metricNames.alerts, "Mutation/Validate bad api path received");
58
58
  this.addSummary(this.#metricNames.mutate, "Mutation operation summary");
59
59
  this.addSummary(this.#metricNames.validate, "Validation operation summary");
60
60
  this.addGauge(this.#metricNames.cacheMiss, "Number of cache misses per window", ["window"]);
@@ -148,8 +148,8 @@ export class MetricsCollector {
148
148
  * Increments the retry count gauge.
149
149
  * @param count - The count to increment by.
150
150
  */
151
- incRetryCount = (count: string): void => {
152
- this.incGauge(this.#metricNames.resyncFailureCount, { count });
151
+ incRetryCount = (count: number): void => {
152
+ this.incGauge(this.#metricNames.resyncFailureCount, { count: count.toString() });
153
153
  };
154
154
 
155
155
  /**
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
package/src/sdk/sdk.ts CHANGED
@@ -46,11 +46,15 @@ export function containers(
46
46
  export async function writeEvent(
47
47
  cr: GenericKind,
48
48
  event: Partial<kind.CoreEvent>,
49
- eventType: string,
50
- eventReason: string,
51
- reportingComponent: string,
52
- reportingInstance: string,
49
+ options: {
50
+ eventType: string;
51
+ eventReason: string;
52
+ reportingComponent: string;
53
+ reportingInstance: string;
54
+ },
53
55
  ): Promise<void> {
56
+ const { eventType, eventReason, reportingComponent, reportingInstance } = options;
57
+
54
58
  await K8s(kind.CoreEvent).Create({
55
59
  type: eventType,
56
60
  reason: eventReason,