Skip to content

Commit 579fb28

Browse files
committed
Enable starting/stopping controllers and their caches
1 parent fdb2241 commit 579fb28

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

pkg/cache/cache.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ type Informers interface {
5858
// of the underlying object.
5959
GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind) (Informer, error)
6060

61+
// Remove the informer for the given object.
62+
Remove(ctx context.Context, obj runtime.Object) error
63+
6164
// Start runs all the informers known to this cache until the context is closed.
6265
// It blocks.
6366
Start(ctx context.Context) error

pkg/cache/informer_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func indexByField(indexer Informer, field string, extractor client.IndexerFunc)
237237
}
238238

239239
// Remove removes an informer specified by the obj argument from the cache and stops it if it existed.
240-
func (ip *informerCache) Remove(obj runtime.Object) error {
240+
func (ip *informerCache) Remove(ctx context.Context, obj runtime.Object) error {
241241
gvk, err := apiutil.GVKForObject(obj, ip.Scheme)
242242
if err != nil {
243243
return err

pkg/cache/informertest/fake_cache.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ func (c *FakeInformers) Start(ctx context.Context) error {
125125
return c.Error
126126
}
127127

128+
// Remove implements Cache
129+
func (c *FakeInformers) Remove(ctx context.Context, obj runtime.Object) error {
130+
return c.Error
131+
}
132+
128133
// IndexField implements Cache
129134
func (c *FakeInformers) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error {
130135
return nil

pkg/cache/multi_namespace_cache.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ func (c *multiNamespaceCache) GetInformerForKind(ctx context.Context, gvk schema
9494
return &multiNamespaceInformer{namespaceToInformer: informers}, nil
9595
}
9696

97+
func (c *multiNamespaceCache) Remove(ctx context.Context, obj runtime.Object) error {
98+
for _, cache := range c.namespaceToCache {
99+
if err := cache.Remove(ctx, obj); err != nil {
100+
return err
101+
}
102+
}
103+
return nil
104+
}
105+
97106
func (c *multiNamespaceCache) Start(ctx context.Context) error {
98107
for ns, cache := range c.namespaceToCache {
99108
go func(ns string, cache Cache) {

pkg/internal/controller/controller.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ type Controller struct {
7979
// undergo a major refactoring and redesign to allow for context to not be stored in a struct.
8080
ctx context.Context
8181

82+
// saveWatches indicates not to clear startWatches when the controller is stopped.
83+
saveWatches bool
84+
8285
// startWatches maintains a list of sources, handlers, and predicates to start when the controller is started.
8386
startWatches []watchDescription
8487

@@ -180,9 +183,15 @@ func (c *Controller) Start(ctx context.Context) error {
180183

181184
// All the watches have been started, we can reset the local slice.
182185
//
183-
// We should never hold watches more than necessary, each watch source can hold a backing cache,
186+
// We should not usually hold watches more than necessary, each watch source can hold a backing cache,
184187
// which won't be garbage collected if we hold a reference to it.
185-
c.startWatches = nil
188+
189+
// The exception to this is when the saveWatches is set to true,
190+
// meaning the controller is intending to run Start() again.
191+
// In this case it needs knowledge of the watches for when the controller is restarted.
192+
if !c.saveWatches {
193+
c.startWatches = nil
194+
}
186195

187196
if c.JitterPeriod == 0 {
188197
c.JitterPeriod = 1 * time.Second
@@ -302,3 +311,13 @@ func (c *Controller) InjectFunc(f inject.Func) error {
302311
func (c *Controller) updateMetrics(reconcileTime time.Duration) {
303312
ctrlmetrics.ReconcileTime.WithLabelValues(c.Name).Observe(reconcileTime.Seconds())
304313
}
314+
315+
// ResetStart sets Started to false to enable running Start on the controller again.
316+
func (c *Controller) ResetStart() {
317+
c.Started = false
318+
}
319+
320+
// SaveWatches indicates that watches should not be cleared when the controller is stopped.
321+
func (c *Controller) SaveWatches() {
322+
c.saveWatches = true
323+
}

0 commit comments

Comments
 (0)