<p>I'm trying to modify the Spec of non-owned objects as part of the <code>Reconcile</code> of my Custom Resource, but it seems like it ignores any fields that are not primitives. I am using controller-runtime.</p>

<p>I figured since it was only working on primitives, maybe it's an issue related to DeepCopy. However, removing it did not solve the issue, and I read that any Updates on objects have to be on deep copies to avoid messing up the cache.</p>

<p>I also tried setting <code>client.FieldOwner(...)</code> since it says that that's required for Updates that are done server-side. I wasn't sure what to set it to, so I made it <code>req.NamespacedName.String()</code>. That did not work either.</p>

<p>Here is the Reconcile loop for my controller:</p>

<pre class="lang-golang prettyprint-override"><code>func (r *MyCustomObjectReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
    // ...

    var myCustomObject customv1.MyCustomObject
    if err := r.Get(ctx, req.NamespacedName, &amp;myCustomObject); err != nil {
        log.Error(err, "unable to fetch ReleaseDefinition")
        return ctrl.Result{}, client.IgnoreNotFound(err)
    }

    // ...

    deployList := &amp;kappsv1.DeploymentList{}
    labels := map[string]string{
        "mylabel": myCustomObject.Name,
    }
    if err := r.List(ctx, deployList, client.MatchingLabels(labels)); err != nil {
        log.Error(err, "unable to fetch Deployments")
        return ctrl.Result{}, err
    }

    // make a deep copy to avoid messing up the cache (used by other controllers)
    myCustomObjectSpec := myCustomObject.Spec.DeepCopy()

    // the two fields of my CRD that affect the Deployments
    port := myCustomObjectSpec.Port // type: *int32
    customenv := myCustomObjectSpec.CustomEnv // type: map[string]string

    for _, dep := range deployList.Items {
        newDeploy := dep.DeepCopy() // already returns a pointer

        // Do these things:
        // 1. replace first container's containerPort with myCustomObjectSpec.Port
        // 2. replace first container's Env with values from myCustomObjectSpec.CustomEnv
                // 3. Update the Deployment

        container := newDeploy.Spec.Template.Spec.Containers[0]

        // 1. Replace container's port
        container.Ports[0].ContainerPort = *port

        envVars := make([]kcorev1.EnvVar, 0, len(customenv))
        for key, val := range customenv {
            envVars = append(envVars, kcorev1.EnvVar{
                Name:  key,
                Value: val,
            })
        }

        // 2. Replace container's Env variables
        container.Env = envVars

        // 3. Perform update for deployment (port works, env gets ignored)
        if err := r.Update(ctx, newDeploy); err != nil {
            log.Error(err, "unable to update deployment", "deployment", dep.Name)
            return ctrl.Result{}, err
        }
    }
    return ctrl.Result{}, nil
}
</code></pre>

<p>The Spec for my CRD looks like:</p>

<pre><code>// MyCustomObjectSpec defines the desired state of MyCustomObject
type MyCustomObjectSpec struct {
    // CustomEnv is a list of environment variables to set in the containers.
    // +optional
    CustomEnv map[string]string `json:"customEnv,omitempty"`

    // Port is the port that the backend container is listening on.
    // +optional
    Port *int32 `json:"port,omitempty"`
}
</code></pre>

<p>I expected that when I <code>kubectl apply</code> a new CR with changes to the Port and CustomEnv fields, it would modify the deployments as described in <code>Reconcile</code>. However, only the Port is updated, and the changes to the container's <code>Env</code> are ignored.</p>

Andrew DiNunzio
<p>The problem was that I needed a pointer to the Container I was modifying.</p>

<p>Doing this instead worked:</p>

<pre class="lang-golang prettyprint-override"><code>container := &amp;newDeploy.Spec.Template.Spec.Containers[0]
</code></pre>
