mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
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 <kolyshkin@gmail.com>
This commit is contained in:
parent
3a1b0f2864
commit
19477b7eed
@ -338,17 +338,11 @@ var isCgroup2UnifiedMode = func() bool {
|
|||||||
return libcontainercgroups.IsCgroup2UnifiedMode()
|
return libcontainercgroups.IsCgroup2UnifiedMode()
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
swapControllerAvailability bool
|
|
||||||
swapControllerAvailabilityOnce sync.Once
|
|
||||||
)
|
|
||||||
|
|
||||||
// Note: this function variable is being added here so it would be possible to mock
|
// 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 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.
|
// 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/
|
// 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"
|
const warn = "Failed to detect the availability of the swap controller, assuming not available"
|
||||||
p := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
|
p := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
|
||||||
if isCgroup2UnifiedMode() {
|
if isCgroup2UnifiedMode() {
|
||||||
@ -356,7 +350,7 @@ var swapControllerAvailable = func() bool {
|
|||||||
cm, err := libcontainercgroups.ParseCgroupFile("/proc/self/cgroup")
|
cm, err := libcontainercgroups.ParseCgroupFile("/proc/self/cgroup")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.V(5).ErrorS(fmt.Errorf("failed to parse /proc/self/cgroup: %w", err), warn)
|
klog.V(5).ErrorS(fmt.Errorf("failed to parse /proc/self/cgroup: %w", err), warn)
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
// Fr cgroup v2 unified hierarchy, there are no per-controller
|
// Fr cgroup v2 unified hierarchy, there are no per-controller
|
||||||
// cgroup paths, so the cm map returned by ParseCgroupFile above
|
// cgroup paths, so the cm map returned by ParseCgroupFile above
|
||||||
@ -368,12 +362,11 @@ var swapControllerAvailable = func() bool {
|
|||||||
if !errors.Is(err, os.ErrNotExist) {
|
if !errors.Is(err, os.ErrNotExist) {
|
||||||
klog.V(5).ErrorS(err, warn)
|
klog.V(5).ErrorS(err, warn)
|
||||||
}
|
}
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
swapControllerAvailability = true
|
|
||||||
})
|
return true
|
||||||
return swapControllerAvailability
|
})
|
||||||
}
|
|
||||||
|
|
||||||
type swapConfigurationHelper struct {
|
type swapConfigurationHelper struct {
|
||||||
machineInfo cadvisorv1.MachineInfo
|
machineInfo cadvisorv1.MachineInfo
|
||||||
|
Loading…
Reference in New Issue
Block a user