From 84c5c9f4e29bfd3cead5c89552c9bc7dc90c45ad Mon Sep 17 00:00:00 2001 From: k8s-infra-cherrypick-robot <90416843+k8s-infra-cherrypick-robot@users.noreply.github.com> Date: Mon, 19 Dec 2022 15:10:51 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20controllers=20without=20For()=20?= =?UTF-8?q?fail=20to=20start=20(#2108)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 controllers without For() fail to start * Add a test for Builder without For Co-authored-by: Tsuzu <8574909+tsuzu@users.noreply.github.com> --- pkg/builder/controller.go | 20 +++++++++++--------- pkg/builder/controller_test.go | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/pkg/builder/controller.go b/pkg/builder/controller.go index 9991343be7..03f9633a74 100644 --- a/pkg/builder/controller.go +++ b/pkg/builder/controller.go @@ -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 diff --git a/pkg/builder/controller_test.go b/pkg/builder/controller_test.go index fec0634ea6..782c20ab16 100644 --- a/pkg/builder/controller_test.go +++ b/pkg/builder/controller_test.go @@ -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() {