Skip to content

Improve Challenge finalizer handling#8470

Closed
erikgb wants to merge 1 commit intocert-manager:masterfrom
erikgb:fix-challenge-finalizer
Closed

Improve Challenge finalizer handling#8470
erikgb wants to merge 1 commit intocert-manager:masterfrom
erikgb:fix-challenge-finalizer

Conversation

@erikgb
Copy link
Copy Markdown
Member

@erikgb erikgb commented Feb 2, 2026

Pull Request Motivation

I started looking into updateObject for challenges when facing issues working on eventually graduating our ServerSideApply feature gate in #7908. Because of serious limitations in the fake client, in particular, kubernetes/kubernetes#136672, I wanted to refactor code in this area.

In this PR, I try to separate the challenge status subresource updates from finalizers modifications on the challenge main resource. This reduces the pain when migrating to SSA in tests using the fake ("unreal") client.

To set/unset finalizers, I propose using server-side apply unconditionally, so we ignore the ServerSideApply feature gate here. This was done because I was unable to construct an API supporting both CSA and SSA well. The alternative would be to bring the feature gate reasoning into the functional code, but I'm personally not a huge fan of this.

Kind

/kind bug

Release Note

Use server-side-apply to maintain challenge finalizers

@cert-manager-prow cert-manager-prow bot added kind/bug Categorizes issue or PR as related to a bug. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. labels Feb 2, 2026
@cert-manager-prow
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign maelvls for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@cert-manager-prow cert-manager-prow bot added area/acme Indicates a PR directly modifies the ACME Issuer code size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 2, 2026
@erikgb
Copy link
Copy Markdown
Member Author

erikgb commented Feb 2, 2026

/test pull-cert-manager-master-e2e-v1-35-upgrade

@erikgb
Copy link
Copy Markdown
Member Author

erikgb commented Feb 2, 2026

/retest

@erikgb erikgb force-pushed the fix-challenge-finalizer branch from c0e2207 to d38f004 Compare February 2, 2026 14:36
@erikgb
Copy link
Copy Markdown
Member Author

erikgb commented Feb 2, 2026

/test pull-cert-manager-master-e2e-v1-35-upgrade

@erikgb erikgb force-pushed the fix-challenge-finalizer branch 9 times, most recently from 063650c to 531222d Compare February 2, 2026 20:41
@cert-manager-prow cert-manager-prow bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Feb 2, 2026
@erikgb erikgb changed the title WIP: Improve Challenge finalizer handling Improve Challenge finalizer handling Feb 2, 2026
@cert-manager-prow cert-manager-prow bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 2, 2026
@erikgb erikgb requested a review from Copilot February 2, 2026 21:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves challenge finalizer handling in the ACME challenges controller by separating finalizer updates from status updates, and unconditionally using server-side apply (SSA) for finalizer modifications regardless of the ServerSideApply feature gate setting.

Changes:

  • Separates finalizer updates from status updates in the challenge sync logic
  • Uses SSA unconditionally for setting/unsetting finalizers with CSA-to-SSA migration support
  • Ensures finalizers are only removed after successful challenge cleanup, preventing premature removal

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pkg/controller/test/context_builder.go Adds default "test-manager" field manager to test context builder
pkg/controller/certificatesigningrequests/acme/acme_test.go Updates test assertions to use CreateActionWithOptions and include FieldManager
pkg/controller/certificaterequests/acme/acme_test.go Updates test assertions to use CreateActionWithOptions and include FieldManager
pkg/controller/acmechallenges/update_test.go Refactors tests to separate applyFinalizers and updateStatus test cases
pkg/controller/acmechallenges/update.go Implements new applyFinalizers method using SSA with CSA migration support
pkg/controller/acmechallenges/sync_test.go Updates test expectations to reflect new finalizer handling behavior
pkg/controller/acmechallenges/sync.go Refactors sync logic to call finalize and applyFinalizers separately
pkg/controller/acmechallenges/finalizer_test.go Updates test expectations for applyFinalizersRequired function
pkg/controller/acmechallenges/finalizer.go Refactors finalizer logic to check if finalizers need to be applied
pkg/controller/acmechallenges/controller.go Updates scheduler to use updateStatus instead of updateObject

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@erikgb erikgb force-pushed the fix-challenge-finalizer branch from bf7925e to 55dd47d Compare February 2, 2026 21:55
}
// After adding the finalizer, return and let the controller reconcile again
// so that subsequent processing happens with the finalizer already in place.
return nil
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Any idea why we are returning here? Why not continue with the reconciliation?

@erikgb erikgb requested a review from Copilot February 2, 2026 21:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cert-manager-prow cert-manager-prow bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 8, 2026
@cert-manager-prow cert-manager-prow bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 9, 2026
@erikgb erikgb force-pushed the fix-challenge-finalizer branch 2 times, most recently from 566fbf1 to 08da47e Compare February 9, 2026 19:50
@cert-manager-prow cert-manager-prow bot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Feb 9, 2026
@erikgb erikgb force-pushed the fix-challenge-finalizer branch from 08da47e to 1b876bf Compare February 10, 2026 19:54
@cert-manager-prow cert-manager-prow bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 10, 2026
@erikgb erikgb requested a review from Copilot February 10, 2026 19:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if _, err = c.applyFinalizers(ctx, ch, nil); err != nil {
return err
}
return nil
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

In the challengeFinished path, updateStatus() may set the named return err to an aggregate, but the function later does return nil, which overwrites err and drops any status update error. Consider returning err at the end of this branch (or explicitly handling/propagating the updateStatus error) so failures to persist status still cause a retry.

Suggested change
return nil
return err

Copilot uses AI. Check for mistakes.
Comment on lines +73 to +88
func (o *defaultObjectUpdater) applyFinalizers(ctx context.Context, challenge *cmacme.Challenge, finalizers []string) (*cmacme.Challenge, error) {
if err := o.upgradeManagedFields(ctx, challenge); err != nil {
return nil, err
return nil, ignoreNotFound(err)
}

ac := cmacmeac.Challenge(challenge.Name, challenge.Namespace).
// Set UID to ensure we never create a new challenge.
// Apply semantics are always create-or-update,
// and the challenge might have been deleted.
WithUID(challenge.UID).
// FIXME: This will claim ownership of all finalizers, which is obviously wrong.
WithFinalizers(challenge.Finalizers...)
return o.cl.AcmeV1().Challenges(challenge.Namespace).Apply(
WithFinalizers(finalizers...)
obj, err := o.cl.AcmeV1().Challenges(challenge.Namespace).Apply(
ctx, ac,
metav1.ApplyOptions{Force: true, FieldManager: o.fieldManager},
)
return obj, ignoreNotFound(err)
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

applyFinalizers uses server-side apply with Force: true on metadata.finalizers, which is an atomic list. Applying a subset (or omitting it to clear) effectively takes ownership of the entire field and can unintentionally drop other controllers’/users’ finalizers. Consider switching finalizer management to a non-SSA approach that can surgically add/remove only cert-manager’s finalizer while preserving others (e.g., read-modify-write Update with conflict retry), or otherwise ensure foreign finalizers are retained.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I am pretty sure this comment is incorrect. Finalizers is NOT an atomic list, but has set semantics with ownership tracked by finalizer.

@erikgb erikgb changed the title WIP: Improve Challenge finalizer handling Improve Challenge finalizer handling Feb 10, 2026
@cert-manager-prow cert-manager-prow bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 10, 2026
@erikgb
Copy link
Copy Markdown
Member Author

erikgb commented Feb 10, 2026

/test pull-cert-manager-master-e2e-v1-35

@erikgb
Copy link
Copy Markdown
Member Author

erikgb commented Feb 16, 2026

@inteon @wallrj, a gentle ping for review of this one. 🙏

@maelvls maelvls added this to the 1.20 milestone Feb 17, 2026
Signed-off-by: Erik Godding Boye <egboye@gmail.com>
@inteon inteon force-pushed the fix-challenge-finalizer branch from 1b876bf to f196ce7 Compare February 17, 2026 11:25
@cert-manager-prow cert-manager-prow bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 17, 2026
@cert-manager-prow
Copy link
Copy Markdown
Contributor

PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@maelvls maelvls removed this from the 1.20 milestone Feb 24, 2026
@inteon inteon marked this pull request as draft February 24, 2026 10:47
@cert-manager-prow cert-manager-prow bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 24, 2026
@erikgb
Copy link
Copy Markdown
Member Author

erikgb commented Feb 28, 2026

The main goal of this PR has been achieved by #8519.

/close

@cert-manager-prow
Copy link
Copy Markdown
Contributor

@erikgb: Closed this PR.

Details

In response to this:

The main goal of this PR has been achieved by #8519.

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/acme Indicates a PR directly modifies the ACME Issuer code dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. kind/bug Categorizes issue or PR as related to a bug. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants