From 19477b7eed249cb40fa9503838972d3865ef83e2 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 7 Nov 2024 13:09:09 -0800 Subject: [PATCH] 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 --- .../kuberuntime_container_linux.go | 53 ++++++++----------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/pkg/kubelet/kuberuntime/kuberuntime_container_linux.go b/pkg/kubelet/kuberuntime/kuberuntime_container_linux.go index cd972d678e4..06ec537233c 100644 --- a/pkg/kubelet/kuberuntime/kuberuntime_container_linux.go +++ b/pkg/kubelet/kuberuntime/kuberuntime_container_linux.go @@ -338,42 +338,35 @@ var isCgroup2UnifiedMode = func() bool { return libcontainercgroups.IsCgroup2UnifiedMode() } -var ( - swapControllerAvailability bool - swapControllerAvailabilityOnce sync.Once -) - // Note: this function variable is being added here so it would be possible to mock // the swap controller availability for unit tests by assigning a new function to it. Without it, // the swap controller availability would solely depend on the environment running the test. -var swapControllerAvailable = func() bool { +var swapControllerAvailable = sync.OnceValue(func() bool { // See https://github.com/containerd/containerd/pull/7838/ - swapControllerAvailabilityOnce.Do(func() { - const warn = "Failed to detect the availability of the swap controller, assuming not available" - p := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes" - if isCgroup2UnifiedMode() { - // memory.swap.max does not exist in the cgroup root, so we check /sys/fs/cgroup//memory.swap.max - cm, err := libcontainercgroups.ParseCgroupFile("/proc/self/cgroup") - if err != nil { - klog.V(5).ErrorS(fmt.Errorf("failed to parse /proc/self/cgroup: %w", err), warn) - return - } - // Fr cgroup v2 unified hierarchy, there are no per-controller - // cgroup paths, so the cm map returned by ParseCgroupFile above - // has a single element where the key is empty string ("") and - // the value is the cgroup path the is in. - p = filepath.Join("/sys/fs/cgroup", cm[""], "memory.swap.max") + const warn = "Failed to detect the availability of the swap controller, assuming not available" + p := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes" + if isCgroup2UnifiedMode() { + // memory.swap.max does not exist in the cgroup root, so we check /sys/fs/cgroup//memory.swap.max + cm, err := libcontainercgroups.ParseCgroupFile("/proc/self/cgroup") + if err != nil { + klog.V(5).ErrorS(fmt.Errorf("failed to parse /proc/self/cgroup: %w", err), warn) + return false } - if _, err := os.Stat(p); err != nil { - if !errors.Is(err, os.ErrNotExist) { - klog.V(5).ErrorS(err, warn) - } - return + // Fr cgroup v2 unified hierarchy, there are no per-controller + // cgroup paths, so the cm map returned by ParseCgroupFile above + // has a single element where the key is empty string ("") and + // the value is the cgroup path the is in. + p = filepath.Join("/sys/fs/cgroup", cm[""], "memory.swap.max") + } + if _, err := os.Stat(p); err != nil { + if !errors.Is(err, os.ErrNotExist) { + klog.V(5).ErrorS(err, warn) } - swapControllerAvailability = true - }) - return swapControllerAvailability -} + return false + } + + return true +}) type swapConfigurationHelper struct { machineInfo cadvisorv1.MachineInfo