pepr 0.2.9 → 0.3.0-rc0

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 (82) hide show
  1. package/README.md +10 -3
  2. package/dist/fixtures/data/cm1.json +75 -0
  3. package/dist/fixtures/data/deployment1.json +170 -0
  4. package/dist/fixtures/data/ns1.json +72 -0
  5. package/dist/fixtures/data/pod1.json +271 -0
  6. package/dist/fixtures/data/pod2.json +257 -0
  7. package/dist/fixtures/data/svc1.json +100 -0
  8. package/dist/fixtures/loader.js +60 -0
  9. package/dist/package.json +23 -41
  10. package/dist/src/cli/build.js +3 -1
  11. package/dist/src/cli/dev.js +31 -19
  12. package/dist/src/cli/index.js +1 -0
  13. package/dist/src/cli/init/index.js +3 -1
  14. package/dist/src/cli/init/templates.js +3 -2
  15. package/dist/src/cli/init/utils.js +1 -1
  16. package/dist/src/cli/init/utils.test.js +29 -0
  17. package/dist/src/cli/init/walkthrough.js +1 -1
  18. package/dist/src/cli/init/walkthrough.test.js +21 -0
  19. package/dist/src/cli/run.js +17 -17
  20. package/dist/src/cli/update.js +3 -1
  21. package/dist/src/lib/capability.js +1 -1
  22. package/dist/src/lib/controller.js +9 -1
  23. package/dist/src/lib/fetch.js +39 -6
  24. package/dist/src/lib/fetch.test.js +98 -0
  25. package/dist/src/lib/filter.test.js +208 -0
  26. package/dist/src/lib/k8s/kinds.test.js +296 -0
  27. package/dist/src/lib/k8s/webhook.js +22 -22
  28. package/dist/src/lib/logger.test.js +64 -0
  29. package/dist/src/lib/processor.js +4 -1
  30. package/{dist/index.d.ts → index.ts} +21 -3
  31. package/package.json +23 -41
  32. package/src/lib/capability.ts +158 -0
  33. package/src/lib/controller.ts +127 -0
  34. package/src/lib/fetch.test.ts +115 -0
  35. package/src/lib/fetch.ts +75 -0
  36. package/src/lib/filter.test.ts +231 -0
  37. package/src/lib/filter.ts +87 -0
  38. package/{dist/src/lib/k8s/index.d.ts → src/lib/k8s/index.ts} +6 -0
  39. package/src/lib/k8s/kinds.test.ts +333 -0
  40. package/src/lib/k8s/kinds.ts +489 -0
  41. package/src/lib/k8s/tls.ts +90 -0
  42. package/src/lib/k8s/types.ts +183 -0
  43. package/src/lib/k8s/upstream.ts +49 -0
  44. package/src/lib/k8s/webhook.ts +547 -0
  45. package/src/lib/logger.test.ts +80 -0
  46. package/src/lib/logger.ts +136 -0
  47. package/src/lib/module.ts +63 -0
  48. package/src/lib/processor.ts +98 -0
  49. package/src/lib/request.ts +140 -0
  50. package/src/lib/types.ts +211 -0
  51. package/dist/cli.d.ts +0 -2
  52. package/dist/cli.js +0 -4
  53. package/dist/run.d.ts +0 -2
  54. package/dist/run.js +0 -4
  55. package/dist/src/cli/banner.d.ts +0 -1
  56. package/dist/src/cli/build.d.ts +0 -7
  57. package/dist/src/cli/capability.d.ts +0 -2
  58. package/dist/src/cli/deploy.d.ts +0 -2
  59. package/dist/src/cli/dev.d.ts +0 -2
  60. package/dist/src/cli/index.d.ts +0 -1
  61. package/dist/src/cli/init/index.d.ts +0 -2
  62. package/dist/src/cli/init/templates.d.ts +0 -94
  63. package/dist/src/cli/init/utils.d.ts +0 -20
  64. package/dist/src/cli/init/walkthrough.d.ts +0 -7
  65. package/dist/src/cli/root.d.ts +0 -4
  66. package/dist/src/cli/run.d.ts +0 -1
  67. package/dist/src/cli/test.d.ts +0 -2
  68. package/dist/src/cli/update.d.ts +0 -2
  69. package/dist/src/lib/capability.d.ts +0 -28
  70. package/dist/src/lib/controller.d.ts +0 -17
  71. package/dist/src/lib/fetch.d.ts +0 -23
  72. package/dist/src/lib/filter.d.ts +0 -10
  73. package/dist/src/lib/k8s/kinds.d.ts +0 -11
  74. package/dist/src/lib/k8s/tls.d.ts +0 -17
  75. package/dist/src/lib/k8s/types.d.ts +0 -147
  76. package/dist/src/lib/k8s/upstream.d.ts +0 -3
  77. package/dist/src/lib/k8s/webhook.d.ts +0 -34
  78. package/dist/src/lib/logger.d.ts +0 -55
  79. package/dist/src/lib/module.d.ts +0 -32
  80. package/dist/src/lib/processor.d.ts +0 -4
  81. package/dist/src/lib/request.d.ts +0 -77
  82. package/dist/src/lib/types.d.ts +0 -187
@@ -0,0 +1,136 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // SPDX-FileCopyrightText: 2023-Present The Pepr Authors
3
+
4
+ /**
5
+ * Enumeration representing different logging levels.
6
+ */
7
+ export enum LogLevel {
8
+ debug = 0,
9
+ info = 1,
10
+ warn = 2,
11
+ error = 3,
12
+ }
13
+
14
+ enum ConsoleColors {
15
+ Reset = "\x1b[0m",
16
+ Bright = "\x1b[1m",
17
+ Dim = "\x1b[2m",
18
+ Underscore = "\x1b[4m",
19
+ Blink = "\x1b[5m",
20
+ Reverse = "\x1b[7m",
21
+ Hidden = "\x1b[8m",
22
+
23
+ FgBlack = "\x1b[30m",
24
+ FgRed = "\x1b[31m",
25
+ FgGreen = "\x1b[32m",
26
+ FgYellow = "\x1b[33m",
27
+ FgBlue = "\x1b[34m",
28
+ FgMagenta = "\x1b[35m",
29
+ FgCyan = "\x1b[36m",
30
+ FgWhite = "\x1b[37m",
31
+
32
+ BgBlack = "\x1b[40m",
33
+ BgRed = "\x1b[41m",
34
+ BgGreen = "\x1b[42m",
35
+ BgYellow = "\x1b[43m",
36
+ BgBlue = "\x1b[44m",
37
+ BgMagenta = "\x1b[45m",
38
+ BgCyan = "\x1b[46m",
39
+ BgWhite = "\x1b[47m",
40
+ }
41
+
42
+ /**
43
+ * Simple logger class that logs messages at different log levels.
44
+ */
45
+ export class Logger {
46
+ private _logLevel: LogLevel;
47
+
48
+ /**
49
+ * Create a new logger instance.
50
+ * @param logLevel - The minimum log level to log messages for.
51
+ */
52
+ constructor(logLevel: LogLevel) {
53
+ this._logLevel = logLevel;
54
+ }
55
+
56
+ /**
57
+ * Change the log level of the logger.
58
+ * @param logLevel - The log level to log the message at.
59
+ */
60
+ public SetLogLevel(logLevel: string): void {
61
+ this._logLevel = LogLevel[logLevel as keyof typeof LogLevel];
62
+ this.debug(`Log level set to ${logLevel}`);
63
+ }
64
+
65
+ /**
66
+ * Log a debug message.
67
+ * @param message - The message to log.
68
+ */
69
+ public debug<T>(message: T, prefix?: string): void {
70
+ this.log(LogLevel.debug, message, prefix);
71
+ }
72
+
73
+ /**
74
+ * Log an info message.
75
+ * @param message - The message to log.
76
+ */
77
+ public info<T>(message: T, prefix?: string): void {
78
+ this.log(LogLevel.info, message, prefix);
79
+ }
80
+
81
+ /**
82
+ * Log a warning message.
83
+ * @param message - The message to log.
84
+ */
85
+ public warn<T>(message: T, prefix?: string): void {
86
+ this.log(LogLevel.warn, message, prefix);
87
+ }
88
+
89
+ /**
90
+ * Log an error message.
91
+ * @param message - The message to log.
92
+ */
93
+ public error<T>(message: T, prefix?: string): void {
94
+ this.log(LogLevel.error, message, prefix);
95
+ }
96
+
97
+ /**
98
+ * Log a message at the specified log level.
99
+ * @param logLevel - The log level of the message.
100
+ * @param message - The message to log.
101
+ */
102
+ private log<T>(logLevel: LogLevel, message: T, callerPrefix = ""): void {
103
+ const color = {
104
+ [LogLevel.debug]: ConsoleColors.FgBlack,
105
+ [LogLevel.info]: ConsoleColors.FgCyan,
106
+ [LogLevel.warn]: ConsoleColors.FgYellow,
107
+ [LogLevel.error]: ConsoleColors.FgRed,
108
+ };
109
+
110
+ if (logLevel >= this._logLevel) {
111
+ // Prefix the message with the colored log level.
112
+ let prefix = "[" + LogLevel[logLevel] + "]\t" + callerPrefix;
113
+
114
+ prefix = this.colorize(prefix, color[logLevel]);
115
+
116
+ // If the message is not a string, use the debug method to log the object.
117
+ if (typeof message !== "string") {
118
+ console.log(prefix);
119
+ console.debug("%o", message);
120
+ } else {
121
+ console.log(prefix + "\t" + message);
122
+ }
123
+ }
124
+ }
125
+
126
+ private colorize(text: string, color: ConsoleColors): string {
127
+ return color + text + ConsoleColors.Reset;
128
+ }
129
+ }
130
+
131
+ /** Log is an instance of Logger used to generate log entries. */
132
+ const Log = new Logger(LogLevel.info);
133
+ if (process.env.LOG_LEVEL) {
134
+ Log.SetLogLevel(process.env.LOG_LEVEL);
135
+ }
136
+ export default Log;
@@ -0,0 +1,63 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // SPDX-FileCopyrightText: 2023-Present The Pepr Authors
3
+
4
+ import R from "ramda";
5
+ import { Capability } from "./capability";
6
+ import { Controller } from "./controller";
7
+ import { Request, Response } from "./k8s/types";
8
+ import { ModuleConfig } from "./types";
9
+
10
+ const alwaysIgnore = {
11
+ namespaces: ["kube-system", "pepr-system"],
12
+ labels: [{ "pepr.dev": "ignore" }],
13
+ };
14
+
15
+ export type PackageJSON = {
16
+ description: string;
17
+ pepr: ModuleConfig;
18
+ };
19
+
20
+ export type PeprModuleOptions = {
21
+ deferStart?: boolean;
22
+
23
+ /** A user-defined callback to pre-process or intercept a Pepr request from K8s immediately before it is processed */
24
+ beforeHook?: (req: Request) => void;
25
+
26
+ /** A user-defined callback to post-process or intercept a Pepr response just before it is returned to K8s */
27
+ afterHook?: (res: Response) => void;
28
+ };
29
+
30
+ export class PeprModule {
31
+ private _controller: Controller;
32
+
33
+ /**
34
+ * Create a new Pepr runtime
35
+ *
36
+ * @param config The configuration for the Pepr runtime
37
+ * @param capabilities The capabilities to be loaded into the Pepr runtime
38
+ * @param _deferStart (optional) If set to `true`, the Pepr runtime will not be started automatically. This can be used to start the Pepr runtime manually with `start()`.
39
+ */
40
+ constructor({ description, pepr }: PackageJSON, capabilities: Capability[] = [], opts: PeprModuleOptions = {}) {
41
+ const config: ModuleConfig = R.mergeDeepWith(R.concat, pepr, alwaysIgnore);
42
+ config.description = description;
43
+
44
+ this._controller = new Controller(config, capabilities, opts.beforeHook, opts.afterHook);
45
+
46
+ // Stop processing if deferStart is set to true
47
+ if (opts.deferStart) {
48
+ return;
49
+ }
50
+
51
+ this.start();
52
+ }
53
+
54
+ /**
55
+ * Start the Pepr runtime manually.
56
+ * Normally this is called automatically when the Pepr module is instantiated, but can be called manually if `deferStart` is set to `true` in the constructor.
57
+ *
58
+ * @param port
59
+ */
60
+ start(port = 3000) {
61
+ this._controller.startServer(port);
62
+ }
63
+ }
@@ -0,0 +1,98 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // SPDX-FileCopyrightText: 2023-Present The Pepr Authors
3
+
4
+ import { compare } from "fast-json-patch";
5
+ import { Capability } from "./capability";
6
+ import { shouldSkipRequest } from "./filter";
7
+ import { Request, Response } from "./k8s/types";
8
+ import logger from "./logger";
9
+ import { PeprRequest } from "./request";
10
+ import { ModuleConfig } from "./types";
11
+
12
+ export async function processor(config: ModuleConfig, capabilities: Capability[], req: Request): Promise<Response> {
13
+ const wrapped = new PeprRequest(req);
14
+ const response: Response = {
15
+ uid: req.uid,
16
+ warnings: [],
17
+ allowed: false,
18
+ };
19
+
20
+ logger.info(`Processing '${req.uid}' for '${req.kind.kind}' '${req.name}'`);
21
+
22
+ for (const { name, bindings } of capabilities) {
23
+ const prefix = `${req.uid} ${req.name}: ${name}`;
24
+ logger.info(`Processing capability ${name}`, prefix);
25
+
26
+ for (const action of bindings) {
27
+ // Continue to the next action without doing anything if this one should be skipped
28
+ if (shouldSkipRequest(action, req)) {
29
+ continue;
30
+ }
31
+
32
+ logger.info(`Processing matched action ${action.kind.kind}`, prefix);
33
+
34
+ // Add annotations to the request to indicate that the capability started processing
35
+ // this will allow tracking of failed mutations that were permitted to continue
36
+ const updateStatus = (status: string) => {
37
+ // Only update the status if the request is a CREATE or UPDATE (we don't use CONNECT)
38
+ if (req.operation == "DELETE") {
39
+ return;
40
+ }
41
+
42
+ const identifier = `${config.uuid}.pepr.dev/${name}`;
43
+ wrapped.Raw.metadata = wrapped.Raw.metadata || {};
44
+ wrapped.Raw.metadata.annotations = wrapped.Raw.metadata.annotations || {};
45
+ wrapped.Raw.metadata.annotations[identifier] = status;
46
+ };
47
+
48
+ updateStatus("started");
49
+
50
+ try {
51
+ // Run the action
52
+ await action.callback(wrapped);
53
+
54
+ logger.info(`Action succeeded`, prefix);
55
+
56
+ // Add annotations to the request to indicate that the capability succeeded
57
+ updateStatus("succeeded");
58
+ } catch (e) {
59
+ // Annoying ts false positive
60
+ response.warnings = response.warnings || [];
61
+ response.warnings.push(`Action failed: ${e}`);
62
+
63
+ // If errors are not allowed, note the failure in the Response
64
+ if (config.onError) {
65
+ logger.error(`Action failed: ${e}`, prefix);
66
+ response.result = "Pepr module configured to reject on error";
67
+ return response;
68
+ } else {
69
+ logger.warn(`Action failed: ${e}`, prefix);
70
+ updateStatus("warning");
71
+ }
72
+ }
73
+ }
74
+ }
75
+
76
+ // If we've made it this far, the request is allowed
77
+ response.allowed = true;
78
+
79
+ // Compare the original request to the modified request to get the patches
80
+ const patches = compare(req.object, wrapped.Raw);
81
+
82
+ // Only add the patch if there are patches to apply
83
+ if (patches.length > 0) {
84
+ response.patchType = "JSONPatch";
85
+ // Webhook must be base64-encoded
86
+ // https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/#response
87
+ response.patch = Buffer.from(JSON.stringify(patches)).toString("base64");
88
+ }
89
+
90
+ // Remove the warnings array if it's empty
91
+ if (response.warnings && response.warnings.length < 1) {
92
+ delete response.warnings;
93
+ }
94
+
95
+ logger.debug(patches);
96
+
97
+ return response;
98
+ }
@@ -0,0 +1,140 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // SPDX-FileCopyrightText: 2023-Present The Pepr Authors
3
+
4
+ import R from "ramda";
5
+ import { KubernetesObject, Request } from "./k8s";
6
+ import { DeepPartial } from "./types";
7
+
8
+ /**
9
+ * The RequestWrapper class provides methods to modify Kubernetes objects in the context
10
+ * of a mutating webhook request.
11
+ */
12
+ export class PeprRequest<T extends KubernetesObject> {
13
+ private _input: Request<T>;
14
+
15
+ public Raw: T;
16
+
17
+ get PermitSideEffects() {
18
+ return !this._input.dryRun;
19
+ }
20
+
21
+ /**
22
+ * Indicates whether the request is a dry run.
23
+ * @returns true if the request is a dry run, false otherwise.
24
+ */
25
+ get IsDryRun() {
26
+ return this._input.dryRun;
27
+ }
28
+
29
+ /**
30
+ * Provides access to the old resource in the request if available.
31
+ * @returns The old Kubernetes resource object or null if not available.
32
+ */
33
+ get OldResource() {
34
+ return this._input.oldObject;
35
+ }
36
+
37
+ /**
38
+ * Provides access to the request object.
39
+ * @returns The request object containing the Kubernetes resource.
40
+ */
41
+ get Request() {
42
+ return this._input;
43
+ }
44
+
45
+ /**
46
+ * Creates a new instance of the Action class.
47
+ * @param input - The request object containing the Kubernetes resource to modify.
48
+ */
49
+ constructor(input: Request<T>) {
50
+ // Deep clone the object to prevent mutation of the original object
51
+ this.Raw = R.clone(input.object);
52
+ // Store the input
53
+ this._input = input;
54
+ }
55
+
56
+ /**
57
+ * Deep merges the provided object with the current resource.
58
+ *
59
+ * @param obj - The object to merge with the current resource.
60
+ */
61
+ Merge(obj: DeepPartial<T>) {
62
+ this.Raw = R.mergeDeepRight(this.Raw, obj) as unknown as T;
63
+ }
64
+
65
+ /**
66
+ * Updates a label on the Kubernetes resource.
67
+ * @param key - The key of the label to update.
68
+ * @param value - The value of the label.
69
+ * @returns The current Action instance for method chaining.
70
+ */
71
+ SetLabel(key: string, value: string) {
72
+ const ref = this.Raw;
73
+
74
+ ref.metadata = ref.metadata ?? {};
75
+ ref.metadata.labels = ref.metadata.labels ?? {};
76
+ ref.metadata.labels[key] = value;
77
+
78
+ return this;
79
+ }
80
+
81
+ /**
82
+ * Updates an annotation on the Kubernetes resource.
83
+ * @param key - The key of the annotation to update.
84
+ * @param value - The value of the annotation.
85
+ * @returns The current Action instance for method chaining.
86
+ */
87
+ SetAnnotation(key: string, value: string) {
88
+ const ref = this.Raw;
89
+
90
+ ref.metadata = ref.metadata ?? {};
91
+ ref.metadata.annotations = ref.metadata.annotations ?? {};
92
+ ref.metadata.annotations[key] = value;
93
+
94
+ return this;
95
+ }
96
+
97
+ /**
98
+ * Removes a label from the Kubernetes resource.
99
+ * @param key - The key of the label to remove.
100
+ * @returns The current Action instance for method chaining.
101
+ */
102
+ RemoveLabel(key: string) {
103
+ if (this.Raw.metadata?.labels?.[key]) {
104
+ delete this.Raw.metadata.labels[key];
105
+ }
106
+ return this;
107
+ }
108
+
109
+ /**
110
+ * Removes an annotation from the Kubernetes resource.
111
+ * @param key - The key of the annotation to remove.
112
+ * @returns The current Action instance for method chaining.
113
+ */
114
+ RemoveAnnotation(key: string) {
115
+ if (this.Raw.metadata?.annotations?.[key]) {
116
+ delete this.Raw.metadata.annotations[key];
117
+ }
118
+ return this;
119
+ }
120
+
121
+ /**
122
+ * Check if a label exists on the Kubernetes resource.
123
+ *
124
+ * @param key the label key to check
125
+ * @returns
126
+ */
127
+ HasLabel(key: string) {
128
+ return this.Raw?.metadata?.labels?.[key] !== undefined;
129
+ }
130
+
131
+ /**
132
+ * Check if an annotation exists on the Kubernetes resource.
133
+ *
134
+ * @param key the annotation key to check
135
+ * @returns
136
+ */
137
+ HasAnnotation(key: string) {
138
+ return this.Raw?.metadata?.annotations?.[key] !== undefined;
139
+ }
140
+ }
@@ -0,0 +1,211 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // SPDX-FileCopyrightText: 2023-Present The Pepr Authors
3
+
4
+ import { GroupVersionKind, KubernetesObject, WebhookIgnore } from "./k8s";
5
+ import { PeprRequest } from "./request";
6
+
7
+ /**
8
+ * The behavior of this module when an error occurs.
9
+ */
10
+ export enum ErrorBehavior {
11
+ ignore = "ignore",
12
+ audit = "audit",
13
+ reject = "reject",
14
+ }
15
+
16
+ /**
17
+ * The phase of the Kubernetes admission webhook that the capability is registered for.
18
+ *
19
+ * Currently only `mutate` is supported.
20
+ */
21
+ export enum HookPhase {
22
+ mutate = "mutate",
23
+ validate = "validate",
24
+ }
25
+
26
+ /**
27
+ * Recursively make all properties in T optional.
28
+ */
29
+ export type DeepPartial<T> = {
30
+ [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
31
+ };
32
+
33
+ /**
34
+ * The type of Kubernetes mutating webhook event that the capability action is registered for.
35
+ */
36
+
37
+ export enum Event {
38
+ Create = "CREATE",
39
+ Update = "UPDATE",
40
+ Delete = "DELETE",
41
+ CreateOrUpdate = "CREATEORUPDATE",
42
+ }
43
+
44
+ export interface CapabilityCfg {
45
+ /**
46
+ * The name of the capability. This should be unique.
47
+ */
48
+ name: string;
49
+ /**
50
+ * A description of the capability and what it does.
51
+ */
52
+ description: string;
53
+ /**
54
+ * List of namespaces that this capability applies to, if empty, applies to all namespaces (cluster-wide).
55
+ * This does not supersede the `alwaysIgnore` global configuration.
56
+ */
57
+ namespaces?: string[];
58
+
59
+ /**
60
+ * FUTURE USE.
61
+ *
62
+ * Declare if this capability should be used for mutation or validation. Currently this is not used
63
+ * and everything is considered a mutation.
64
+ */
65
+ mutateOrValidate?: HookPhase;
66
+ }
67
+
68
+ export type ModuleSigning = {
69
+ /**
70
+ * Specifies the signing policy.
71
+ * "requireAuthorizedKey" - only authorized keys are accepted.
72
+ * "requireAnyKey" - any key is accepted, as long as it's valid.
73
+ * "none" - no signing required.
74
+ */
75
+ signingPolicy?: "requireAuthorizedKey" | "requireAnyKey" | "none";
76
+ /**
77
+ * List of authorized keys for the "requireAuthorizedKey" policy.
78
+ * These keys are allowed to sign Pepr capabilities.
79
+ */
80
+ authorizedKeys?: string[];
81
+ };
82
+
83
+ /** Global configuration for the Pepr runtime. */
84
+ export type ModuleConfig = {
85
+ /** The user-defined name for the module */
86
+ name: string;
87
+ /** The version of Pepr that the module was originally generated with */
88
+ version?: string;
89
+ /** A unique identifier for this Pepr module. This is automatically generated by Pepr. */
90
+ uuid: string;
91
+ /** A description of the Pepr module and what it does. */
92
+ description?: string;
93
+ /** Reject K8s resource AdmissionRequests on error. */
94
+ onError: ErrorBehavior | string;
95
+ /** Configure global exclusions that will never be processed by Pepr. */
96
+ alwaysIgnore: WebhookIgnore;
97
+ /**
98
+ * FUTURE USE.
99
+ *
100
+ * Configure the signing policy for Pepr capabilities.
101
+ * This setting determines the requirements for signing keys in Pepr.
102
+ */
103
+ signing?: ModuleSigning;
104
+ };
105
+
106
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
107
+ export type GenericClass = abstract new () => any;
108
+
109
+ export type WhenSelector<T extends GenericClass> = {
110
+ /** Register a capability action to be executed when a Kubernetes resource is created or updated. */
111
+ IsCreatedOrUpdated: () => BindingAll<T>;
112
+ /** Register a capability action to be executed when a Kubernetes resource is created. */
113
+ IsCreated: () => BindingAll<T>;
114
+ /** Register a capability action to be executed when a Kubernetes resource is updated. */
115
+ IsUpdated: () => BindingAll<T>;
116
+ /** Register a capability action to be executed when a Kubernetes resource is deleted. */
117
+ IsDeleted: () => BindingAll<T>;
118
+ };
119
+
120
+ export type Binding = {
121
+ event?: Event;
122
+ readonly kind: GroupVersionKind;
123
+ readonly filters: {
124
+ name: string;
125
+ namespaces: string[];
126
+ labels: Record<string, string>;
127
+ annotations: Record<string, string>;
128
+ };
129
+ readonly callback: CapabilityAction<GenericClass, InstanceType<GenericClass>>;
130
+ };
131
+
132
+ export type BindingFilter<T extends GenericClass> = BindToActionOrSet<T> & {
133
+ /**
134
+ * Only apply the capability action if the resource has the specified label. If no value is specified, the label must exist.
135
+ * Note multiple calls to this method will result in an AND condition. e.g.
136
+ *
137
+ * ```ts
138
+ * When(a.Deployment)
139
+ * .IsCreated()
140
+ * .WithLabel("foo", "bar")
141
+ * .WithLabel("baz", "qux")
142
+ * .Then(...)
143
+ * ```
144
+ *
145
+ * Will only apply the capability action if the resource has both the `foo=bar` and `baz=qux` labels.
146
+ *
147
+ * @param key
148
+ * @param value
149
+ */
150
+ WithLabel: (key: string, value?: string) => BindingFilter<T>;
151
+ /**
152
+ * Only apply the capability action if the resource has the specified annotation. If no value is specified, the annotation must exist.
153
+ * Note multiple calls to this method will result in an AND condition. e.g.
154
+ *
155
+ * ```ts
156
+ * When(a.Deployment)
157
+ * .IsCreated()
158
+ * .WithAnnotation("foo", "bar")
159
+ * .WithAnnotation("baz", "qux")
160
+ * .Then(...)
161
+ * ```
162
+ *
163
+ * Will only apply the capability action if the resource has both the `foo=bar` and `baz=qux` annotations.
164
+ *
165
+ * @param key
166
+ * @param value
167
+ */
168
+ WithAnnotation: (key: string, value?: string) => BindingFilter<T>;
169
+ };
170
+
171
+ export type BindingWithName<T extends GenericClass> = BindingFilter<T> & {
172
+ /** Only apply the capability action if the resource name matches the specified name. */
173
+ WithName: (name: string) => BindingFilter<T>;
174
+ };
175
+
176
+ export type BindingAll<T extends GenericClass> = BindingWithName<T> & {
177
+ /** Only apply the capability action if the resource is in one of the specified namespaces.*/
178
+ InNamespace: (...namespaces: string[]) => BindingFilter<T>;
179
+ };
180
+
181
+ export type BindToAction<T extends GenericClass> = {
182
+ /**
183
+ * Create a new capability action with the specified callback function and previously specified
184
+ * filters.
185
+ * @param action The capability action to be executed when the Kubernetes resource is processed by the AdmissionController.
186
+ */
187
+ Then: (action: CapabilityAction<T, InstanceType<T>>) => BindToAction<T>;
188
+ };
189
+
190
+ export type BindToActionOrSet<T extends GenericClass> = BindToAction<T> & {
191
+ /**
192
+ * Merge the specified updates into the resource, this can only be used once per binding.
193
+ * Note this is just a convenience method for `request.Merge(values)`.
194
+ *
195
+ * Example change the `minReadySeconds` to 3 of a deployment when it is created:
196
+ *
197
+ * ```ts
198
+ * When(a.Deployment)
199
+ * .IsCreated()
200
+ * .ThenSet({ spec: { minReadySeconds: 3 } });
201
+ * ```
202
+ *
203
+ * @param merge
204
+ * @returns
205
+ */
206
+ ThenSet: (val: DeepPartial<InstanceType<T>>) => BindToAction<T>;
207
+ };
208
+
209
+ export type CapabilityAction<T extends GenericClass, K extends KubernetesObject = InstanceType<T>> = (
210
+ req: PeprRequest<K>
211
+ ) => Promise<void> | void | Promise<PeprRequest<K>> | PeprRequest<K>;
package/dist/cli.d.ts DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- import "./src/cli";
package/dist/cli.js DELETED
@@ -1,4 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- require("./src/cli");
package/dist/run.d.ts DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- import "./src/cli/run";
package/dist/run.js DELETED
@@ -1,4 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- require("./src/cli/run");