
The rpc only reports one field, i.e. the cgroup driver, to kubelet. Containerd determines the effective cgroup driver by looking at all runtime handlers, starting from the default runtime handler (the rest in alphabetical order), and returning the cgroup driver setting of the first runtime handler that supports one. If no runtime handler supports cgroup driver (i.e. has a config option for it) containerd falls back to auto-detection, returning systemd if systemd is running and cgroupfs otherwise. This patch implements the CRI server side of Kubernetes KEP-4033: https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/4033-group-driver-detection-over-cri Signed-off-by: Markus Lehtonen <markus.lehtonen@intel.com>
88 lines
2.0 KiB
Go
88 lines
2.0 KiB
Go
package fs2
|
|
|
|
import (
|
|
"bufio"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
)
|
|
|
|
func isCpuSet(r *configs.Resources) bool {
|
|
return r.CpuWeight != 0 || r.CpuQuota != 0 || r.CpuPeriod != 0
|
|
}
|
|
|
|
func setCpu(dirPath string, r *configs.Resources) error {
|
|
if !isCpuSet(r) {
|
|
return nil
|
|
}
|
|
|
|
// NOTE: .CpuShares is not used here. Conversion is the caller's responsibility.
|
|
if r.CpuWeight != 0 {
|
|
if err := cgroups.WriteFile(dirPath, "cpu.weight", strconv.FormatUint(r.CpuWeight, 10)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if r.CpuQuota != 0 || r.CpuPeriod != 0 {
|
|
str := "max"
|
|
if r.CpuQuota > 0 {
|
|
str = strconv.FormatInt(r.CpuQuota, 10)
|
|
}
|
|
period := r.CpuPeriod
|
|
if period == 0 {
|
|
// This default value is documented in
|
|
// https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html
|
|
period = 100000
|
|
}
|
|
str += " " + strconv.FormatUint(period, 10)
|
|
if err := cgroups.WriteFile(dirPath, "cpu.max", str); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func statCpu(dirPath string, stats *cgroups.Stats) error {
|
|
const file = "cpu.stat"
|
|
f, err := cgroups.OpenFile(dirPath, file, os.O_RDONLY)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
sc := bufio.NewScanner(f)
|
|
for sc.Scan() {
|
|
t, v, err := fscommon.ParseKeyValue(sc.Text())
|
|
if err != nil {
|
|
return &parseError{Path: dirPath, File: file, Err: err}
|
|
}
|
|
switch t {
|
|
case "usage_usec":
|
|
stats.CpuStats.CpuUsage.TotalUsage = v * 1000
|
|
|
|
case "user_usec":
|
|
stats.CpuStats.CpuUsage.UsageInUsermode = v * 1000
|
|
|
|
case "system_usec":
|
|
stats.CpuStats.CpuUsage.UsageInKernelmode = v * 1000
|
|
|
|
case "nr_periods":
|
|
stats.CpuStats.ThrottlingData.Periods = v
|
|
|
|
case "nr_throttled":
|
|
stats.CpuStats.ThrottlingData.ThrottledPeriods = v
|
|
|
|
case "throttled_usec":
|
|
stats.CpuStats.ThrottlingData.ThrottledTime = v * 1000
|
|
}
|
|
}
|
|
if err := sc.Err(); err != nil {
|
|
return &parseError{Path: dirPath, File: file, Err: err}
|
|
}
|
|
return nil
|
|
}
|