Skip to content

Commit

Permalink
Fix organization context race condition (#252)
Browse files Browse the repository at this point in the history
* use WithOrgID
  • Loading branch information
TheoBrigitte authored Feb 7, 2025
1 parent 46dad51 commit 69a3178
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixes

- Fix race condition when switching organization in Grafana client by using WithOrgID method

## [0.13.2] - 2025-02-06

### Added
Expand Down
28 changes: 6 additions & 22 deletions internal/controller/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,15 @@ func (r DashboardReconciler) configureDashboard(ctx context.Context, dashboardCM
return nil
}

// We always switch back to the shared org
defer func() {
if _, err = r.GrafanaAPI.SignedInUser.UserSetUsingOrg(grafana.SharedOrg.ID); err != nil {
logger.Error(err, "failed to change current org for signed in user")
}
}()

// Switch context to the dashboards-defined org
organization, err := grafana.FindOrgByName(r.GrafanaAPI, dashboardOrg)
if err != nil {
logger.Error(err, "failed to find organization", "organization", dashboardOrg)
return errors.WithStack(err)
}
if _, err = r.GrafanaAPI.SignedInUser.UserSetUsingOrg(organization.ID); err != nil {
logger.Error(err, "failed to change current org for signed in user")
return errors.WithStack(err)
}
currentOrgID := r.GrafanaAPI.OrgID()
r.GrafanaAPI.WithOrgID(organization.ID)
defer r.GrafanaAPI.WithOrgID(currentOrgID)

for _, dashboardString := range dashboardCM.Data {
var dashboard map[string]any
Expand Down Expand Up @@ -269,23 +261,15 @@ func (r DashboardReconciler) reconcileDelete(ctx context.Context, dashboardCM *v
return nil
}

// We always switch back to the shared org
defer func() {
if _, err = r.GrafanaAPI.SignedInUser.UserSetUsingOrg(grafana.SharedOrg.ID); err != nil {
logger.Error(err, "failed to change current org for signed in user")
}
}()

// Switch context to the dashboards-defined org
organization, err := grafana.FindOrgByName(r.GrafanaAPI, dashboardOrg)
if err != nil {
logger.Error(err, "failed to find organization", "organization", dashboardOrg)
return errors.WithStack(err)
}
if _, err = r.GrafanaAPI.SignedInUser.UserSetUsingOrg(organization.ID); err != nil {
logger.Error(err, "failed to change current org for signed in user")
return errors.WithStack(err)
}
currentOrgID := r.GrafanaAPI.OrgID()
r.GrafanaAPI.WithOrgID(organization.ID)
defer r.GrafanaAPI.WithOrgID(currentOrgID)

for _, dashboardString := range dashboardCM.Data {
var dashboard map[string]interface{}
Expand Down
16 changes: 3 additions & 13 deletions pkg/grafana/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,9 @@ func ConfigureDefaultDatasources(ctx context.Context, grafanaAPI *client.Grafana

// TODO using a serviceaccount later would be better as they are scoped to an organization

var err error
// Switch context to the current org
if _, err = grafanaAPI.SignedInUser.UserSetUsingOrg(organization.ID); err != nil {
logger.Error(err, "failed to change current org for signed in user")
return nil, errors.WithStack(err)
}

// We always switch back to the shared org
defer func() {
if _, err = grafanaAPI.SignedInUser.UserSetUsingOrg(SharedOrg.ID); err != nil {
logger.Error(err, "failed to change current org for signed in user")
}
}()
currentOrgID := grafanaAPI.OrgID()
grafanaAPI.WithOrgID(organization.ID)
defer grafanaAPI.WithOrgID(currentOrgID)

configuredDatasourcesInGrafana, err := listDatasourcesForOrganization(ctx, grafanaAPI)
if err != nil {
Expand Down

0 comments on commit 69a3178

Please sign in to comment.