Skip to content

Commit 0a54f21

Browse files
committed
Rework code to pass object back on status update
1 parent c527239 commit 0a54f21

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

pkg/client/fake/client.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,9 @@ func (t versionedTracker) update(gvr schema.GroupVersionResource, obj runtime.Ob
403403
if err := copyStatusFrom(obj, oldObject); err != nil {
404404
return fmt.Errorf("failed to copy non-status field for object with status subresouce: %w", err)
405405
}
406-
407-
obj = oldObject.DeepCopyObject().(client.Object)
406+
passedRV := accessor.GetResourceVersion()
407+
reflect.ValueOf(obj).Elem().Set(reflect.ValueOf(oldObject.DeepCopyObject()).Elem())
408+
accessor.SetResourceVersion(passedRV)
408409
} else { // copy status from original object
409410
if err := copyStatusFrom(oldObject, obj); err != nil {
410411
return fmt.Errorf("failed to copy the status for object with status subresource: %w", err)
@@ -437,14 +438,6 @@ func (t versionedTracker) update(gvr schema.GroupVersionResource, obj runtime.Ob
437438
intResourceVersion++
438439
accessor.SetResourceVersion(strconv.FormatUint(intResourceVersion, 10))
439440

440-
// obtain the current obj accessor's pointer,
441-
// so we can make an update on the current obj
442-
currentAccessor, err := meta.Accessor(obj)
443-
if err != nil {
444-
return fmt.Errorf("failed to get accessor for object: %w", err)
445-
}
446-
currentAccessor.SetResourceVersion(strconv.FormatUint(intResourceVersion, 10))
447-
448441
if !deleting && !deletionTimestampEqual(accessor, oldAccessor) {
449442
return fmt.Errorf("error: Unable to edit %s: metadata.deletionTimestamp field is immutable", accessor.GetName())
450443
}

pkg/client/fake/client_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ import (
4545
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
4646
)
4747

48+
const machineIDFromStatusUpdate = "machine-id-from-status-update"
49+
4850
var _ = Describe("Fake client", func() {
4951
var dep *appsv1.Deployment
5052
var dep2 *appsv1.Deployment
@@ -1464,7 +1466,7 @@ var _ = Describe("Fake client", func() {
14641466
"some-label-key": "some-label-value",
14651467
}
14661468

1467-
obj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1469+
obj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
14681470
Expect(cl.Status().Update(context.Background(), obj)).NotTo(HaveOccurred())
14691471

14701472
actual := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: obj.Name}}
@@ -1473,7 +1475,7 @@ var _ = Describe("Fake client", func() {
14731475
objOriginal.APIVersion = actual.APIVersion
14741476
objOriginal.Kind = actual.Kind
14751477
objOriginal.ResourceVersion = actual.ResourceVersion
1476-
objOriginal.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1478+
objOriginal.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
14771479
Expect(cmp.Diff(objOriginal, actual)).To(BeEmpty())
14781480
})
14791481

@@ -1494,7 +1496,7 @@ var _ = Describe("Fake client", func() {
14941496
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
14951497
expectedObj := obj.DeepCopy()
14961498

1497-
obj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1499+
obj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
14981500
Expect(cl.Status().Update(context.Background(), obj)).NotTo(HaveOccurred())
14991501

15001502
obj.Annotations = map[string]string{
@@ -1511,7 +1513,7 @@ var _ = Describe("Fake client", func() {
15111513
expectedObj.APIVersion = actual.APIVersion
15121514
expectedObj.Kind = actual.Kind
15131515
expectedObj.ResourceVersion = actual.ResourceVersion
1514-
expectedObj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1516+
expectedObj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
15151517
Expect(cmp.Diff(expectedObj, actual)).To(BeEmpty())
15161518
})
15171519

@@ -1541,7 +1543,7 @@ var _ = Describe("Fake client", func() {
15411543
Expect(cl.Update(context.Background(), obj)).NotTo(HaveOccurred())
15421544

15431545
obj.Spec.PodCIDR = "cidr-from-status-update"
1544-
obj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1546+
obj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
15451547
Expect(cl.Status().Update(context.Background(), obj)).NotTo(HaveOccurred())
15461548

15471549
actual := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: obj.Name}}
@@ -1550,7 +1552,7 @@ var _ = Describe("Fake client", func() {
15501552
expectedObj.APIVersion = actual.APIVersion
15511553
expectedObj.Kind = actual.Kind
15521554
expectedObj.ResourceVersion = actual.ResourceVersion
1553-
expectedObj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1555+
expectedObj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
15541556
Expect(cmp.Diff(expectedObj, actual)).To(BeEmpty())
15551557
})
15561558

0 commit comments

Comments
 (0)