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: update component-webhooks example to use new Pepr k8s fluent client #2090

Merged
merged 17 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
84 changes: 49 additions & 35 deletions examples/component-webhooks/capabilities/hook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Capability, a, Log, k8s } from "pepr";
import { Capability, a, Log, K8s, kind } from "pepr";

/**
* The Webhook Capability is an example capability to demonstrate using webhooks to interact with Zarf package deployments.
Expand Down Expand Up @@ -63,9 +63,9 @@ When(a.Secret)
// Update the secret noting that the webhook is running for this component
secretData.componentWebhooks[deployedComponent.name] = {
"test-webhook": {
"name": "test-webhook",
"status": "Running",
"observedGeneration": secretData.generation,
name: "test-webhook",
status: "Running",
observedGeneration: secretData.generation,
},
};

Expand All @@ -86,46 +86,60 @@ When(a.Secret)
async function sleepAndChangeStatus(secretName: string, componentName: string) {
await sleep(10);
lucasrod16 marked this conversation as resolved.
Show resolved Hide resolved

// Configure the k8s api client
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sCoreApi = kc.makeApiClient(k8s.CoreV1Api);
const ns = "zarf";

let secret: a.Secret;

// Fetch the package secret
try {
const response = await k8sCoreApi.readNamespacedSecret(secretName, "zarf");
const v1Secret = response.body;
secret = await K8s(kind.Secret).InNamespace(ns).Get(secretName);
} catch (err) {
Log.error(
`Error: Failed to get package secret '${secretName}' in namespace '${ns}': ${JSON.stringify(
err,
)}`,
);
}

const secretString = atob(v1Secret.data.data);
const secretData = JSON.parse(secretString);
const secretString = atob(secret.data.data);
const secretData = JSON.parse(secretString);

// Update the webhook status if the observedGeneration matches
const componentWebhook =
secretData.componentWebhooks[componentName]?.["test-webhook"];
// Update the webhook status if the observedGeneration matches
const componentWebhook =
secretData.componentWebhooks[componentName]?.["test-webhook"];

if (componentWebhook?.observedGeneration === secretData.generation) {
componentWebhook.status = "Succeeded";
if (componentWebhook?.observedGeneration === secretData.generation) {
componentWebhook.status = "Succeeded";

secretData.componentWebhooks[componentName]["test-webhook"] =
componentWebhook;
}
secretData.componentWebhooks[componentName]["test-webhook"] =
componentWebhook;
}

secret.data.data = btoa(JSON.stringify(secretData));

v1Secret.data.data = btoa(JSON.stringify(secretData));

// Patch the secret back to the cluster
await k8sCoreApi.patchNamespacedSecret(
secretName,
"zarf",
v1Secret,
undefined,
undefined,
undefined,
undefined,
undefined,
{ headers: { "Content-Type": "application/strategic-merge-patch+json" } },
// Patch the secret back to the cluster
// Use Server-Side force apply to forcefully take ownership of the package secret data.data field
// Doing a Server-Side apply without the force option will result in a FieldManagerConflict error due to Zarf owning the object.
// See the following PR for more information: https://github.com/defenseunicorns/kubernetes-fluent-client/pull/20
try {
await K8s(kind.Secret).Apply(
{
metadata: {
name: secretName,
namespace: ns,
},
data: {
data: secret.data.data,
},
},
{ force: true },
);
} catch (err) {
Log.error(`Unable to update the package secret: ${JSON.stringify(err)}`);
return err;
Log.error(
`Error: Failed to update package secret '${secretName}' in namespace '${ns}': ${JSON.stringify(
err,
)}`,
);
}
}

Expand Down
Loading