Skip to content

Commit

Permalink
Prevent deadlock when starting new Informers
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed May 14, 2019
1 parent af376fb commit 2749a31
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
6 changes: 6 additions & 0 deletions pkg/controller/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
type Controller struct {
Context context.Context

informerMutex sync.Mutex
discovery *discovery.Client
dynamicListers map[schema.GroupVersionResource][]cache.GenericLister
dynclient dynamic.Interface
Expand Down Expand Up @@ -139,6 +140,11 @@ func (c *Controller) AddTarget(target string) error {
}

func (c *Controller) AddInformers(gvr *schema.GroupVersionResource, handler cache.ResourceEventHandler) []cache.SharedInformer {
c.informerMutex.Lock()
defer c.informerMutex.Unlock()
if _, ok := c.dynamicListers[*gvr]; ok {
return []cache.SharedInformer{}
}
c.dynamicListers[*gvr] = make([]cache.GenericLister, len(c.factories))
informers := make([]cache.SharedInformer, len(c.factories))
for i, factory := range c.factories {
Expand Down
19 changes: 8 additions & 11 deletions pkg/controller/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,15 @@ func (src *ResourceSource) Register(c *Controller, stopCh <-chan struct{}, rule
c.sources[*gvr][key] = make(map[Resource]*Rule)
c.sources[*gvr][key][*rule.Target] = rule
}

if _, ok := c.dynamicListers[*gvr]; !ok {
// Create new informers for the gvr we're watching.
informers := c.AddInformers(gvr, &QueuingEventHandler{
Queue: c.queue,
GVR: gvr,
})
for _, informer := range informers {
informer.Run(stopCh)
}
}
}()
// Create new informers for the gvr we're watching.
informers := c.AddInformers(gvr, &QueuingEventHandler{
Queue: c.queue,
GVR: gvr,
})
for _, informer := range informers {
informer.Run(stopCh)
}
}

// Try invoking the rule.
Expand Down

0 comments on commit 2749a31

Please sign in to comment.