Skip to content

Commit aba2bb6

Browse files
committed
chore: Align Dashboard, Datasource and Folder finalize to always DELETE:
1 parent 93bc8e6 commit aba2bb6

File tree

3 files changed

+11
-23
lines changed

3 files changed

+11
-23
lines changed

controllers/dashboard_controller.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,26 +221,22 @@ func (r *GrafanaDashboardReconciler) finalize(ctx context.Context, cr *v1beta1.G
221221
log := logf.FromContext(ctx)
222222
log.Info("Finalizing GrafanaDashboard")
223223

224+
uid := content.CustomUIDOrUID(cr, cr.Status.UID)
225+
224226
instances, err := GetScopedMatchingInstances(ctx, r.Client, cr)
225227
if err != nil {
226228
return fmt.Errorf("fetching instances: %w", err)
227229
}
228230

229231
for _, grafana := range instances {
230-
found, uid := grafana.Status.Dashboards.Find(cr.Namespace, cr.Name)
231-
if !found {
232-
log.Info("dashboard not found on instance - skipping finalize", "grafana", grafana.Name, "uid", uid)
233-
continue
234-
}
235-
236232
grafanaClient, err := client2.NewGeneratedGrafanaClient(ctx, r.Client, &grafana)
237233
if err != nil {
238234
return fmt.Errorf("creating grafana http client: %w", err)
239235
}
240236

241237
isCleanupInGrafanaRequired := true
242238

243-
resp, err := grafanaClient.Dashboards.GetDashboardByUID(*uid)
239+
resp, err := grafanaClient.Dashboards.GetDashboardByUID(uid)
244240
if err != nil {
245241
var notFound *dashboards.GetDashboardByUIDNotFound
246242
if !errors.As(err, &notFound) {
@@ -256,7 +252,7 @@ func (r *GrafanaDashboardReconciler) finalize(ctx context.Context, cr *v1beta1.G
256252
dash = resp.GetPayload()
257253
}
258254

259-
_, err = grafanaClient.Dashboards.DeleteDashboardByUID(*uid) //nolint:errcheck
255+
_, err = grafanaClient.Dashboards.DeleteDashboardByUID(uid) //nolint:errcheck
260256
if err != nil {
261257
var notFound *dashboards.DeleteDashboardByUIDNotFound
262258
if !errors.As(err, &notFound) {

controllers/datasource_controller.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,24 +222,20 @@ func (r *GrafanaDatasourceReconciler) finalize(ctx context.Context, cr *v1beta1.
222222
return fmt.Errorf("fetching instances: %w", err)
223223
}
224224

225-
for _, grafana := range instances {
226-
found, uid := grafana.Status.Datasources.Find(cr.Namespace, cr.Name)
227-
if !found {
228-
log.Info("datasource not found on instance - skipping finalize", "grafana", grafana.Name, "uid", uid)
229-
continue
230-
}
225+
uid := cr.CustomUIDOrUID()
231226

227+
for _, grafana := range instances {
232228
grafanaClient, err := client2.NewGeneratedGrafanaClient(ctx, r.Client, &grafana)
233229
if err != nil {
234230
return err
235231
}
236232

237-
_, err = grafanaClient.Datasources.DeleteDataSourceByUID(*uid) // nolint:errcheck
233+
_, err = grafanaClient.Datasources.DeleteDataSourceByUID(uid) // nolint:errcheck
238234

239235
var notFound *datasources.DeleteDataSourceByUIDNotFound
240236
if err != nil {
241237
if !errors.As(err, &notFound) {
242-
return fmt.Errorf("deleting datasource %s: %w", *uid, err)
238+
return fmt.Errorf("deleting datasource %s: %w", uid, err)
243239
}
244240
}
245241

controllers/folder_controller.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ func (r *GrafanaFolderReconciler) finalize(ctx context.Context, folder *grafanav
154154
log := logf.FromContext(ctx)
155155
log.Info("Finalizing GrafanaFolder")
156156

157+
uid := folder.CustomUIDOrUID()
158+
157159
instances, err := GetScopedMatchingInstances(ctx, r.Client, folder)
158160
if err != nil {
159161
return fmt.Errorf("fetching instances: %w", err)
@@ -163,18 +165,12 @@ func (r *GrafanaFolderReconciler) finalize(ctx context.Context, folder *grafanav
163165
params := folders.NewDeleteFolderParams().WithForceDeleteRules(&reftrue)
164166

165167
for _, grafana := range instances {
166-
found, uid := grafana.Status.Folders.Find(folder.Namespace, folder.Name)
167-
if !found {
168-
log.Info("folder not found on instance - skipping finalize", "grafana", grafana.Name, "uid", uid)
169-
continue
170-
}
171-
172168
grafanaClient, err := client2.NewGeneratedGrafanaClient(ctx, r.Client, &grafana)
173169
if err != nil {
174170
return err
175171
}
176172

177-
_, err = grafanaClient.Folders.DeleteFolder(params.WithFolderUID(*uid)) //nolint
173+
_, err = grafanaClient.Folders.DeleteFolder(params.WithFolderUID(uid)) //nolint
178174
if err != nil {
179175
var notFound *folders.DeleteFolderNotFound
180176
if !errors.As(err, &notFound) {

0 commit comments

Comments
 (0)