Skip to content

Commit b40bcb8

Browse files
k8s-infra-cherrypick-robotberlin-abalvaroaleman
authored
[release-0.16] 🐛 Fix returning object after status update (#2490)
* Draft: Test that an object is updatable after updating its status. * Draft: ensure we udpate the new obj's accessor's ResourceVersion after doing the deep copy * Rework code to pass object back on status update --------- Co-authored-by: Adam Berlin <[email protected]> Co-authored-by: Alvaro Aleman <[email protected]>
1 parent 1cf5b88 commit b40bcb8

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

pkg/client/fake/client.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +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-
obj = oldObject.DeepCopyObject().(client.Object)
406+
passedRV := accessor.GetResourceVersion()
407+
reflect.ValueOf(obj).Elem().Set(reflect.ValueOf(oldObject.DeepCopyObject()).Elem())
408+
accessor.SetResourceVersion(passedRV)
407409
} else { // copy status from original object
408410
if err := copyStatusFrom(oldObject, obj); err != nil {
409411
return fmt.Errorf("failed to copy the status for object with status subresource: %w", err)

pkg/client/fake/client_test.go

+87-4
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,9 +1478,87 @@ 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
})
1484+
1485+
It("should be able to update an object after updating an object's status", func() {
1486+
obj := &corev1.Node{
1487+
ObjectMeta: metav1.ObjectMeta{
1488+
Name: "node",
1489+
},
1490+
Spec: corev1.NodeSpec{
1491+
PodCIDR: "old-cidr",
1492+
},
1493+
Status: corev1.NodeStatus{
1494+
NodeInfo: corev1.NodeSystemInfo{
1495+
MachineID: "machine-id",
1496+
},
1497+
},
1498+
}
1499+
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
1500+
expectedObj := obj.DeepCopy()
1501+
1502+
obj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
1503+
Expect(cl.Status().Update(context.Background(), obj)).NotTo(HaveOccurred())
1504+
1505+
obj.Annotations = map[string]string{
1506+
"some-annotation-key": "some",
1507+
}
1508+
expectedObj.Annotations = map[string]string{
1509+
"some-annotation-key": "some",
1510+
}
1511+
Expect(cl.Update(context.Background(), obj)).NotTo(HaveOccurred())
1512+
1513+
actual := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: obj.Name}}
1514+
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(actual), actual)).NotTo(HaveOccurred())
1515+
1516+
expectedObj.APIVersion = actual.APIVersion
1517+
expectedObj.Kind = actual.Kind
1518+
expectedObj.ResourceVersion = actual.ResourceVersion
1519+
expectedObj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
1520+
Expect(cmp.Diff(expectedObj, actual)).To(BeEmpty())
1521+
})
1522+
1523+
It("should be able to update an object's status after updating an object", func() {
1524+
obj := &corev1.Node{
1525+
ObjectMeta: metav1.ObjectMeta{
1526+
Name: "node",
1527+
},
1528+
Spec: corev1.NodeSpec{
1529+
PodCIDR: "old-cidr",
1530+
},
1531+
Status: corev1.NodeStatus{
1532+
NodeInfo: corev1.NodeSystemInfo{
1533+
MachineID: "machine-id",
1534+
},
1535+
},
1536+
}
1537+
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
1538+
expectedObj := obj.DeepCopy()
1539+
1540+
obj.Annotations = map[string]string{
1541+
"some-annotation-key": "some",
1542+
}
1543+
expectedObj.Annotations = map[string]string{
1544+
"some-annotation-key": "some",
1545+
}
1546+
Expect(cl.Update(context.Background(), obj)).NotTo(HaveOccurred())
1547+
1548+
obj.Spec.PodCIDR = cidrFromStatusUpdate
1549+
obj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
1550+
Expect(cl.Status().Update(context.Background(), obj)).NotTo(HaveOccurred())
1551+
1552+
actual := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: obj.Name}}
1553+
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(actual), actual)).NotTo(HaveOccurred())
1554+
1555+
expectedObj.APIVersion = actual.APIVersion
1556+
expectedObj.Kind = actual.Kind
1557+
expectedObj.ResourceVersion = actual.ResourceVersion
1558+
expectedObj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
1559+
Expect(cmp.Diff(expectedObj, actual)).To(BeEmpty())
1560+
})
1561+
14791562
It("Should only override status fields of typed objects that have a status subresource on status update", func() {
14801563
obj := &corev1.Node{
14811564
ObjectMeta: metav1.ObjectMeta{
@@ -1536,7 +1619,7 @@ var _ = Describe("Fake client", func() {
15361619
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
15371620
objOriginal := obj.DeepCopy()
15381621

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

pkg/client/interfaces.go

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ type SubResourceWriter interface {
142142
// Create saves the subResource object in the Kubernetes cluster. obj must be a
143143
// struct pointer so that obj can be updated with the content returned by the Server.
144144
Create(ctx context.Context, obj Object, subResource Object, opts ...SubResourceCreateOption) error
145+
145146
// Update updates the fields corresponding to the status subresource for the
146147
// given obj. obj must be a struct pointer so that obj can be updated
147148
// with the content returned by the Server.

0 commit comments

Comments
 (0)