From b330abf7cd84f18bb2117979a016b96afa037ee4 Mon Sep 17 00:00:00 2001 From: Saswata Mukherjee Date: Wed, 18 Oct 2023 20:27:40 +0530 Subject: [PATCH] Check kubeinformer error (#1260) Signed-off-by: Saswata Mukherjee --- .../pkg/controller/dashboard_controller.go | 18 ++++++++++++------ .../controller/dashboard_controller_test.go | 6 ++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/loaders/dashboards/pkg/controller/dashboard_controller.go b/loaders/dashboards/pkg/controller/dashboard_controller.go index 147e8dc7a..d08a9e0bc 100644 --- a/loaders/dashboards/pkg/controller/dashboard_controller.go +++ b/loaders/dashboards/pkg/controller/dashboard_controller.go @@ -54,7 +54,12 @@ func RunGrafanaDashboardController(stop <-chan struct{}) { klog.Fatal("Failed to build kubeclient", "error", err) } - go newKubeInformer(kubeClient.CoreV1()).Run(stop) + informer, err := newKubeInformer(kubeClient.CoreV1()) + if err != nil { + klog.Fatal("Failed to get informer", "error", err) + } + + go informer.Run(stop) <-stop } @@ -79,7 +84,7 @@ func isDesiredDashboardConfigmap(obj interface{}) bool { return false } -func newKubeInformer(coreClient corev1client.CoreV1Interface) cache.SharedIndexInformer { +func newKubeInformer(coreClient corev1client.CoreV1Interface) (cache.SharedIndexInformer, error) { // get watched namespace watchedNS := os.Getenv("POD_NAMESPACE") watchlist := &cache.ListWatch{ @@ -97,9 +102,7 @@ func newKubeInformer(coreClient corev1client.CoreV1Interface) cache.SharedIndexI cache.Indexers{}, ) - // TODO(saswatamcode): Check error here. - //nolint:errcheck - kubeInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ + _, err := kubeInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) { if !isDesiredDashboardConfigmap(obj) { return @@ -125,8 +128,11 @@ func newKubeInformer(coreClient corev1client.CoreV1Interface) cache.SharedIndexI deleteDashboard(obj) }, }) + if err != nil { + return nil, err + } - return kubeInformer + return kubeInformer, nil } func hasCustomFolder(folderTitle string) float64 { diff --git a/loaders/dashboards/pkg/controller/dashboard_controller_test.go b/loaders/dashboards/pkg/controller/dashboard_controller_test.go index 85cb693d9..9867b5849 100644 --- a/loaders/dashboards/pkg/controller/dashboard_controller_test.go +++ b/loaders/dashboards/pkg/controller/dashboard_controller_test.go @@ -91,7 +91,6 @@ func createFakeServer(t *testing.T) { } func TestGrafanaDashboardController(t *testing.T) { - coreClient := fake.NewSimpleClientset().CoreV1() stop := make(chan struct{}) @@ -100,7 +99,10 @@ func TestGrafanaDashboardController(t *testing.T) { os.Setenv("POD_NAMESPACE", "ns2") - informer := newKubeInformer(coreClient) + informer, err := newKubeInformer(coreClient) + if err != nil { + t.Fatalf("failed to create informer with %v", err) + } go informer.Run(stop) cm, err := createDashboard()