Skip to content

Commit aa2bc6a

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

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

pkg/client/fake/client.go

Lines changed: 3 additions & 10 deletions
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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ import (
4545
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
4646
)
4747

48+
const (
49+
machineIDFromStatusUpdate = "machine-id-from-status-update"
50+
cidrFromStatusUpdate = "cidr-from-status-update"
51+
)
52+
4853
var _ = Describe("Fake client", func() {
4954
var dep *appsv1.Deployment
5055
var dep2 *appsv1.Deployment
@@ -1456,15 +1461,15 @@ var _ = Describe("Fake client", func() {
14561461
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
14571462
objOriginal := obj.DeepCopy()
14581463

1459-
obj.Spec.PodCIDR = "cidr-from-status-update"
1464+
obj.Spec.PodCIDR = cidrFromStatusUpdate
14601465
obj.Annotations = map[string]string{
14611466
"some-annotation-key": "some-annotation-value",
14621467
}
14631468
obj.Labels = map[string]string{
14641469
"some-label-key": "some-label-value",
14651470
}
14661471

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

14701475
actual := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: obj.Name}}
@@ -1473,7 +1478,7 @@ var _ = Describe("Fake client", func() {
14731478
objOriginal.APIVersion = actual.APIVersion
14741479
objOriginal.Kind = actual.Kind
14751480
objOriginal.ResourceVersion = actual.ResourceVersion
1476-
objOriginal.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1481+
objOriginal.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
14771482
Expect(cmp.Diff(objOriginal, actual)).To(BeEmpty())
14781483
})
14791484

@@ -1494,7 +1499,7 @@ var _ = Describe("Fake client", func() {
14941499
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
14951500
expectedObj := obj.DeepCopy()
14961501

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

15001505
obj.Annotations = map[string]string{
@@ -1511,7 +1516,7 @@ var _ = Describe("Fake client", func() {
15111516
expectedObj.APIVersion = actual.APIVersion
15121517
expectedObj.Kind = actual.Kind
15131518
expectedObj.ResourceVersion = actual.ResourceVersion
1514-
expectedObj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1519+
expectedObj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
15151520
Expect(cmp.Diff(expectedObj, actual)).To(BeEmpty())
15161521
})
15171522

@@ -1540,8 +1545,8 @@ var _ = Describe("Fake client", func() {
15401545
}
15411546
Expect(cl.Update(context.Background(), obj)).NotTo(HaveOccurred())
15421547

1543-
obj.Spec.PodCIDR = "cidr-from-status-update"
1544-
obj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1548+
obj.Spec.PodCIDR = cidrFromStatusUpdate
1549+
obj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
15451550
Expect(cl.Status().Update(context.Background(), obj)).NotTo(HaveOccurred())
15461551

15471552
actual := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: obj.Name}}
@@ -1550,7 +1555,7 @@ var _ = Describe("Fake client", func() {
15501555
expectedObj.APIVersion = actual.APIVersion
15511556
expectedObj.Kind = actual.Kind
15521557
expectedObj.ResourceVersion = actual.ResourceVersion
1553-
expectedObj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1558+
expectedObj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
15541559
Expect(cmp.Diff(expectedObj, actual)).To(BeEmpty())
15551560
})
15561561

@@ -1614,7 +1619,7 @@ var _ = Describe("Fake client", func() {
16141619
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
16151620
objOriginal := obj.DeepCopy()
16161621

1617-
obj.Spec.PodCIDR = "cidr-from-status-update"
1622+
obj.Spec.PodCIDR = cidrFromStatusUpdate
16181623
obj.Status.NodeInfo.MachineID = "machine-id"
16191624
Expect(cl.Status().Patch(context.Background(), obj, client.MergeFrom(objOriginal))).NotTo(HaveOccurred())
16201625

0 commit comments

Comments
 (0)