Skip to content

Commit 580f203

Browse files
authored
Merge pull request #2483 from k8s-infra-cherrypick-robot/cherry-pick-2479-to-release-0.16
[release-0.16] 🐛 Do not update anything but status when using subresource client
2 parents 94cefd3 + 339df9a commit 580f203

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

Diff for: pkg/client/fake/client.go

+2-40
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,10 @@ func (t versionedTracker) update(gvr schema.GroupVersionResource, obj runtime.Ob
400400

401401
if t.withStatusSubresource.Has(gvk) {
402402
if isStatus { // copy everything but status and metadata.ResourceVersion from original object
403-
if err := copyNonStatusFrom(oldObject, obj); err != nil {
403+
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)
406407
} else { // copy status from original object
407408
if err := copyStatusFrom(oldObject, obj); err != nil {
408409
return fmt.Errorf("failed to copy the status for object with status subresource: %w", err)
@@ -949,45 +950,6 @@ func dryPatch(action testing.PatchActionImpl, tracker testing.ObjectTracker) (ru
949950
return obj, nil
950951
}
951952

952-
func copyNonStatusFrom(old, new runtime.Object) error {
953-
newClientObject, ok := new.(client.Object)
954-
if !ok {
955-
return fmt.Errorf("%T is not a client.Object", new)
956-
}
957-
// The only thing other than status we have to retain
958-
rv := newClientObject.GetResourceVersion()
959-
960-
oldMapStringAny, err := toMapStringAny(old)
961-
if err != nil {
962-
return fmt.Errorf("failed to convert old to *unstructured.Unstructured: %w", err)
963-
}
964-
newMapStringAny, err := toMapStringAny(new)
965-
if err != nil {
966-
return fmt.Errorf("failed to convert new to *unststructured.Unstructured: %w", err)
967-
}
968-
969-
// delete everything other than status in case it has fields that were not present in
970-
// the old object
971-
for k := range newMapStringAny {
972-
if k != "status" {
973-
delete(newMapStringAny, k)
974-
}
975-
}
976-
// copy everything other than status from the old object
977-
for k := range oldMapStringAny {
978-
if k != "status" {
979-
newMapStringAny[k] = oldMapStringAny[k]
980-
}
981-
}
982-
983-
if err := fromMapStringAny(newMapStringAny, new); err != nil {
984-
return fmt.Errorf("failed to convert back from map[string]any: %w", err)
985-
}
986-
newClientObject.SetResourceVersion(rv)
987-
988-
return nil
989-
}
990-
991953
// copyStatusFrom copies the status from old into new
992954
func copyStatusFrom(old, new runtime.Object) error {
993955
oldMapStringAny, err := toMapStringAny(old)

Diff for: pkg/client/fake/client_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,13 @@ var _ = Describe("Fake client", func() {
14601460
objOriginal := obj.DeepCopy()
14611461

14621462
obj.Spec.PodCIDR = "cidr-from-status-update"
1463+
obj.Annotations = map[string]string{
1464+
"some-annotation-key": "some-annotation-value",
1465+
}
1466+
obj.Labels = map[string]string{
1467+
"some-label-key": "some-label-value",
1468+
}
1469+
14631470
obj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
14641471
Expect(cl.Status().Update(context.Background(), obj)).NotTo(HaveOccurred())
14651472

@@ -1472,6 +1479,36 @@ var _ = Describe("Fake client", func() {
14721479
objOriginal.Status.NodeInfo.MachineID = "machine-id-from-status-update"
14731480
Expect(cmp.Diff(objOriginal, actual)).To(BeEmpty())
14741481
})
1482+
It("Should only override status fields of typed objects that have a status subresource on status update", func() {
1483+
obj := &corev1.Node{
1484+
ObjectMeta: metav1.ObjectMeta{
1485+
Name: "node",
1486+
},
1487+
Spec: corev1.NodeSpec{
1488+
PodCIDR: "old-cidr",
1489+
},
1490+
Status: corev1.NodeStatus{
1491+
NodeInfo: corev1.NodeSystemInfo{
1492+
MachineID: "machine-id",
1493+
},
1494+
},
1495+
}
1496+
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
1497+
objOriginal := obj.DeepCopy()
1498+
1499+
obj.Status.Phase = corev1.NodeRunning
1500+
Expect(cl.Status().Update(context.Background(), obj)).NotTo(HaveOccurred())
1501+
1502+
actual := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: obj.Name}}
1503+
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(actual), actual)).NotTo(HaveOccurred())
1504+
1505+
objOriginal.APIVersion = actual.APIVersion
1506+
objOriginal.Kind = actual.Kind
1507+
objOriginal.ResourceVersion = actual.ResourceVersion
1508+
Expect(cmp.Diff(objOriginal, actual)).ToNot(BeEmpty())
1509+
Expect(objOriginal.Status.NodeInfo.MachineID).To(Equal(actual.Status.NodeInfo.MachineID))
1510+
Expect(objOriginal.Status.Phase).ToNot(Equal(actual.Status.Phase))
1511+
})
14751512

14761513
It("should not change the status of typed objects that have a status subresource on patch", func() {
14771514
obj := &corev1.Node{

0 commit comments

Comments
 (0)