diff --git a/pkg/controller/cache.go b/pkg/controller/cache.go index 74e53b570..1712d9ed8 100644 --- a/pkg/controller/cache.go +++ b/pkg/controller/cache.go @@ -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) @@ -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 diff --git a/pkg/controller/engine.go b/pkg/controller/engine.go index 8cc926b63..fe9dfa000 100644 --- a/pkg/controller/engine.go +++ b/pkg/controller/engine.go @@ -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)) }() diff --git a/pkg/controller/engine_test.go b/pkg/controller/engine_test.go index 9098bec14..a07daf784 100644 --- a/pkg/controller/engine_test.go +++ b/pkg/controller/engine_test.go @@ -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