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

feat: force options on apply #20

Merged
merged 2 commits into from
Oct 9, 2023
Merged

feat: force options on apply #20

merged 2 commits into from
Oct 9, 2023

Conversation

cmwylie19
Copy link
Contributor

@cmwylie19 cmwylie19 commented Oct 6, 2023

Description

@lucasrod16 discovered a bug in Pepr when trying to apply a secret that was initially created by Zarf:

  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}'`,
    );
  }

The error had to do with a FieldManagerConflict:

"Apply failed with 1 conflict: conflict with \\\"zarf\\\" using v1: .data.data\",\"reason\":\"Conflict\",\"details\":{\"causes\":[{\"reason\":\"FieldManagerConflict\",\"message\":\"conflict with \\\"zarf\\\" using v1\",\"field\":\".data.data\"}]},\"code\":409},\"ok\":false,\"status\":409,\"statusText\":\"Conflict\"}"}

There should be more options exposed in the future but this initial patch it to unblock @lucasrod16 .

Related Issue

Fixes #9

Relates to #

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Other (security config, docs update, etc)

Checklist before merging

@cmwylie19 cmwylie19 changed the title Force Options on Apply feat: Force Options on Apply Oct 6, 2023
@cmwylie19 cmwylie19 changed the title feat: Force Options on Apply feat: force options on apply Oct 6, 2023
@cmwylie19 cmwylie19 requested a review from jeff-mccoy October 6, 2023 19:04
@lucasrod16
Copy link
Contributor

@cmwylie19 @jeff-mccoy I have a workaround to this problem.

Clearing/stripping the managedFields field from the object and updating the data.data field in the same Patch() operation allows Pepr to take ownership of that field and resolves the field manager conflict error.

https://kubernetes.io/docs/reference/using-api/server-side-apply/#clearing-managedfields

// 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 },
];

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

try {
  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}': ${JSON.stringify(
      err,
    )}`,
  );
}

Copy link
Member

@jeff-mccoy jeff-mccoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall lgtm, small jsdoc issue, but I'lll open a new PR because we have a lot of them

@jeff-mccoy jeff-mccoy merged commit 3fec6ed into main Oct 9, 2023
9 of 11 checks passed
@jeff-mccoy jeff-mccoy deleted the 9 branch October 9, 2023 04:43
@github-actions
Copy link

github-actions bot commented Oct 9, 2023

🎉 This PR is included in version 1.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Apply has no Force option to it, and will fail if it has to overwrite something (WIP)
3 participants