Skip to content

✨Add predicates as variadic args for Owns, For, and Watches func #602

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

Closed
wants to merge 1 commit into from

Conversation

mszostok
Copy link
Contributor

@mszostok mszostok commented Sep 12, 2019

Description

  • This PR adds options for defining the Predicate. Watches, Owns, For, and Watches
  • Add tests coverage for implemented logic using ginko & gomega

Fixes: #572

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 12, 2019
@mszostok mszostok changed the title ✨Add as predicates as variadic args for Owns, For, and Watches func ✨Add predicates as variadic args for Owns, For, and Watches func Sep 13, 2019
@mszostok
Copy link
Contributor Author

Discussed with @DirectXMan12 and he has right,
basically it's not a breaking change if someone is using those methods directly, but if someone had func like

DoAThing(cb func(runtime.Obj) *ctrl.Builder)

then the compiler will throw an error.

Because of that, this will be postponed

@DirectXMan12
Copy link
Contributor

we'll roll this in to the next set of breaking changes. I'll add a milestone or somesuch

@DirectXMan12 DirectXMan12 added this to the breaking-next milestone Dec 3, 2019
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 22, 2020
@vincepri
Copy link
Member

vincepri commented Feb 5, 2020

@mszostok Are you still interested / do you have time to work on this?

@mszostok
Copy link
Contributor Author

mszostok commented Feb 5, 2020

Hi @vincepri

Yes, if you are interested in having that functionality then I can fix it. You have some deadline? Because I can do it even tomorrow:)

@vincepri
Copy link
Member

vincepri commented Feb 5, 2020

@mszostok Either this week / next week would be great to have this merged :) we're trying to get to a release soon-ish. Thank you for your help!

@gerred
Copy link
Contributor

gerred commented Feb 5, 2020

@mszostok If you need a pair on this, I'm available too. 👍 on getting this landed.

@DirectXMan12
Copy link
Contributor

(lmk if you need someone to force-push a rebase here)

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 6, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mszostok
To complete the pull request process, please assign gerred
You can assign the PR to them by writing /assign @gerred in a comment when ready.

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

@vincepri
Copy link
Member

vincepri commented Feb 7, 2020

@mszostok Is this PR ready to be reviewed in more depth? Is it still a draft?

@DirectXMan12
Copy link
Contributor

I'm gonna fix this up. I'm pretty sure this is ready.

IsController: true,
}
if err := blder.ctrl.Watch(src, hdler, blder.predicates...); err != nil {
allPredicates := append(blder.globalPredicates, obj.predicates...)
Copy link
Contributor

Choose a reason for hiding this comment

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

this needs to be a copy first, otherwise you'll end up growing the list with other objects' predicates.

Copy link
Contributor

Choose a reason for hiding this comment

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

(or, rather, you could, depending on what Go decides to do with the slice)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you are right I missed it

This allows per-watch predicates for For, Owns, and Watches in the
builder.  These are in addition to the global predicate set, which apply
to all watches.
@DirectXMan12
Copy link
Contributor

Should be good now

@DirectXMan12 DirectXMan12 marked this pull request as ready for review February 10, 2020 21:35
@k8s-ci-robot k8s-ci-robot 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, 2020
@DirectXMan12
Copy link
Contributor

(or rather, it looks good to me)

return blder
}

// Owns defines types of Objects being *generated* by the ControllerManagedBy, and configures the ControllerManagedBy to respond to
// create / delete / update events by *reconciling the owner object*. This is the equivalent of calling
// Watches(&source.Kind{Type: <ForType-apiType>}, &handler.EnqueueRequestForOwner{OwnerType: apiType, IsController: true})
func (blder *Builder) Owns(apiType runtime.Object) *Builder {
blder.managedObjects = append(blder.managedObjects, apiType)
func (blder *Builder) Owns(apiType runtime.Object, prct ...predicate.Predicate) *Builder {
Copy link
Member

Choose a reason for hiding this comment

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

@DirectXMan12 Is this the best way to add this functionality? Having varargs here prevents us to add more functionality in the future if we wanted to expose more configurable options.

We could opt instead for a generic interface, similar for what we do for client.List for example.

Copy link
Contributor Author

@mszostok mszostok Feb 13, 2020

Choose a reason for hiding this comment

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

yep, it is a functional options pattern and it could be better in that case at the beginning I just do not want to make it to complicated because I thought I will not break a contract

Comment on lines +199 to +200
allPredicates := append([]predicate.Predicate(nil), blder.globalPredicates...)
allPredicates = append(allPredicates, w.predicates...)
Copy link
Member

Choose a reason for hiding this comment

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

This could be simplified with

allPredicates := append(blder.globalPredicates, w.predicates...)

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 was like that at the beginning but @DirectXMan12 is right, it should be copied: #602 (comment)

// For defines the type of Object being *reconciled*, and configures the ControllerManagedBy to respond to create / delete /
// update events by *reconciling the object*.
// This is the equivalent of calling
// Watches(&source.Kind{Type: apiType}, &handler.EnqueueRequestForObject{})
func (blder *Builder) For(apiType runtime.Object) *Builder {
blder.apiType = apiType
func (blder *Builder) For(reconciledAPIType runtime.Object, prct ...predicate.Predicate) *Builder {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func (blder *Builder) For(reconciledAPIType runtime.Object, prct ...predicate.Predicate) *Builder {
func (blder *Builder) For(object runtime.Object, predicates ...predicate.Predicate) *Builder {

No need for shorter names

// simpleWatch represents the information required to construct a "simple" For
// or Owns watch.
type simpleWatch struct {
item runtime.Object
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
item runtime.Object
object runtime.Object

Comment on lines +190 to +191
allPredicates := append([]predicate.Predicate(nil), blder.globalPredicates...)
allPredicates = append(allPredicates, obj.predicates...)
Copy link
Member

Choose a reason for hiding this comment

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

This could be simplified with

allPredicates := append(blder.globalPredicates, w.predicates...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

same as here: #602 (comment)

@mszostok
Copy link
Contributor Author

@vincepri @DirectXMan12 should I change it to functional options pattern as described here? #602 (comment)

I can do it today

@mszostok
Copy link
Contributor Author

Hi

@vincepri @DirectXMan12 so I provided implementation with Functional options. Here is PR: #799

I've prepared a dedicated pull request so you can check which option is better.

@mszostok
Copy link
Contributor Author

closing in favor of: #799

@mszostok mszostok closed this Mar 13, 2020
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. 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.

Cannot specify per-watch predicates on builders
5 participants