Skip to content

Commit 3b91edb

Browse files
committed
unit tests to ensure pod metadata cannot be updated during resize.
1 parent 7ac302b commit 3b91edb

File tree

3 files changed

+423
-439
lines changed

3 files changed

+423
-439
lines changed

pkg/api/pod/testing/make.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
// Tweak is a function that modifies a Pod.
2828
type Tweak func(*api.Pod)
2929
type TweakContainer func(*api.Container)
30+
type TweakPodStatus func(*api.PodStatus)
3031

3132
// MakePod helps construct Pod objects (which pass API validation) more
3233
// legibly and tersely than a Go struct definition. By default this produces
@@ -297,3 +298,34 @@ func SetContainerRestartPolicy(policy api.ContainerRestartPolicy) TweakContainer
297298
cnr.RestartPolicy = &policy
298299
}
299300
}
301+
302+
func MakePodStatus(tweaks ...TweakPodStatus) api.PodStatus {
303+
ps := api.PodStatus{}
304+
305+
for _, tweak := range tweaks {
306+
tweak(&ps)
307+
}
308+
309+
return ps
310+
}
311+
312+
func SetContainerStatuses(containerStatuses ...api.ContainerStatus) TweakPodStatus {
313+
return func(podstatus *api.PodStatus) {
314+
podstatus.ContainerStatuses = containerStatuses
315+
}
316+
}
317+
318+
func MakeContainerStatus(name string, allocatedResources api.ResourceList) api.ContainerStatus {
319+
cs := api.ContainerStatus{
320+
Name: name,
321+
AllocatedResources: allocatedResources,
322+
}
323+
324+
return cs
325+
}
326+
327+
func SetResizeStatus(resizeStatus api.PodResizeStatus) TweakPodStatus {
328+
return func(podstatus *api.PodStatus) {
329+
podstatus.Resize = resizeStatus
330+
}
331+
}

pkg/registry/core/pod/strategy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ var EphemeralContainersStrategy = podEphemeralContainersStrategy{Strategy}
247247

248248
// dropNonEphemeralContainerUpdates discards all changes except for pod.Spec.EphemeralContainers and certain metadata
249249
func dropNonEphemeralContainerUpdates(newPod, oldPod *api.Pod) *api.Pod {
250-
pod := mungePod(newPod, oldPod)
250+
pod := dropPodUpdates(newPod, oldPod)
251251
pod.Spec.EphemeralContainers = newPod.Spec.EphemeralContainers
252252
return pod
253253
}
@@ -282,7 +282,7 @@ var ResizeStrategy = podResizeStrategy{Strategy}
282282

283283
// dropNonPodResizeUpdates discards all changes except for pod.Spec.Containers[*].Resources,ResizePolicy and certain metadata
284284
func dropNonPodResizeUpdates(newPod, oldPod *api.Pod) *api.Pod {
285-
pod := mungePod(newPod, oldPod)
285+
pod := dropPodUpdates(newPod, oldPod)
286286

287287
oldCtrToIndex := make(map[string]int)
288288
for idx, ctr := range pod.Spec.Containers {
@@ -321,8 +321,8 @@ func (podResizeStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.
321321
return nil
322322
}
323323

324-
// mungePod mutates metadata information of old pod with the new pod.
325-
func mungePod(newPod, oldPod *api.Pod) *api.Pod {
324+
// dropPodUpdates drops any changes in the pod.
325+
func dropPodUpdates(newPod, oldPod *api.Pod) *api.Pod {
326326
pod := oldPod.DeepCopy()
327327
pod.Name = newPod.Name
328328
pod.Namespace = newPod.Namespace

0 commit comments

Comments
 (0)