Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom cmp.Options when comparing k8s objects #486

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions reconcilers/resource.go
Expand Up @@ -34,6 +34,8 @@ import (

var (
_ reconcile.Reconciler = (*ResourceReconciler[client.Object])(nil)

CmpOptions = make([]cmp.Option, 0)
)

// ResourceReconciler is a controller-runtime reconciler that reconciles a given
Expand Down Expand Up @@ -233,7 +235,7 @@ func (r *ResourceReconciler[T]) Reconcile(ctx context.Context, req Request) (Res
if !equality.Semantic.DeepEqual(resourceStatus, originalResourceStatus) && resource.GetDeletionTimestamp() == nil {
if duck.IsDuck(resource, c.Scheme()) {
// patch status
log.Info("patching status", "diff", cmp.Diff(originalResourceStatus, resourceStatus))
log.Info("patching status", "diff", cmp.Diff(originalResourceStatus, resourceStatus, CmpOptions...))
if patchErr := c.Status().Patch(ctx, resource, client.MergeFrom(originalResource)); patchErr != nil {
if !errors.Is(patchErr, ErrQuiet) {
log.Error(patchErr, "unable to patch status")
Expand All @@ -246,7 +248,7 @@ func (r *ResourceReconciler[T]) Reconcile(ctx context.Context, req Request) (Res
"Patched status")
} else {
// update status
log.Info("updating status", "diff", cmp.Diff(originalResourceStatus, resourceStatus))
log.Info("updating status", "diff", cmp.Diff(originalResourceStatus, resourceStatus, CmpOptions...))
if updateErr := c.Status().Update(ctx, resource); updateErr != nil {
if !errors.Is(updateErr, ErrQuiet) {
log.Error(updateErr, "unable to update status")
Expand Down
2 changes: 1 addition & 1 deletion reconcilers/resourcemanager.go
Expand Up @@ -203,7 +203,7 @@ func (r *ResourceManager[T]) Manage(ctx context.Context, resource client.Object,
log.Info("resource is in sync, no update required")
return actual, nil
}
log.Info("updating resource", "diff", cmp.Diff(r.sanitize(actual), r.sanitize(current)))
log.Info("updating resource", "diff", cmp.Diff(r.sanitize(actual), r.sanitize(current), CmpOptions...))
if err := c.Update(ctx, current); err != nil {
if !errors.Is(err, ErrQuiet) {
log.Error(err, "unable to update resource", "resource", namespaceName(current))
Expand Down
16 changes: 9 additions & 7 deletions testing/config.go
Expand Up @@ -218,7 +218,7 @@ func (c *ExpectConfig) AssertClientPatchExpectations(t *testing.T) {
}
actual := NewPatchRef(c.client.PatchActions[i])

if diff := cmp.Diff(exp, actual); diff != "" {
if diff := cmp.Diff(exp, actual, CmpOptions...); diff != "" {
c.errorf(t, "ExpectPatches[%d] differs%s (%s, %s):\n%s", i, c.configNameMsg(), DiffRemovedColor.Sprint("-expected"), DiffAddedColor.Sprint("+actual"), ColorizeDiff(diff))
}
}
Expand All @@ -243,7 +243,7 @@ func (c *ExpectConfig) AssertClientDeleteExpectations(t *testing.T) {
}
actual := NewDeleteRef(c.client.DeleteActions[i])

if diff := cmp.Diff(exp, actual); diff != "" {
if diff := cmp.Diff(exp, actual, CmpOptions...); diff != "" {
c.errorf(t, "ExpectDeletes[%d] differs%s (%s, %s):\n%s", i, c.configNameMsg(), DiffRemovedColor.Sprint("-expected"), DiffAddedColor.Sprint("+actual"), ColorizeDiff(diff))
}
}
Expand All @@ -268,7 +268,7 @@ func (c *ExpectConfig) AssertClientDeleteCollectionExpectations(t *testing.T) {
}
actual := NewDeleteCollectionRef(c.client.DeleteCollectionActions[i])

if diff := cmp.Diff(exp, actual, NormalizeLabelSelector, NormalizeFieldSelector); diff != "" {
if diff := cmp.Diff(exp, actual, append(CmpOptions, NormalizeLabelSelector, NormalizeFieldSelector)...); diff != "" {
c.errorf(t, "ExpectDeleteCollections[%d] differs%s (%s, %s):\n%s", i, c.configNameMsg(), DiffRemovedColor.Sprint("-expected"), DiffAddedColor.Sprint("+actual"), ColorizeDiff(diff))
}
}
Expand Down Expand Up @@ -303,7 +303,7 @@ func (c *ExpectConfig) AssertClientStatusPatchExpectations(t *testing.T) {
}
actual := NewPatchRef(c.client.StatusPatchActions[i])

if diff := cmp.Diff(exp, actual); diff != "" {
if diff := cmp.Diff(exp, actual, CmpOptions...); diff != "" {
c.errorf(t, "ExpectStatusPatches[%d] differs%s (%s, %s):\n%s", i, c.configNameMsg(), DiffRemovedColor.Sprint("-expected"), DiffAddedColor.Sprint("+actual"), ColorizeDiff(diff))
}
}
Expand All @@ -328,7 +328,7 @@ func (c *ExpectConfig) AssertRecorderExpectations(t *testing.T) {
continue
}

if diff := cmp.Diff(exp, actualEvents[i]); diff != "" {
if diff := cmp.Diff(exp, actualEvents[i], CmpOptions...); diff != "" {
c.errorf(t, "ExpectEvents[%d] differs%s (%s, %s):\n%s", i, c.configNameMsg(), DiffRemovedColor.Sprint("-expected"), DiffAddedColor.Sprint("+actual"), ColorizeDiff(diff))
}
}
Expand All @@ -355,7 +355,7 @@ func (c *ExpectConfig) AssertTrackerExpectations(t *testing.T) {
continue
}

if diff := cmp.Diff(exp, actualTracks[i], NormalizeLabelSelector); diff != "" {
if diff := cmp.Diff(exp, actualTracks[i], append(CmpOptions, NormalizeLabelSelector)...); diff != "" {
c.errorf(t, "ExpectTracks[%d] differs%s (%s, %s):\n%s", i, c.configNameMsg(), DiffRemovedColor.Sprint("-expected"), DiffAddedColor.Sprint("+actual"), ColorizeDiff(diff))
}
}
Expand All @@ -379,7 +379,7 @@ func (c *ExpectConfig) compareActions(t *testing.T, actionName string, expectedA
}
actual := actualActions[i].GetObject()

if diff := cmp.Diff(exp.DeepCopyObject(), actual, diffOptions...); diff != "" {
if diff := cmp.Diff(exp.DeepCopyObject(), actual, append(CmpOptions, diffOptions...)...); diff != "" {
c.errorf(t, "Expect%ss[%d] differs%s (%s, %s):\n%s", actionName, i, c.configNameMsg(), DiffRemovedColor.Sprint("-expected"), DiffAddedColor.Sprint("+actual"), ColorizeDiff(diff))
}
}
Expand Down Expand Up @@ -441,6 +441,8 @@ var (
}
return pointer.String(s.String())
})

CmpOptions = make([]cmp.Option, 0)
)

type PatchRef struct {
Expand Down
2 changes: 1 addition & 1 deletion testing/reconciler.go
Expand Up @@ -206,7 +206,7 @@ func (tc *ReconcilerTestCase) Run(t *testing.T, scheme *runtime.Scheme, factory
}
if err == nil {
// result is only significant if there wasn't an error
if diff := cmp.Diff(normalizeResult(tc.ExpectedResult), normalizeResult(result)); diff != "" {
if diff := cmp.Diff(normalizeResult(tc.ExpectedResult), normalizeResult(result), CmpOptions...); diff != "" {
t.Errorf("ExpectedResult differs (%s, %s): %s", DiffRemovedColor.Sprint("-expected"), DiffAddedColor.Sprint("+actual"), ColorizeDiff(diff))
}
}
Expand Down
6 changes: 3 additions & 3 deletions testing/subreconciler.go
Expand Up @@ -164,7 +164,7 @@ func (tc *SubReconcilerTestCase[T]) Run(t *testing.T, scheme *runtime.Scheme, fa
// Set func for verifying stashed values
if tc.VerifyStashedValue == nil {
tc.VerifyStashedValue = func(t *testing.T, key reconcilers.StashKey, expected, actual interface{}) {
if diff := cmp.Diff(expected, actual, IgnoreLastTransitionTime, SafeDeployDiff, IgnoreTypeMeta, IgnoreCreationTimestamp, IgnoreResourceVersion, cmpopts.EquateEmpty()); diff != "" {
if diff := cmp.Diff(expected, actual, append(CmpOptions, IgnoreLastTransitionTime, SafeDeployDiff, IgnoreTypeMeta, IgnoreCreationTimestamp, IgnoreResourceVersion, cmpopts.EquateEmpty())...); diff != "" {
t.Errorf("ExpectStashedValues[%q] differs (%s, %s): %s", key, DiffRemovedColor.Sprint("-expected"), DiffAddedColor.Sprint("+actual"), ColorizeDiff(diff))
}
}
Expand Down Expand Up @@ -261,7 +261,7 @@ func (tc *SubReconcilerTestCase[T]) Run(t *testing.T, scheme *runtime.Scheme, fa
}
if err == nil {
// result is only significant if there wasn't an error
if diff := cmp.Diff(normalizeResult(tc.ExpectedResult), normalizeResult(result)); diff != "" {
if diff := cmp.Diff(normalizeResult(tc.ExpectedResult), normalizeResult(result), CmpOptions...); diff != "" {
t.Errorf("ExpectedResult differs (%s, %s): %s", DiffRemovedColor.Sprint("-expected"), DiffAddedColor.Sprint("+actual"), ColorizeDiff(diff))
}
}
Expand All @@ -279,7 +279,7 @@ func (tc *SubReconcilerTestCase[T]) Run(t *testing.T, scheme *runtime.Scheme, fa
// mirror defaulting of the resource
expectedResource.SetResourceVersion("999")
}
if diff := cmp.Diff(expectedResource, resource, IgnoreLastTransitionTime, SafeDeployDiff, IgnoreTypeMeta, cmpopts.EquateEmpty()); diff != "" {
if diff := cmp.Diff(expectedResource, resource, append(CmpOptions, IgnoreLastTransitionTime, SafeDeployDiff, IgnoreTypeMeta, cmpopts.EquateEmpty())...); diff != "" {
t.Errorf("ExpectResource differs (%s, %s): %s", DiffRemovedColor.Sprint("-expected"), DiffAddedColor.Sprint("+actual"), ColorizeDiff(diff))
}

Expand Down
2 changes: 1 addition & 1 deletion testing/webhook.go
Expand Up @@ -216,7 +216,7 @@ func (tc *AdmissionWebhookTestCase) Run(t *testing.T, scheme *runtime.Scheme, fa
}()

tc.ExpectedResponse.Complete(*tc.Request)
if diff := cmp.Diff(tc.ExpectedResponse, response); diff != "" {
if diff := cmp.Diff(tc.ExpectedResponse, response, CmpOptions...); diff != "" {
t.Errorf("ExpectedResponse differs (%s, %s): %s", DiffRemovedColor.Sprint("-expected"), DiffAddedColor.Sprint("+actual"), ColorizeDiff(diff))
}

Expand Down