Skip to content

Commit 3541317

Browse files
Fix reusing merge objects for patch without copying (#34)
I was seeing an issue where there was a delta in the spec, but it was never being applied back to the object. I tracked down that the `latest` object returned from `sdkFind` was correct, but the patch was not being applied. When using the method that patches both spec and status, changes to the `latest` were being applied in the spec, but not in the status. The root cause of this is that the `client.MergeFrom` is overriding the `status` fields when merging the `spec` with `desired`. Therefore after patching the spec `desired == latest`. This patch ensures that we copy the `latest` object as part of patching so as to not override changes when using `client.MergeFrom`.
1 parent cdc2824 commit 3541317

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

pkg/runtime/reconciler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ func (r *resourceReconciler) patchResourceMetadataAndSpec(
342342
rlog.Enter("kc.Patch (metadata + spec)")
343343
err = r.kc.Patch(
344344
ctx,
345-
latest.RuntimeObject(),
346-
client.MergeFrom(desired.RuntimeObject()),
345+
latest.RuntimeObject().DeepCopyObject(),
346+
client.MergeFrom(desired.RuntimeObject().DeepCopyObject()),
347347
)
348348
rlog.Exit("kc.Patch (metadata + spec)", err)
349349
if err != nil {
@@ -376,7 +376,7 @@ func (r *resourceReconciler) patchResourceStatus(
376376
err = r.kc.Status().Patch(
377377
ctx,
378378
latest.RuntimeObject().DeepCopyObject(),
379-
client.MergeFrom(desired.RuntimeObject()),
379+
client.MergeFrom(desired.RuntimeObject().DeepCopyObject()),
380380
)
381381
rlog.Exit("kc.Patch (status)", err)
382382
if err != nil {

pkg/runtime/reconciler_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func TestReconcilerUpdate(t *testing.T) {
5858

5959
desiredRTObj := &k8srtmocks.Object{}
6060
desiredRTObj.On("GetObjectKind").Return(objKind)
61+
desiredRTObj.On("DeepCopyObject").Return(desiredRTObj)
6162

6263
desiredMetaObj := &k8sobj.Unstructured{}
6364
desiredMetaObj.SetAnnotations(map[string]string{})
@@ -174,6 +175,7 @@ func TestReconcilerUpdate_PatchMetadataAndSpec_DiffInMetadata(t *testing.T) {
174175

175176
desiredRTObj := &k8srtmocks.Object{}
176177
desiredRTObj.On("GetObjectKind").Return(objKind)
178+
desiredRTObj.On("DeepCopyObject").Return(desiredRTObj)
177179

178180
desiredMetaObj := &k8sobj.Unstructured{}
179181
desiredMetaObj.SetAnnotations(map[string]string{})
@@ -291,6 +293,7 @@ func TestReconcilerUpdate_PatchMetadataAndSpec_DiffInSpec(t *testing.T) {
291293

292294
desiredRTObj := &k8srtmocks.Object{}
293295
desiredRTObj.On("GetObjectKind").Return(objKind)
296+
desiredRTObj.On("DeepCopyObject").Return(desiredRTObj)
294297

295298
desiredMetaObj := &k8sobj.Unstructured{}
296299
desiredMetaObj.SetAnnotations(map[string]string{})

0 commit comments

Comments
 (0)