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

Add fake.Cache.RemoveInformer #1314

Merged
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
35 changes: 34 additions & 1 deletion pkg/syncer/syncertest/fake/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down