Skip to content

Commit f0919d2

Browse files
committed
Enable starting/stopping controllers and their caches
1 parent e42b9b4 commit f0919d2

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
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(obj runtime.Object) error
63+
6164
// Start runs all the informers known to this cache until the given channel is closed.
6265
// It blocks.
6366
Start(stopCh <-chan struct{}) error

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(stopCh <-chan struct{}) error {
125125
return c.Error
126126
}
127127

128+
// Remove implements Cache
129+
func (c *FakeInformers) Remove(obj runtime.Object) error {
130+
return c.Error
131+
}
132+
128133
// IndexField implements Cache
129134
func (c *FakeInformers) IndexField(ctx context.Context, obj runtime.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(obj runtime.Object) error {
98+
for _, cache := range c.namespaceToCache {
99+
if err := cache.Remove(obj); err != nil {
100+
return err
101+
}
102+
}
103+
return nil
104+
}
105+
97106
func (c *multiNamespaceCache) Start(stopCh <-chan struct{}) 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
@@ -72,6 +72,9 @@ type Controller struct {
7272
// Started is true if the Controller has been Started
7373
Started bool
7474

75+
// saveWatches indicates not to clear startWatches when the controller is stopped.
76+
saveWatches bool
77+
7578
// TODO(community): Consider initializing a logger with the Controller Name as the tag
7679

7780
// startWatches maintains a list of sources, handlers, and predicates to start when the controller is started.
@@ -172,9 +175,15 @@ func (c *Controller) Start(stop <-chan struct{}) error {
172175

173176
// All the watches have been started, we can reset the local slice.
174177
//
175-
// We should never hold watches more than necessary, each watch source can hold a backing cache,
178+
// We should usually hold watches more than necessary, each watch source can hold a backing cache,
176179
// which won't be garbage collected if we hold a reference to it.
177-
c.startWatches = nil
180+
181+
// The exception to this is when the saveWatches is set to true,
182+
// meaning the controller is intending to run Start() again.
183+
// In this case it needs to knowledge of the watches for when the controller is restarted.
184+
if !c.saveWatches {
185+
c.startWatches = nil
186+
}
178187

179188
if c.JitterPeriod == 0 {
180189
c.JitterPeriod = 1 * time.Second
@@ -293,3 +302,13 @@ func (c *Controller) InjectFunc(f inject.Func) error {
293302
func (c *Controller) updateMetrics(reconcileTime time.Duration) {
294303
ctrlmetrics.ReconcileTime.WithLabelValues(c.Name).Observe(reconcileTime.Seconds())
295304
}
305+
306+
// ResetStart sets Started to false to enable running Start on the controller again.
307+
func (c *Controller) ResetStart() {
308+
c.Started = false
309+
}
310+
311+
// SaveWatches indicates that watches should not be cleared when the controller is stopped.
312+
func (c *Controller) SaveWatches() {
313+
c.saveWatches = true
314+
}

0 commit comments

Comments
 (0)