Skip to content

Commit

Permalink
controller/engine: add queueing index method to NamedController
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
  • Loading branch information
sttts committed Feb 22, 2024
1 parent feb7810 commit 9f2041d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pkg/controller/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,33 @@ func (e *Engine) Start(name string, o controller.Options, w ...Watch) error {
// NamedController is a controller that's not yet started. It gives access to
// the underlying cache, which may be used e.g. to add indexes.
type NamedController interface {
// Start the named controller. Start does not block.
Start(ctx context.Context) error
// GetCache returns the cache used by the named controller. If the
// controller hasn't been started, neither has the cache.
//
// Warning: Be careful when calling methods with a context before the
// controller has been started. The pass context will be used to e.g. start
// informers, and the life-cycle will differ from that of the controller. In
// particular, use IndexField instead to add indexes to the cache before the
// controller is started.
GetCache() cache.Cache
IndexField(obj client.Object, field string, extractValue client.IndexerFunc)
}

type namedController struct {
name string
e *Engine
ca cache.Cache
ctrl controller.Controller

indexes []index
}

type index struct {
obj client.Object
field string
extractValue client.IndexerFunc
}

// Create the named controller. Each controller gets its own cache
Expand Down Expand Up @@ -265,6 +283,12 @@ func (c *namedController) Start(ctx context.Context) error {
c.e.errors[c.name] = nil
c.e.mx.Unlock()

for _, idx := range c.indexes {
if err := c.ca.IndexField(ctx, idx.obj, idx.field, idx.extractValue); err != nil {
return errors.Wrap(err, "cannot add index")
}
}

go func() {
<-c.e.mgr.Elected()
c.e.done(c.name, errors.Wrap(c.ca.Start(ctx), errCrashCache))
Expand All @@ -281,7 +305,18 @@ func (c *namedController) Start(ctx context.Context) error {
return nil
}

// IndexField queues an index for addition to the cache on start.
func (c *namedController) IndexField(obj client.Object, field string, extractValue client.IndexerFunc) {
c.indexes = append(c.indexes, index{obj: obj, field: field, extractValue: extractValue})
}

// GetCache returns the cache used by the named controller.
//
// Warning: Be careful when calling methods with a context before the controller
// has been started. The pass context will be used to e.g. start informers, and
// the life-cycle will differ from that of the controller. In particular, use
// IndexField instead to add indexes to the cache before the controller is
// started.
func (c *namedController) GetCache() cache.Cache {
return c.ca
}

0 comments on commit 9f2041d

Please sign in to comment.