@@ -19,6 +19,7 @@ package qos
19
19
import (
20
20
v1 "k8s.io/api/core/v1"
21
21
utilfeature "k8s.io/apiserver/pkg/util/feature"
22
+ resourcehelper "k8s.io/component-helpers/resource"
22
23
v1qos "k8s.io/kubernetes/pkg/apis/core/v1/helper/qos"
23
24
"k8s.io/kubernetes/pkg/features"
24
25
"k8s.io/kubernetes/pkg/kubelet/types"
@@ -63,14 +64,41 @@ func GetContainerOOMScoreAdjust(pod *v1.Pod, container *v1.Container, memoryCapa
63
64
// which use more than their request will have an OOM score of 1000 and will be prime
64
65
// targets for OOM kills.
65
66
// Note that this is a heuristic, it won't work if a container has many small processes.
66
- memoryRequest := container .Resources .Requests .Memory ().Value ()
67
- oomScoreAdjust := 1000 - (1000 * memoryRequest )/ memoryCapacity
67
+ containerMemReq := container .Resources .Requests .Memory ().Value ()
68
+
69
+ var oomScoreAdjust , remainingReqPerContainer int64
70
+ // When PodLevelResources feature is enabled, the OOM score adjustment formula is modified
71
+ // to account for pod-level memory requests. Any extra pod memory request that's
72
+ // not allocated to the containers is divided equally among all containers and
73
+ // added to their individual memory requests when calculating the OOM score
74
+ // adjustment. Otherwise, only container-level memory requests are used. See
75
+ // https://github.com/kubernetes/enhancements/blob/master/keps/sig-node/2837-pod-level-resource-spec/README.md#oom-score-adjustment
76
+ // for more details.
77
+ if utilfeature .DefaultFeatureGate .Enabled (features .PodLevelResources ) &&
78
+ resourcehelper .IsPodLevelRequestsSet (pod ) {
79
+ // TODO(ndixita): Refactor to use this formula in all cases, as
80
+ // remainingReqPerContainer will be 0 when pod-level resources are not set.
81
+ remainingReqPerContainer = remainingPodMemReqPerContainer (pod )
82
+ oomScoreAdjust = 1000 - (1000 * (containerMemReq + remainingReqPerContainer ) / memoryCapacity )
83
+ } else {
84
+ oomScoreAdjust = 1000 - (1000 * containerMemReq )/ memoryCapacity
85
+ }
68
86
69
87
// adapt the sidecarContainer memoryRequest for OOM ADJ calculation
70
88
// calculate the oom score adjustment based on: max-memory( currentSideCarContainer , min-memory(regular containers) ) .
71
89
if utilfeature .DefaultFeatureGate .Enabled (features .SidecarContainers ) && isSidecarContainer (pod , container ) {
72
90
// check min memory quantity in regular containers
73
91
minMemoryRequest := minRegularContainerMemory (* pod )
92
+
93
+ // When calculating minMemoryOomScoreAdjust for sidecar containers with PodLevelResources enabled,
94
+ // we add the per-container share of unallocated pod memory requests to the minimum memory request.
95
+ // This ensures the OOM score adjustment i.e. minMemoryOomScoreAdjust
96
+ // calculation remains consistent
97
+ // with how we handle pod-level memory requests for regular containers.
98
+ if utilfeature .DefaultFeatureGate .Enabled (features .PodLevelResources ) &&
99
+ resourcehelper .IsPodLevelRequestsSet (pod ) {
100
+ minMemoryRequest += remainingReqPerContainer
101
+ }
74
102
minMemoryOomScoreAdjust := 1000 - (1000 * minMemoryRequest )/ memoryCapacity
75
103
// the OOM adjustment for sidecar container will match
76
104
// or fall below the OOM score adjustment of regular containers in the Pod.
0 commit comments