Skip to content

Commit

Permalink
🐛 controllers without For() fail to start (#2108)
Browse files Browse the repository at this point in the history
* 🐛 controllers without For() fail to start

* Add a test for Builder without For

Co-authored-by: Tsuzu <8574909+tsuzu@users.noreply.github.com>
  • Loading branch information
k8s-infra-cherrypick-robot and tsuzu authored Dec 19, 2022
1 parent ddcb99d commit 84c5c9f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
20 changes: 11 additions & 9 deletions pkg/builder/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,17 @@ func (blder *Builder) project(obj client.Object, proj objectProjection) (client.

func (blder *Builder) doWatch() error {
// Reconcile type
typeForSrc, err := blder.project(blder.forInput.object, blder.forInput.objectProjection)
if err != nil {
return err
}
src := &source.Kind{Type: typeForSrc}
hdler := &handler.EnqueueRequestForObject{}
allPredicates := append(blder.globalPredicates, blder.forInput.predicates...)
if err := blder.ctrl.Watch(src, hdler, allPredicates...); err != nil {
return err
if blder.forInput.object != nil {
typeForSrc, err := blder.project(blder.forInput.object, blder.forInput.objectProjection)
if err != nil {
return err
}
src := &source.Kind{Type: typeForSrc}
hdler := &handler.EnqueueRequestForObject{}
allPredicates := append(blder.globalPredicates, blder.forInput.predicates...)
if err := blder.ctrl.Watch(src, hdler, allPredicates...); err != nil {
return err
}
}

// Watches the managed types
Expand Down
17 changes: 17 additions & 0 deletions pkg/builder/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,23 @@ var _ = Describe("application", func() {
defer cancel()
doReconcileTest(ctx, "4", m, true, bldr)
})

It("should Reconcile without For", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

bldr := ControllerManagedBy(m).
Named("Deployment").
Watches( // Equivalent of For
&source.Kind{Type: &appsv1.Deployment{}}, &handler.EnqueueRequestForObject{}).
Watches( // Equivalent of Owns
&source.Kind{Type: &appsv1.ReplicaSet{}},
&handler.EnqueueRequestForOwner{OwnerType: &appsv1.Deployment{}, IsController: true})

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
doReconcileTest(ctx, "9", m, true, bldr)
})
})

Describe("Set custom predicates", func() {
Expand Down

0 comments on commit 84c5c9f

Please sign in to comment.