Skip to content

Commit

Permalink
bugfix - datasource controller was dropping secureJsonData (#1500)
Browse files Browse the repository at this point in the history
This addresses a bug introduced in the changeover to using the openapi
Grafana library where the secureJsonData field was being dropped.

To address this it's necessary to use the `UpdateDataSourceCommand`
struct to ensure we're also not dropping the version if it's present.

This is a bit counter-intuitive to use the `Update` struct here and
perhaps worth looking at another solution at some point.

Thanks to matt-delaney-cruise for the initial PR around this.
  • Loading branch information
NissesSenap committed Apr 19, 2024
1 parent 74a796b commit ea03cf4
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions controllers/datasource_controller.go
Expand Up @@ -315,7 +315,7 @@ func (r *GrafanaDatasourceReconciler) onDatasourceDeleted(ctx context.Context, n
return nil
}

func (r *GrafanaDatasourceReconciler) onDatasourceCreated(ctx context.Context, grafana *v1beta1.Grafana, cr *v1beta1.GrafanaDatasource, datasource *models.DataSource, hash string) error {
func (r *GrafanaDatasourceReconciler) onDatasourceCreated(ctx context.Context, grafana *v1beta1.Grafana, cr *v1beta1.GrafanaDatasource, datasource *models.UpdateDataSourceCommand, hash string) error {
if grafana.IsExternal() && cr.Spec.Plugins != nil {
return fmt.Errorf("external grafana instances don't support plugins, please remove spec.plugins from your datasource cr")
}
Expand Down Expand Up @@ -436,7 +436,7 @@ func (r *GrafanaDatasourceReconciler) GetMatchingDatasourceInstances(ctx context
return instances, err
}

func (r *GrafanaDatasourceReconciler) getDatasourceContent(ctx context.Context, cr *v1beta1.GrafanaDatasource) (*models.DataSource, string, error) {
func (r *GrafanaDatasourceReconciler) getDatasourceContent(ctx context.Context, cr *v1beta1.GrafanaDatasource) (*models.UpdateDataSourceCommand, string, error) {
initialBytes, err := json.Marshal(cr.Spec.Datasource)
if err != nil {
return nil, "", err
Expand Down Expand Up @@ -473,10 +473,10 @@ func (r *GrafanaDatasourceReconciler) getDatasourceContent(ctx context.Context,
if err != nil {
return nil, "", err
}
var res models.DataSource

err = json.Unmarshal(newBytes, &res)
if err != nil {
// We use UpdateDataSourceCommand here because models.DataSource lacks the SecureJsonData field
var res models.UpdateDataSourceCommand
if err = json.Unmarshal(newBytes, &res); err != nil {
return nil, "", err
}

Expand Down

0 comments on commit ea03cf4

Please sign in to comment.