<p>I want to compare two Kubernetes API objects (e.g. <code>v1.PodSpec</code>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 <code>reflect.DeepEqual()</code> call is not sufficient for comparison.</p>

<p>E.g. after this:</p>

<pre><code>expected := &amp;v1.Container{
    Name:  "busybox",
    Image: "busybox",
}

actual := getContainerSpecFromApi(...)
</code></pre>

<p><code>expected.ImagePullPolicy</code> will be <code>""</code>, while <code>actual.ImagePullPolicy</code> will be <code>"IfNotPresent"</code> (the default value), so the comparison fails.</p>

<p>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?</p>

<p>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 <code>k8s.io/api*</code>?). Something like this:</p>

<pre><code>expected = api.ApplyContainerDefaults(expected)
if !reflect.DeepEqual(expected, actual) {
    reconcile(expected, actual)
}
</code></pre>

<p>There are helpers to fill in default values in place of empty/zero ones.</p>

<p>Look at the <a href="https://godoc.org/k8s.io/kubernetes/pkg/apis/apps/v1#SetObjectDefaults_Deployment" rel="noreferrer">SetObjectDefaults_Deployment</a> for Deployment for instance.</p>

<p>Looks like the proper way to call it is via <a href="https://godoc.org/k8s.io/apimachinery/pkg/runtime#Scheme.Default" rel="noreferrer"><code>(*runtime.Scheme).Default</code></a>.
Below is the snippet to show the general idea:</p>

<pre><code>import (
    "reflect"

    appsv1 "k8s.io/api/apps/v1"
    "k8s.io/client-go/kubernetes/scheme"
)

func compare() {
    scheme := scheme.Scheme

    // fetch the existing &amp;appsv1.Deployment via API
    actual := ...
    expected := &amp;appsv1.Deployment{}

    // fill in the fields to generate your expected state
    // ...

    scheme.Default(expected)
    // now you should have your empty values filled in
    if !reflect.DeepEqual(expected.Spec, actual.Spec) {
        reconcile(expected, actual)
    }
}
</code></pre>

<p>If you need less strict comparison for instance if you need to tolerate some injected containers then something more relaxed should be used like <a href="https://github.com/amaizfinance/redis-operator/blob/master/pkg/controller/redis/deepcontains.go" rel="noreferrer">this</a>.</p>
