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

Builder.For should panic when called twice #1184

Closed
prometherion opened this issue Sep 28, 2020 · 9 comments
Closed

Builder.For should panic when called twice #1184

prometherion opened this issue Sep 28, 2020 · 9 comments
Labels
lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Comments

@prometherion
Copy link

I know that a Controller should just control for a single Resource kind, this is an edgy-case.

With the following said

// 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{})

I'm considering For a shorthand for Watches that allows multiple inputs

// 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-forInput>}, &handler.EnqueueRequestForOwner{OwnerType: apiType, IsController: true})
func (blder *Builder) Owns(object runtime.Object, opts ...OwnsOption) *Builder {
input := OwnsInput{object: object}
for _, opt := range opts {
opt.ApplyToOwns(&input)
}
blder.ownsInput = append(blder.ownsInput, input)
return blder
}

but that's not true, because I got a single ForInput

func (blder *Builder) For(object runtime.Object, opts ...ForOption) *Builder {
if blder.forInput.object != nil {
blder.forInput.err = fmt.Errorf("For(...) should only be called once, could not assign multiple objects for reconciliation")
return blder
}
input := ForInput{object: object}
for _, opt := range opts {
opt.ApplyToFor(&input)
}
blder.forInput = input
return blder
}

Returning an error would be easier, although breaking the method chaining that is a de-facto standard with the Builder pattern: maybe is better to panic rather than making this non-predictable.

WDYT?

@dvob
Copy link
Contributor

dvob commented Oct 8, 2020

I just ran into the problem that the builder fails if Builder.For is not used. See the following example:
dvob@766c244

I think it would be the best if Builder.For would behave in the same way as Watches and Owns (so define forInput as slice []ForInput). This would fix the case when For is not used and would make it more predictable if it is used multiple times. Probably in most of the cases using Builder.For multiple times does not make sense but I don't think that a user should be stopped of doing that.

@dvob
Copy link
Contributor

dvob commented Oct 8, 2020

Just found #1173 and #1176 which adds an error if For is called multiple times.
But as said before: To me it would make more sense to allow multiple calls to For and not artificially restrict users.

@vincepri
Copy link
Member

vincepri commented Oct 9, 2020

@alvaroaleman What do you think here? I'm ok having it panic given that's definitely a user error

@alvaroaleman
Copy link
Member

#1176 already added an error that is returned on Build, that sounds good enough to me? (The error could probably be a bit more helpful and tell what was the pre-existing type and whats the new one, but thats nitpicking)

But as said before: To me it would make more sense to allow multiple calls to For and not artificially restrict users.

No. There is only one root type per controller. You have Watches to watch subordinate types.

@dvob
Copy link
Contributor

dvob commented Oct 9, 2020

Ok, but can we change the builder that it is possible to use it without For?
dvob@766c244

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 7, 2021
@fejta-bot
Copy link

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Feb 6, 2021
@fejta-bot
Copy link

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-contributor-experience at kubernetes/community.
/close

@k8s-ci-robot
Copy link
Contributor

@fejta-bot: Closing this issue.

In response to this:

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-contributor-experience at kubernetes/community.
/close

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.
Projects
None yet
Development

No branches or pull requests

6 participants