Skip to content

Commit

Permalink
Merge pull request #1517 from grafana/fix/ondashboardeleted
Browse files Browse the repository at this point in the history
fix(GrafanaDashboard): properly finish reconciliation in onDashboardDeleted when dashboard is missing
  • Loading branch information
theSuess committed May 13, 2024
2 parents 3e81660 + 3114854 commit d8cd864
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ linters-settings:
default-signifies-exhaustive: false

nestif:
min-complexity: 20
min-complexity: 30

goconst:
min-len: 3
Expand Down
46 changes: 26 additions & 20 deletions controllers/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, &notFound) {
// nothing to do if the dashboard doesn't exist
return nil
if !errors.As(err, &notFound) {
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, &notFound) {
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, &notFound) {
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")
}
}
}

Expand Down

0 comments on commit d8cd864

Please sign in to comment.