From 31148543c341eb8ec980f39927dae032e7f062a7 Mon Sep 17 00:00:00 2001 From: Igor Beliakov Date: Sun, 5 May 2024 13:01:58 +0200 Subject: [PATCH] fix(grafanadashboard): properly finish reconciliation in onDashboardDeleted when dashboard is missing --- .golangci.yaml | 2 +- controllers/dashboard_controller.go | 46 ++++++++++++++++------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 870641e62..22d4134f5 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -58,7 +58,7 @@ linters-settings: default-signifies-exhaustive: false nestif: - min-complexity: 20 + min-complexity: 30 goconst: min-len: 3 diff --git a/controllers/dashboard_controller.go b/controllers/dashboard_controller.go index efb091918..0d20ac1e8 100644 --- a/controllers/dashboard_controller.go +++ b/controllers/dashboard_controller.go @@ -302,37 +302,43 @@ func (r *GrafanaDashboardReconciler) onDashboardDeleted(ctx context.Context, nam return err } + isCleanupInGrafanaRequired := true + resp, err := grafanaClient.Dashboards.GetDashboardByUID(*uid) if err != nil { var notFound *dashboards.GetDashboardByUIDNotFound - if errors.As(err, ¬Found) { - // nothing to do if the dashboard doesn't exist - return nil + if !errors.As(err, ¬Found) { + return err } - return err + isCleanupInGrafanaRequired = false } - dash := resp.GetPayload() - - _, err = grafanaClient.Dashboards.DeleteDashboardByUID(*uid) //nolint:errcheck - if err != nil { - var notFound *dashboards.DeleteDashboardByUIDNotFound - if !errors.As(err, ¬Found) { - return err + if isCleanupInGrafanaRequired { + var dash *models.DashboardFullWithMeta + if resp != nil { + dash = resp.GetPayload() } - } - if dash != nil && dash.Meta.FolderUID != "" { - resp, err := r.DeleteFolderIfEmpty(grafanaClient, dash.Meta.FolderUID) + _, err = grafanaClient.Dashboards.DeleteDashboardByUID(*uid) //nolint:errcheck if err != nil { - return err - } - if resp.StatusCode == 200 { - r.Log.Info("unused folder successfully removed") + var notFound *dashboards.DeleteDashboardByUIDNotFound + if !errors.As(err, ¬Found) { + return err + } } - if resp.StatusCode == 432 { - r.Log.Info("folder still in use by other dashboards") + + if dash != nil && dash.Meta != nil && dash.Meta.FolderUID != "" { + resp, err := r.DeleteFolderIfEmpty(grafanaClient, dash.Meta.FolderUID) + if err != nil { + return err + } + if resp.StatusCode == 200 { + r.Log.Info("unused folder successfully removed") + } + if resp.StatusCode == 432 { + r.Log.Info("folder still in use by other dashboards") + } } }