Merge pull request #111083 from nixpanic/mount-utils/no-systemd

mount-utils: only detect systemd when needed
This commit is contained in:
Kubernetes Prow Robot 2022-07-14 13:18:57 -07:00 committed by GitHub
commit a35c1f37b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -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

View File

@ -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 {