Skip to content

Commit

Permalink
feature: support memory qos for cgroup v2
Browse files Browse the repository at this point in the history
Map container.requests.memory to memory.min on cgroup v2.
See kubernetes/enhancements#2571
  • Loading branch information
payall4u authored and borgerli committed Jun 1, 2021
1 parent f31bfd8 commit 75f7521
Show file tree
Hide file tree
Showing 4 changed files with 476 additions and 286 deletions.
2 changes: 2 additions & 0 deletions pkg/kubelet/cm/cgroup_manager_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const (
libcontainerSystemd libcontainerCgroupManagerType = "systemd"
// systemdSuffix is the cgroup name suffix for systemd
systemdSuffix string = ".slice"
// MemoryMin is memory.min for cgroup v2
MemoryMin string = "memory.min"
)

var RootCgroupName = CgroupName([]string{})
Expand Down
9 changes: 9 additions & 0 deletions pkg/kubelet/kuberuntime/kuberuntime_container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
package kuberuntime

import (
"strconv"
"time"

cgroupfs "github.com/opencontainers/runc/libcontainer/cgroups/fs"
Expand All @@ -28,6 +29,7 @@ import (
"k8s.io/klog/v2"
v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
kubefeatures "k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/kubelet/cm"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/kubelet/qos"
)
Expand Down Expand Up @@ -55,6 +57,7 @@ func (m *kubeGenericRuntimeManager) generateLinuxContainerConfig(container *v1.C
cpuRequest := container.Resources.Requests.Cpu()
cpuLimit := container.Resources.Limits.Cpu()
memoryLimit := container.Resources.Limits.Memory().Value()
memoryRequest := container.Resources.Requests.Memory().Value()
oomScoreAdj := int64(qos.GetContainerOOMScoreAdjust(pod, container,
int64(m.machineInfo.MemoryCapacity)))
// If request is not specified, but limit is, we want request to default to limit.
Expand All @@ -71,6 +74,12 @@ func (m *kubeGenericRuntimeManager) generateLinuxContainerConfig(container *v1.C
if memoryLimit != 0 {
lc.Resources.MemoryLimitInBytes = memoryLimit
}
if memoryRequest != 0 {
if lc.Resources.Unified == nil {
lc.Resources.Unified = make(map[string]string)
}
lc.Resources.Unified[cm.MemoryMin] = strconv.FormatInt(memoryRequest, 10)
}
// Set OOM score of the container based on qos policy. Processes in lower-priority pods should
// be killed first if the system runs out of memory.
lc.Resources.OomScoreAdj = oomScoreAdj
Expand Down
Loading

0 comments on commit 75f7521

Please sign in to comment.