Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fake client.Update() should not store omit empty Status fields #2476

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,21 +990,8 @@ func copyNonStatusFrom(old, new runtime.Object) error {

// copyStatusFrom copies the status from old into new
func copyStatusFrom(old, new runtime.Object) error {
oldMapStringAny, err := toMapStringAny(old)
if err != nil {
return fmt.Errorf("failed to convert old to *unstructured.Unstructured: %w", err)
}
newMapStringAny, err := toMapStringAny(new)
if err != nil {
return fmt.Errorf("failed to convert new to *unststructured.Unstructured: %w", err)
}

newMapStringAny["status"] = oldMapStringAny["status"]

if err := fromMapStringAny(newMapStringAny, new); err != nil {
return fmt.Errorf("failed to convert back from map[string]any: %w", err)
}

reflect.ValueOf(new).Elem().FieldByName("Status").Set(
reflect.ValueOf(old).Elem().FieldByName("Status"))
return nil
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,23 @@ var _ = Describe("Fake client", func() {
Expect(obj.Status).To(BeEquivalentTo(corev1.NodeStatus{NodeInfo: corev1.NodeSystemInfo{MachineID: "machine-id"}}))
})

It("should not change the status of typed objects that have a status subresource on update with omitempty fields", func() {
obj := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node",
},
}
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()

obj.Status.Config = &corev1.NodeConfigStatus{}
obj.Status.Config.Error = "some string"
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{}))
})

It("should return a conflict error when an incorrect RV is used on status update", func() {
obj := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Loading