Skip to content

Commit

Permalink
Avoid accidental mutations during sanitize (#213)
Browse files Browse the repository at this point in the history
The object passed to the sanitize method is now a copy.

Signed-off-by: Scott Andrews <andrewssc@vmware.com>
  • Loading branch information
scothis committed Apr 21, 2022
1 parent bb1d838 commit dba890c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions reconcilers/reconcilers.go
Expand Up @@ -917,6 +917,10 @@ func (r *ChildReconciler) sanitize(child client.Object) interface{} {
if child == nil {
return nil
}

// avoid accidental mutations in Sanitize method
child = child.DeepCopyObject().(client.Object)

fn := reflect.ValueOf(r.Sanitize)
out := fn.Call([]reflect.Value{
reflect.ValueOf(child),
Expand Down
30 changes: 30 additions & 0 deletions reconcilers/reconcilers_test.go
Expand Up @@ -794,6 +794,36 @@ func TestChildReconciler(t *testing.T) {
ExpectCreates: []client.Object{
configMapCreate,
},
}, {
Name: "sanitize is mutation safe",
Parent: resource.
SpecDie(func(d *dies.TestResourceSpecDie) {
d.AddField("foo", "bar")
}),
Metadata: map[string]interface{}{
"SubReconciler": func(t *testing.T, c reconcilers.Config) reconcilers.SubReconciler {
r := defaultChildReconciler(c)
r.Sanitize = func(child *corev1.ConfigMap) interface{} {
child.Data["ignore"] = "me"
return child
}
return r
},
},
ExpectEvents: []rtesting.Event{
rtesting.NewEvent(resource, scheme, corev1.EventTypeNormal, "Created",
`Created ConfigMap %q`, testName),
},
ExpectParent: resourceReady.
SpecDie(func(d *dies.TestResourceSpecDie) {
d.AddField("foo", "bar")
}).
StatusDie(func(d *dies.TestResourceStatusDie) {
d.AddField("foo", "bar")
}),
ExpectCreates: []client.Object{
configMapCreate,
},
}, {
Name: "error listing children",
Parent: resourceReady,
Expand Down

0 comments on commit dba890c

Please sign in to comment.