diff --git a/pkg/handlers/controllers.go b/pkg/handlers/controllers.go index 08f080f..cb97b45 100644 --- a/pkg/handlers/controllers.go +++ b/pkg/handlers/controllers.go @@ -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") diff --git a/pkg/k8s/controller.go b/pkg/k8s/controller.go index 7be2800..317a88f 100644 --- a/pkg/k8s/controller.go +++ b/pkg/k8s/controller.go @@ -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() {