Skip to content

Commit 5bffc55

Browse files
committed
common: move implementation of GetSpec to private function
Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent 56ced55 commit 5bffc55

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

container/common/helpers.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ var bootTime = func() time.Time {
6161
}()
6262

6363
func GetSpec(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoFactory, hasNetwork, hasFilesystem bool) (info.ContainerSpec, error) {
64+
return getSpecInternal(cgroupPaths, machineInfoFactory, hasNetwork, hasFilesystem, cgroups.IsCgroup2UnifiedMode())
65+
}
66+
67+
func getSpecInternal(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoFactory, hasNetwork, hasFilesystem, cgroup2UnifiedMode bool) (info.ContainerSpec, error) {
6468
var spec info.ContainerSpec
6569

6670
// Assume unified hierarchy containers.
@@ -77,7 +81,7 @@ func GetSpec(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoF
7781
// Use clone_children/events as a workaround as it isn't usually modified. It is only likely changed
7882
// immediately after creating a container. If the directory modified time is lower, we use that.
7983
cgroupPathFile := path.Join(cgroupPathDir, "cgroup.clone_children")
80-
if cgroups.IsCgroup2UnifiedMode() {
84+
if cgroup2UnifiedMode {
8185
cgroupPathFile = path.Join(cgroupPathDir, "cgroup.events")
8286
}
8387
fi, err := os.Stat(cgroupPathFile)
@@ -103,7 +107,7 @@ func GetSpec(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoF
103107
cpuRoot, ok := cgroupPaths["cpu"]
104108
if ok {
105109
if utils.FileExists(cpuRoot) {
106-
if cgroups.IsCgroup2UnifiedMode() {
110+
if cgroup2UnifiedMode {
107111
spec.HasCpu = true
108112

109113
weight := readUInt64(cpuRoot, "cpu.weight")
@@ -152,7 +156,7 @@ func GetSpec(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoF
152156
if utils.FileExists(cpusetRoot) {
153157
spec.HasCpu = true
154158
mask := ""
155-
if cgroups.IsCgroup2UnifiedMode() {
159+
if cgroup2UnifiedMode {
156160
mask = readString(cpusetRoot, "cpuset.cpus.effective")
157161
} else {
158162
mask = readString(cpusetRoot, "cpuset.cpus")
@@ -164,20 +168,20 @@ func GetSpec(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoF
164168
// Memory
165169
memoryRoot, ok := cgroupPaths["memory"]
166170
if ok {
167-
if !cgroups.IsCgroup2UnifiedMode() {
168-
if utils.FileExists(memoryRoot) {
169-
spec.HasMemory = true
170-
spec.Memory.Limit = readUInt64(memoryRoot, "memory.limit_in_bytes")
171-
spec.Memory.SwapLimit = readUInt64(memoryRoot, "memory.memsw.limit_in_bytes")
172-
spec.Memory.Reservation = readUInt64(memoryRoot, "memory.soft_limit_in_bytes")
173-
}
174-
} else {
171+
if cgroup2UnifiedMode {
175172
if utils.FileExists(path.Join(memoryRoot, "memory.max")) {
176173
spec.HasMemory = true
177174
spec.Memory.Reservation = readUInt64(memoryRoot, "memory.high")
178175
spec.Memory.Limit = readUInt64(memoryRoot, "memory.max")
179176
spec.Memory.SwapLimit = readUInt64(memoryRoot, "memory.swap.max")
180177
}
178+
} else {
179+
if utils.FileExists(memoryRoot) {
180+
spec.HasMemory = true
181+
spec.Memory.Limit = readUInt64(memoryRoot, "memory.limit_in_bytes")
182+
spec.Memory.SwapLimit = readUInt64(memoryRoot, "memory.memsw.limit_in_bytes")
183+
spec.Memory.Reservation = readUInt64(memoryRoot, "memory.soft_limit_in_bytes")
184+
}
181185
}
182186
}
183187

@@ -202,7 +206,7 @@ func GetSpec(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoF
202206
spec.HasFilesystem = hasFilesystem
203207

204208
ioControllerName := "blkio"
205-
if cgroups.IsCgroup2UnifiedMode() {
209+
if cgroup2UnifiedMode {
206210
ioControllerName = "io"
207211
}
208212
if blkioRoot, ok := cgroupPaths[ioControllerName]; ok && utils.FileExists(blkioRoot) {

0 commit comments

Comments
 (0)