From c9e13c613a8b4c3d4871057ad4507c6c5e37daf5 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Mon, 29 Mar 2021 12:03:55 +0200 Subject: [PATCH] common: move implementation of GetSpec to private function Signed-off-by: Giuseppe Scrivano (cherry picked from commit 5bffc556cdc2be9fbbb521fda6c302a86ca20c1d) Signed-off-by: Kir Kolyshkin --- container/common/helpers.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/container/common/helpers.go b/container/common/helpers.go index 5c686e12fd..19241e7937 100644 --- a/container/common/helpers.go +++ b/container/common/helpers.go @@ -61,6 +61,10 @@ var bootTime = func() time.Time { }() func GetSpec(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoFactory, hasNetwork, hasFilesystem bool) (info.ContainerSpec, error) { + return getSpecInternal(cgroupPaths, machineInfoFactory, hasNetwork, hasFilesystem, cgroups.IsCgroup2UnifiedMode()) +} + +func getSpecInternal(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoFactory, hasNetwork, hasFilesystem, cgroup2UnifiedMode bool) (info.ContainerSpec, error) { var spec info.ContainerSpec // Assume unified hierarchy containers. @@ -77,7 +81,7 @@ func GetSpec(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoF // Use clone_children/events as a workaround as it isn't usually modified. It is only likely changed // immediately after creating a container. If the directory modified time is lower, we use that. cgroupPathFile := path.Join(cgroupPathDir, "cgroup.clone_children") - if cgroups.IsCgroup2UnifiedMode() { + if cgroup2UnifiedMode { cgroupPathFile = path.Join(cgroupPathDir, "cgroup.events") } fi, err := os.Stat(cgroupPathFile) @@ -103,7 +107,7 @@ func GetSpec(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoF cpuRoot, ok := cgroupPaths["cpu"] if ok { if utils.FileExists(cpuRoot) { - if cgroups.IsCgroup2UnifiedMode() { + if cgroup2UnifiedMode { spec.HasCpu = true weight := readUInt64(cpuRoot, "cpu.weight") @@ -152,7 +156,7 @@ func GetSpec(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoF if utils.FileExists(cpusetRoot) { spec.HasCpu = true mask := "" - if cgroups.IsCgroup2UnifiedMode() { + if cgroup2UnifiedMode { mask = readString(cpusetRoot, "cpuset.cpus.effective") } else { mask = readString(cpusetRoot, "cpuset.cpus") @@ -164,20 +168,20 @@ func GetSpec(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoF // Memory memoryRoot, ok := cgroupPaths["memory"] if ok { - if !cgroups.IsCgroup2UnifiedMode() { - if utils.FileExists(memoryRoot) { - spec.HasMemory = true - spec.Memory.Limit = readUInt64(memoryRoot, "memory.limit_in_bytes") - spec.Memory.SwapLimit = readUInt64(memoryRoot, "memory.memsw.limit_in_bytes") - spec.Memory.Reservation = readUInt64(memoryRoot, "memory.soft_limit_in_bytes") - } - } else { + if cgroup2UnifiedMode { if utils.FileExists(path.Join(memoryRoot, "memory.max")) { spec.HasMemory = true spec.Memory.Reservation = readUInt64(memoryRoot, "memory.high") spec.Memory.Limit = readUInt64(memoryRoot, "memory.max") spec.Memory.SwapLimit = readUInt64(memoryRoot, "memory.swap.max") } + } else { + if utils.FileExists(memoryRoot) { + spec.HasMemory = true + spec.Memory.Limit = readUInt64(memoryRoot, "memory.limit_in_bytes") + spec.Memory.SwapLimit = readUInt64(memoryRoot, "memory.memsw.limit_in_bytes") + spec.Memory.Reservation = readUInt64(memoryRoot, "memory.soft_limit_in_bytes") + } } } @@ -202,7 +206,7 @@ func GetSpec(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoF spec.HasFilesystem = hasFilesystem ioControllerName := "blkio" - if cgroups.IsCgroup2UnifiedMode() { + if cgroup2UnifiedMode { ioControllerName = "io" } if blkioRoot, ok := cgroupPaths[ioControllerName]; ok && utils.FileExists(blkioRoot) {