Skip to content

Commit be18097

Browse files
authored
Merge pull request #1176 from prafull01/multi-apitype
✨ Add error check for multiple apiTypes as reconciliation object
2 parents 5757a38 + 67e49ce commit be18097

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

pkg/builder/controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,18 @@ func ControllerManagedBy(m manager.Manager) *Builder {
5959
type ForInput struct {
6060
object runtime.Object
6161
predicates []predicate.Predicate
62+
err error
6263
}
6364

6465
// For defines the type of Object being *reconciled*, and configures the ControllerManagedBy to respond to create / delete /
6566
// update events by *reconciling the object*.
6667
// This is the equivalent of calling
6768
// Watches(&source.Kind{Type: apiType}, &handler.EnqueueRequestForObject{})
6869
func (blder *Builder) For(object runtime.Object, opts ...ForOption) *Builder {
70+
if blder.forInput.object != nil {
71+
blder.forInput.err = fmt.Errorf("For(...) should only be called once, could not assign multiple objects for reconciliation")
72+
return blder
73+
}
6974
input := ForInput{object: object}
7075
for _, opt := range opts {
7176
opt.ApplyToFor(&input)
@@ -159,6 +164,9 @@ func (blder *Builder) Build(r reconcile.Reconciler) (controller.Controller, erro
159164
if blder.mgr == nil {
160165
return nil, fmt.Errorf("must provide a non-nil Manager")
161166
}
167+
if blder.forInput.err != nil {
168+
return nil, blder.forInput.err
169+
}
162170

163171
// Set the Config
164172
blder.loadRestConfig()

pkg/builder/controller_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ var _ = Describe("application", func() {
9090
Expect(instance).NotTo(BeNil())
9191
})
9292

93+
It("should return error if given two apiType objects in For function", func() {
94+
By("creating a controller manager")
95+
m, err := manager.New(cfg, manager.Options{})
96+
Expect(err).NotTo(HaveOccurred())
97+
98+
instance, err := ControllerManagedBy(m).
99+
For(&appsv1.ReplicaSet{}).
100+
For(&appsv1.Deployment{}).
101+
Owns(&appsv1.ReplicaSet{}).
102+
Build(noop)
103+
Expect(err).To(MatchError(ContainSubstring("For(...) should only be called once, could not assign multiple objects for reconciliation")))
104+
Expect(instance).To(BeNil())
105+
})
106+
93107
It("should return an error if there is no GVK for an object, and thus we can't default the controller name", func() {
94108
By("creating a controller manager")
95109
m, err := manager.New(cfg, manager.Options{})

0 commit comments

Comments
 (0)