pepr 0.3.2 → 0.4.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.
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +1420 -0
- package/dist/controller.js +157 -0
- package/dist/{src/lib → lib}/capability.d.ts +1 -1
- package/dist/lib/capability.d.ts.map +1 -0
- package/dist/lib/controller.d.ts.map +1 -0
- package/dist/{src/lib → lib}/fetch.d.ts +1 -2
- package/dist/lib/fetch.d.ts.map +1 -0
- package/dist/{src/lib → lib}/filter.d.ts +1 -1
- package/dist/lib/filter.d.ts.map +1 -0
- package/dist/lib/k8s/index.d.ts.map +1 -0
- package/dist/lib/k8s/kinds.d.ts.map +1 -0
- package/dist/lib/k8s/tls.d.ts.map +1 -0
- package/dist/lib/k8s/types.d.ts.map +1 -0
- package/dist/{src/lib → lib}/k8s/upstream.d.ts +2 -2
- package/dist/lib/k8s/upstream.d.ts.map +1 -0
- package/dist/{src/lib → lib}/k8s/webhook.d.ts +1 -1
- package/dist/lib/k8s/webhook.d.ts.map +1 -0
- package/dist/lib/logger.d.ts.map +1 -0
- package/dist/lib/module.d.ts.map +1 -0
- package/dist/lib/processor.d.ts.map +1 -0
- package/dist/{src/lib → lib}/request.d.ts +1 -1
- package/dist/lib/request.d.ts.map +1 -0
- package/dist/{src/lib → lib}/types.d.ts +5 -1
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/{src/lib/index.d.ts → lib.d.ts} +8 -8
- package/dist/lib.d.ts.map +1 -0
- package/dist/lib.js +1150 -0
- package/dist/lib.js.map +7 -0
- package/package.json +8 -12
- package/src/cli.ts +37 -0
- package/src/lib/capability.ts +2 -1
- package/src/lib/controller.ts +15 -1
- package/src/lib/fetch.ts +3 -2
- package/src/lib/filter.ts +1 -1
- package/src/lib/k8s/upstream.ts +2 -2
- package/src/lib/k8s/webhook.ts +8 -3
- package/src/lib/module.ts +3 -2
- package/src/lib/processor.ts +2 -2
- package/src/lib/request.ts +4 -4
- package/src/lib/types.ts +6 -1
- package/src/{lib/index.ts → lib.ts} +7 -7
- package/dist/package.json +0 -66
- package/dist/src/cli/banner.js +0 -70
- package/dist/src/cli/build.js +0 -120
- package/dist/src/cli/capability.js +0 -14
- package/dist/src/cli/deploy.js +0 -54
- package/dist/src/cli/dev.js +0 -114
- package/dist/src/cli/index.js +0 -35
- package/dist/src/cli/init/index.js +0 -81
- package/dist/src/cli/init/templates/.prettierrc.json +0 -13
- package/dist/src/cli/init/templates/capabilities/hello-pepr.samples.json +0 -115
- package/dist/src/cli/init/templates/data.json +0 -1
- package/dist/src/cli/init/templates/pepr.code-snippets.json +0 -21
- package/dist/src/cli/init/templates/tsconfig.module.json +0 -12
- package/dist/src/cli/init/templates.js +0 -96
- package/dist/src/cli/init/utils.js +0 -56
- package/dist/src/cli/init/walkthrough.js +0 -84
- package/dist/src/cli/root.js +0 -21
- package/dist/src/cli/run.js +0 -58
- package/dist/src/cli/test.js +0 -50
- package/dist/src/cli/update.js +0 -58
- package/dist/src/lib/capability.d.ts.map +0 -1
- package/dist/src/lib/capability.js +0 -125
- package/dist/src/lib/controller.d.ts.map +0 -1
- package/dist/src/lib/controller.js +0 -108
- package/dist/src/lib/fetch.d.ts.map +0 -1
- package/dist/src/lib/fetch.js +0 -92
- package/dist/src/lib/filter.d.ts.map +0 -1
- package/dist/src/lib/filter.js +0 -77
- package/dist/src/lib/index.d.ts.map +0 -1
- package/dist/src/lib/index.js +0 -26
- package/dist/src/lib/k8s/index.d.ts.map +0 -1
- package/dist/src/lib/k8s/index.js +0 -39
- package/dist/src/lib/k8s/kinds.d.ts.map +0 -1
- package/dist/src/lib/k8s/kinds.js +0 -447
- package/dist/src/lib/k8s/tls.d.ts.map +0 -1
- package/dist/src/lib/k8s/tls.js +0 -74
- package/dist/src/lib/k8s/types.d.ts.map +0 -1
- package/dist/src/lib/k8s/types.js +0 -20
- package/dist/src/lib/k8s/upstream.d.ts.map +0 -1
- package/dist/src/lib/k8s/upstream.js +0 -50
- package/dist/src/lib/k8s/webhook.d.ts.map +0 -1
- package/dist/src/lib/k8s/webhook.js +0 -502
- package/dist/src/lib/logger.d.ts.map +0 -1
- package/dist/src/lib/logger.js +0 -125
- package/dist/src/lib/module.d.ts.map +0 -1
- package/dist/src/lib/module.js +0 -43
- package/dist/src/lib/processor.d.ts.map +0 -1
- package/dist/src/lib/processor.js +0 -85
- package/dist/src/lib/request.d.ts.map +0 -1
- package/dist/src/lib/request.js +0 -124
- package/dist/src/lib/types.d.ts.map +0 -1
- package/dist/src/lib/types.js +0 -34
- /package/dist/{src/lib → lib}/controller.d.ts +0 -0
- /package/dist/{src/lib → lib}/k8s/index.d.ts +0 -0
- /package/dist/{src/lib → lib}/k8s/kinds.d.ts +0 -0
- /package/dist/{src/lib → lib}/k8s/tls.d.ts +0 -0
- /package/dist/{src/lib → lib}/k8s/types.d.ts +0 -0
- /package/dist/{src/lib → lib}/logger.d.ts +0 -0
- /package/dist/{src/lib → lib}/module.d.ts +0 -0
- /package/dist/{src/lib → lib}/processor.d.ts +0 -0
package/dist/src/cli/test.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
4
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
-
};
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
const child_process_1 = require("child_process");
|
|
9
|
-
const chokidar_1 = require("chokidar");
|
|
10
|
-
const path_1 = require("path");
|
|
11
|
-
const util_1 = require("util");
|
|
12
|
-
const logger_1 = __importDefault(require("../../src/lib/logger"));
|
|
13
|
-
const build_1 = require("./build");
|
|
14
|
-
const exec = (0, util_1.promisify)(child_process_1.exec);
|
|
15
|
-
function default_1(program) {
|
|
16
|
-
program
|
|
17
|
-
.command("test")
|
|
18
|
-
.description("Test a Pepr Module locally")
|
|
19
|
-
.option("-w, --watch", "Watch for changes and re-run the test")
|
|
20
|
-
.action(async (opts) => {
|
|
21
|
-
logger_1.default.info("Test Module");
|
|
22
|
-
await buildAndTest();
|
|
23
|
-
if (opts.watch) {
|
|
24
|
-
const moduleFiles = (0, path_1.resolve)(".", "**", "*.ts");
|
|
25
|
-
const watcher = (0, chokidar_1.watch)(moduleFiles);
|
|
26
|
-
watcher.on("ready", () => {
|
|
27
|
-
logger_1.default.info(`Watching for changes in ${moduleFiles}`);
|
|
28
|
-
watcher.on("all", async (event, path) => {
|
|
29
|
-
logger_1.default.debug({ event, path }, "File changed");
|
|
30
|
-
await buildAndTest();
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
exports.default = default_1;
|
|
37
|
-
async function buildAndTest() {
|
|
38
|
-
const { path } = await (0, build_1.buildModule)();
|
|
39
|
-
logger_1.default.info(`Module built successfully at ${path}`);
|
|
40
|
-
try {
|
|
41
|
-
const { stdout, stderr } = await exec(`node ${path}`);
|
|
42
|
-
console.log(stdout);
|
|
43
|
-
console.log(stderr);
|
|
44
|
-
}
|
|
45
|
-
catch (e) {
|
|
46
|
-
logger_1.default.debug(e);
|
|
47
|
-
logger_1.default.error(`Error running module: ${e}`);
|
|
48
|
-
process.exit(1);
|
|
49
|
-
}
|
|
50
|
-
}
|
package/dist/src/cli/update.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
4
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
-
};
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
const child_process_1 = require("child_process");
|
|
9
|
-
const path_1 = require("path");
|
|
10
|
-
const prompts_1 = require("prompts");
|
|
11
|
-
const logger_1 = __importDefault(require("../../src/lib/logger"));
|
|
12
|
-
const templates_1 = require("./init/templates");
|
|
13
|
-
const utils_1 = require("./init/utils");
|
|
14
|
-
function default_1(program) {
|
|
15
|
-
program
|
|
16
|
-
.command("update")
|
|
17
|
-
.description("Update this Pepr module")
|
|
18
|
-
.option("--skip-template-update", "Skip updating the template files`")
|
|
19
|
-
.action(async (opts) => {
|
|
20
|
-
if (!opts.skipTemplateUpdate) {
|
|
21
|
-
const { confirm } = await (0, prompts_1.prompt)({
|
|
22
|
-
type: "confirm",
|
|
23
|
-
name: "confirm",
|
|
24
|
-
message: "This will overwrite previously auto-generated files inluding the capabilities/HelloPepr.ts file.\n" +
|
|
25
|
-
"Are you sure you want to continue?",
|
|
26
|
-
});
|
|
27
|
-
// If the user doesn't confirm, exit
|
|
28
|
-
if (!confirm) {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
console.log("Updating the Pepr module...");
|
|
33
|
-
try {
|
|
34
|
-
await (0, utils_1.write)((0, path_1.resolve)(templates_1.prettierRC.path), templates_1.prettierRC.data);
|
|
35
|
-
await (0, utils_1.write)((0, path_1.resolve)(templates_1.tsConfig.path), templates_1.tsConfig.data);
|
|
36
|
-
await (0, utils_1.write)((0, path_1.resolve)(".vscode", templates_1.snippet.path), templates_1.snippet.data);
|
|
37
|
-
await (0, utils_1.write)((0, path_1.resolve)("capabilities", templates_1.samplesYaml.path), templates_1.samplesYaml.data);
|
|
38
|
-
await (0, utils_1.write)((0, path_1.resolve)("capabilities", templates_1.helloPeprTS.path), templates_1.helloPeprTS.data);
|
|
39
|
-
// Update Pepr for the module
|
|
40
|
-
(0, child_process_1.execSync)("npm install pepr@latest", {
|
|
41
|
-
stdio: "inherit",
|
|
42
|
-
});
|
|
43
|
-
// Update Pepr globally
|
|
44
|
-
(0, child_process_1.execSync)("npm install -g pepr@latest", {
|
|
45
|
-
stdio: "inherit",
|
|
46
|
-
});
|
|
47
|
-
console.log(`Module updated!`);
|
|
48
|
-
}
|
|
49
|
-
catch (e) {
|
|
50
|
-
logger_1.default.debug(e);
|
|
51
|
-
if (e instanceof Error) {
|
|
52
|
-
logger_1.default.error(e.message);
|
|
53
|
-
}
|
|
54
|
-
process.exit(1);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
exports.default = default_1;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"capability.d.ts","sourceRoot":"","sources":["../../../src/lib/capability.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAA2B,MAAM,OAAO,CAAC;AAElE,OAAO,EAEL,OAAO,EAIP,aAAa,EAGb,YAAY,EACZ,SAAS,EACT,YAAY,EACb,MAAM,SAAS,CAAC;AAEjB;;GAEG;AACH,qBAAa,UAAW,YAAW,aAAa;IAC9C,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,WAAW,CAAC,CAAuB;IAG3C,OAAO,CAAC,iBAAiB,CAAoB;IAE7C,OAAO,CAAC,SAAS,CAAiB;IAElC,IAAI,QAAQ,IAAI,OAAO,EAAE,CAExB;IAED,IAAI,IAAI,WAEP;IAED,IAAI,WAAW,WAEd;IAED,IAAI,UAAU,aAEb;IAED,IAAI,gBAAgB,cAEnB;gBAEW,GAAG,EAAE,aAAa;IAQ9B;;;;;;;;OAQG;IACH,IAAI,4CAA6C,gBAAgB,qBAuF/D;CACH"}
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
4
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
-
};
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.Capability = void 0;
|
|
9
|
-
const k8s_1 = require("./k8s");
|
|
10
|
-
const logger_1 = __importDefault(require("./logger"));
|
|
11
|
-
const types_1 = require("./types");
|
|
12
|
-
/**
|
|
13
|
-
* A capability is a unit of functionality that can be registered with the Pepr runtime.
|
|
14
|
-
*/
|
|
15
|
-
class Capability {
|
|
16
|
-
get bindings() {
|
|
17
|
-
return this._bindings;
|
|
18
|
-
}
|
|
19
|
-
get name() {
|
|
20
|
-
return this._name;
|
|
21
|
-
}
|
|
22
|
-
get description() {
|
|
23
|
-
return this._description;
|
|
24
|
-
}
|
|
25
|
-
get namespaces() {
|
|
26
|
-
return this._namespaces || [];
|
|
27
|
-
}
|
|
28
|
-
get mutateOrValidate() {
|
|
29
|
-
return this._mutateOrValidate;
|
|
30
|
-
}
|
|
31
|
-
constructor(cfg) {
|
|
32
|
-
// Currently everything is considered a mutation
|
|
33
|
-
this._mutateOrValidate = types_1.HookPhase.mutate;
|
|
34
|
-
this._bindings = [];
|
|
35
|
-
/**
|
|
36
|
-
* The When method is used to register a capability action to be executed when a Kubernetes resource is
|
|
37
|
-
* processed by Pepr. The action will be executed if the resource matches the specified kind and any
|
|
38
|
-
* filters that are applied.
|
|
39
|
-
*
|
|
40
|
-
* @param model the KubernetesObject model to match
|
|
41
|
-
* @param kind if using a custom KubernetesObject not available in `a.*`, specify the GroupVersionKind
|
|
42
|
-
* @returns
|
|
43
|
-
*/
|
|
44
|
-
this.When = (model, kind) => {
|
|
45
|
-
const matchedKind = (0, k8s_1.modelToGroupVersionKind)(model.name);
|
|
46
|
-
// If the kind is not specified and the model is not a KubernetesObject, throw an error
|
|
47
|
-
if (!matchedKind && !kind) {
|
|
48
|
-
throw new Error(`Kind not specified for ${model.name}`);
|
|
49
|
-
}
|
|
50
|
-
const binding = {
|
|
51
|
-
// If the kind is not specified, use the matched kind from the model
|
|
52
|
-
kind: kind || matchedKind,
|
|
53
|
-
filters: {
|
|
54
|
-
name: "",
|
|
55
|
-
namespaces: [],
|
|
56
|
-
labels: {},
|
|
57
|
-
annotations: {},
|
|
58
|
-
},
|
|
59
|
-
callback: () => undefined,
|
|
60
|
-
};
|
|
61
|
-
const prefix = `${this._name}: ${model.name}`;
|
|
62
|
-
logger_1.default.info(`Binding created`, prefix);
|
|
63
|
-
const Then = (cb) => {
|
|
64
|
-
logger_1.default.info(`Binding action created`, prefix);
|
|
65
|
-
logger_1.default.debug(cb.toString(), prefix);
|
|
66
|
-
// Push the binding to the list of bindings for this capability as a new BindingAction
|
|
67
|
-
// with the callback function to preserve
|
|
68
|
-
this._bindings.push({
|
|
69
|
-
...binding,
|
|
70
|
-
callback: cb,
|
|
71
|
-
});
|
|
72
|
-
// Now only allow adding actions to the same binding
|
|
73
|
-
return { Then };
|
|
74
|
-
};
|
|
75
|
-
const ThenSet = (merge) => {
|
|
76
|
-
// Add the new action to the binding
|
|
77
|
-
Then(req => req.Merge(merge));
|
|
78
|
-
return { Then };
|
|
79
|
-
};
|
|
80
|
-
function InNamespace(...namespaces) {
|
|
81
|
-
logger_1.default.debug(`Add namespaces filter ${namespaces}`, prefix);
|
|
82
|
-
binding.filters.namespaces.push(...namespaces);
|
|
83
|
-
return { WithLabel, WithAnnotation, WithName, Then, ThenSet };
|
|
84
|
-
}
|
|
85
|
-
function WithName(name) {
|
|
86
|
-
logger_1.default.debug(`Add name filter ${name}`, prefix);
|
|
87
|
-
binding.filters.name = name;
|
|
88
|
-
return { WithLabel, WithAnnotation, Then, ThenSet };
|
|
89
|
-
}
|
|
90
|
-
function WithLabel(key, value = "") {
|
|
91
|
-
logger_1.default.debug(`Add label filter ${key}=${value}`, prefix);
|
|
92
|
-
binding.filters.labels[key] = value;
|
|
93
|
-
return { WithLabel, WithAnnotation, Then, ThenSet };
|
|
94
|
-
}
|
|
95
|
-
const WithAnnotation = (key, value = "") => {
|
|
96
|
-
logger_1.default.debug(`Add annotation filter ${key}=${value}`, prefix);
|
|
97
|
-
binding.filters.annotations[key] = value;
|
|
98
|
-
return { WithLabel, WithAnnotation, Then, ThenSet };
|
|
99
|
-
};
|
|
100
|
-
const bindEvent = (event) => {
|
|
101
|
-
binding.event = event;
|
|
102
|
-
return {
|
|
103
|
-
InNamespace,
|
|
104
|
-
Then,
|
|
105
|
-
ThenSet,
|
|
106
|
-
WithAnnotation,
|
|
107
|
-
WithLabel,
|
|
108
|
-
WithName,
|
|
109
|
-
};
|
|
110
|
-
};
|
|
111
|
-
return {
|
|
112
|
-
IsCreatedOrUpdated: () => bindEvent(types_1.Event.CreateOrUpdate),
|
|
113
|
-
IsCreated: () => bindEvent(types_1.Event.Create),
|
|
114
|
-
IsUpdated: () => bindEvent(types_1.Event.Update),
|
|
115
|
-
IsDeleted: () => bindEvent(types_1.Event.Delete),
|
|
116
|
-
};
|
|
117
|
-
};
|
|
118
|
-
this._name = cfg.name;
|
|
119
|
-
this._description = cfg.description;
|
|
120
|
-
this._namespaces = cfg.namespaces;
|
|
121
|
-
logger_1.default.info(`Capability ${this._name} registered`);
|
|
122
|
-
logger_1.default.debug(cfg);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
exports.Capability = Capability;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../../src/lib/controller.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAQvC,qBAAa,UAAU;IAKnB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,YAAY;IAC7B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC;IAC5B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC;IAP7B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAa;IACjC,OAAO,CAAC,OAAO,CAAS;gBAGL,MAAM,EAAE,YAAY,EACpB,YAAY,EAAE,UAAU,EAAE,EAC1B,UAAU,CAAC,SAAQ,OAAO,KAAK,IAAI,aAAA,EACnC,SAAS,CAAC,SAAQ,QAAQ,KAAK,IAAI,aAAA;IAuBtD,+BAA+B;IACxB,WAAW,SAAU,MAAM,UAoBhC;IAEF,OAAO,CAAC,MAAM,CAYZ;IAEF,OAAO,CAAC,OAAO,CAOb;IAEF,OAAO,CAAC,MAAM,CA+BZ;CACH"}
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
4
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
-
};
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.Controller = void 0;
|
|
9
|
-
const express_1 = __importDefault(require("express"));
|
|
10
|
-
const fs_1 = __importDefault(require("fs"));
|
|
11
|
-
const https_1 = __importDefault(require("https"));
|
|
12
|
-
const processor_1 = require("./processor");
|
|
13
|
-
// Load SSL certificate and key
|
|
14
|
-
const options = {
|
|
15
|
-
key: fs_1.default.readFileSync(process.env.SSL_KEY_PATH || "/etc/certs/tls.key"),
|
|
16
|
-
cert: fs_1.default.readFileSync(process.env.SSL_CERT_PATH || "/etc/certs/tls.crt"),
|
|
17
|
-
};
|
|
18
|
-
class Controller {
|
|
19
|
-
constructor(config, capabilities, beforeHook, afterHook) {
|
|
20
|
-
this.config = config;
|
|
21
|
-
this.capabilities = capabilities;
|
|
22
|
-
this.beforeHook = beforeHook;
|
|
23
|
-
this.afterHook = afterHook;
|
|
24
|
-
this.app = (0, express_1.default)();
|
|
25
|
-
this.running = false;
|
|
26
|
-
/** Start the webhook server */
|
|
27
|
-
this.startServer = (port) => {
|
|
28
|
-
if (this.running) {
|
|
29
|
-
throw new Error("Cannot start Pepr module: Pepr module was not instantiated with deferStart=true");
|
|
30
|
-
}
|
|
31
|
-
// Create HTTPS server
|
|
32
|
-
const server = https_1.default.createServer(options, this.app).listen(port, () => {
|
|
33
|
-
console.log(`Server listening on port ${port}`);
|
|
34
|
-
// Track that the server is running
|
|
35
|
-
this.running = true;
|
|
36
|
-
});
|
|
37
|
-
// Listen for the SIGTERM signal and gracefully close the server
|
|
38
|
-
process.on("SIGTERM", () => {
|
|
39
|
-
console.log("Received SIGTERM, closing server");
|
|
40
|
-
server.close(() => {
|
|
41
|
-
console.log("Server closed");
|
|
42
|
-
process.exit(0);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
this.logger = (req, res, next) => {
|
|
47
|
-
const startTime = Date.now();
|
|
48
|
-
res.on("finish", () => {
|
|
49
|
-
const now = new Date().toISOString();
|
|
50
|
-
const elapsedTime = Date.now() - startTime;
|
|
51
|
-
const message = `[${now}] ${req.method} ${req.originalUrl} - ${res.statusCode} - ${elapsedTime} ms\n`;
|
|
52
|
-
res.statusCode >= 400 ? console.error(message) : console.info(message);
|
|
53
|
-
});
|
|
54
|
-
next();
|
|
55
|
-
};
|
|
56
|
-
this.healthz = (req, res) => {
|
|
57
|
-
try {
|
|
58
|
-
res.send("OK");
|
|
59
|
-
}
|
|
60
|
-
catch (err) {
|
|
61
|
-
console.error(err);
|
|
62
|
-
res.status(500).send("Internal Server Error");
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
this.mutate = async (req, res) => {
|
|
66
|
-
try {
|
|
67
|
-
// Run the before hook if it exists
|
|
68
|
-
this.beforeHook && this.beforeHook(req.body?.request || {});
|
|
69
|
-
const name = req.body?.request?.name || "";
|
|
70
|
-
const namespace = req.body?.request?.namespace || "";
|
|
71
|
-
const gvk = req.body?.request?.kind || { group: "", version: "", kind: "" };
|
|
72
|
-
console.log(`Mutate request: ${gvk.group}/${gvk.version}/${gvk.kind}`);
|
|
73
|
-
name && console.log(` ${namespace}/${name}\n`);
|
|
74
|
-
// Process the request
|
|
75
|
-
const response = await (0, processor_1.processor)(this.config, this.capabilities, req.body.request);
|
|
76
|
-
// Run the after hook if it exists
|
|
77
|
-
this.afterHook && this.afterHook(response);
|
|
78
|
-
// Log the response
|
|
79
|
-
console.debug(response);
|
|
80
|
-
// Send a no prob bob response
|
|
81
|
-
res.send({
|
|
82
|
-
apiVersion: "admission.k8s.io/v1",
|
|
83
|
-
kind: "AdmissionReview",
|
|
84
|
-
response,
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
catch (err) {
|
|
88
|
-
console.error(err);
|
|
89
|
-
res.status(500).send("Internal Server Error");
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
// Middleware for logging requests
|
|
93
|
-
this.app.use(this.logger);
|
|
94
|
-
// Middleware for parsing JSON
|
|
95
|
-
this.app.use(express_1.default.json());
|
|
96
|
-
// Health check endpoint
|
|
97
|
-
this.app.get("/healthz", this.healthz);
|
|
98
|
-
// Mutate endpoint
|
|
99
|
-
this.app.post("/mutate", this.mutate);
|
|
100
|
-
if (beforeHook) {
|
|
101
|
-
console.info(`Using beforeHook: ${beforeHook}`);
|
|
102
|
-
}
|
|
103
|
-
if (afterHook) {
|
|
104
|
-
console.info(`Using afterHook: ${afterHook}`);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
exports.Controller = Controller;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../../src/lib/fetch.ts"],"names":[],"mappings":";AAIA,OAAO,CAAC,EAAE,EAAc,WAAW,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAErE,OAAO,EAAE,CAAC,IAAI,QAAQ,EAAE,CAAC;AAEzB,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI;IAC7B,IAAI,EAAE,CAAC,CAAC;IACR,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,wBAAsB,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,GAAG,WAAW,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CA8CpG"}
|
package/dist/src/lib/fetch.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
4
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
5
|
-
if (k2 === undefined) k2 = k;
|
|
6
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
7
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
8
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
9
|
-
}
|
|
10
|
-
Object.defineProperty(o, k2, desc);
|
|
11
|
-
}) : (function(o, m, k, k2) {
|
|
12
|
-
if (k2 === undefined) k2 = k;
|
|
13
|
-
o[k2] = m[k];
|
|
14
|
-
}));
|
|
15
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
16
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
17
|
-
}) : function(o, v) {
|
|
18
|
-
o["default"] = v;
|
|
19
|
-
});
|
|
20
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
21
|
-
if (mod && mod.__esModule) return mod;
|
|
22
|
-
var result = {};
|
|
23
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
24
|
-
__setModuleDefault(result, mod);
|
|
25
|
-
return result;
|
|
26
|
-
};
|
|
27
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
28
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
29
|
-
};
|
|
30
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
-
exports.fetch = exports.fetchRaw = void 0;
|
|
32
|
-
const http_status_codes_1 = require("http-status-codes");
|
|
33
|
-
const node_fetch_1 = __importStar(require("node-fetch"));
|
|
34
|
-
exports.fetchRaw = node_fetch_1.default;
|
|
35
|
-
const logger_1 = __importDefault(require("./logger"));
|
|
36
|
-
/**
|
|
37
|
-
* Perform an async HTTP call and return the parsed JSON response, optionally
|
|
38
|
-
* as a specific type.
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```ts
|
|
42
|
-
* fetch<string[]>("https://example.com/api/foo");
|
|
43
|
-
* ```
|
|
44
|
-
*
|
|
45
|
-
* @param url The URL or Request object to fetch
|
|
46
|
-
* @param init Additional options for the request
|
|
47
|
-
* @returns
|
|
48
|
-
*/
|
|
49
|
-
async function fetch(url, init) {
|
|
50
|
-
let data = undefined;
|
|
51
|
-
try {
|
|
52
|
-
logger_1.default.debug(`Fetching ${url}`);
|
|
53
|
-
const resp = await (0, node_fetch_1.default)(url, init);
|
|
54
|
-
const contentType = resp.headers.get("content-type") || "";
|
|
55
|
-
if (resp.ok) {
|
|
56
|
-
// Parse the response as JSON if the content type is JSON
|
|
57
|
-
if (contentType.includes("application/json")) {
|
|
58
|
-
data = await resp.json();
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
// Otherwise, return however the response was read
|
|
62
|
-
data = (await resp.text());
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return {
|
|
66
|
-
data,
|
|
67
|
-
ok: resp.ok,
|
|
68
|
-
status: resp.status,
|
|
69
|
-
statusText: resp.statusText,
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
catch (e) {
|
|
73
|
-
if (e instanceof node_fetch_1.FetchError) {
|
|
74
|
-
logger_1.default.debug(`Fetch failed: ${e instanceof Error ? e.message : e}`);
|
|
75
|
-
// Parse the error code from the FetchError or default to 400 (Bad Request)
|
|
76
|
-
const status = parseInt(e.code || "400");
|
|
77
|
-
return {
|
|
78
|
-
data,
|
|
79
|
-
ok: false,
|
|
80
|
-
status,
|
|
81
|
-
statusText: e.message,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
return {
|
|
85
|
-
data,
|
|
86
|
-
ok: false,
|
|
87
|
-
status: http_status_codes_1.StatusCodes.BAD_REQUEST,
|
|
88
|
-
statusText: "Unknown error",
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
exports.fetch = fetch;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"filter.d.ts","sourceRoot":"","sources":["../../../src/lib/filter.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAEhC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,WAwE/D"}
|
package/dist/src/lib/filter.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
4
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
-
};
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.shouldSkipRequest = void 0;
|
|
9
|
-
const logger_1 = __importDefault(require("./logger"));
|
|
10
|
-
/**
|
|
11
|
-
* shouldSkipRequest determines if a request should be skipped based on the binding filters.
|
|
12
|
-
*
|
|
13
|
-
* @param binding the capability action binding
|
|
14
|
-
* @param req the incoming request
|
|
15
|
-
* @returns
|
|
16
|
-
*/
|
|
17
|
-
function shouldSkipRequest(binding, req) {
|
|
18
|
-
const { group, kind, version } = binding.kind || {};
|
|
19
|
-
const { namespaces, labels, annotations, name } = binding.filters || {};
|
|
20
|
-
const { metadata } = req.object || {};
|
|
21
|
-
// Test for matching operation
|
|
22
|
-
if (binding.event && !binding.event.includes(req.operation)) {
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
// Test name first, since it's the most specific
|
|
26
|
-
if (name && name !== req.name) {
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
// Test for matching kinds
|
|
30
|
-
if (kind !== req.kind.kind) {
|
|
31
|
-
return true;
|
|
32
|
-
}
|
|
33
|
-
// Test for matching groups
|
|
34
|
-
if (group && group !== req.kind.group) {
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
// Test for matching versions
|
|
38
|
-
if (version && version !== req.kind.version) {
|
|
39
|
-
return true;
|
|
40
|
-
}
|
|
41
|
-
// Test for matching namespaces
|
|
42
|
-
if (namespaces.length && !namespaces.includes(req.namespace || "")) {
|
|
43
|
-
logger_1.default.debug("Namespace does not match");
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
// Test for matching labels
|
|
47
|
-
for (const [key, value] of Object.entries(labels)) {
|
|
48
|
-
const testKey = metadata?.labels?.[key];
|
|
49
|
-
// First check if the label exists
|
|
50
|
-
if (!testKey) {
|
|
51
|
-
logger_1.default.debug(`Label ${key} does not exist`);
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
// Then check if the value matches, if specified
|
|
55
|
-
if (value && testKey !== value) {
|
|
56
|
-
logger_1.default.debug(`${testKey} does not match ${value}`);
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
// Test for matching annotations
|
|
61
|
-
for (const [key, value] of Object.entries(annotations)) {
|
|
62
|
-
const testKey = metadata?.annotations?.[key];
|
|
63
|
-
// First check if the annotation exists
|
|
64
|
-
if (!testKey) {
|
|
65
|
-
logger_1.default.debug(`Annotation ${key} does not exist`);
|
|
66
|
-
return true;
|
|
67
|
-
}
|
|
68
|
-
// Then check if the value matches, if specified
|
|
69
|
-
if (value && testKey !== value) {
|
|
70
|
-
logger_1.default.debug(`${testKey} does not match ${value}`);
|
|
71
|
-
return true;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
// No failed filters, so we should not skip this request
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
exports.shouldSkipRequest = shouldSkipRequest;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,yBAAyB,CAAC;AAC1C,OAAO,EAAE,WAAW,IAAI,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,CAAC,EAAE,MAAM,OAAO,CAAC;AACxC,OAAO,GAAG,MAAM,UAAU,CAAC;AAC3B,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAGxC,OAAO,KAAK,KAAK,oBAAoB,MAAM,yBAAyB,CAAC;AACrE,OAAO,KAAK,KAAK,UAAU,MAAM,OAAO,CAAC;AAEzC,OAAO,EACL,CAAC;AACD,mGAAmG;AACnG,UAAU,EACV,WAAW,EACX,YAAY,EACZ,UAAU,EACV,GAAG,EACH,KAAK,EACL,KAAK,EACL,QAAQ,EACR,WAAW,EACX,GAAG,EAGH,UAAU,EACV,oBAAoB,GACrB,CAAC"}
|
package/dist/src/lib/index.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.k8s = exports.fetchStatus = exports.fetchRaw = exports.fetch = exports.utils = exports.Log = exports.Capability = exports.RegisterKind = exports.PeprRequest = exports.PeprModule = exports.a = void 0;
|
|
7
|
-
const client_node_1 = __importDefault(require("@kubernetes/client-node"));
|
|
8
|
-
exports.k8s = client_node_1.default;
|
|
9
|
-
const http_status_codes_1 = require("http-status-codes");
|
|
10
|
-
Object.defineProperty(exports, "fetchStatus", { enumerable: true, get: function () { return http_status_codes_1.StatusCodes; } });
|
|
11
|
-
const ramda_1 = __importDefault(require("ramda"));
|
|
12
|
-
exports.utils = ramda_1.default;
|
|
13
|
-
const capability_1 = require("./capability");
|
|
14
|
-
Object.defineProperty(exports, "Capability", { enumerable: true, get: function () { return capability_1.Capability; } });
|
|
15
|
-
const fetch_1 = require("./fetch");
|
|
16
|
-
Object.defineProperty(exports, "fetch", { enumerable: true, get: function () { return fetch_1.fetch; } });
|
|
17
|
-
Object.defineProperty(exports, "fetchRaw", { enumerable: true, get: function () { return fetch_1.fetchRaw; } });
|
|
18
|
-
const k8s_1 = require("./k8s");
|
|
19
|
-
Object.defineProperty(exports, "RegisterKind", { enumerable: true, get: function () { return k8s_1.RegisterKind; } });
|
|
20
|
-
Object.defineProperty(exports, "a", { enumerable: true, get: function () { return k8s_1.a; } });
|
|
21
|
-
const logger_1 = __importDefault(require("./logger"));
|
|
22
|
-
exports.Log = logger_1.default;
|
|
23
|
-
const module_1 = require("./module");
|
|
24
|
-
Object.defineProperty(exports, "PeprModule", { enumerable: true, get: function () { return module_1.PeprModule; } });
|
|
25
|
-
const request_1 = require("./request");
|
|
26
|
-
Object.defineProperty(exports, "PeprRequest", { enumerable: true, get: function () { return request_1.PeprRequest; } });
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/lib/k8s/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,+FAA+F;AAC/F,OAAO,EAAE,IAAI,IAAI,CAAC,EAAE,CAAC;AAErB,OAAO,EAAE,uBAAuB,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAExE,cAAc,SAAS,CAAC"}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
4
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
5
|
-
if (k2 === undefined) k2 = k;
|
|
6
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
7
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
8
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
9
|
-
}
|
|
10
|
-
Object.defineProperty(o, k2, desc);
|
|
11
|
-
}) : (function(o, m, k, k2) {
|
|
12
|
-
if (k2 === undefined) k2 = k;
|
|
13
|
-
o[k2] = m[k];
|
|
14
|
-
}));
|
|
15
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
16
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
17
|
-
}) : function(o, v) {
|
|
18
|
-
o["default"] = v;
|
|
19
|
-
});
|
|
20
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
21
|
-
if (mod && mod.__esModule) return mod;
|
|
22
|
-
var result = {};
|
|
23
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
24
|
-
__setModuleDefault(result, mod);
|
|
25
|
-
return result;
|
|
26
|
-
};
|
|
27
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
28
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
29
|
-
};
|
|
30
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
-
exports.RegisterKind = exports.gvkMap = exports.modelToGroupVersionKind = exports.a = void 0;
|
|
32
|
-
// Export kinds as a single object
|
|
33
|
-
const kind = __importStar(require("./upstream"));
|
|
34
|
-
exports.a = kind;
|
|
35
|
-
var kinds_1 = require("./kinds");
|
|
36
|
-
Object.defineProperty(exports, "modelToGroupVersionKind", { enumerable: true, get: function () { return kinds_1.modelToGroupVersionKind; } });
|
|
37
|
-
Object.defineProperty(exports, "gvkMap", { enumerable: true, get: function () { return kinds_1.gvkMap; } });
|
|
38
|
-
Object.defineProperty(exports, "RegisterKind", { enumerable: true, get: function () { return kinds_1.RegisterKind; } });
|
|
39
|
-
__exportStar(require("./types"), exports);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"kinds.d.ts","sourceRoot":"","sources":["../../../../src/lib/k8s/kinds.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3C,eAAO,MAAM,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CA4cnD,CAAC;AAEF,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,MAAM,GAAG,gBAAgB,CAErE;AAED;;;;;GAKG;AACH,eAAO,MAAM,YAAY,UAAW,YAAY,oBAAoB,gBAAgB,SAUnF,CAAC"}
|