From b8b69f256cf84d08864f9e7b837076b234c5ff24 Mon Sep 17 00:00:00 2001 From: Jonathan Jamroga Date: Wed, 12 Jun 2024 13:04:46 -0400 Subject: [PATCH] Testing unmanaged controllers --- pkg/reconcile/runner.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/pkg/reconcile/runner.go b/pkg/reconcile/runner.go index c3e0d8a1..eac5203d 100644 --- a/pkg/reconcile/runner.go +++ b/pkg/reconcile/runner.go @@ -12,6 +12,7 @@ import ( "github.com/go-logr/logr" "github.com/pkg/errors" + "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/skv2/pkg/ezkube" "github.com/solo-io/skv2/pkg/utils" "sigs.k8s.io/controller-runtime/pkg/client" @@ -71,6 +72,8 @@ type Options struct { // If provided, attempt to verify the resource before beginning the reconcile loop Verifier verifier.ServerResourceVerifier + + UseUnmanagedController bool } func NewLoop( @@ -117,9 +120,16 @@ func (r *runner) RunReconciler(ctx context.Context, reconciler Reconciler, predi reconciler: reconciler, } - ctl, err := controller.New(r.name, r.mgr, controller.Options{ - Reconciler: rec, - }) + var ctl controller.Controller + if r.options.UseUnmanagedController { + ctl, err = controller.NewUnmanaged(r.name, r.mgr, controller.Options{ + Reconciler: rec, + }) + } else { + ctl, err = controller.New(r.name, r.mgr, controller.Options{ + Reconciler: rec, + }) + } if err != nil { return err } @@ -153,6 +163,23 @@ func (r *runner) RunReconciler(ctx context.Context, reconciler Reconciler, predi return err } + if r.options.UseUnmanagedController { + // Start our controller in a goroutine so that we do not block. + go func() { + // Block until our controller manager is elected leader. We presume our + // entire process will terminate if we lose leadership, so we don't need + // to handle that. + <-r.mgr.Elected() + + // Start our controller. This will block until the stop channel is + // closed, or the controller returns an error. + if err := ctl.Start(ctx); err != nil { + contextutils.LoggerFrom(ctx).Error(err) + return + } + }() + } + // Only wait for cache sync if specified in options if r.options.WaitForCacheSync { rec.logger.V(1).Info("waiting for cache sync...")