From a800b15d0429296e3c235f2961e8f71f8fd60ec1 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Tue, 12 Jul 2022 09:53:11 +0200 Subject: [PATCH] mount-utils: only detect systemd when needed The detectSystemd() function adds quite some logging when systemd is not detected. This is not critical when the k8s.io/mount-utils package is used by external applications or CSI-drivers. In that case, it may not be required to detect systemd at all. To reduce logging in cases where systemd does not need to be detected, add a hasSystemd() helper function that calls detectSystemd() only once. --- staging/src/k8s.io/mount-utils/mount_linux.go | 16 +++++++++++++--- .../src/k8s.io/mount-utils/mount_linux_test.go | 8 ++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/staging/src/k8s.io/mount-utils/mount_linux.go b/staging/src/k8s.io/mount-utils/mount_linux.go index 8718b6d4ee9..459e53d83a7 100644 --- a/staging/src/k8s.io/mount-utils/mount_linux.go +++ b/staging/src/k8s.io/mount-utils/mount_linux.go @@ -58,7 +58,7 @@ const ( // kubelet is running in the host's root mount namespace. type Mounter struct { mounterPath string - withSystemd bool + withSystemd *bool withSafeNotMountedBehavior bool } @@ -70,11 +70,21 @@ var _ MounterForceUnmounter = &Mounter{} func New(mounterPath string) Interface { return &Mounter{ mounterPath: mounterPath, - withSystemd: detectSystemd(), withSafeNotMountedBehavior: detectSafeNotMountedBehavior(), } } +// hasSystemd validates that the withSystemd bool is set, if it is not, +// detectSystemd will be called once for this Mounter instance. +func (mounter *Mounter) hasSystemd() bool { + if mounter.withSystemd == nil { + withSystemd := detectSystemd() + mounter.withSystemd = &withSystemd + } + + return *mounter.withSystemd +} + // Mount mounts source to target as fstype with given options. 'source' and 'fstype' must // be an empty string in case it's not required, e.g. for remount, or for auto filesystem // type, where kernel handles fstype for you. The mount 'options' is a list of options, @@ -154,7 +164,7 @@ func (mounter *Mounter) doMount(mounterPath string, mountCmd string, source stri mountCmd = mounterPath } - if mounter.withSystemd && systemdMountRequired { + if mounter.hasSystemd() && systemdMountRequired { // Try to run mount via systemd-run --scope. This will escape the // service where kubelet runs and any fuse daemons will be started in a // specific scope. kubelet service than can be restarted without killing diff --git a/staging/src/k8s.io/mount-utils/mount_linux_test.go b/staging/src/k8s.io/mount-utils/mount_linux_test.go index dc5939041ab..8083656aa47 100644 --- a/staging/src/k8s.io/mount-utils/mount_linux_test.go +++ b/staging/src/k8s.io/mount-utils/mount_linux_test.go @@ -525,6 +525,14 @@ func TestSensitiveMountOptions(t *testing.T) { } } +func TestHasSystemd(t *testing.T) { + mounter := &Mounter{} + _ = mounter.hasSystemd() + if mounter.withSystemd == nil { + t.Error("Failed to run detectSystemd()") + } +} + func mountArgsContainString(t *testing.T, mountArgs []string, wanted string) bool { for _, mountArg := range mountArgs { if mountArg == wanted {