@@ -18,7 +18,9 @@ package qos
18
18
19
19
import (
20
20
v1 "k8s.io/api/core/v1"
21
+ utilfeature "k8s.io/apiserver/pkg/util/feature"
21
22
v1qos "k8s.io/kubernetes/pkg/apis/core/v1/helper/qos"
23
+ "k8s.io/kubernetes/pkg/features"
22
24
"k8s.io/kubernetes/pkg/kubelet/types"
23
25
)
24
26
@@ -63,6 +65,20 @@ func GetContainerOOMScoreAdjust(pod *v1.Pod, container *v1.Container, memoryCapa
63
65
// Note that this is a heuristic, it won't work if a container has many small processes.
64
66
memoryRequest := container .Resources .Requests .Memory ().Value ()
65
67
oomScoreAdjust := 1000 - (1000 * memoryRequest )/ memoryCapacity
68
+
69
+ // adapt the sidecarContainer memoryRequest for OOM ADJ calculation
70
+ // calculate the oom score adjustment based on: max-memory( currentSideCarContainer , min-memory(regular containers) ) .
71
+ if utilfeature .DefaultFeatureGate .Enabled (features .SidecarContainers ) && isSidecarContainer (pod , container ) {
72
+ // check min memory quantity in regular containers
73
+ minMemoryRequest := minRegularContainerMemory (* pod )
74
+ minMemoryOomScoreAdjust := 1000 - (1000 * minMemoryRequest )/ memoryCapacity
75
+ // the OOM adjustment for sidecar container will match
76
+ // or fall below the OOM score adjustment of regular containers in the Pod.
77
+ if oomScoreAdjust > minMemoryOomScoreAdjust {
78
+ oomScoreAdjust = minMemoryOomScoreAdjust
79
+ }
80
+ }
81
+
66
82
// A guaranteed pod using 100% of memory can have an OOM score of 10. Ensure
67
83
// that burstable pods have a higher OOM score adjustment.
68
84
if int (oomScoreAdjust ) < (1000 + guaranteedOOMScoreAdj ) {
@@ -74,3 +90,18 @@ func GetContainerOOMScoreAdjust(pod *v1.Pod, container *v1.Container, memoryCapa
74
90
}
75
91
return int (oomScoreAdjust )
76
92
}
93
+
94
+ // isSidecarContainer returns a boolean indicating whether a container is a sidecar or not.
95
+ // Since v1.Container does not directly specify whether a container is a sidecar,
96
+ // this function uses available indicators (container.RestartPolicy == v1.ContainerRestartPolicyAlways)
97
+ // to make that determination.
98
+ func isSidecarContainer (pod * v1.Pod , container * v1.Container ) bool {
99
+ if container .RestartPolicy != nil && * container .RestartPolicy == v1 .ContainerRestartPolicyAlways {
100
+ for _ , initContainer := range pod .Spec .InitContainers {
101
+ if initContainer .Name == container .Name {
102
+ return true
103
+ }
104
+ }
105
+ }
106
+ return false
107
+ }
0 commit comments