Skip to content

Commit 1208f25

Browse files
committed
Verify oom_score_adj for containers that have been restarted in pod resize e2e
1 parent ec1b493 commit 1208f25

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

pkg/kubelet/kubelet_pods.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,7 @@ func (kl *Kubelet) convertToAPIContainerStatuses(pod *v1.Pod, podStatus *kubecon
21092109
if _, exists := resources.Requests[v1.ResourceMemory]; exists {
21102110
// Get memory requests from actuated resources
21112111
if actuatedResources, found := kl.allocationManager.GetActuatedResources(pod.UID, allocatedContainer.Name); found {
2112-
resources.Requests[v1.ResourceMemory] = actuatedResources.Requests.Memory().DeepCopy()
2112+
resources.Requests[v1.ResourceMemory] = *actuatedResources.Requests.Memory()
21132113
}
21142114
}
21152115
}

test/e2e/framework/pod/resize.go

+29-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
utilerrors "k8s.io/apimachinery/pkg/util/errors"
3131
helpers "k8s.io/component-helpers/resource"
3232
kubecm "k8s.io/kubernetes/pkg/kubelet/cm"
33+
kubeqos "k8s.io/kubernetes/pkg/kubelet/qos"
3334
"k8s.io/kubernetes/test/e2e/framework"
3435
imageutils "k8s.io/kubernetes/test/utils/image"
3536

@@ -359,22 +360,22 @@ func VerifyPodContainersCgroupValues(ctx context.Context, f *framework.Framework
359360
return utilerrors.NewAggregate(errs)
360361
}
361362

362-
func verifyPodRestarts(pod *v1.Pod, wantInfo []ResizableContainerInfo) error {
363+
func verifyPodRestarts(f *framework.Framework, pod *v1.Pod, wantInfo []ResizableContainerInfo) error {
363364
ginkgo.GinkgoHelper()
364365

365366
initCtrStatuses, ctrStatuses := separateContainerStatuses(wantInfo)
366367
errs := []error{}
367-
if err := verifyContainerRestarts(pod.Status.InitContainerStatuses, initCtrStatuses); err != nil {
368+
if err := verifyContainerRestarts(f, pod, pod.Status.InitContainerStatuses, initCtrStatuses); err != nil {
368369
errs = append(errs, err)
369370
}
370-
if err := verifyContainerRestarts(pod.Status.ContainerStatuses, ctrStatuses); err != nil {
371+
if err := verifyContainerRestarts(f, pod, pod.Status.ContainerStatuses, ctrStatuses); err != nil {
371372
errs = append(errs, err)
372373
}
373374

374375
return utilerrors.NewAggregate(errs)
375376
}
376377

377-
func verifyContainerRestarts(gotStatuses []v1.ContainerStatus, wantStatuses []v1.ContainerStatus) error {
378+
func verifyContainerRestarts(f *framework.Framework, pod *v1.Pod, gotStatuses []v1.ContainerStatus, wantStatuses []v1.ContainerStatus) error {
378379
ginkgo.GinkgoHelper()
379380

380381
if len(gotStatuses) != len(wantStatuses) {
@@ -386,11 +387,34 @@ func verifyContainerRestarts(gotStatuses []v1.ContainerStatus, wantStatuses []v1
386387
for i, gotStatus := range gotStatuses {
387388
if gotStatus.RestartCount != wantStatuses[i].RestartCount {
388389
errs = append(errs, fmt.Errorf("unexpected number of restarts for container %s: got %d, want %d", gotStatus.Name, gotStatus.RestartCount, wantStatuses[i].RestartCount))
390+
} else if gotStatus.RestartCount > 0 {
391+
err := verifyOomScoreAdj(f, pod, gotStatus.Name)
392+
if err != nil {
393+
errs = append(errs, err)
394+
}
389395
}
390396
}
391397
return utilerrors.NewAggregate(errs)
392398
}
393399

400+
func verifyOomScoreAdj(f *framework.Framework, pod *v1.Pod, containerName string) error {
401+
container := FindContainerInPod(pod, containerName)
402+
if container == nil {
403+
return fmt.Errorf("failed to find container %s in pod %s", containerName, pod.Name)
404+
}
405+
406+
node, err := f.ClientSet.CoreV1().Nodes().Get(context.Background(), pod.Spec.NodeName, metav1.GetOptions{})
407+
if err != nil {
408+
return err
409+
}
410+
411+
nodeMemoryCapacity := node.Status.Capacity[v1.ResourceMemory]
412+
oomScoreAdj := kubeqos.GetContainerOOMScoreAdjust(pod, container, int64(nodeMemoryCapacity.Value()))
413+
expectedOomScoreAdj := strconv.FormatInt(int64(oomScoreAdj), 10)
414+
415+
return VerifyOomScoreAdjValue(f, pod, container.Name, expectedOomScoreAdj)
416+
}
417+
394418
func WaitForPodResizeActuation(ctx context.Context, f *framework.Framework, podClient *PodClient, pod *v1.Pod, expectedContainers []ResizableContainerInfo) *v1.Pod {
395419
ginkgo.GinkgoHelper()
396420
// Wait for resize to complete.
@@ -440,7 +464,7 @@ func ExpectPodResized(ctx context.Context, f *framework.Framework, resizedPod *v
440464
if resourceErrs := VerifyPodStatusResources(resizedPod, expectedContainers); resourceErrs != nil {
441465
errs = append(errs, fmt.Errorf("container status resources don't match expected: %w", formatErrors(resourceErrs)))
442466
}
443-
if restartErrs := verifyPodRestarts(resizedPod, expectedContainers); restartErrs != nil {
467+
if restartErrs := verifyPodRestarts(f, resizedPod, expectedContainers); restartErrs != nil {
444468
errs = append(errs, fmt.Errorf("container restart counts don't match expected: %w", formatErrors(restartErrs)))
445469
}
446470

test/e2e/framework/pod/utils.go

+15
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,21 @@ func FindPodConditionByType(podStatus *v1.PodStatus, conditionType v1.PodConditi
256256
return nil
257257
}
258258

259+
// FindContainerByName finds the v1.Container in a pod by its name in the provided pod
260+
func FindContainerInPod(pod *v1.Pod, containerName string) *v1.Container {
261+
for _, container := range pod.Spec.InitContainers {
262+
if container.Name == containerName {
263+
return &container
264+
}
265+
}
266+
for _, container := range pod.Spec.Containers {
267+
if container.Name == containerName {
268+
return &container
269+
}
270+
}
271+
return nil
272+
}
273+
259274
// FindContainerStatusInPod finds a container status by its name in the provided pod
260275
func FindContainerStatusInPod(pod *v1.Pod, containerName string) *v1.ContainerStatus {
261276
for _, containerStatus := range pod.Status.InitContainerStatuses {

0 commit comments

Comments
 (0)