pepr 0.0.0-development

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 (58) hide show
  1. package/.prettierignore +1 -0
  2. package/CODE_OF_CONDUCT.md +133 -0
  3. package/LICENSE +201 -0
  4. package/README.md +151 -0
  5. package/SECURITY.md +18 -0
  6. package/SUPPORT.md +16 -0
  7. package/codecov.yaml +19 -0
  8. package/commitlint.config.js +1 -0
  9. package/package.json +70 -0
  10. package/src/cli.ts +48 -0
  11. package/src/lib/assets/deploy.ts +122 -0
  12. package/src/lib/assets/destroy.ts +33 -0
  13. package/src/lib/assets/helm.ts +219 -0
  14. package/src/lib/assets/index.ts +175 -0
  15. package/src/lib/assets/loader.ts +41 -0
  16. package/src/lib/assets/networking.ts +89 -0
  17. package/src/lib/assets/pods.ts +353 -0
  18. package/src/lib/assets/rbac.ts +111 -0
  19. package/src/lib/assets/store.ts +49 -0
  20. package/src/lib/assets/webhooks.ts +147 -0
  21. package/src/lib/assets/yaml.ts +234 -0
  22. package/src/lib/capability.ts +314 -0
  23. package/src/lib/controller/index.ts +326 -0
  24. package/src/lib/controller/store.ts +219 -0
  25. package/src/lib/errors.ts +20 -0
  26. package/src/lib/filter.ts +110 -0
  27. package/src/lib/helpers.ts +342 -0
  28. package/src/lib/included-files.ts +19 -0
  29. package/src/lib/k8s.ts +169 -0
  30. package/src/lib/logger.ts +27 -0
  31. package/src/lib/metrics.ts +120 -0
  32. package/src/lib/module.ts +136 -0
  33. package/src/lib/mutate-processor.ts +160 -0
  34. package/src/lib/mutate-request.ts +153 -0
  35. package/src/lib/queue.ts +89 -0
  36. package/src/lib/schedule.ts +175 -0
  37. package/src/lib/storage.ts +192 -0
  38. package/src/lib/tls.ts +90 -0
  39. package/src/lib/types.ts +215 -0
  40. package/src/lib/utils.ts +57 -0
  41. package/src/lib/validate-processor.ts +80 -0
  42. package/src/lib/validate-request.ts +102 -0
  43. package/src/lib/watch-processor.ts +124 -0
  44. package/src/lib.ts +27 -0
  45. package/src/runtime/controller.ts +75 -0
  46. package/src/sdk/sdk.ts +116 -0
  47. package/src/templates/.eslintrc.template.json +18 -0
  48. package/src/templates/.prettierrc.json +13 -0
  49. package/src/templates/README.md +21 -0
  50. package/src/templates/capabilities/hello-pepr.samples.json +160 -0
  51. package/src/templates/capabilities/hello-pepr.ts +426 -0
  52. package/src/templates/gitignore +4 -0
  53. package/src/templates/package.json +20 -0
  54. package/src/templates/pepr.code-snippets.json +21 -0
  55. package/src/templates/pepr.ts +17 -0
  56. package/src/templates/settings.json +10 -0
  57. package/src/templates/tsconfig.json +9 -0
  58. package/src/templates/tsconfig.module.json +19 -0
@@ -0,0 +1,426 @@
1
+ import {
2
+ Capability,
3
+ K8s,
4
+ Log,
5
+ PeprMutateRequest,
6
+ RegisterKind,
7
+ a,
8
+ fetch,
9
+ fetchStatus,
10
+ kind,
11
+ } from "pepr";
12
+
13
+ /**
14
+ * The HelloPepr Capability is an example capability to demonstrate some general concepts of Pepr.
15
+ * To test this capability you run `pepr dev`and then run the following command:
16
+ * `kubectl apply -f capabilities/hello-pepr.samples.yaml`
17
+ */
18
+ export const HelloPepr = new Capability({
19
+ name: "hello-pepr",
20
+ description: "A simple example capability to show how things work.",
21
+ namespaces: ["pepr-demo", "pepr-demo-2"],
22
+ });
23
+
24
+ // Use the 'When' function to create a new action, use 'Store' to persist data
25
+ const { When, Store } = HelloPepr;
26
+
27
+ /**
28
+ * ---------------------------------------------------------------------------------------------------
29
+ * Mutate Action (Namespace) *
30
+ * ---------------------------------------------------------------------------------------------------
31
+ *
32
+ * This action removes the label `remove-me` when a Namespace is created.
33
+ * Note we don't need to specify the namespace here, because we've already specified
34
+ * it in the Capability definition above.
35
+ */
36
+ When(a.Namespace)
37
+ .IsCreated()
38
+ .Mutate(ns => ns.RemoveLabel("remove-me"));
39
+
40
+ /**
41
+ * ---------------------------------------------------------------------------------------------------
42
+ * Watch Action with K8s SSA (Namespace) *
43
+ * ---------------------------------------------------------------------------------------------------
44
+ *
45
+ * This action watches for the `pepr-demo-2` namespace to be created, then creates a ConfigMap with
46
+ * the name `pepr-ssa-demo` and adds the namespace UID to the ConfigMap data. Because Pepr uses
47
+ * server-side apply for this operation, the ConfigMap will be created or updated if it already exists.
48
+ */
49
+ When(a.Namespace)
50
+ .IsCreated()
51
+ .WithName("pepr-demo-2")
52
+ .Watch(async ns => {
53
+ Log.info("Namespace pepr-demo-2 was created.");
54
+
55
+ try {
56
+ // Apply the ConfigMap using K8s server-side apply
57
+ await K8s(kind.ConfigMap).Apply({
58
+ metadata: {
59
+ name: "pepr-ssa-demo",
60
+ namespace: "pepr-demo-2",
61
+ },
62
+ data: {
63
+ "ns-uid": ns.metadata.uid,
64
+ },
65
+ });
66
+ } catch (error) {
67
+ // You can use the Log object to log messages to the Pepr controller pod
68
+ Log.error(error, "Failed to apply ConfigMap using server-side apply.");
69
+ }
70
+
71
+ // You can share data between actions using the Store, including between different types of actions
72
+ Store.setItem("watch-data", "This data was stored by a Watch Action.");
73
+ });
74
+
75
+ /**
76
+ * ---------------------------------------------------------------------------------------------------
77
+ * Mutate Action (CM Example 1) *
78
+ * ---------------------------------------------------------------------------------------------------
79
+ *
80
+ * This is a single action. They can be in the same file or put imported from other files.
81
+ * In this example, when a ConfigMap is created with the name `example-1`, then add a label and annotation.
82
+ *
83
+ * Equivalent to manually running:
84
+ * `kubectl label configmap example-1 pepr=was-here`
85
+ * `kubectl annotate configmap example-1 pepr.dev=annotations-work-too`
86
+ */
87
+ When(a.ConfigMap)
88
+ .IsCreated()
89
+ .WithName("example-1")
90
+ .Mutate(request => {
91
+ request
92
+ .SetLabel("pepr", "was-here")
93
+ .SetAnnotation("pepr.dev", "annotations-work-too");
94
+
95
+ // Use the Store to persist data between requests and Pepr controller pods
96
+ Store.setItem("example-1", "was-here");
97
+
98
+ // This data is written asynchronously and can be read back via `Store.getItem()` or `Store.subscribe()`
99
+ Store.setItem("example-1-data", JSON.stringify(request.Raw.data));
100
+ });
101
+
102
+ /**
103
+ * ---------------------------------------------------------------------------------------------------
104
+ * Mutate & Validate Actions (CM Example 2) *
105
+ * ---------------------------------------------------------------------------------------------------
106
+ *
107
+ * This combines 3 different types of actions: 'Mutate', 'Validate', and 'Watch'. The order
108
+ * of the actions is required, but each action is optional. In this example, when a ConfigMap is created
109
+ * with the name `example-2`, then add a label and annotation, validate that the ConfigMap has the label
110
+ * `pepr`, and log the request.
111
+ */
112
+ When(a.ConfigMap)
113
+ .IsCreated()
114
+ .WithName("example-2")
115
+ .Mutate(request => {
116
+ // This Mutate Action will mutate the request before it is persisted to the cluster
117
+
118
+ // Use `request.Merge()` to merge the new data with the existing data
119
+ request.Merge({
120
+ metadata: {
121
+ labels: {
122
+ pepr: "was-here",
123
+ },
124
+ annotations: {
125
+ "pepr.dev": "annotations-work-too",
126
+ },
127
+ },
128
+ });
129
+ })
130
+ .Validate(request => {
131
+ // This Validate Action will validate the request before it is persisted to the cluster
132
+
133
+ // Approve the request if the ConfigMap has the label 'pepr'
134
+ if (request.HasLabel("pepr")) {
135
+ return request.Approve();
136
+ }
137
+
138
+ // Otherwise, deny the request with an error message (optional)
139
+ return request.Deny("ConfigMap must have label 'pepr'");
140
+ })
141
+ .Watch((cm, phase) => {
142
+ // This Watch Action will watch the ConfigMap after it has been persisted to the cluster
143
+ Log.info(cm, `ConfigMap was ${phase} with the name example-2`);
144
+ });
145
+
146
+ /**
147
+ * ---------------------------------------------------------------------------------------------------
148
+ * Mutate Action (CM Example 2a) *
149
+ * ---------------------------------------------------------------------------------------------------
150
+ *
151
+ * This action shows a simple validation that will deny any ConfigMap that has the
152
+ * annotation `evil`. Note that the `Deny()` function takes an optional second parameter that is a
153
+ * user-defined status code to return.
154
+ */
155
+ When(a.ConfigMap)
156
+ .IsCreated()
157
+ .Validate(request => {
158
+ if (request.HasAnnotation("evil")) {
159
+ return request.Deny("No evil CM annotations allowed.", 400);
160
+ }
161
+
162
+ return request.Approve();
163
+ });
164
+
165
+ /**
166
+ * ---------------------------------------------------------------------------------------------------
167
+ * Mutate Action (CM Example 3) *
168
+ * ---------------------------------------------------------------------------------------------------
169
+ *
170
+ * This action combines different styles. Unlike the previous actions, this one will look
171
+ * for any ConfigMap in the `pepr-demo` namespace that has the label `change=by-label` during either
172
+ * CREATE or UPDATE. Note that all conditions added such as `WithName()`, `WithLabel()`, `InNamespace()`,
173
+ * are ANDs so all conditions must be true for the request to be processed.
174
+ */
175
+ When(a.ConfigMap)
176
+ .IsCreatedOrUpdated()
177
+ .WithLabel("change", "by-label")
178
+ .Mutate(request => {
179
+ // The K8s object e are going to mutate
180
+ const cm = request.Raw;
181
+
182
+ // Get the username and uid of the K8s request
183
+ const { username, uid } = request.Request.userInfo;
184
+
185
+ // Store some data about the request in the configmap
186
+ cm.data["username"] = username;
187
+ cm.data["uid"] = uid;
188
+
189
+ // You can still mix other ways of making changes too
190
+ request.SetAnnotation("pepr.dev", "making-waves");
191
+ });
192
+
193
+ // This action validates the label `change=by-label` is deleted
194
+ When(a.ConfigMap)
195
+ .IsDeleted()
196
+ .WithLabel("change", "by-label")
197
+ .Validate(request => {
198
+ // Log and then always approve the request
199
+ Log.info("CM with label 'change=by-label' was deleted.");
200
+ return request.Approve();
201
+ });
202
+
203
+ /**
204
+ * ---------------------------------------------------------------------------------------------------
205
+ * Mutate Action (CM Example 4) *
206
+ * ---------------------------------------------------------------------------------------------------
207
+ *
208
+ * This action show how you can use the `Mutate()` function without an inline function.
209
+ * This is useful if you want to keep your actions small and focused on a single task,
210
+ * or if you want to reuse the same function in multiple actions.
211
+ */
212
+ When(a.ConfigMap).IsCreated().WithName("example-4").Mutate(example4Cb);
213
+
214
+ // This function uses the complete type definition, but is not required.
215
+ function example4Cb(cm: PeprMutateRequest<a.ConfigMap>) {
216
+ cm.SetLabel("pepr.dev/first", "true");
217
+ cm.SetLabel("pepr.dev/second", "true");
218
+ cm.SetLabel("pepr.dev/third", "true");
219
+ }
220
+
221
+ /**
222
+ * ---------------------------------------------------------------------------------------------------
223
+ * Mutate Action (CM Example 4a) *
224
+ * ---------------------------------------------------------------------------------------------------
225
+ *
226
+ * This is the same as Example 4, except this only operates on a CM in the `pepr-demo-2` namespace.
227
+ * Note because the Capability defines namespaces, the namespace specified here must be one of those.
228
+ * Alternatively, you can remove the namespace from the Capability definition and specify it here.
229
+ */
230
+ When(a.ConfigMap)
231
+ .IsCreated()
232
+ .InNamespace("pepr-demo-2")
233
+ .WithName("example-4a")
234
+ .Mutate(example4Cb);
235
+
236
+ /**
237
+ * ---------------------------------------------------------------------------------------------------
238
+ * Mutate Action (CM Example 5) *
239
+ * ---------------------------------------------------------------------------------------------------
240
+ *
241
+ * This action is a bit more complex. It will look for any ConfigMap in the `pepr-demo`
242
+ * namespace that has the label `chuck-norris` during CREATE. When it finds one, it will fetch a
243
+ * random Chuck Norris joke from the API and add it to the ConfigMap. This is a great example of how
244
+ * you can use Pepr to make changes to your K8s objects based on external data.
245
+ *
246
+ * Note the use of the `async` keyword. This is required for any action that uses `await` or `fetch()`.
247
+ *
248
+ * Also note we are passing a type to the `fetch()` function. This is optional, but it will help you
249
+ * avoid mistakes when working with the data returned from the API. You can also use the `as` keyword to
250
+ * cast the data returned from the API.
251
+ *
252
+ * These are equivalent:
253
+ * ```ts
254
+ * const joke = await fetch<TheChuckNorrisJoke>("https://api.chucknorris.io/jokes/random?category=dev");
255
+ * const joke = await fetch("https://api.chucknorris.io/jokes/random?category=dev") as TheChuckNorrisJoke;
256
+ * ```
257
+ *
258
+ * Alternatively, you can drop the type completely:
259
+ *
260
+ * ```ts
261
+ * fetch("https://api.chucknorris.io/jokes/random?category=dev")
262
+ * ```
263
+ */
264
+ interface TheChuckNorrisJoke {
265
+ icon_url: string;
266
+ id: string;
267
+ url: string;
268
+ value: string;
269
+ }
270
+
271
+ When(a.ConfigMap)
272
+ .IsCreated()
273
+ .WithLabel("chuck-norris")
274
+ .Mutate(async change => {
275
+ // Try/catch is not needed as a response object will always be returned
276
+ const response = await fetch<TheChuckNorrisJoke>(
277
+ "https://api.chucknorris.io/jokes/random?category=dev",
278
+ );
279
+
280
+ // Instead, check the `response.ok` field
281
+ if (response.ok) {
282
+ // Add the Chuck Norris joke to the configmap
283
+ change.Raw.data["chuck-says"] = response.data.value;
284
+ return;
285
+ }
286
+
287
+ // You can also assert on different HTTP response codes
288
+ if (response.status === fetchStatus.NOT_FOUND) {
289
+ // Do something else
290
+ return;
291
+ }
292
+ });
293
+
294
+ /**
295
+ * ---------------------------------------------------------------------------------------------------
296
+ * Mutate Action (Secret Base64 Handling) *
297
+ * ---------------------------------------------------------------------------------------------------
298
+ *
299
+ * The K8s JS client provides incomplete support for base64 encoding/decoding handling for secrets,
300
+ * unlike the GO client. To make this less painful, Pepr automatically handles base64 encoding/decoding
301
+ * secret data before and after the action is executed.
302
+ */
303
+ When(a.Secret)
304
+ .IsCreated()
305
+ .WithName("secret-1")
306
+ .Mutate(request => {
307
+ const secret = request.Raw;
308
+
309
+ // This will be encoded at the end of all processing back to base64: "Y2hhbmdlLXdpdGhvdXQtZW5jb2Rpbmc="
310
+ secret.data.magic = "change-without-encoding";
311
+
312
+ // You can modify the data directly, and it will be encoded at the end of all processing
313
+ secret.data.example += " - modified by Pepr";
314
+ });
315
+
316
+ /**
317
+ * ---------------------------------------------------------------------------------------------------
318
+ * Mutate Action (Untyped Custom Resource) *
319
+ * ---------------------------------------------------------------------------------------------------
320
+ *
321
+ * Out of the box, Pepr supports all the standard Kubernetes objects. However, you can also create
322
+ * your own types. This is useful if you are working with an Operator that creates custom resources.
323
+ * There are two ways to do this, the first is to use the `When()` function with a `GenericKind`,
324
+ * the second is to create a new class that extends `GenericKind` and use the `RegisterKind()` function.
325
+ *
326
+ * This example shows how to use the `When()` function with a `GenericKind`. Note that you
327
+ * must specify the `group`, `version`, and `kind` of the object (if applicable). This is how Pepr knows
328
+ * if the action should be triggered or not. Since we are using a `GenericKind`,
329
+ * Pepr will not be able to provide any intellisense for the object, so you will need to refer to the
330
+ * Kubernetes API documentation for the object you are working with.
331
+ *
332
+ * You will need to wait for the CRD in `hello-pepr.samples.yaml` to be created, then you can apply
333
+ *
334
+ * ```yaml
335
+ * apiVersion: pepr.dev/v1
336
+ * kind: Unicorn
337
+ * metadata:
338
+ * name: example-1
339
+ * namespace: pepr-demo
340
+ * spec:
341
+ * message: replace-me
342
+ * counter: 0
343
+ * ```
344
+ */
345
+ When(a.GenericKind, {
346
+ group: "pepr.dev",
347
+ version: "v1",
348
+ kind: "Unicorn",
349
+ })
350
+ .IsCreated()
351
+ .WithName("example-1")
352
+ .Mutate(request => {
353
+ request.Merge({
354
+ spec: {
355
+ message: "Hello Pepr without type data!",
356
+ counter: Math.random(),
357
+ },
358
+ });
359
+ });
360
+
361
+ /**
362
+ * ---------------------------------------------------------------------------------------------------
363
+ * Mutate Action (Typed Custom Resource) *
364
+ * ---------------------------------------------------------------------------------------------------
365
+ *
366
+ * This example shows how to use the `RegisterKind()` function to create a new type. This is useful
367
+ * if you are working with an Operator that creates custom resources and you want to have intellisense
368
+ * for the object. Note that you must specify the `group`, `version`, and `kind` of the object (if applicable)
369
+ * as this is how Pepr knows if the action should be triggered or not.
370
+ *
371
+ * Once you register a new Kind with Pepr, you can use the `When()` function with the new Kind. Ideally,
372
+ * you should register custom Kinds at the top of your Capability file or Pepr Module so they are available
373
+ * to all actions, but we are putting it here for demonstration purposes.
374
+ *
375
+ * You will need to wait for the CRD in `hello-pepr.samples.yaml` to be created, then you can apply
376
+ *
377
+ * ```yaml
378
+ * apiVersion: pepr.dev/v1
379
+ * kind: Unicorn
380
+ * metadata:
381
+ * name: example-2
382
+ * namespace: pepr-demo
383
+ * spec:
384
+ * message: replace-me
385
+ * counter: 0
386
+ * ```*
387
+ */
388
+ class UnicornKind extends a.GenericKind {
389
+ spec: {
390
+ /**
391
+ * JSDoc comments can be added to explain more details about the field.
392
+ *
393
+ * @example
394
+ * ```ts
395
+ * request.Raw.spec.message = "Hello Pepr!";
396
+ * ```
397
+ * */
398
+ message: string;
399
+ counter: number;
400
+ };
401
+ }
402
+
403
+ RegisterKind(UnicornKind, {
404
+ group: "pepr.dev",
405
+ version: "v1",
406
+ kind: "Unicorn",
407
+ });
408
+
409
+ When(UnicornKind)
410
+ .IsCreated()
411
+ .WithName("example-2")
412
+ .Mutate(request => {
413
+ request.Merge({
414
+ spec: {
415
+ message: "Hello Pepr with type data!",
416
+ counter: Math.random(),
417
+ },
418
+ });
419
+ });
420
+
421
+ /**
422
+ * A callback function that is called once the Pepr Store is fully loaded.
423
+ */
424
+ Store.onReady(data => {
425
+ Log.info(data, "Pepr Store Ready");
426
+ });
@@ -0,0 +1,4 @@
1
+ # Ignore node_modules and Pepr build artifacts
2
+ node_modules
3
+ dist
4
+ insecure*
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "pepr-dev-module",
3
+ "description": "static test package.json for local pepr core development",
4
+ "pepr": {
5
+ "name": "Development Module",
6
+ "uuid": "20e17cf6-a2e4-46b2-b626-75d88d96c88b",
7
+ "description": "Development module for pepr",
8
+ "onError": "reject",
9
+ "logLevel": "debug",
10
+ "customLabels": {
11
+ "namespace": {
12
+ "pepr.dev": ""
13
+ }
14
+ },
15
+ "alwaysIgnore": {
16
+ "namespaces": []
17
+ },
18
+ "includedFiles": []
19
+ }
20
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ "Create a new Pepr capability": {
3
+ "prefix": "create pepr capability",
4
+ "body": [
5
+ "import { Capability, a } from 'pepr';",
6
+ "",
7
+ "export const ${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/} = new Capability({",
8
+ "\tname: '${TM_FILENAME_BASE}',",
9
+ "\tdescription: '${1:A brief description of this capability.}',",
10
+ "\tnamespaces: [${2:}],",
11
+ "});",
12
+ "",
13
+ "// Use the 'When' function to create a new action",
14
+ "const { When } = ${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/};",
15
+ "",
16
+ "// When(a.<Kind>).Is<Event>().Then(change => change.<changes>",
17
+ "When(${3:})"
18
+ ],
19
+ "description": "Creates a new Pepr capability with a specified description, optional namespaces, and adds a When statement for the specified value."
20
+ }
21
+ }
@@ -0,0 +1,17 @@
1
+ import { PeprModule } from "pepr";
2
+ // cfg loads your pepr configuration from package.json
3
+ import cfg from "./package.json";
4
+
5
+ // HelloPepr is a demo capability that is included with Pepr. Comment or delete the line below to remove it.
6
+ import { HelloPepr } from "./capabilities/hello-pepr";
7
+
8
+ /**
9
+ * This is the main entrypoint for this Pepr module. It is run when the module is started.
10
+ * This is where you register your Pepr configurations and capabilities.
11
+ */
12
+ new PeprModule(cfg, [
13
+ // "HelloPepr" is a demo capability that is included with Pepr. Comment or delete the line below to remove it.
14
+ HelloPepr,
15
+
16
+ // Your additional capabilities go here
17
+ ]);
@@ -0,0 +1,10 @@
1
+ {
2
+ "debug.javascript.terminalOptions": {
3
+ "enableTurboSourcemaps": true,
4
+ "resolveSourceMapLocations": [
5
+ "${workspaceFolder}/**",
6
+ "node_modules/kubernetes-fluent-client/**",
7
+ "node_modules/pepr/**"
8
+ ]
9
+ }
10
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "./tsconfig.module.json",
3
+ "compilerOptions": {
4
+ "paths": {
5
+ "pepr": ["../lib.ts"]
6
+ },
7
+ "rootDir": "../../"
8
+ }
9
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "allowSyntheticDefaultImports": true,
4
+ "declaration": true,
5
+ "declarationMap": true,
6
+ "emitDeclarationOnly": true,
7
+ "esModuleInterop": true,
8
+ "lib": ["ES2022"],
9
+ "module": "CommonJS",
10
+ "moduleResolution": "node",
11
+ "outDir": "dist",
12
+ "resolveJsonModule": true,
13
+ "rootDir": ".",
14
+ "strict": false,
15
+ "target": "ES2022",
16
+ "useUnknownInCatchVariables": false
17
+ },
18
+ "include": ["**/*.ts"]
19
+ }