Skip to content

Register Secret event handlers only on informers whose object type they handle - #9005

Closed
wallrj-cyberark wants to merge 2 commits into
cert-manager:masterfrom
wallrj-cyberark:8994-fix-b-split-informer-interface
Closed

wallrj-cyberark wants to merge 2 commits into
cert-manager:masterfrom
wallrj-cyberark:8994-fix-b-split-informer-interface

Conversation

@wallrj-cyberark

@wallrj-cyberark wallrj-cyberark commented Jul 10, 2026 •

Copy link
Copy Markdown
Member

Summary

Stop registering a single Secret event handler on both the typed informer and the metadata informer. AddEventHandler now registers only on the typed informer (cert-manager-labelled Secrets, delivered as *corev1.Secret), and a new AddMetadataEventHandler method on the Informer interface registers only on the metadata informer (all other Secrets, delivered as *v1.PartialObjectMetadata). A handler is therefore only ever called with the object type it was written for.

  • The issuer and clusterissuer controllers register on both informers: they react to any Secret change, including external credential Secrets.
  • The certificate sub-controllers register only on the typed informer: they only watch cert-manager-managed Secrets.
  • The non-filtered baseSecretInformer has no separate metadata informer, so its AddMetadataEventHandler is a no-op.

Background

#8994 (handler log spam and dropped events, found shortly after the 1.21.0 release): the generics refactor in #8407 changed handler signatures from func(interface{}) to func(U), so a handler typed for *corev1.Secret failed its type assertion whenever the metadata informer delivered a *v1.PartialObjectMetadata.

That bug is already fixed on master by #9004 (cherry-picked to release-1.21 as #9037), which widened the handler's return type so the assertion tolerates deliveries from either informer. This PR is a follow-up built on top of that fix: it removes the underlying flaw #9004 worked around, by making it impossible to register a handler on an informer whose object type it can't handle.

#9006 explored a larger controller-runtime alignment that included this same split; it was closed in favour of this PR (its closing comment sketches a path for the controller-runtime migration).

Testing

Release Note

Separate Secret informer event handler registration into typed and metadata-only paths, so a handler is only registered on the informer whose object type it can actually handle.

@cert-manager-prow cert-manager-prow Bot added dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Jul 10, 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 inteon 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

Add AddMetadataEventHandler to the Informer interface to separate handler
registration for the typed informer (cert-manager-labelled Secrets as
*corev1.Secret) from the metadata informer (non-labelled Secrets as
*v1.PartialObjectMetadata).

Previously, AddEventHandler registered the same handler on both informers,
causing type assertion failures when a handler typed as *corev1.Secret
received *v1.PartialObjectMetadata from the metadata informer. With this
change, each informer has its own registration method and controllers
opt in to metadata events explicitly.

The issuer and clusterissuer controllers register on both informers
because they need to react to changes in any Secret (including external
credential Secrets). The certificate sub-controllers only register on the
typed informer because they only watch cert-manager-managed Secrets.

Fixes: cert-manager#8994

Signed-off-by: Richard Wall <[email protected]>
Co-Authored-By: Claude <[email protected]>
Signed-off-by: Richard Wall <[email protected]>
@wallrj-cyberark
wallrj-cyberark force-pushed the 8994-fix-b-split-informer-interface branch from a030e83 to e9afe36 Compare July 10, 2026 16:46
@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 do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Jul 17, 2026
@wallrj-cyberark

Copy link
Copy Markdown
Member Author

Updated the description: #8994 itself is now fixed on master via #9004 (merged, cherry-picked to release-1.21 as #9037), so this PR is repositioned as a standalone structural follow-up built on top of that fix, rather than a competing approach. Added e2e log evidence (0 event-handler errors in this PR's own CI run) and the release note.

/kind bug

@cert-manager-prow cert-manager-prow Bot added kind/bug Categorizes issue or PR as related to a bug. and removed needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Jul 17, 2026
@wallrj

wallrj commented Aug 8, 2026

Copy link
Copy Markdown
Member

Wrapping this up from my personal account (I authored it as @wallrj-cyberark, my work account).

Re-verified today against current master (cc6a960): the branch merges cleanly, builds, and go test passes for internal/informers, issuers, clusterissuers and certificates/.... CI is green and the description has been updated now that approach C (#9006) is closed in favour of this PR. It just needs a review.

/assign @inteon

with claude fable-5

@wallrj wallrj changed the title Fix #8994 (approach B): split informer handler registration Register Secret event handlers only on informers whose object type they handle Aug 8, 2026
@wallrj
wallrj requested a balanced review from Copilot August 20, 2026 18:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Separates typed and metadata-only Secret event registration to prevent handlers receiving incompatible object types.

Changes:

  • Adds AddMetadataEventHandler to the informer interface.
  • Routes filtered Secret handlers to the appropriate informer.
  • Registers metadata handlers where controllers require unmanaged Secret events.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
internal/informers/core.go Extends the informer contract.
internal/informers/core_basic.go Adds the metadata-handler no-op wrapper.
internal/informers/core_filteredsecrets.go Separates typed and metadata registrations.
pkg/controller/issuers/controller.go Watches both Secret informer paths.
pkg/controller/clusterissuers/controller.go Watches both Secret informer paths.
pkg/controller/certificaterequests/selfsigned/selfsigned.go Registers both Secret handler paths.
pkg/controller/certificatesigningrequests/selfsigned/selfsigned.go Registers both Secret handler paths.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 197 to 203
func (i *informer) AddEventHandler(handler cache.ResourceEventHandler) (cache.ResourceEventHandlerRegistration, error) {
_, err := i.typedInformer.AddEventHandler(handler)
return nil, err
}

func (i *informer) AddMetadataEventHandler(handler cache.ResourceEventHandler) (cache.ResourceEventHandlerRegistration, error) {
_, err := i.metadataInformer.AddEventHandler(handler)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Done in d89568e: added Test_informer_handlerRegistrationSplit, which asserts via counting stubs that AddEventHandler registers only on the typed informer and AddMetadataEventHandler only on the metadata informer, and Test_singleInformer_AddMetadataEventHandler_noop, which asserts the base informer's metadata path registers nothing. A return to dual registration now fails the unit suite.

with claude fable-5

Prove that AddEventHandler registers only on the typed informer,
AddMetadataEventHandler registers only on the metadata informer, and the
base (non-filtered) Secret informer's AddMetadataEventHandler is a
no-op. Guards against a return to the dual registration that caused
issue 8994.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01P3MSNdj39v66Wbeebgs3FW
Signed-off-by: Richard Wall <[email protected]>
@cert-manager-prow cert-manager-prow Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 1, 2026
@wallrj-cyberark
wallrj-cyberark requested review from erikgb and inteon and removed request for erikgb September 1, 2026 12:03
@wallrj-cyberark

Copy link
Copy Markdown
Member Author

/retest

Comment on lines +101 to +105
// singleInformer wraps a cache.SharedIndexInformer to satisfy the Informer
// interface. AddMetadataEventHandler is a no-op because the base (non-filtered)
// Secret informer caches all Secrets fully — there is no separate metadata
// informer.
type singleInformer struct {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems weird to me, someone might call AddMetadataEventHandler thinking they added the handler to the metadata event handler, right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe I should not have made the event handlers generic?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  1. The reason we want the event handlers to be typed is so we can more cleanly define filters, eg. don't do anything if the contents of the Secret object did not change/ a specific field did not change.

  2. When we queue resources, we queue the NamespacedNames and fetch the resource on reconcile to get the latest version of the resource and allow the queue to deduplicate.

  3. Some of the advanced filters will probably only work for the typed resources, not the partialmedatadata resources. 🤔

@wallrj

wallrj commented Sep 12, 2026

Copy link
Copy Markdown
Member

Closing this PR. The bug it grew out of is fixed, and the change points in the opposite direction to where we want the informer code to go.

The closing comment on #9006 sketches a migration path: move one small controller, such as clusterissuers, to controller-runtime end to end, then use it as the template for the rest.

[Claude Fable 5]

@wallrj wallrj closed this Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/bug Categorizes issue or PR as related to a bug. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants