Skip to content

Commit

Permalink
Testing unmanaged controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
jjamroga committed Jun 12, 2024
1 parent 0442ff2 commit b8b69f2
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions pkg/reconcile/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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...")
Expand Down

0 comments on commit b8b69f2

Please sign in to comment.