diff --git a/pkg/registry/core/pod/strategy.go b/pkg/registry/core/pod/strategy.go index 5320526fcf7..f9905f1a4cb 100644 --- a/pkg/registry/core/pod/strategy.go +++ b/pkg/registry/core/pod/strategy.go @@ -226,6 +226,8 @@ func (podStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime. if newPod.Status.QOSClass == "" { newPod.Status.QOSClass = oldPod.Status.QOSClass } + + podutil.DropDisabledPodFields(newPod, oldPod) } func (podStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { diff --git a/pkg/registry/core/pod/strategy_test.go b/pkg/registry/core/pod/strategy_test.go index 1f3a2289e88..2b3fd197040 100644 --- a/pkg/registry/core/pod/strategy_test.go +++ b/pkg/registry/core/pod/strategy_test.go @@ -3325,3 +3325,85 @@ func TestEphemeralContainersPrepareForUpdate(t *testing.T) { }) } } + +func TestStatusPrepareForUpdate(t *testing.T) { + testCases := []struct { + description string + oldPod *api.Pod + newPod *api.Pod + expected *api.Pod + }{ + { + description: "preserve old owner references", + oldPod: &api.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pod", + OwnerReferences: []metav1.OwnerReference{{APIVersion: "v1", Kind: "ReplicaSet", Name: "rs-1"}}, + }, + }, + newPod: &api.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pod", + OwnerReferences: []metav1.OwnerReference{{APIVersion: "v1", Kind: "ReplicaSet", Name: "rs-2"}}, + }, + }, + expected: &api.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pod", + OwnerReferences: []metav1.OwnerReference{{APIVersion: "v1", Kind: "ReplicaSet", Name: "rs-1"}}, + }, + }, + }, + { + description: "preserve old qos if empty", + oldPod: &api.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod"}, + Status: api.PodStatus{ + QOSClass: "Guaranteed", + }, + }, + newPod: &api.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod"}, + }, + expected: &api.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod"}, + Status: api.PodStatus{ + QOSClass: "Guaranteed", + }, + }, + }, + { + description: "drop disabled status fields", + oldPod: &api.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod"}, + Status: api.PodStatus{}, + }, + newPod: &api.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod"}, + Status: api.PodStatus{ + ResourceClaimStatuses: []api.PodResourceClaimStatus{ + {Name: "my-claim", ResourceClaimName: ptr.To("pod-my-claim")}, + }, + ContainerStatuses: []api.ContainerStatus{ + {Resources: &api.ResourceRequirements{}}, + }, + }, + }, + expected: &api.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod"}, + Status: api.PodStatus{ + ContainerStatuses: []api.ContainerStatus{{}}, + }, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.description, func(t *testing.T) { + StatusStrategy.PrepareForUpdate(genericapirequest.NewContext(), tc.newPod, tc.oldPod) + if !cmp.Equal(tc.expected, tc.newPod) { + t.Errorf("StatusStrategy.PrepareForUpdate() diff = %v", cmp.Diff(tc.expected, tc.newPod)) + } + }) + } +}