<p>I am trying to update an object's Env field but I don't want to have to iterate through them to figure out which fields already exist and need to be updated instead of inserted if that isn't necessary. I'm using controller-runtime.</p>

<p>I tried patching using an ApplyPatchType, like so:<br>
<code>r.Patch(ctx, myDeployment, client.Apply)</code><br>
where client.Apply is <a href="https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/client/patch.go#L28" rel="nofollow noreferrer">here</a>, and <code>myDeployment</code> is an <code>*appsv1.Deployment</code> that I found with <code>List(...)</code> and then modified.</p>

<p>This failed with a <code>415 Unsupported Media Type</code>. I assume it has something to do with it needing to be encoded in YAML (since the type's value is "application/apply-patch+yaml"</p>

<p>I got it working by doing this (r has an embedded controller-runtime Client):</p>

<pre class="lang-golang prettyprint-override"><code>newDeploy := dep.DeepCopy()
original := dep.DeepCopy()
// ... (changing newDeploy)
rawObj, _ := apijson.Marshal(newDeploy)
r.Patch(ctx, original, client.ConstantPatch(types.StrategicMergePatchType, rawObj))
</code></pre>

<p>This feels inefficient though, with possibly unnecessary DeepCopies, and I feel like I'm unnecessarily Marshalling my object. I figured I could somehow use the <code>Patch</code> command similar to how I use the <code>Update</code> command, where I could just pass in the object in its already modified state and get the StrategicMergePatchType (or ApplyPatchType) behavior.</p>

<p>It seems to me like if I want to just pass in the modified object and have the client "figure out" what to patch, I want to use the client.Apply (ApplyPatchType) behavior, since I think that StrategicMergePatchType is just for diffs to apply (though providing the whole object can still be a valid diff I suppose). Any guidance is appreciated!</p>

<p>For now, I got the behavior I'm looking for without having to manually write the merging logic by using <a href="https://github.com/kubernetes-sigs/controller-runtime/blob/ee41a803ccc9c8a308b751433fc35282c716a9ff/pkg/client/patch.go#L76" rel="nofollow noreferrer">client.MergeFrom</a>:</p>

<blockquote>
  <p>MergeFrom creates a Patch that patches using the merge-patch strategy with the given object as base.</p>
</blockquote>

<p>Relevant code:</p>

<p><code>r.Patch(ctx, newDeploy, client.MergeFrom(original))</code></p>
