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 {