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

✨ Use generics for predicates #2265

Conversation

nikola-jokic
Copy link

Fixes #2214

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 7, 2023
@k8s-ci-robot
Copy link
Contributor

Welcome @nikola-jokic!

It looks like this is your first PR to kubernetes-sigs/controller-runtime 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-runtime has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Apr 7, 2023
@k8s-ci-robot
Copy link
Contributor

Hi @nikola-jokic. 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/test-infra repository.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: nikola-jokic
Once this PR has been reviewed and has the lgtm label, please assign alvaroaleman for approval. For more information see the Kubernetes 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 the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Apr 7, 2023
@nikola-jokic
Copy link
Author

Hey @alvaroaleman 👋

I hope this is what you meant by using generics ☺️

return Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return filter(e.Object)
v := e.Object.(T)
Copy link
Member

Choose a reason for hiding this comment

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

We still have the type assertion here and its possible to misuse this by constructing a predicate for the wrong type. What I'd want is the whole chain of source/predicate/handler using generics. I'll update the issue to clarify that

Copy link
Author

Choose a reason for hiding this comment

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

On it 😄

Copy link
Author

Choose a reason for hiding this comment

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

Update

The only thing I'm having trouble with is builder
Right now, builder can watch multiple resources and I can't really instantiate many watches with different type parameters using generics without providing a top level function and potentially changing the interface of the Watch method
I'm looking into ways of doing it, so the change is as minimal as possible

Copy link
Member

Choose a reason for hiding this comment

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

I looked into this some time ago. Golang doesn't support generic methods, which is why we can not keep assembling everything in the controller.Watch but instead have to provide predicate+handler in a constructor to the source

@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 19, 2023
@nikola-jokic nikola-jokic marked this pull request as draft April 19, 2023 11:01
@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 Apr 19, 2023
ctrl controller.Controller
ctrlOptions controller.Options
name string
}

func newObjectPredicate[T client.ObjectConstraint](p predicate.Predicate[T]) predicate.Predicate[client.Object] {
Copy link
Author

Choose a reason for hiding this comment

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

This is heavily work in progress, but I wanted to push changes to make sure this approach is okay
Since controller is able to watch on multiple resources, the only way I could think of (since we can't use generic methods) is to create a generic function that will convert this generic type to a client.Object
That way, the complexity is internal, and we use client.Object interface internally. The caller still can leverage generics to use those types

The only thing is that the project needs to be go version 1.20, because they added interfaces to be comparable in that version

Copy link
Author

Choose a reason for hiding this comment

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

This does not work now, there are many tests where I'd have to provide types and similar
The controller watch is also something that needs to be changed or provided another method like controller.WatchType(WithWatch() that can be a generic function returning wrapped objects

Copy link
Author

Choose a reason for hiding this comment

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

Since many types can handle multiple types, wrapping them in interfaces was the only thing that I could think of to provide a nice API... Some types can be improved, but the only thing I wanted to ask is, is there a different approach I should take?

Copy link
Member

Choose a reason for hiding this comment

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

This is the issue I was referring to in #2265 (comment). We have to change how this works in order to accomodate generics. The source needs to get the handler and predicates at its construction time rather than at Start time, because using Start entails passing it through the controller and that would entail that we can only support one type per controller.

Copy link
Author

Choose a reason for hiding this comment

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

I see, I'll work on it

@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 21, 2023
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. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Evaluate using generics for source/predicate/handler chain
3 participants