From 6f9bd5644493ebd2f854ed4e7d99818ce6e49215 Mon Sep 17 00:00:00 2001 From: Karl Isenberg Date: Mon, 8 Jul 2024 11:22:15 -0700 Subject: [PATCH] Add fake.Cache.RemoveInformer This new method is required to satisfy the Informers interface in new versions of controller-runtime. It allows removing and stopping an informer from a set of informers. The individual informers are stopped using informer-specific context wrappers with cancel funcs. This blocks upgrading our controller-runitme dependency. --- pkg/syncer/syncertest/fake/cache.go | 35 ++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/pkg/syncer/syncertest/fake/cache.go b/pkg/syncer/syncertest/fake/cache.go index 7f8906e774..3c9bf9a85f 100644 --- a/pkg/syncer/syncertest/fake/cache.go +++ b/pkg/syncer/syncertest/fake/cache.go @@ -106,7 +106,10 @@ func (c *Cache) Start(ctx context.Context) error { // Start each informer for gvk, informer := range c.informersByGVK { klog.V(5).Infof("starting informer for %s", kinds.GVKToString(gvk)) - go informer.Informer.Run(c.informerCtx.Done()) + // Create a context for each informer so we can stop them independently, if needed. + singleCtx, singleCtxCancel := context.WithCancel(c.informerCtx) + informer.Stop = singleCtxCancel + go informer.Informer.Run(singleCtx.Done()) } // Set started to true so we immediately start any informers added later. @@ -190,6 +193,33 @@ func (c *Cache) GetInformerForKind(ctx context.Context, gvk schema.GroupVersionK return entry.Informer, nil } +// RemoveInformer removes the informer for the GroupVersionKind of the specified +// object from the cache and stops it if it was running. +func (c *Cache) RemoveInformer(ctx context.Context, obj client.Object) error { + gvk, err := kinds.Lookup(obj, c.Scheme()) + if err != nil { + return err + } + return c.RemoveInformerForKind(ctx, gvk) +} + +// RemoveInformerForKind removes the informer for the specific GroupVersionKind +// from the cache and stops it if it was running. +func (c *Cache) RemoveInformerForKind(_ context.Context, gvk schema.GroupVersionKind) error { + c.mux.Lock() + defer c.mux.Unlock() + + entry, found := c.informersByGVK[gvk] + if found { + // Stop if started + if entry.Stop != nil { + entry.Stop() + } + delete(c.informersByGVK, gvk) + } + return nil +} + // IndexField adds an indexer to the underlying cache, using extraction function to get // value(s) from the given field. This index can then be used by passing a field selector // to List. For one-to-one compatibility with "normal" field selectors, only return one value. @@ -344,6 +374,9 @@ type MapEntry struct { // CacheReader wraps Informer and implements the CacheReader interface for a single type Reader client.Reader + + // Stop the informer + Stop func() } // fieldIndexName constructs the name of the index over the given field,