pepr 0.1.4 → 0.1.6
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.
- package/README.md +1 -2
- package/dist/pepr-20e17cf6-a2e4-46b2-b626-75d88d96c88b.js +393 -0
- package/dist/pepr-cli.js +230 -6
- package/dist/pepr-cli.js.map +1 -1
- package/package.json +1 -1
- package/tsconfig.json +1 -12
- package/rollup.config.mjs +0 -27
- package/src/lib/filter.test.ts +0 -211
- package/src/lib/k8s/kinds.test.ts +0 -341
- package/src/lib/logger.test.ts +0 -80
package/README.md
CHANGED
|
@@ -51,8 +51,7 @@ import "./test-mutations";
|
|
|
51
51
|
### demo/hello-world/index.ts
|
|
52
52
|
|
|
53
53
|
```typescript
|
|
54
|
-
import { a } from "
|
|
55
|
-
import { Capability } from "@pepr";
|
|
54
|
+
import { Capability, a } from "pepr";
|
|
56
55
|
|
|
57
56
|
const { When } = new Capability({
|
|
58
57
|
// The unique name of the capability
|
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('@kubernetes/client-node/dist');
|
|
4
|
+
var R = require('ramda');
|
|
5
|
+
var fastJsonPatch = require('fast-json-patch');
|
|
6
|
+
|
|
7
|
+
function _interopNamespaceDefault(e) {
|
|
8
|
+
var n = Object.create(null);
|
|
9
|
+
if (e) {
|
|
10
|
+
Object.keys(e).forEach(function (k) {
|
|
11
|
+
if (k !== 'default') {
|
|
12
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
13
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () { return e[k]; }
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
n.default = e;
|
|
21
|
+
return Object.freeze(n);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
var R__namespace = /*#__PURE__*/_interopNamespaceDefault(R);
|
|
25
|
+
|
|
26
|
+
var LogLevel;
|
|
27
|
+
(function (LogLevel) {
|
|
28
|
+
LogLevel[LogLevel["debug"] = 0] = "debug";
|
|
29
|
+
LogLevel[LogLevel["info"] = 1] = "info";
|
|
30
|
+
LogLevel[LogLevel["warn"] = 2] = "warn";
|
|
31
|
+
LogLevel[LogLevel["error"] = 3] = "error";
|
|
32
|
+
})(LogLevel || (LogLevel = {}));
|
|
33
|
+
var ConsoleColors;
|
|
34
|
+
(function (ConsoleColors) {
|
|
35
|
+
ConsoleColors["Reset"] = "\u001B[0m";
|
|
36
|
+
ConsoleColors["Bright"] = "\u001B[1m";
|
|
37
|
+
ConsoleColors["Dim"] = "\u001B[2m";
|
|
38
|
+
ConsoleColors["Underscore"] = "\u001B[4m";
|
|
39
|
+
ConsoleColors["Blink"] = "\u001B[5m";
|
|
40
|
+
ConsoleColors["Reverse"] = "\u001B[7m";
|
|
41
|
+
ConsoleColors["Hidden"] = "\u001B[8m";
|
|
42
|
+
ConsoleColors["FgBlack"] = "\u001B[30m";
|
|
43
|
+
ConsoleColors["FgRed"] = "\u001B[31m";
|
|
44
|
+
ConsoleColors["FgGreen"] = "\u001B[32m";
|
|
45
|
+
ConsoleColors["FgYellow"] = "\u001B[33m";
|
|
46
|
+
ConsoleColors["FgBlue"] = "\u001B[34m";
|
|
47
|
+
ConsoleColors["FgMagenta"] = "\u001B[35m";
|
|
48
|
+
ConsoleColors["FgCyan"] = "\u001B[36m";
|
|
49
|
+
ConsoleColors["FgWhite"] = "\u001B[37m";
|
|
50
|
+
ConsoleColors["BgBlack"] = "\u001B[40m";
|
|
51
|
+
ConsoleColors["BgRed"] = "\u001B[41m";
|
|
52
|
+
ConsoleColors["BgGreen"] = "\u001B[42m";
|
|
53
|
+
ConsoleColors["BgYellow"] = "\u001B[43m";
|
|
54
|
+
ConsoleColors["BgBlue"] = "\u001B[44m";
|
|
55
|
+
ConsoleColors["BgMagenta"] = "\u001B[45m";
|
|
56
|
+
ConsoleColors["BgCyan"] = "\u001B[46m";
|
|
57
|
+
ConsoleColors["BgWhite"] = "\u001B[47m";
|
|
58
|
+
})(ConsoleColors || (ConsoleColors = {}));
|
|
59
|
+
class Logger {
|
|
60
|
+
constructor(logLevel) {
|
|
61
|
+
this._logLevel = logLevel;
|
|
62
|
+
}
|
|
63
|
+
SetLogLevel(logLevel) {
|
|
64
|
+
this._logLevel = LogLevel[logLevel];
|
|
65
|
+
this.debug(`Log level set to ${logLevel}`);
|
|
66
|
+
}
|
|
67
|
+
debug(message, prefix) {
|
|
68
|
+
this.log(LogLevel.debug, message, prefix);
|
|
69
|
+
}
|
|
70
|
+
info(message, prefix) {
|
|
71
|
+
this.log(LogLevel.info, message, prefix);
|
|
72
|
+
}
|
|
73
|
+
warn(message, prefix) {
|
|
74
|
+
this.log(LogLevel.warn, message, prefix);
|
|
75
|
+
}
|
|
76
|
+
error(message, prefix) {
|
|
77
|
+
this.log(LogLevel.error, message, prefix);
|
|
78
|
+
}
|
|
79
|
+
log(logLevel, message, callerPrefix = "") {
|
|
80
|
+
const color = {
|
|
81
|
+
[LogLevel.debug]: ConsoleColors.FgBlack,
|
|
82
|
+
[LogLevel.info]: ConsoleColors.FgCyan,
|
|
83
|
+
[LogLevel.warn]: ConsoleColors.FgYellow,
|
|
84
|
+
[LogLevel.error]: ConsoleColors.FgRed,
|
|
85
|
+
};
|
|
86
|
+
if (logLevel >= this._logLevel) {
|
|
87
|
+
let prefix = "[" + LogLevel[logLevel] + "]\t" + callerPrefix;
|
|
88
|
+
prefix = this.colorize(prefix, color[logLevel]);
|
|
89
|
+
if (typeof message !== "string") {
|
|
90
|
+
console.log(prefix);
|
|
91
|
+
console.debug("%o", message);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
console.log(prefix + "\t" + message);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
colorize(text, color) {
|
|
99
|
+
return color + text + ConsoleColors.Reset;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
var logger = new Logger(LogLevel.info);
|
|
103
|
+
|
|
104
|
+
var Operation;
|
|
105
|
+
(function (Operation) {
|
|
106
|
+
Operation["CREATE"] = "CREATE";
|
|
107
|
+
Operation["UPDATE"] = "UPDATE";
|
|
108
|
+
Operation["DELETE"] = "DELETE";
|
|
109
|
+
Operation["CONNECT"] = "CONNECT";
|
|
110
|
+
})(Operation || (Operation = {}));
|
|
111
|
+
|
|
112
|
+
var ErrorBehavior;
|
|
113
|
+
(function (ErrorBehavior) {
|
|
114
|
+
ErrorBehavior["ignore"] = "ignore";
|
|
115
|
+
ErrorBehavior["audit"] = "audit";
|
|
116
|
+
ErrorBehavior["reject"] = "reject";
|
|
117
|
+
})(ErrorBehavior || (ErrorBehavior = {}));
|
|
118
|
+
var HookPhase;
|
|
119
|
+
(function (HookPhase) {
|
|
120
|
+
HookPhase["mutate"] = "mutate";
|
|
121
|
+
HookPhase["valdiate"] = "validate";
|
|
122
|
+
})(HookPhase || (HookPhase = {}));
|
|
123
|
+
var Event;
|
|
124
|
+
(function (Event) {
|
|
125
|
+
Event["Create"] = "create";
|
|
126
|
+
Event["Update"] = "update";
|
|
127
|
+
Event["Delete"] = "delete";
|
|
128
|
+
Event["CreateOrUpdate"] = "createOrUpdate";
|
|
129
|
+
})(Event || (Event = {}));
|
|
130
|
+
|
|
131
|
+
class RequestWrapper {
|
|
132
|
+
get PermitSideEffects() {
|
|
133
|
+
return !this._input.dryRun;
|
|
134
|
+
}
|
|
135
|
+
get IsDryRun() {
|
|
136
|
+
return this._input.dryRun;
|
|
137
|
+
}
|
|
138
|
+
get OldResource() {
|
|
139
|
+
return this._input.oldObject;
|
|
140
|
+
}
|
|
141
|
+
get Request() {
|
|
142
|
+
return this._input;
|
|
143
|
+
}
|
|
144
|
+
constructor(input) {
|
|
145
|
+
this.Raw = R__namespace.clone(input.object);
|
|
146
|
+
this._input = input;
|
|
147
|
+
}
|
|
148
|
+
Merge(obj) {
|
|
149
|
+
this.Raw = R__namespace.mergeDeepRight(this.Raw, obj);
|
|
150
|
+
}
|
|
151
|
+
SetLabel(key, value) {
|
|
152
|
+
const ref = this.Raw;
|
|
153
|
+
ref.metadata = ref.metadata ?? {};
|
|
154
|
+
ref.metadata.labels = ref.metadata.labels ?? {};
|
|
155
|
+
ref.metadata.labels[key] = value;
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
SetAnnotation(key, value) {
|
|
159
|
+
const ref = this.Raw;
|
|
160
|
+
ref.metadata = ref.metadata ?? {};
|
|
161
|
+
ref.metadata.annotations = ref.metadata.annotations ?? {};
|
|
162
|
+
ref.metadata.annotations[key] = value;
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
RemoveLabel(key) {
|
|
166
|
+
if (this.Raw.metadata?.labels?.[key]) {
|
|
167
|
+
delete this.Raw.metadata.labels[key];
|
|
168
|
+
}
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
RemoveAnnotation(key) {
|
|
172
|
+
if (this.Raw.metadata?.annotations?.[key]) {
|
|
173
|
+
delete this.Raw.metadata.annotations[key];
|
|
174
|
+
}
|
|
175
|
+
return this;
|
|
176
|
+
}
|
|
177
|
+
HasLabel(key) {
|
|
178
|
+
return this.Raw?.metadata?.labels?.[key] !== undefined;
|
|
179
|
+
}
|
|
180
|
+
HasAnnotation(key) {
|
|
181
|
+
return this.Raw?.metadata?.annotations?.[key] !== undefined;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function shouldSkipRequest(binding, req) {
|
|
186
|
+
const { group, kind, version } = binding.kind;
|
|
187
|
+
const { namespaces, labels, annotations } = binding.filters;
|
|
188
|
+
const { metadata } = req.object;
|
|
189
|
+
if (kind !== req.kind.kind) {
|
|
190
|
+
logger.debug(`${req.kind.kind} does not match ${kind}`);
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
if (group && group !== req.kind.group) {
|
|
194
|
+
logger.debug(`${req.kind.group} does not match ${group}`);
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
if (version && version !== req.kind.version) {
|
|
198
|
+
logger.debug(`${req.kind.version} does not match ${version}`);
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
if (namespaces.length && !namespaces.includes(req.namespace || "")) {
|
|
202
|
+
logger.debug(`${req.namespace} is not in ${namespaces}`);
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
for (const [key, value] of Object.entries(labels)) {
|
|
206
|
+
if (metadata?.labels?.[key] !== value) {
|
|
207
|
+
logger.debug(`${metadata?.labels?.[key]} does not match ${value}`);
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
for (const [key, value] of Object.entries(annotations)) {
|
|
212
|
+
if (metadata?.annotations?.[key] !== value) {
|
|
213
|
+
logger.debug(`${metadata?.annotations?.[key]} does not match ${value}`);
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function processor(config, capabilities, req) {
|
|
221
|
+
const wrapped = new RequestWrapper(req);
|
|
222
|
+
const response = {
|
|
223
|
+
uid: req.uid,
|
|
224
|
+
patchType: "JSONPatch",
|
|
225
|
+
warnings: [],
|
|
226
|
+
allowed: false,
|
|
227
|
+
};
|
|
228
|
+
logger.info(`Processing '${req.uid}' for '${req.kind.kind}' '${req.name}'`);
|
|
229
|
+
for (const { name, bindings } of capabilities) {
|
|
230
|
+
const prefix = `${req.uid} ${req.name}: ${name}`;
|
|
231
|
+
logger.info(`Processing capability ${name}`, prefix);
|
|
232
|
+
for (const action of bindings) {
|
|
233
|
+
if (shouldSkipRequest(action, req)) {
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
logger.info(`Processing matched action ${action.kind.kind}`, prefix);
|
|
237
|
+
const { metadata } = wrapped.Raw;
|
|
238
|
+
const identifier = `pepr.dev/${config.uuid}/${name}`;
|
|
239
|
+
metadata.annotations = metadata.annotations || {};
|
|
240
|
+
metadata.annotations[identifier] = "started";
|
|
241
|
+
try {
|
|
242
|
+
action.callback(wrapped);
|
|
243
|
+
metadata.annotations[identifier] = "succeeded";
|
|
244
|
+
}
|
|
245
|
+
catch (e) {
|
|
246
|
+
response.warnings.push(`Action failed: ${e}`);
|
|
247
|
+
if (config.onError) {
|
|
248
|
+
logger.error(`Action failed: ${e}`, prefix);
|
|
249
|
+
response.result = "Pepr module configured to reject on error";
|
|
250
|
+
return response;
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
logger.warn(`Action failed: ${e}`, prefix);
|
|
254
|
+
metadata.annotations[identifier] = "warning";
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
response.allowed = true;
|
|
260
|
+
const patches = fastJsonPatch.compare(req.object, wrapped.Raw);
|
|
261
|
+
if (patches.length > 0) {
|
|
262
|
+
response.patch = JSON.stringify(patches);
|
|
263
|
+
}
|
|
264
|
+
if (response.warnings.length < 1) {
|
|
265
|
+
delete response.warnings;
|
|
266
|
+
}
|
|
267
|
+
logger.debug(patches);
|
|
268
|
+
return response;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
class PeprModule {
|
|
272
|
+
get kinds() {
|
|
273
|
+
return this._kinds;
|
|
274
|
+
}
|
|
275
|
+
get UUID() {
|
|
276
|
+
return this._config.uuid;
|
|
277
|
+
}
|
|
278
|
+
constructor({ description, pepr }, additionalCfg) {
|
|
279
|
+
this._state = [];
|
|
280
|
+
this._kinds = [];
|
|
281
|
+
this.Register = (capability) => {
|
|
282
|
+
logger.info(`Registering capability ${capability.name}`);
|
|
283
|
+
this._kinds = capability.bindings.map(({ kind }) => kind);
|
|
284
|
+
this._state.push(capability);
|
|
285
|
+
};
|
|
286
|
+
this.ProcessRequest = (req) => {
|
|
287
|
+
return processor(this._config, this._state, req);
|
|
288
|
+
};
|
|
289
|
+
this._config = {
|
|
290
|
+
alwaysIgnore: {
|
|
291
|
+
namespaces: ["kube-system", "pepr-system"],
|
|
292
|
+
labels: [{ "pepr.dev": "ignore" }],
|
|
293
|
+
},
|
|
294
|
+
...pepr,
|
|
295
|
+
...additionalCfg,
|
|
296
|
+
description,
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
var name = "pepr";
|
|
302
|
+
var version = "0.1.4";
|
|
303
|
+
var description = "Kubernetes application engine";
|
|
304
|
+
var author = "Defense Unicorns";
|
|
305
|
+
var homepage = "https://github.com/defenseunicorns/pepr";
|
|
306
|
+
var license = "Apache-2.0";
|
|
307
|
+
var bin = "dist/pepr-cli.js";
|
|
308
|
+
var repository = "defenseunicorns/pepr";
|
|
309
|
+
var engines = {
|
|
310
|
+
node: ">=18.0.0"
|
|
311
|
+
};
|
|
312
|
+
var pepr = {
|
|
313
|
+
name: "Development Module",
|
|
314
|
+
uuid: "20e17cf6-a2e4-46b2-b626-75d88d96c88b",
|
|
315
|
+
description: "Development module for pepr",
|
|
316
|
+
version: "dev",
|
|
317
|
+
onError: "ignore"
|
|
318
|
+
};
|
|
319
|
+
var scripts = {
|
|
320
|
+
webhook: "node dist/default.js",
|
|
321
|
+
build: "rollup -c",
|
|
322
|
+
start: "rollup -c -w --watch.onEnd 'sleep 1 && node dist/pepr-test.js'",
|
|
323
|
+
test: "ava",
|
|
324
|
+
lint: "npx eslint src",
|
|
325
|
+
"lint:fix": "npm run lint -- --fix",
|
|
326
|
+
prettier: "npx prettier src --check",
|
|
327
|
+
"prettier:fix": "npm run prettier -- --write",
|
|
328
|
+
prepublishOnly: "npm run lint:fix && npm run prettier:fix && npm run test && npm run build"
|
|
329
|
+
};
|
|
330
|
+
var dependencies = {
|
|
331
|
+
"@kubernetes/client-node": "^0.18.1",
|
|
332
|
+
"@rollup/plugin-json": "^6.0.0",
|
|
333
|
+
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
334
|
+
"@rollup/plugin-typescript": "^11.0.0",
|
|
335
|
+
"@types/ramda": "^0.28.23",
|
|
336
|
+
chokidar: "^3.5.3",
|
|
337
|
+
commander: "^10.0.0",
|
|
338
|
+
express: "^4.18.2",
|
|
339
|
+
"fast-json-patch": "^3.1.1",
|
|
340
|
+
prompts: "^2.4.2",
|
|
341
|
+
ramda: "^0.28.0",
|
|
342
|
+
rollup: "^3.20.2",
|
|
343
|
+
uuid: "^9.0.0",
|
|
344
|
+
tslib: "^2.5.0",
|
|
345
|
+
typescript: "^5.0.2"
|
|
346
|
+
};
|
|
347
|
+
var devDependencies = {
|
|
348
|
+
"@types/prompts": "^2.4.4",
|
|
349
|
+
"@types/uuid": "^9.0.1",
|
|
350
|
+
"@typescript-eslint/eslint-plugin": "^5.57.0",
|
|
351
|
+
"@typescript-eslint/parser": "^5.57.0",
|
|
352
|
+
ava: "^5.2.0",
|
|
353
|
+
eslint: "^8.37.0",
|
|
354
|
+
prettier: "^2.8.7",
|
|
355
|
+
"rollup-plugin-visualizer": "^5.9.0",
|
|
356
|
+
"ts-node": "^10.9.1",
|
|
357
|
+
"tsconfig-paths": "^4.1.2"
|
|
358
|
+
};
|
|
359
|
+
var ava = {
|
|
360
|
+
extensions: [
|
|
361
|
+
"ts"
|
|
362
|
+
],
|
|
363
|
+
require: [
|
|
364
|
+
"ts-node/register",
|
|
365
|
+
"tsconfig-paths/register"
|
|
366
|
+
]
|
|
367
|
+
};
|
|
368
|
+
var cfg = {
|
|
369
|
+
name: name,
|
|
370
|
+
version: version,
|
|
371
|
+
description: description,
|
|
372
|
+
author: author,
|
|
373
|
+
homepage: homepage,
|
|
374
|
+
license: license,
|
|
375
|
+
bin: bin,
|
|
376
|
+
repository: repository,
|
|
377
|
+
engines: engines,
|
|
378
|
+
pepr: pepr,
|
|
379
|
+
scripts: scripts,
|
|
380
|
+
dependencies: dependencies,
|
|
381
|
+
devDependencies: devDependencies,
|
|
382
|
+
ava: ava
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
const { Register, ProcessRequest } = new PeprModule(cfg, {
|
|
386
|
+
alwaysIgnore: {
|
|
387
|
+
namespaces: [],
|
|
388
|
+
labels: [],
|
|
389
|
+
},
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
exports.ProcessRequest = ProcessRequest;
|
|
393
|
+
exports.Register = Register;
|