Skip to content

⚠️ Migration to the new events API #3262

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

clebs
Copy link
Contributor

@clebs clebs commented Jul 22, 2025

This Pull Request migrates from the old events API (k8s.io/client-go/tools/record) to the new API (k8s.io/client-go/tools/events).

Closes #2141

@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 22, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

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

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 22, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @clebs. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 22, 2025
@clebs
Copy link
Contributor Author

clebs commented Jul 22, 2025

Open Items:

  • StartRecordingToSink: manage the context/channel in the provider on ensureRecording and Stop.
  • StartWatcher: see if this is still needed and how to adapt it if it is.
  • Leader election: resourcelock.ResourceLockConfig is still using the old API.
    This has now been solved by adding an adapter that turns old API calls into new API calls (albeit pretty simplistic). So that way on the backend we have only the new API and places that require the old can use the adapter.
  • Update unit tests.

@clebs clebs changed the title ⚠ Migration to the new events API ⚠️ Migration to the new events API Jul 22, 2025
@alvaroaleman
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 22, 2025
@k8s-ci-robot
Copy link
Contributor

@clebs: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-controller-runtime-test 54f66ac link true /test pull-controller-runtime-test
pull-controller-runtime-apidiff 54f66ac link false /test pull-controller-runtime-apidiff

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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. I understand the commands that are listed here.

Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

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

two questions

GetEventRecorder(name string) events.EventRecorder
// GetOldEventRecorder returns an EventRecorder for the old events API.
// The old API is not 100% supported anymore, use the new one whenever possible.
GetOldEventRecorder(name string) record.EventRecorder
Copy link
Member

Choose a reason for hiding this comment

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

Rather than extending the interface here, would it be possible to build a wrapper that makes a record.EventRecorder out of a events.EventRecorder?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it is already built as a wrapper under the hood in intrec. I added it here with the idea to support both APIs for some time. If it is not what we are going for, having the wrapper only where it is needed is better.

Copy link
Member

@alvaroaleman alvaroaleman Jul 23, 2025

Choose a reason for hiding this comment

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

I think there are two main ways we could go about this:

  1. We make this a non-breaking change by keeping the existing GetEventRecorderFor with the existing signature and add a new GetEventRecorder with the new events.EventRecorder as return but mark the GetEventRecorderFor as // deprecated. At some point in a future release we will then remove GetEventRecorderFor
  2. We make this a breaking change by changing the signature of the existing GetEventRecorderFor to return an events.EventRecorder and we provide an adapter somewhere to go from events.EventRecorder to a record.EventRecorder for places like the leader election

My vote if this is possible would be to do 1) as this is the least disruptive for c-r users but its possible I've missed something and it isn't possible

Copy link
Contributor Author

@clebs clebs Jul 24, 2025

Choose a reason for hiding this comment

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

Option 1 is doable, but I have to see if the wrapper approach is good enough. Reason being, that the wrapper calls the new API under the hood and both APIs are quite different. The old API has AnnotatedEventf while the new one does not and the new one has things like relatedObject and action.

Otherwise, the other way to support both is having 2 distinct implementations in separate packages and duplicate the broadcaster, provider, interfaces and injection at the manager and cluster levels.
Finally also duplicate the tests so both versions are covered.

I will test if the wrapper is viable, present the results and then we can make an informed decision.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have tested out the wrapper and it has the one downside: we loose the ability to set annotations on events since there is no equivalent AnnotatedEventf. I also have tried to get some info on why that is the case.

Seeing that, the wrapper can not provide the same features as the old API has, so the only option to support both is probably having both implementations at the same time.

@alvaroaleman my proposal is to duplicate the internal events package and have both implementations separate from each other even if that means lots of duplication for some time. It will make it easier later on to cleanup the old API when removed. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

@alvaroaleman my proposal is to duplicate the internal events package and have both implementations separate from each other even if that means lots of duplication for some time. It will make it easier later on to cleanup the old API when removed. What do you think?

Not great, but given its for a limited time only that seems okay to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am also not a great fan.

I will do the exercise and see how much I can reuse without having a tangled nightmare.

Worst case it is all duplicate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alvaroaleman I have gone through this and now this PR supports both the old and new events APIs with as little duplication as possible.

There is still a fair amount of duplication but it is confined mostly to internal code and should still be relatively easy to cleanup later on.

If you agree with this approach I will move to adding tests for the new API and move this out of draft.

@clebs clebs force-pushed the new-events-api branch 2 times, most recently from 6bfbd83 to 0528c33 Compare July 24, 2025 12:54
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 29, 2025
clebs added 7 commits July 30, 2025 16:00
The StartEventWatcher call is no longer needed since calling
StartRecordingToSink already sets up a watcher and manages it via its context.

Signed-off-by: Borja Clemente <[email protected]>
Signed-off-by: Borja Clemente <[email protected]>
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 30, 2025
Signed-off-by: Borja Clemente <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. 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.

Switch to use events.EventRecorder instead of record.EventRecorder
5 participants