Merge pull request #1374 from amshinde/k8s-empty-dir

volumes: Handle k8s empty-dirs of "default" medium type
This commit is contained in:
Peng Tao
2019-03-18 12:53:35 +08:00
committed by GitHub
6 changed files with 49 additions and 7 deletions

View File

@@ -16,6 +16,7 @@ import (
"path"
"path/filepath"
"strings"
"syscall"
"testing"
vc "github.com/kata-containers/runtime/virtcontainers"
@@ -177,12 +178,30 @@ func findLastParam(key string, params []vc.Param) (string, error) {
}
func TestSetEphemeralStorageType(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip(testDisabledNeedRoot)
}
assert := assert.New(t)
dir, err := ioutil.TempDir(testDir, "foo")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
ephePath := filepath.Join(dir, k8sEmptyDir, "tmp-volume")
err = os.MkdirAll(ephePath, testDirMode)
assert.Nil(err)
err = syscall.Mount("tmpfs", ephePath, "tmpfs", 0, "")
assert.Nil(err)
defer syscall.Unmount(ephePath, 0)
ociSpec := oci.CompatOCISpec{}
var ociMounts []specs.Mount
mount := specs.Mount{
Source: "/var/lib/kubelet/pods/366c3a77-4869-11e8-b479-507b9ddd5ce4/volumes/kubernetes.io~empty-dir/cache-volume",
Source: ephePath,
}
ociMounts = append(ociMounts, mount)

View File

@@ -14,6 +14,8 @@ import (
"path/filepath"
"strings"
"syscall"
vc "github.com/kata-containers/runtime/virtcontainers"
)
const (
@@ -43,7 +45,9 @@ func IsEphemeralStorage(path string) bool {
if len(splitSourceSlice) > 1 {
storageType := splitSourceSlice[len(splitSourceSlice)-2]
if storageType == k8sEmptyDir {
return true
if _, fsType, _ := vc.GetDevicePathAndFsType(path); fsType == "tmpfs" {
return true
}
}
}
return false

View File

@@ -368,7 +368,24 @@ func TestGetFileContents(t *testing.T) {
}
func TestIsEphemeralStorage(t *testing.T) {
sampleEphePath := "/var/lib/kubelet/pods/366c3a75-4869-11e8-b479-507b9ddd5ce4/volumes/kubernetes.io~empty-dir/cache-volume"
if os.Geteuid() != 0 {
t.Skip(testDisabledNeedRoot)
}
dir, err := ioutil.TempDir(testDir, "foo")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
sampleEphePath := filepath.Join(dir, k8sEmptyDir, "tmp-volume")
err = os.MkdirAll(sampleEphePath, testDirMode)
assert.Nil(t, err)
err = syscall.Mount("tmpfs", sampleEphePath, "tmpfs", 0, "")
assert.Nil(t, err)
defer syscall.Unmount(sampleEphePath, 0)
isEphe := IsEphemeralStorage(sampleEphePath)
if !isEphe {
t.Fatalf("Unable to correctly determine volume type")

View File

@@ -1138,7 +1138,7 @@ func (c *Container) hotplugDrive() error {
}
// If device mapper device, then fetch the full path of the device
devicePath, fsType, err := getDevicePathAndFsType(dev.mountPoint)
devicePath, fsType, err := GetDevicePathAndFsType(dev.mountPoint)
if err != nil {
return err
}

View File

@@ -164,7 +164,9 @@ const (
procTypeIndex
)
func getDevicePathAndFsType(mountPoint string) (devicePath, fsType string, err error) {
// GetDevicePathAndFsType gets the device for the mount point and the file system type
// of the mount.
func GetDevicePathAndFsType(mountPoint string) (devicePath, fsType string, err error) {
if mountPoint == "" {
err = fmt.Errorf("Mount point cannot be empty")
return

View File

@@ -238,7 +238,7 @@ func TestGetDeviceForPathBindMount(t *testing.T) {
}
func TestGetDevicePathAndFsTypeEmptyMount(t *testing.T) {
_, _, err := getDevicePathAndFsType("")
_, _, err := GetDevicePathAndFsType("")
if err == nil {
t.Fatal()
@@ -246,7 +246,7 @@ func TestGetDevicePathAndFsTypeEmptyMount(t *testing.T) {
}
func TestGetDevicePathAndFsTypeSuccessful(t *testing.T) {
path, fstype, err := getDevicePathAndFsType("/proc")
path, fstype, err := GetDevicePathAndFsType("/proc")
if err != nil {
t.Fatal(err)