I want to compare two Kubernetes API objects (e.g. v1.PodSpec
s): one of them was created manually (expected state), the other one was received from the Kubernetes API/client (actual state).
The problem is that even if the two objects are semantically equal, the manually created struct has zerovalues for unspecified fields where the other struct has default values, and so the two doesn't match. It means that a simple reflect.DeepEqual()
call is not sufficient for comparison.
E.g. after this:
expected := &v1.Container{
Name: "busybox",
Image: "busybox",
}
actual := getContainerSpecFromApi(...)
expected.ImagePullPolicy
will be ""
, while actual.ImagePullPolicy
will be "IfNotPresent"
(the default value), so the comparison fails.
Is there an idiomatic way to replace zerovalues with default values in Kubernetes API structs specifically? Or alternatively is a constructor function that initializes the struct with default values available for them somewhere?
EDIT:
Currently I am using handwritten equality tests for each K8s API object types, but this doesn't seem to be maintainable to me. I am looking for a simple (set of) function(s) that "knows" the default values for all built-in Kubernetes API object fields (maybe somewhere under k8s.io/api*
?). Something like this:
expected = api.ApplyContainerDefaults(expected)
if !reflect.DeepEqual(expected, actual) {
reconcile(expected, actual)
}