Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GrafanaDashboard): properly finish reconciliation in onDashboardDeleted when dashboard is missing #1517

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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