From db33aa98675ea836e0d378a81f59f1091b79e0d0 Mon Sep 17 00:00:00 2001 From: ste Date: Sat, 18 Jan 2025 01:15:06 +0100 Subject: [PATCH] fix: Ignore missing datasources on finalize --- controllers/datasource_controller.go | 88 ++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 26 deletions(-) diff --git a/controllers/datasource_controller.go b/controllers/datasource_controller.go index f6d60931c..438437121 100644 --- a/controllers/datasource_controller.go +++ b/controllers/datasource_controller.go @@ -221,7 +221,7 @@ func (r *GrafanaDatasourceReconciler) Reconcile(ctx context.Context, req ctrl.Re if cr.IsUpdatedUID() { r.Log.Info("datasource uid got updated, deleting datasources with the old uid") - if err = r.finalize(ctx, cr); err != nil { + if err = r.deleteOldDatasource(ctx, cr); err != nil { return ctrl.Result{}, err } @@ -284,7 +284,7 @@ func (r *GrafanaDatasourceReconciler) Reconcile(ctx context.Context, req ctrl.Re return ctrl.Result{RequeueAfter: cr.Spec.ResyncPeriod.Duration}, nil } -func (r *GrafanaDatasourceReconciler) finalize(ctx context.Context, cr *v1beta1.GrafanaDatasource) error { +func (r *GrafanaDatasourceReconciler) deleteOldDatasource(ctx context.Context, cr *v1beta1.GrafanaDatasource) error { instances, err := GetScopedMatchingInstances(r.Log, ctx, r.Client, cr) if err != nil { return fmt.Errorf("fetching instances: %w", err) @@ -292,38 +292,74 @@ func (r *GrafanaDatasourceReconciler) finalize(ctx context.Context, cr *v1beta1. for _, grafana := range instances { grafana := grafana - if found, uid := grafana.Status.Datasources.Find(cr.Namespace, cr.Name); found { - grafanaClient, err := client2.NewGeneratedGrafanaClient(ctx, r.Client, &grafana) - if err != nil { + + found, uid := grafana.Status.Datasources.Find(cr.Namespace, cr.Name) + if !found { + continue + } + + grafanaClient, err := client2.NewGeneratedGrafanaClient(ctx, r.Client, &grafana) + if err != nil { + return err + } + + datasource, err := grafanaClient.Datasources.GetDataSourceByUID(*uid) + if err != nil { + var notFound *datasources.GetDataSourceByUIDNotFound + if !errors.As(err, ¬Found) { return err } - - datasource, err := grafanaClient.Datasources.GetDataSourceByUID(*uid) + } else { + _, err = grafanaClient.Datasources.DeleteDataSourceByUID(datasource.Payload.UID) //nolint if err != nil { - var notFound *datasources.GetDataSourceByUIDNotFound - if errors.As(err, ¬Found) { - return err - } - } else { - _, err = grafanaClient.Datasources.DeleteDataSourceByUID(datasource.Payload.UID) //nolint - if err != nil { - var notFound *datasources.DeleteDataSourceByUIDNotFound - if errors.As(err, ¬Found) { - return err - } - } + return fmt.Errorf("deleting datasource to update uid %s: %w", *uid, err) } + } - if grafana.IsInternal() { - err = ReconcilePlugins(ctx, r.Client, r.Scheme, &grafana, nil, fmt.Sprintf("%v-datasource", cr.Name)) - if err != nil { - return err - } + grafana.Status.Datasources = grafana.Status.Datasources.Remove(cr.Namespace, cr.Name) + return r.Client.Status().Update(ctx, &grafana) + } + + return nil +} + +func (r *GrafanaDatasourceReconciler) finalize(ctx context.Context, cr *v1beta1.GrafanaDatasource) error { + instances, err := GetScopedMatchingInstances(r.Log, ctx, r.Client, cr) + if err != nil { + return fmt.Errorf("fetching instances: %w", err) + } + + for _, grafana := range instances { + grafana := grafana + + found, uid := grafana.Status.Datasources.Find(cr.Namespace, cr.Name) + if !found { + continue + } + + grafanaClient, err := client2.NewGeneratedGrafanaClient(ctx, r.Client, &grafana) + if err != nil { + return err + } + + _, err = grafanaClient.Datasources.DeleteDataSourceByUID(*uid) // nolint:errcheck + if err != nil { + var notFound *datasources.DeleteDataSourceByUIDNotFound + if errors.As(err, ¬Found) { + return nil } + return fmt.Errorf("deleting datasource %s: %w", *uid, err) + } - grafana.Status.Datasources = grafana.Status.Datasources.Remove(cr.Namespace, cr.Name) - return r.Client.Status().Update(ctx, &grafana) + if grafana.IsInternal() { + err = ReconcilePlugins(ctx, r.Client, r.Scheme, &grafana, nil, fmt.Sprintf("%v-datasource", cr.Name)) + if err != nil { + return err + } } + + grafana.Status.Datasources = grafana.Status.Datasources.Remove(cr.Namespace, cr.Name) + return r.Client.Status().Update(ctx, &grafana) } return nil