Skip to content

Commit

Permalink
fix: Ignore missing datasources on finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
Baarsgaard committed Jan 21, 2025
1 parent 03b2702 commit db33aa9
Showing 1 changed file with 62 additions and 26 deletions.
88 changes: 62 additions & 26 deletions controllers/datasource_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -284,46 +284,82 @@ 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)
}

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, &notFound) {
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, &notFound) {
return err
}
} else {
_, err = grafanaClient.Datasources.DeleteDataSourceByUID(datasource.Payload.UID) //nolint
if err != nil {
var notFound *datasources.DeleteDataSourceByUIDNotFound
if errors.As(err, &notFound) {
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, &notFound) {
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
Expand Down

0 comments on commit db33aa9

Please sign in to comment.