Repository navigation
Register Secret event handlers only on informers whose object type they handle - #9005
wallrj-cyberark wants to merge 2 commits into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
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]>
a030e83 to
e9afe36
Compare
|
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 |
|
Wrapping this up from my personal account (I authored it as @wallrj-cyberark, my work account). Re-verified today against current /assign @inteon with claude fable-5 |
There was a problem hiding this comment.
Pull request overview
Separates typed and metadata-only Secret event registration to prevent handlers receiving incompatible object types.
Changes:
- Adds
AddMetadataEventHandlerto 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.
| 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) |
There was a problem hiding this comment.
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]>
|
/retest |
| // 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 { |
There was a problem hiding this comment.
This seems weird to me, someone might call AddMetadataEventHandler thinking they added the handler to the metadata event handler, right?
There was a problem hiding this comment.
Maybe I should not have made the event handlers generic?
There was a problem hiding this comment.
-
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.
-
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.
-
Some of the advanced filters will probably only work for the typed resources, not the partialmedatadata resources. 🤔
|
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 [Claude Fable 5] |
Summary
Stop registering a single Secret event handler on both the typed informer and the metadata informer.
AddEventHandlernow registers only on the typed informer (cert-manager-labelled Secrets, delivered as*corev1.Secret), and a newAddMetadataEventHandlermethod on theInformerinterface 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.baseSecretInformerhas no separate metadata informer, so itsAddMetadataEventHandleris 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{})tofunc(U), so a handler typed for*corev1.Secretfailed its type assertion whenever the metadata informer delivered a*v1.PartialObjectMetadata.That bug is already fixed on
masterby #9004 (cherry-picked torelease-1.21as #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
go testpasses in./internal/informers/...,./pkg/controller/certificates/...,./pkg/controller/issuers/...and./pkg/controller/clusterissuers/....OnAdd/OnUpdate/OnDeletemissing-object errors reported in cert-manager 1.21: filteredEventHandler log spam and dropped events for PartialObjectMetadata Secrets #8994 — and not because the run was idle: the Secret metadata informer is active ("Caches populated" type="*v1.PartialObjectMetadata") and 4,390 of the 13,007 log lines are Secret-related.Release Note