Skip to content

Commit

Permalink
Merge pull request #2484 from kubernetes-sigs/status-zeroing
Browse files Browse the repository at this point in the history
🐛 Fix status subresource getting updated on Update when it is empty
  • Loading branch information
k8s-ci-robot committed Sep 9, 2023
2 parents 14d669d + e368149 commit 9dd4fc7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
1 change: 1 addition & 0 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ func fromMapStringAny(u map[string]any, target runtime.Object) error {
return fmt.Errorf("failed to serialize: %w", err)
}

zero(target)
if err := json.Unmarshal(serialized, &target); err != nil {
return fmt.Errorf("failed to deserialize: %w", err)
}
Expand Down
22 changes: 7 additions & 15 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1409,22 +1409,19 @@ var _ = Describe("Fake client", func() {
})

It("should not change the status of typed objects that have a status subresource on update", func() {
obj := &corev1.Node{
obj := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "node",
},
Status: corev1.NodeStatus{
NodeInfo: corev1.NodeSystemInfo{MachineID: "machine-id"},
Name: "pod",
},
}
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()

obj.Status.NodeInfo.MachineID = "updated-machine-id"
obj.Status.Phase = "Running"
Expect(cl.Update(context.Background(), obj)).To(Succeed())

Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(obj), obj)).To(Succeed())

Expect(obj.Status).To(BeEquivalentTo(corev1.NodeStatus{NodeInfo: corev1.NodeSystemInfo{MachineID: "machine-id"}}))
Expect(obj.Status).To(BeEquivalentTo(corev1.PodStatus{}))
})

It("should return a conflict error when an incorrect RV is used on status update", func() {
Expand Down Expand Up @@ -1511,25 +1508,20 @@ var _ = Describe("Fake client", func() {
})

It("should not change the status of typed objects that have a status subresource on patch", func() {
obj := &corev1.Node{
obj := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "node",
},
Status: corev1.NodeStatus{
NodeInfo: corev1.NodeSystemInfo{
MachineID: "machine-id",
},
},
}
Expect(cl.Create(context.Background(), obj)).To(Succeed())
original := obj.DeepCopy()

obj.Status.NodeInfo.MachineID = "machine-id-from-patch"
obj.Status.Phase = "Running"
Expect(cl.Patch(context.Background(), obj, client.MergeFrom(original))).To(Succeed())

Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(obj), obj)).To(Succeed())

Expect(obj.Status).To(BeEquivalentTo(corev1.NodeStatus{NodeInfo: corev1.NodeSystemInfo{MachineID: "machine-id"}}))
Expect(obj.Status).To(BeEquivalentTo(corev1.PodStatus{}))
})

It("should not change non-status field of typed objects that have a status subresource on status patch", func() {
Expand Down

0 comments on commit 9dd4fc7

Please sign in to comment.