Skip to content

Commit

Permalink
controller/engine: sync informers on controller start
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@upbound.io>
  • Loading branch information
sttts committed Jan 9, 2024
1 parent 3fb59b8 commit 7f21c79
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/controller/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ func (c *GVKRoutedCache) Start(_ context.Context) error {
return nil
}

// WaitForCacheSync for a GVKRoutedCache waits for all delegates to sync, and
// returns false if any of them fails to sync.
// WaitForCacheSync for a GVKRoutedCache waits for all delegates and the
// fallback to sync, and returns false if any of them fails to sync.
func (c *GVKRoutedCache) WaitForCacheSync(ctx context.Context) bool {
c.lock.RLock()
syncedCh := make(chan bool, len(c.delegates)+1)
Expand Down Expand Up @@ -186,7 +186,7 @@ func (c *GVKRoutedCache) WaitForCacheSync(ctx context.Context) bool {
}
}

return true
return c.fallback.WaitForCacheSync(ctx)
}

// IndexField adds an index with the given field name on the given object type
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ func (c *namedController) Start(ctx context.Context) error {
}()
go func() {
<-c.e.mgr.Elected()
if synced := c.ca.WaitForCacheSync(ctx); !synced {
c.e.done(c.name, errors.New(errCrashCache))
return
}
c.e.done(c.name, errors.Wrap(c.ctrl.Start(ctx), errCrashController))
}()

Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (c *MockCache) Start(stop context.Context) error {
return c.MockStart(stop)
}

func (c *MockCache) WaitForCacheSync(_ context.Context) bool {
return true
}

type MockController struct {
controller.Controller

Expand Down

0 comments on commit 7f21c79

Please sign in to comment.