Skip to content

Commit

Permalink
feat: InvalidSpec condition when json or valuesFrom are invalid
Browse files Browse the repository at this point in the history
refactor: BuildDatasourceModel aligned with contactpoint
  • Loading branch information
Baarsgaard committed Jan 16, 2025
1 parent a1ed539 commit 5135ecd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
34 changes: 20 additions & 14 deletions controllers/datasource_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,6 @@ func (r *GrafanaDatasourceReconciler) Reconcile(ctx context.Context, req ctrl.Re
cr.Status.NoMatchingInstances = false
r.Log.Info("found matching Grafana instances for datasource", "count", len(instances))

datasource, hash, err := r.getDatasourceContent(ctx, cr)
if err != nil {
return ctrl.Result{}, fmt.Errorf("could not retrieve datasource contents: %w", err)
}

if cr.IsUpdatedUID() {
r.Log.Info("datasource uid got updated, deleting datasources with the old uid")
if err = r.finalize(ctx, cr); err != nil {
Expand All @@ -237,6 +232,15 @@ func (r *GrafanaDatasourceReconciler) Reconcile(ctx context.Context, req ctrl.Re
return ctrl.Result{}, nil
}

datasource, hash, err := r.buildDatasourceModel(ctx, cr)
if err != nil {
setInvalidSpec(&cr.Status.Conditions, cr.Generation, "InvalidModel", err.Error())
meta.RemoveStatusCondition(&cr.Status.Conditions, conditionDatasourceSynchronized)
return ctrl.Result{}, fmt.Errorf("could not build datasource model: %w", err)
}

removeInvalidSpec(&cr.Status.Conditions)

pluginErrors := make(map[string]string)
applyErrors := make(map[string]string)
for _, grafana := range instances {
Expand All @@ -259,6 +263,7 @@ func (r *GrafanaDatasourceReconciler) Reconcile(ctx context.Context, req ctrl.Re
}
}

// NOTE New Condition?
// Specific to datasources
if len(pluginErrors) > 0 {
err := fmt.Errorf("%v", pluginErrors)
Expand Down Expand Up @@ -424,39 +429,40 @@ func (r *GrafanaDatasourceReconciler) SetupWithManager(mgr ctrl.Manager, ctx con
return err
}

func (r *GrafanaDatasourceReconciler) getDatasourceContent(ctx context.Context, cr *v1beta1.GrafanaDatasource) (*models.UpdateDataSourceCommand, string, error) {
func (r *GrafanaDatasourceReconciler) buildDatasourceModel(ctx context.Context, cr *v1beta1.GrafanaDatasource) (*models.UpdateDataSourceCommand, string, error) {
// Overwrite OrgID to ensure the field is useless
cr.Spec.Datasource.OrgID = nil

initialBytes, err := json.Marshal(cr.Spec.Datasource)
if err != nil {
return nil, "", err
return nil, "", fmt.Errorf("encoding existing datasource model as json: %w", err)
}

// Unstructured object for mutating target paths
simpleContent, err := simplejson.NewJson(initialBytes)
if err != nil {
return nil, "", err
return nil, "", fmt.Errorf("parsing marshaled json as simplejson")
}

simpleContent.Set("uid", cr.CustomUIDOrUID())

for _, ref := range cr.Spec.ValuesFrom {
ref := ref
val, key, err := getReferencedValue(ctx, r.Client, cr, ref.ValueFrom)
for _, override := range cr.Spec.ValuesFrom {
val, key, err := getReferencedValue(ctx, r.Client, cr, override.ValueFrom)
if err != nil {
return nil, "", err
return nil, "", fmt.Errorf("getting referenced value: %w", err)
}

patternToReplace := simpleContent.GetPath(strings.Split(ref.TargetPath, ".")...)
patternToReplace := simpleContent.GetPath(strings.Split(override.TargetPath, ".")...)
patternString, err := patternToReplace.String()
if err != nil {
return nil, "", fmt.Errorf("pattern must be a string")
}

patternString = strings.ReplaceAll(patternString, fmt.Sprintf("${%v}", key), val)
patternString = strings.ReplaceAll(patternString, fmt.Sprintf("$%v", key), val)
simpleContent.SetPath(strings.Split(ref.TargetPath, "."), patternString)

r.Log.V(1).Info("overriding value", "key", override.TargetPath, "value", val)
simpleContent.SetPath(strings.Split(override.TargetPath, "."), patternString)
}

newBytes, err := simpleContent.MarshalJSON()
Expand Down
2 changes: 1 addition & 1 deletion controllers/datasource_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestGetDatasourceContent(t *testing.T) {
},
}

content, hash, err := reconciler.getDatasourceContent(context.TODO(), cr)
content, hash, err := reconciler.buildDatasourceModel(context.TODO(), cr)
got := content.SecureJSONData["httpHeaderValue1"]

assert.Nil(t, err)
Expand Down

0 comments on commit 5135ecd

Please sign in to comment.