Skip to content

Commit

Permalink
Allow resources to contain unexported fields (#487)
Browse files Browse the repository at this point in the history
* Allow resources to contain unexported fields

Using unexported fields in Kubernetes resources is not common, but does
happen. Previously, these fields would cause cmp.Diff to panic. Now, we
ignore the content of unexported fields when computing a diff. As the
diff is for logging and test assertions this is relatively safe.

Semantic equality is used for detecting when a managed resource needs to
be updated. Custom equality func can be defined separately as needed.

* Review feedback

Signed-off-by: Scott Andrews <scott@andrews.me>

---------

Signed-off-by: Scott Andrews <scott@andrews.me>
  • Loading branch information
scothis committed Mar 6, 2024
1 parent 3180680 commit e41e6dc
Show file tree
Hide file tree
Showing 14 changed files with 1,528 additions and 9 deletions.
66 changes: 66 additions & 0 deletions internal/resources/dies/dies.go
Expand Up @@ -99,3 +99,69 @@ func (d *TestDuckSpecDie) AddField(key, value string) *TestDuckSpecDie {
r.Fields[key] = value
})
}

// +die:object=true
type _ = resources.TestResourceUnexportedFields

// +die:ignore={unexportedFields}
type _ = resources.TestResourceUnexportedFieldsSpec

func (d *TestResourceUnexportedFieldsSpecDie) AddField(key, value string) *TestResourceUnexportedFieldsSpecDie {
return d.DieStamp(func(r *resources.TestResourceUnexportedFieldsSpec) {
if r.Fields == nil {
r.Fields = map[string]string{}
}
r.Fields[key] = value
})
}

func (d *TestResourceUnexportedFieldsSpecDie) AddUnexportedField(key, value string) *TestResourceUnexportedFieldsSpecDie {
return d.DieStamp(func(r *resources.TestResourceUnexportedFieldsSpec) {
f := r.GetUnexportedFields()
if f == nil {
f = map[string]string{}
}
f[key] = value
r.SetUnexportedFields(f)
})
}

func (d *TestResourceUnexportedFieldsSpecDie) TemplateDie(fn func(d *diecorev1.PodTemplateSpecDie)) *TestResourceUnexportedFieldsSpecDie {
return d.DieStamp(func(r *resources.TestResourceUnexportedFieldsSpec) {
d := diecorev1.PodTemplateSpecBlank.DieImmutable(false).DieFeed(r.Template)
fn(d)
r.Template = d.DieRelease()
})
}

// +die:ignore={unexportedFields}
type _ = resources.TestResourceUnexportedFieldsStatus

func (d *TestResourceUnexportedFieldsStatusDie) ConditionsDie(conditions ...*diemetav1.ConditionDie) *TestResourceUnexportedFieldsStatusDie {
return d.DieStamp(func(r *resources.TestResourceUnexportedFieldsStatus) {
r.Conditions = make([]metav1.Condition, len(conditions))
for i := range conditions {
r.Conditions[i] = conditions[i].DieRelease()
}
})
}

func (d *TestResourceUnexportedFieldsStatusDie) AddField(key, value string) *TestResourceUnexportedFieldsStatusDie {
return d.DieStamp(func(r *resources.TestResourceUnexportedFieldsStatus) {
if r.Fields == nil {
r.Fields = map[string]string{}
}
r.Fields[key] = value
})
}

func (d *TestResourceUnexportedFieldsStatusDie) AddUnexportedField(key, value string) *TestResourceUnexportedFieldsStatusDie {
return d.DieStamp(func(r *resources.TestResourceUnexportedFieldsStatus) {
f := r.GetUnexportedFields()
if f == nil {
f = map[string]string{}
}
f[key] = value
r.SetUnexportedFields(f)
})
}

0 comments on commit e41e6dc

Please sign in to comment.