Skip to content

Commit

Permalink
Fix bug of deleting entities that were stale after reupserting
Browse files Browse the repository at this point in the history
  • Loading branch information
talsabagport committed May 28, 2023
1 parent 6b70d13 commit 44c117a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
12 changes: 8 additions & 4 deletions pkg/handlers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,18 @@ func NewControllersHandler(exporterConfig *config.Config, k8sClient *k8s.Client,
func (c *ControllersHandler) Handle(stopCh <-chan struct{}) {
klog.Info("Starting informers")
c.informersFactory.Start(stopCh)
klog.Info("Starting controllers")
klog.Info("Waiting for informers cache sync")
for _, controller := range c.controllers {
if err := controller.Run(1, stopCh); err != nil {
klog.Fatalf("Error running controller: %s", err.Error())
if err := controller.WaitForCacheSync(stopCh); err != nil {
klog.Fatalf("Error while waiting for informer cache sync: %s", err.Error())
}
}
klog.Info("Deleting stale entities")
go c.RunDeleteStaleEntities()
c.RunDeleteStaleEntities()
klog.Info("Starting controllers")
for _, controller := range c.controllers {
controller.Run(1, stopCh)
}

<-stopCh
klog.Info("Shutting down controllers")
Expand Down
12 changes: 7 additions & 5 deletions pkg/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,23 @@ func (c *Controller) Shutdown() {
klog.Infof("Closed controller for resource '%s'", c.resource.Kind)
}

func (c *Controller) Run(workers int, stopCh <-chan struct{}) error {
defer utilruntime.HandleCrash()

func (c *Controller) WaitForCacheSync(stopCh <-chan struct{}) error {
klog.Infof("Waiting for informer cache to sync for resource '%s'", c.resource.Kind)
if ok := cache.WaitForCacheSync(stopCh, c.informer.HasSynced); !ok {
return fmt.Errorf("failed to wait for caches to sync")
}

return nil
}

func (c *Controller) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()

klog.Infof("Starting workers for resource '%s'", c.resource.Kind)
for i := 0; i < workers; i++ {
go wait.Until(c.runWorker, time.Second, stopCh)
}
klog.Infof("Started workers for resource '%s'", c.resource.Kind)

return nil
}

func (c *Controller) runWorker() {
Expand Down

0 comments on commit 44c117a

Please sign in to comment.