Skip to content

Commit

Permalink
kubelet/kuberuntime: use sync.OnceValue
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
kolyshkin committed Nov 7, 2024
1 parent 3a1b0f2 commit 19477b7
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions pkg/kubelet/kuberuntime/kuberuntime_container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/<SELF>/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 <pid> 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/<SELF>/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 <pid> 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
Expand Down

0 comments on commit 19477b7

Please sign in to comment.