From 3bf38650cc127f91bf6528c7949a30abf02b69fe Mon Sep 17 00:00:00 2001 From: Herve Nicol <12008875+hervenicol@users.noreply.github.com> Date: Fri, 20 Dec 2024 17:02:45 +0100 Subject: [PATCH] add support for dashboard deletion, and a bit of rework --- internal/controller/dashboard_controller.go | 64 +++++++++++++++------ 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/internal/controller/dashboard_controller.go b/internal/controller/dashboard_controller.go index e64f4bff..d4edfaeb 100644 --- a/internal/controller/dashboard_controller.go +++ b/internal/controller/dashboard_controller.go @@ -228,13 +228,24 @@ func (r DashboardReconciler) configureDashboard(ctx context.Context, dashboardCM } }() + // Switch context to the dashboards-defined org + organization, err := grafana.FindOrgByName(r.GrafanaAPI, dashboardOrg) + if err != nil { + logger.Error(err, "failed to find organization", "organization", dashboardOrg) + return errors.WithStack(err) + } + if _, err = r.GrafanaAPI.SignedInUser.UserSetUsingOrg(organization.ID); err != nil { + logger.Error(err, "failed to change current org for signed in user") + return errors.WithStack(err) + } + for _, dashboardString := range dashboardCM.Data { var dashboard map[string]any err = json.Unmarshal([]byte(dashboardString), &dashboard) if err != nil { - logger.Info("Failed converting dashboard to json") - return errors.WithStack(err) + logger.Info("Failed converting dashboard to json", "Error", err) + continue } dashboardUID, err := getDashboardUID(dashboard) @@ -242,17 +253,6 @@ func (r DashboardReconciler) configureDashboard(ctx context.Context, dashboardCM logger.Info("Skipping dashboard, no UID found") continue } - // Switch context to the current org - organization, err := grafana.FindOrgByName(r.GrafanaAPI, dashboardOrg) - if err != nil { - logger.Error(err, "failed to find organization", "organization", dashboardOrg) - return errors.WithStack(err) - } - - if _, err = r.GrafanaAPI.SignedInUser.UserSetUsingOrg(organization.ID); err != nil { - logger.Error(err, "failed to change current org for signed in user") - return errors.WithStack(err) - } // Create or update dashboard _, err = r.GrafanaAPI.Dashboards.PostDashboard(&models.SaveDashboardCommand{ @@ -261,8 +261,8 @@ func (r DashboardReconciler) configureDashboard(ctx context.Context, dashboardCM Overwrite: true, // allows dashboard to be updated by the same UID }) if err != nil { - logger.Info("Failed updating dashboard") - return errors.WithStack(err) + logger.Info("Failed updating dashboard", "Error", err) + continue } logger.Info("updated dashboard", "Dashboard UID", dashboardUID, "Dashboard Org", dashboardOrg) @@ -286,12 +286,30 @@ func (r DashboardReconciler) reconcileDelete(ctx context.Context, dashboardCM *v return nil } + // We always switch back to the shared org + defer func() { + if _, err = r.GrafanaAPI.SignedInUser.UserSetUsingOrg(grafana.SharedOrg.ID); err != nil { + logger.Error(err, "failed to change current org for signed in user") + } + }() + + // Switch context to the dashboards-defined org + organization, err := grafana.FindOrgByName(r.GrafanaAPI, dashboardOrg) + if err != nil { + logger.Error(err, "failed to find organization", "organization", dashboardOrg) + return errors.WithStack(err) + } + if _, err = r.GrafanaAPI.SignedInUser.UserSetUsingOrg(organization.ID); err != nil { + logger.Error(err, "failed to change current org for signed in user") + return errors.WithStack(err) + } + for _, dashboardString := range dashboardCM.Data { var dashboard map[string]interface{} err = json.Unmarshal([]byte(dashboardString), &dashboard) if err != nil { - logger.Info("Failed converting dashboard to json") - return errors.WithStack(err) + logger.Info("Failed converting dashboard to json", "Error", err) + continue } dashboardUID, err := getDashboardUID(dashboard) @@ -301,7 +319,19 @@ func (r DashboardReconciler) reconcileDelete(ctx context.Context, dashboardCM *v } // TODO: search for dashboard by ID + _, err := r.GrafanaAPI.Dashboards.GetDashboardByUID(dashboardUID) + if err != nil { + logger.Info("Failed getting dashboard", "Error", err) + continue + } + // TODO: delete dashboard if it exits + _, err = r.GrafanaAPI.Dashboards.DeleteDashboardByUID(dashboardUID) + if err != nil { + logger.Info("Failed deleting dashboard", "Error", err) + continue + } + logger.Info("deleted dashboard", "Dashboard UID", dashboardUID, "Dashboard Org", dashboardOrg) }