Skip to content

Commit 19477b7

Browse files
committed
kubelet/kuberuntime: use sync.OnceValue
This was added to Go 1.21, and makes the code simpler. (Best reviewed ignoring changes in amount of whitespace). Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 3a1b0f2 commit 19477b7

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

pkg/kubelet/kuberuntime/kuberuntime_container_linux.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -338,42 +338,35 @@ var isCgroup2UnifiedMode = func() bool {
338338
return libcontainercgroups.IsCgroup2UnifiedMode()
339339
}
340340

341-
var (
342-
swapControllerAvailability bool
343-
swapControllerAvailabilityOnce sync.Once
344-
)
345-
346341
// Note: this function variable is being added here so it would be possible to mock
347342
// the swap controller availability for unit tests by assigning a new function to it. Without it,
348343
// 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 {
350345
// 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
366354
}
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)
372364
}
373-
swapControllerAvailability = true
374-
})
375-
return swapControllerAvailability
376-
}
365+
return false
366+
}
367+
368+
return true
369+
})
377370

378371
type swapConfigurationHelper struct {
379372
machineInfo cadvisorv1.MachineInfo

0 commit comments

Comments
 (0)