Skip to content

Commit

Permalink
🌱 Fixups to error when source.Start never returns
Browse files Browse the repository at this point in the history
Addresses the comments on #2997
  • Loading branch information
alvaroaleman committed Nov 10, 2024
1 parent b031a06 commit 7e67580
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pkg/internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,15 @@ func (c *Controller[request]) Start(ctx context.Context) error {
defer utilruntime.HandleCrash()

// NB(directxman12): launch the sources *before* trying to wait for the
// caches to sync so that they have a chance to register their intendeded
// caches to sync so that they have a chance to register their intended
// caches.
errGroup := &errgroup.Group{}
for _, watch := range c.startWatches {
log := c.LogConstructor(nil).WithValues("source", fmt.Sprintf("%s", watch))
didStartSyncingSource := &atomic.Bool{}
errGroup.Go(func() error {
// use a context with timeout for launching sources and syncing caches.
// Use a timeout for starting and syncing the source to avoid silently
// blocking startup indefinitely if it doesn't come up.
sourceStartCtx, cancel := context.WithTimeout(ctx, c.CacheSyncTimeout)
defer cancel()

Expand Down Expand Up @@ -220,7 +221,6 @@ func (c *Controller[request]) Start(ctx context.Context) error {
return err
}

// Start the SharedIndexInformer factories to begin populating the SharedIndexInformer caches
c.LogConstructor(nil).Info("Starting Controller")

// All the watches have been started, we can reset the local slice.
Expand Down
4 changes: 2 additions & 2 deletions pkg/internal/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ var _ = Describe("controller", func() {
})

It("should error when Start() is blocking forever", func() {
ctrl.CacheSyncTimeout = 0
ctrl.CacheSyncTimeout = time.Second

controllerDone := make(chan struct{})
ctrl.startWatches = []source.TypedSource[reconcile.Request]{
Expand Down Expand Up @@ -304,7 +304,7 @@ var _ = Describe("controller", func() {
Expect(q).To(Equal(ctrl.Queue))

started = true
cancel()
cancel() // Cancel the context so ctr.Start doesn't block forever
return nil
})
Expect(ctrl.Watch(src)).NotTo(HaveOccurred())
Expand Down

0 comments on commit 7e67580

Please sign in to comment.