Skip to content

Commit

Permalink
Protect object from mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
jveski committed Nov 6, 2023
1 parent 7782e71 commit 806c353
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
7 changes: 6 additions & 1 deletion internal/reconstitution/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ type Resource struct {
Ref *ResourceRef

Manifest string
Object *unstructured.Unstructured
ReconcileInterval time.Duration
object *unstructured.Unstructured
}

func (r *Resource) Object() *unstructured.Unstructured {
// don't allow callers to mutate the original
return r.object.DeepCopy()
}

// ResourceRef refers to a specific synthesized resource.
Expand Down
2 changes: 1 addition & 1 deletion internal/reconstitution/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (c *cache) buildResource(ctx context.Context, comp types.NamespacedName, sl
Kind: parsed.GetKind(),
},
Manifest: manifest,
Object: parsed,
object: parsed,
}
if resource.ReconcileInterval != nil {
res.ReconcileInterval = resource.ReconcileInterval.Duration
Expand Down
8 changes: 4 additions & 4 deletions internal/reconstitution/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func TestCacheBasics(t *testing.T) {
resource, exists := c.Get(ctx, &expectedReqs[0].ResourceRef, synth.ObservedGeneration)
require.True(t, exists)
assert.NotEmpty(t, resource.Manifest)
assert.Equal(t, "ConfigMap", resource.Object.GetKind())
assert.Equal(t, "slice-0-resource-0", resource.Object.GetName())
assert.Equal(t, "ConfigMap", resource.Object().GetKind())
assert.Equal(t, "slice-0-resource-0", resource.Object().GetName())

// negative
_, exists = c.Get(ctx, &expectedReqs[0].ResourceRef, 123)
Expand Down Expand Up @@ -106,8 +106,8 @@ func TestCacheSecret(t *testing.T) {
resource, exists := c.Get(ctx, &expectedReqs[0].ResourceRef, synth.ObservedGeneration)
require.True(t, exists)
assert.NotEmpty(t, resource.Manifest)
assert.Equal(t, "ConfigMap", resource.Object.GetKind())
assert.Equal(t, "slice-0-resource-0", resource.Object.GetName())
assert.Equal(t, "ConfigMap", resource.Object().GetKind())
assert.Equal(t, "slice-0-resource-0", resource.Object().GetName())
}

func TestCacheInvalidManifest(t *testing.T) {
Expand Down

0 comments on commit 806c353

Please sign in to comment.