@@ -338,42 +338,35 @@ var isCgroup2UnifiedMode = func() bool {
338
338
return libcontainercgroups .IsCgroup2UnifiedMode ()
339
339
}
340
340
341
- var (
342
- swapControllerAvailability bool
343
- swapControllerAvailabilityOnce sync.Once
344
- )
345
-
346
341
// Note: this function variable is being added here so it would be possible to mock
347
342
// the swap controller availability for unit tests by assigning a new function to it. Without it,
348
343
// the swap controller availability would solely depend on the environment running the test.
349
- var swapControllerAvailable = func () bool {
344
+ var swapControllerAvailable = sync . OnceValue ( func () bool {
350
345
// See https://github.com/containerd/containerd/pull/7838/
351
- swapControllerAvailabilityOnce .Do (func () {
352
- const warn = "Failed to detect the availability of the swap controller, assuming not available"
353
- p := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
354
- if isCgroup2UnifiedMode () {
355
- // memory.swap.max does not exist in the cgroup root, so we check /sys/fs/cgroup/<SELF>/memory.swap.max
356
- cm , err := libcontainercgroups .ParseCgroupFile ("/proc/self/cgroup" )
357
- if err != nil {
358
- klog .V (5 ).ErrorS (fmt .Errorf ("failed to parse /proc/self/cgroup: %w" , err ), warn )
359
- return
360
- }
361
- // Fr cgroup v2 unified hierarchy, there are no per-controller
362
- // cgroup paths, so the cm map returned by ParseCgroupFile above
363
- // has a single element where the key is empty string ("") and
364
- // the value is the cgroup path the <pid> is in.
365
- p = filepath .Join ("/sys/fs/cgroup" , cm ["" ], "memory.swap.max" )
346
+ const warn = "Failed to detect the availability of the swap controller, assuming not available"
347
+ p := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
348
+ if isCgroup2UnifiedMode () {
349
+ // memory.swap.max does not exist in the cgroup root, so we check /sys/fs/cgroup/<SELF>/memory.swap.max
350
+ cm , err := libcontainercgroups .ParseCgroupFile ("/proc/self/cgroup" )
351
+ if err != nil {
352
+ klog .V (5 ).ErrorS (fmt .Errorf ("failed to parse /proc/self/cgroup: %w" , err ), warn )
353
+ return false
366
354
}
367
- if _ , err := os .Stat (p ); err != nil {
368
- if ! errors .Is (err , os .ErrNotExist ) {
369
- klog .V (5 ).ErrorS (err , warn )
370
- }
371
- return
355
+ // Fr cgroup v2 unified hierarchy, there are no per-controller
356
+ // cgroup paths, so the cm map returned by ParseCgroupFile above
357
+ // has a single element where the key is empty string ("") and
358
+ // the value is the cgroup path the <pid> is in.
359
+ p = filepath .Join ("/sys/fs/cgroup" , cm ["" ], "memory.swap.max" )
360
+ }
361
+ if _ , err := os .Stat (p ); err != nil {
362
+ if ! errors .Is (err , os .ErrNotExist ) {
363
+ klog .V (5 ).ErrorS (err , warn )
372
364
}
373
- swapControllerAvailability = true
374
- })
375
- return swapControllerAvailability
376
- }
365
+ return false
366
+ }
367
+
368
+ return true
369
+ })
377
370
378
371
type swapConfigurationHelper struct {
379
372
machineInfo cadvisorv1.MachineInfo
0 commit comments