Skip to content

Commit

Permalink
Use kubelet owned directories for mounting rather than /tmp
Browse files Browse the repository at this point in the history
Signed-off-by: Itamar Holder <iholder@redhat.com>
  • Loading branch information
iholder101 committed May 21, 2024
1 parent 74f2988 commit a6b971f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/kubelet/cm/container_manager_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func NewContainerManager(mountUtil mount.Interface, cadvisorInterface cadvisor.I
return nil, fmt.Errorf("running with swap on is not supported, please disable swap or set --fail-swap-on flag to false")
}

if !swap.IsTmpfsNoswapOptionSupported(mountUtil) {
if !swap.IsTmpfsNoswapOptionSupported(mountUtil, nodeConfig.KubeletRootDir) {
nodeRef := nodeRefFromNode(string(nodeConfig.NodeName))
recorder.Event(nodeRef, v1.EventTypeWarning, events.PossibleMemoryBackedVolumesOnDisk,
"The tmpfs noswap option is not supported. Memory-backed volumes (e.g. secrets, emptyDirs, etc.) "+
Expand Down
21 changes: 13 additions & 8 deletions pkg/kubelet/util/swap/swap_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package swap

import (
"bytes"
"errors"
"os"
sysruntime "runtime"
"strings"
Expand All @@ -39,7 +40,7 @@ var (

const TmpfsNoswapOption = "noswap"

func IsTmpfsNoswapOptionSupported(mounter mount.Interface) bool {
func IsTmpfsNoswapOptionSupported(mounter mount.Interface, mountPath string) bool {
isTmpfsNoswapOptionSupportedHelper := func() bool {
if sysruntime.GOOS == "windows" {
return false
Expand All @@ -55,28 +56,32 @@ func IsTmpfsNoswapOptionSupported(mounter mount.Interface) bool {
return true
}

mountDir, err := os.MkdirTemp("", "tmpfs-noswap-test-")
if mountPath == "" {
klog.ErrorS(errors.New("mount path is empty, falling back to /tmp"), "")
}

mountPath, err = os.MkdirTemp(mountPath, "tmpfs-noswap-test-")
if err != nil {
klog.InfoS("error creating dir to test if tmpfs noswap is enabled. Assuming not supported", "mount path", mountDir, "error", err)
klog.InfoS("error creating dir to test if tmpfs noswap is enabled. Assuming not supported", "mount path", mountPath, "error", err)
return false
}

defer func() {
err = os.RemoveAll(mountDir)
err = os.RemoveAll(mountPath)
if err != nil {
klog.ErrorS(err, "error removing test tmpfs dir", "mount path", mountDir)
klog.ErrorS(err, "error removing test tmpfs dir", "mount path", mountPath)
}
}()

err = mounter.MountSensitiveWithoutSystemd("tmpfs", mountDir, "tmpfs", []string{TmpfsNoswapOption}, nil)
err = mounter.MountSensitiveWithoutSystemd("tmpfs", mountPath, "tmpfs", []string{TmpfsNoswapOption}, nil)
if err != nil {
klog.InfoS("error mounting tmpfs with the noswap option. Assuming not supported", "error", err)
return false
}

err = mounter.Unmount(mountDir)
err = mounter.Unmount(mountPath)
if err != nil {
klog.ErrorS(err, "error unmounting test tmpfs dir", "mount path", mountDir)
klog.ErrorS(err, "error unmounting test tmpfs dir", "mount path", mountPath)
}

return true
Expand Down
2 changes: 1 addition & 1 deletion pkg/volume/emptydir/empty_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func (ed *emptyDir) setupTmpfs(dir string) error {
return nil
}

options := ed.generateTmpfsMountOptions(swap.IsTmpfsNoswapOptionSupported(ed.mounter))
options := ed.generateTmpfsMountOptions(swap.IsTmpfsNoswapOptionSupported(ed.mounter, ed.plugin.host.GetPluginDir(emptyDirPluginName)))

klog.V(3).Infof("pod %v: mounting tmpfs for volume %v", ed.pod.UID, ed.volName)
return ed.mounter.MountSensitiveWithoutSystemd("tmpfs", dir, "tmpfs", options, nil)
Expand Down

0 comments on commit a6b971f

Please sign in to comment.