Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove webhooks after exiting npx pepr dev #816

Merged
merged 8 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/030_user-guide/010_pepr-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ Update the current Pepr Module to the latest SDK version. This command is not re

Connect a local cluster to a local version of the Pepr Controller to do real-time debugging of your module. Note the `npx pepr dev` assumes a K3d cluster is running by default. If you are working with Kind or another docker-based K8s distro, you will need to pass the `--host host.docker.internal` option to `npx pepr dev`. If working with a remote cluster you will have to give Pepr a host path to your machine that is reachable from the K8s cluster.

NOTE: This command, by necessity, installs resources into the cluster you run it against. Generally, these resources are removed once the `pepr dev` session ends but there are two notable exceptions:
- the `pepr-system` namespace, and
- the `PeprStore` CRD.

These can't be auto-removed because they're global in scope & doing so would risk wrecking any other Pepr deployments that are already running in-cluster. If (for some strange reason) you're _not_ `pepr dev`-ing against an ephemeral dev cluster and need to keep the cluster clean, you'll have to remove these hold-overs yourself (or not)!

**Options:**

- `-h, --host [host]` - Host to listen on (default: "host.k3d.internal")
Expand Down
20 changes: 19 additions & 1 deletion src/cli/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { validateCapabilityNames } from "../lib/helpers";
import { Assets } from "../lib/assets";
import { buildModule, loadModule } from "./build";
import { RootCmd } from "./root";

import { K8s, kind } from "kubernetes-fluent-client";
import { PeprStore } from "../lib/k8s";
export default function (program: RootCmd) {
program
.command("dev")
Expand Down Expand Up @@ -49,6 +50,9 @@ export default function (program: RootCmd) {

try {
let program: ChildProcess;
const name = `pepr-${cfg.pepr.uuid}`;
const scheduleStore = `pepr-${cfg.pepr.uuid}-schedule`;
const store = `pepr-${cfg.pepr.uuid}-store`;

// Run the processed javascript file
const runFork = async () => {
Expand Down Expand Up @@ -77,6 +81,20 @@ export default function (program: RootCmd) {
},
stdio: "inherit",
});

program.on("close", async () => {
await Promise.all([
K8s(kind.MutatingWebhookConfiguration).Delete(name),
K8s(kind.ValidatingWebhookConfiguration).Delete(name),
K8s(PeprStore).InNamespace("pepr-system").Delete(scheduleStore),
K8s(PeprStore).InNamespace("pepr-system").Delete(store),
]);
});

// listen for CTRL+C and remove webhooks
process.on("SIGINT", () => {
console.debug(`Received SIGINT, removing webhooks`);
});
};

await buildModule(async r => {
Expand Down
Loading