Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Add a workaround for updating webhook status in package secret
Browse files Browse the repository at this point in the history
Pepr fluent client does not currently support force Apply()

Workaround is to clear managedFields and updated webhook status in a single Patch request
  • Loading branch information
Lucas Rodriguez committed Oct 8, 2023
1 parent 0eb03b5 commit fe59a0f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
53 changes: 37 additions & 16 deletions capabilities/webhook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Capability, Log, a, K8s, kind } from "pepr";
// import { Operation } from "fast-json-patch";
import { Operation } from "fast-json-patch";
import { isECRregistry, getAccountId, getRepositoryNames } from "./lib/utils";
import { ECRPublic, publicECRURLPattern } from "./ecr-public";
import { ECRPrivate, privateECRURLPattern } from "./ecr-private";
Expand Down Expand Up @@ -198,7 +198,9 @@ async function updateWebhookStatus(
secret = await K8s(kind.Secret).InNamespace(ns).Get(secretName);
} catch (err) {
Log.error(
`Error: Failed to get package secret '${secretName}' in namespace '${ns}': ${err.message}`,
`Error: Failed to get package secret '${secretName}' in namespace '${ns}': ${JSON.stringify(
err,
)}`,
);
}

Expand Down Expand Up @@ -232,25 +234,44 @@ async function updateWebhookStatus(
secret.data.data = JSON.stringify(deployedPackage);
}

// const patchOperations: Operation[] = [
// { op: "replace", path: "/data/data", value: secret.data.data },
// ];
// Clear managedFields to allow Pepr to take ownership of the secret data.data field and update webhook status
// For more information on clearing managedFields to take ownership of an object's field(s): https://kubernetes.io/docs/reference/using-api/server-side-apply/#clearing-managedfields
// TODO: Update to use Server-Side force apply when available in Pepr: https://github.com/defenseunicorns/kubernetes-fluent-client/issues/9
const patchOps: Operation[] = [
{ op: "replace", path: "/metadata/managedFields", value: [{}] },
{ op: "replace", path: "/data/data", value: secret.data.data },
];

// await K8s(kind.Secret).Patch(patchOperations);
const kube = K8s(kind.Secret, { namespace: ns, name: secretName });

try {
await K8s(kind.Secret).Apply({
metadata: {
name: secretName,
namespace: ns,
},
data: {
data: secret.data.data,
},
});
await kube.Patch(patchOps);
Log.debug(
`Successfully updated package secret '${secretName}' in namespace '${ns}'`,
);
} catch (err) {
Log.error(
`Error: Failed to update package secret '${secretName}' in namespace '${ns}'`,
`Error: Failed to update package secret '${secretName}' in namespace '${ns}': ${JSON.stringify(
err,
)}`,
);
}

// try {
// await K8s(kind.Secret).Apply({
// metadata: {
// name: secretName,
// namespace: ns,
// },
// data: {
// data: secret.data.data,
// },
// });
// } catch (err) {
// Log.error(
// `Error: Failed to update package secret '${secretName}' in namespace '${ns}': ${JSON.stringify(
// err,
// )}`,
// );
// }
}
16 changes: 8 additions & 8 deletions manifests/pepr-module-b95dbd80-e078-5eb9-aaf3-bcb9567417d0.yaml

Large diffs are not rendered by default.

0 comments on commit fe59a0f

Please sign in to comment.