Skip to content

Commit 4ddb9e2

Browse files
authored
fix(librarypanel): finalize plugins, mitigate HTTP errors for non-existent panels (#2180)
* chore(librarypanel): unify naming in finalize * fix(librarypanel): reconcile plugins in finalize * fix(librarypanel): avoid hitting HTTP 403 on non-existent panels
1 parent df919c1 commit 4ddb9e2

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

controllers/librarypanel_controller.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,39 +235,39 @@ func (r *GrafanaLibraryPanelReconciler) reconcileWithInstance(ctx context.Contex
235235
return instance.AddNamespacedResource(ctx, r.Client, cr, cr.NamespacedResource(uid))
236236
}
237237

238-
func (r *GrafanaLibraryPanelReconciler) finalize(ctx context.Context, libraryPanel *v1beta1.GrafanaLibraryPanel) error {
238+
func (r *GrafanaLibraryPanelReconciler) finalize(ctx context.Context, cr *v1beta1.GrafanaLibraryPanel) error {
239239
log := logf.FromContext(ctx)
240240
log.Info("Finalizing GrafanaLibraryPanel")
241241

242-
uid := content.CustomUIDOrUID(libraryPanel, libraryPanel.Status.UID)
242+
uid := content.CustomUIDOrUID(cr, cr.Status.UID)
243243

244-
instances, err := GetScopedMatchingInstances(ctx, r.Client, libraryPanel)
244+
instances, err := GetScopedMatchingInstances(ctx, r.Client, cr)
245245
if err != nil {
246246
return fmt.Errorf("fetching instances: %w", err)
247247
}
248248

249-
for _, instance := range instances {
250-
grafanaClient, err := client2.NewGeneratedGrafanaClient(ctx, r.Client, &instance)
249+
for _, grafana := range instances {
250+
grafanaClient, err := client2.NewGeneratedGrafanaClient(ctx, r.Client, &grafana)
251251
if err != nil {
252252
return err
253253
}
254254

255255
isCleanupInGrafanaRequired := true
256256

257-
resp, err := grafanaClient.LibraryElements.GetLibraryElementConnections(uid)
257+
resp, err := grafanaClient.LibraryElements.GetLibraryElementByUID(uid)
258258
if err != nil {
259-
var notFound *library_elements.GetLibraryElementConnectionsNotFound
259+
var notFound *library_elements.GetLibraryElementByUIDNotFound
260260
if !errors.As(err, &notFound) {
261-
return fmt.Errorf("fetching library panel from instance %s/%s: %w", instance.Namespace, instance.Name, err)
261+
return fmt.Errorf("fetching library panel from instance %s/%s: %w", grafana.Namespace, grafana.Name, err)
262262
}
263263

264264
isCleanupInGrafanaRequired = false
265265
}
266266

267267
// Skip cleanup in instances
268268
if isCleanupInGrafanaRequired {
269-
if len(resp.Payload.Result) > 0 {
270-
return fmt.Errorf("library panel %s/%s/%s on instance %s/%s has existing connections", libraryPanel.Namespace, libraryPanel.Name, uid, instance.Namespace, instance.Name) //nolint
269+
if resp.Payload.Result.Meta.ConnectedDashboards > 0 {
270+
return fmt.Errorf("library panel %s/%s/%s on instance %s/%s has existing connections", cr.Namespace, cr.Name, uid, grafana.Namespace, grafana.Name) //nolint
271271
}
272272

273273
_, err = grafanaClient.LibraryElements.DeleteLibraryElementByUID(uid) //nolint:errcheck
@@ -279,8 +279,15 @@ func (r *GrafanaLibraryPanelReconciler) finalize(ctx context.Context, libraryPan
279279
}
280280
}
281281

282+
if grafana.IsInternal() {
283+
err = ReconcilePlugins(ctx, r.Client, r.Scheme, &grafana, nil, cr.GetPluginConfigMapKey(), cr.GetPluginConfigMapDeprecatedKey())
284+
if err != nil {
285+
return fmt.Errorf("reconciling plugins: %w", err)
286+
}
287+
}
288+
282289
// Update grafana instance Status
283-
err = instance.RemoveNamespacedResource(ctx, r.Client, libraryPanel)
290+
err = grafana.RemoveNamespacedResource(ctx, r.Client, cr)
284291
if err != nil {
285292
return fmt.Errorf("removing Folder from Grafana cr: %w", err)
286293
}

0 commit comments

Comments
 (0)