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(GrafanaFolder): proper status updates for removed CRs, don't fail on missing folder in onFolderDeleted events #1516

Merged
merged 2 commits into from
May 13, 2024
Merged
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
16 changes: 9 additions & 7 deletions controllers/grafanafolder_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/go-logr/logr"
genapi "github.com/grafana/grafana-openapi-client-go/client"
"github.com/grafana/grafana-openapi-client-go/client/dashboards"
"github.com/grafana/grafana-openapi-client-go/client/folders"
"github.com/grafana/grafana-openapi-client-go/models"
client2 "github.com/grafana/grafana-operator/v5/controllers/client"
Expand Down Expand Up @@ -92,14 +91,14 @@ func (r *GrafanaFolderReconciler) syncFolders(ctx context.Context) (ctrl.Result,
}
}

// delete all dashboards that no longer have a cr
for grafana, folders := range foldersToDelete {
// delete all folders that no longer have a cr
for grafana, existingFolders := range foldersToDelete {
grafanaClient, err := client2.NewGeneratedGrafanaClient(ctx, r.Client, grafana)
if err != nil {
return ctrl.Result{Requeue: true}, err
}

for _, folder := range folders {
for _, folder := range existingFolders {
// avoid bombarding the grafana instance with a large number of requests at once, limit
// the sync to a certain number of folders per cycle. This means that it will take longer to sync
// a large number of deleted dashboard crs, but that should be an edge case.
Expand All @@ -108,9 +107,11 @@ func (r *GrafanaFolderReconciler) syncFolders(ctx context.Context) (ctrl.Result,
}

namespace, name, uid := folder.Split()
_, err = grafanaClient.Dashboards.DeleteDashboardByUID(uid) //nolint

params := folders.NewDeleteFolderParams().WithFolderUID(uid)
_, err = grafanaClient.Folders.DeleteFolder(params) //nolint
if err != nil {
var notFound *dashboards.DeleteDashboardByUIDNotFound
var notFound *folders.DeleteFolderNotFound
if errors.As(err, &notFound) {
syncLog.Info("folder no longer exists", "namespace", namespace, "name", name)
} else {
Expand Down Expand Up @@ -269,7 +270,7 @@ func (r *GrafanaFolderReconciler) onFolderDeleted(ctx context.Context, namespace
_, err = grafanaClient.Folders.DeleteFolder(params) //nolint
if err != nil {
var notFound *folders.DeleteFolderNotFound
if errors.As(err, &notFound) {
if !errors.As(err, &notFound) {
return err
}
}
Expand All @@ -281,6 +282,7 @@ func (r *GrafanaFolderReconciler) onFolderDeleted(ctx context.Context, namespace
}
}
}

return nil
}

Expand Down