Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

controller/engine: sync informers on controller start #641

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Comment on lines +274 to +277
Copy link
Member

@negz negz Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is already happening inside of controller-runtime (at least for the non-realtime case). I find their code hard to follow due to all the returning of interfaces, but AFAICT:

  1. Our e.Create calls ctrl.Watch, with source.Kind(ca, ...) as an argument. This adds the source to the controller's internal startWatches slice of watches to start when ctrl.Start is called.
  2. When ctrl.Start is called it iterates over startWatches, calling each source's Start method.
  3. The source should be an internal.Kind, and its Start method calls its cache's WaitForCacheSync method.

That's not guaranteed to be the case when using TriggeredBy though.

Assuming it doesn't hurt to call WaitForCacheSync multiple times I think it's a good idea to add this regardless.

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
Loading