Skip to content

Commit 8f0fe71

Browse files
committed
Draft: Test that an object is updatable after updating its status.
1 parent 9dd4fc7 commit 8f0fe71

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

pkg/client/fake/client_test.go

+78
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,84 @@ var _ = Describe("Fake client", func() {
14761476
objOriginal.Status.NodeInfo.MachineID = "machine-id-from-status-update"
14771477
Expect(cmp.Diff(objOriginal, actual)).To(BeEmpty())
14781478
})
1479+
1480+
It("should be able to update an object after updating an object's status", func() {
1481+
obj := &corev1.Node{
1482+
ObjectMeta: metav1.ObjectMeta{
1483+
Name: "node",
1484+
},
1485+
Spec: corev1.NodeSpec{
1486+
PodCIDR: "old-cidr",
1487+
},
1488+
Status: corev1.NodeStatus{
1489+
NodeInfo: corev1.NodeSystemInfo{
1490+
MachineID: "machine-id",
1491+
},
1492+
},
1493+
}
1494+
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
1495+
expectedObj := obj.DeepCopy()
1496+
1497+
obj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1498+
Expect(cl.Status().Update(context.Background(), obj)).NotTo(HaveOccurred())
1499+
1500+
obj.Annotations = map[string]string{
1501+
"some-annotation-key": "some",
1502+
}
1503+
expectedObj.Annotations = map[string]string{
1504+
"some-annotation-key": "some",
1505+
}
1506+
Expect(cl.Update(context.Background(), obj)).NotTo(HaveOccurred())
1507+
1508+
actual := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: obj.Name}}
1509+
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(actual), actual)).NotTo(HaveOccurred())
1510+
1511+
expectedObj.APIVersion = actual.APIVersion
1512+
expectedObj.Kind = actual.Kind
1513+
expectedObj.ResourceVersion = actual.ResourceVersion
1514+
expectedObj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1515+
Expect(cmp.Diff(expectedObj, actual)).To(BeEmpty())
1516+
})
1517+
1518+
It("should be able to update an object's status after updating an object", func() {
1519+
obj := &corev1.Node{
1520+
ObjectMeta: metav1.ObjectMeta{
1521+
Name: "node",
1522+
},
1523+
Spec: corev1.NodeSpec{
1524+
PodCIDR: "old-cidr",
1525+
},
1526+
Status: corev1.NodeStatus{
1527+
NodeInfo: corev1.NodeSystemInfo{
1528+
MachineID: "machine-id",
1529+
},
1530+
},
1531+
}
1532+
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
1533+
expectedObj := obj.DeepCopy()
1534+
1535+
obj.Annotations = map[string]string{
1536+
"some-annotation-key": "some",
1537+
}
1538+
expectedObj.Annotations = map[string]string{
1539+
"some-annotation-key": "some",
1540+
}
1541+
Expect(cl.Update(context.Background(), obj)).NotTo(HaveOccurred())
1542+
1543+
obj.Spec.PodCIDR = "cidr-from-status-update"
1544+
obj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1545+
Expect(cl.Status().Update(context.Background(), obj)).NotTo(HaveOccurred())
1546+
1547+
actual := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: obj.Name}}
1548+
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(actual), actual)).NotTo(HaveOccurred())
1549+
1550+
expectedObj.APIVersion = actual.APIVersion
1551+
expectedObj.Kind = actual.Kind
1552+
expectedObj.ResourceVersion = actual.ResourceVersion
1553+
expectedObj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
1554+
Expect(cmp.Diff(expectedObj, actual)).To(BeEmpty())
1555+
})
1556+
14791557
It("Should only override status fields of typed objects that have a status subresource on status update", func() {
14801558
obj := &corev1.Node{
14811559
ObjectMeta: metav1.ObjectMeta{

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)