From 6cf8a6606c0a5b4c017ba979a6bea0673b8ed861 Mon Sep 17 00:00:00 2001 From: Michelle Au Date: Thu, 19 Apr 2018 15:40:51 -0700 Subject: [PATCH] Only count mounts that are from other pods --- .../attachdetach/attach_detach_controller.go | 4 ++ .../volume/expand/expand_controller.go | 4 ++ .../volume/persistentvolume/volume_host.go | 4 ++ pkg/kubelet/volume_host.go | 4 ++ pkg/volume/local/local.go | 24 ++++++-- pkg/volume/local/local_test.go | 56 +++++++++++++++++++ pkg/volume/plugins.go | 4 ++ pkg/volume/testing/testing.go | 4 ++ 8 files changed, 99 insertions(+), 5 deletions(-) diff --git a/pkg/controller/volume/attachdetach/attach_detach_controller.go b/pkg/controller/volume/attachdetach/attach_detach_controller.go index 852979d81d1..c7397f57535 100644 --- a/pkg/controller/volume/attachdetach/attach_detach_controller.go +++ b/pkg/controller/volume/attachdetach/attach_detach_controller.go @@ -522,6 +522,10 @@ func (adc *attachDetachController) GetVolumeDevicePluginDir(podUID string) strin return "" } +func (adc *attachDetachController) GetPodsDir() string { + return "" +} + func (adc *attachDetachController) GetPodVolumeDir(podUID types.UID, pluginName, volumeName string) string { return "" } diff --git a/pkg/controller/volume/expand/expand_controller.go b/pkg/controller/volume/expand/expand_controller.go index 95e0b84a30d..caceb853ef1 100644 --- a/pkg/controller/volume/expand/expand_controller.go +++ b/pkg/controller/volume/expand/expand_controller.go @@ -227,6 +227,10 @@ func (expc *expandController) GetVolumeDevicePluginDir(pluginName string) string return "" } +func (expc *expandController) GetPodsDir() string { + return "" +} + func (expc *expandController) GetPodVolumeDir(podUID types.UID, pluginName string, volumeName string) string { return "" } diff --git a/pkg/controller/volume/persistentvolume/volume_host.go b/pkg/controller/volume/persistentvolume/volume_host.go index 13692b97ab5..14878c12e92 100644 --- a/pkg/controller/volume/persistentvolume/volume_host.go +++ b/pkg/controller/volume/persistentvolume/volume_host.go @@ -42,6 +42,10 @@ func (ctrl *PersistentVolumeController) GetVolumeDevicePluginDir(pluginName stri return "" } +func (ctrl *PersistentVolumeController) GetPodsDir() string { + return "" +} + func (ctrl *PersistentVolumeController) GetPodVolumeDir(podUID types.UID, pluginName string, volumeName string) string { return "" } diff --git a/pkg/kubelet/volume_host.go b/pkg/kubelet/volume_host.go index 4c3949e13e5..9336d7cde3b 100644 --- a/pkg/kubelet/volume_host.go +++ b/pkg/kubelet/volume_host.go @@ -93,6 +93,10 @@ func (kvh *kubeletVolumeHost) GetVolumeDevicePluginDir(pluginName string) string return kvh.kubelet.getVolumeDevicePluginDir(pluginName) } +func (kvh *kubeletVolumeHost) GetPodsDir() string { + return kvh.kubelet.getPodsDir() +} + func (kvh *kubeletVolumeHost) GetPodVolumeDir(podUID types.UID, pluginName string, volumeName string) string { dir := kvh.kubelet.getPodVolumeDir(podUID, pluginName, volumeName) if runtime.GOOS == "windows" { diff --git a/pkg/volume/local/local.go b/pkg/volume/local/local.go index f80089e976f..2482533f8dc 100644 --- a/pkg/volume/local/local.go +++ b/pkg/volume/local/local.go @@ -19,8 +19,9 @@ package local import ( "fmt" "os" - "path" + "path/filepath" "runtime" + "strings" "github.com/golang/glog" @@ -31,7 +32,7 @@ import ( "k8s.io/kubernetes/pkg/kubelet/events" "k8s.io/kubernetes/pkg/util/keymutex" "k8s.io/kubernetes/pkg/util/mount" - "k8s.io/kubernetes/pkg/util/strings" + stringsutil "k8s.io/kubernetes/pkg/util/strings" "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/util" "k8s.io/kubernetes/pkg/volume/validation" @@ -219,7 +220,7 @@ type localVolume struct { } func (l *localVolume) GetPath() string { - return l.plugin.host.GetPodVolumeDir(l.podUID, strings.EscapeQualifiedNameForDisk(localVolumePluginName), l.volName) + return l.plugin.host.GetPodVolumeDir(l.podUID, stringsutil.EscapeQualifiedNameForDisk(localVolumePluginName), l.volName) } type localVolumeMounter struct { @@ -280,6 +281,8 @@ func (m *localVolumeMounter) SetUpAt(dir string, fsGroup *int64) error { return err } + // Only count mounts from other pods + refs = m.filterPodMounts(refs) if len(refs) > 0 { fsGroupNew := int64(*fsGroup) fsGroupSame, fsGroupOld, err := volume.IsSameFSGroup(m.globalPath, fsGroupNew) @@ -344,6 +347,17 @@ func (m *localVolumeMounter) SetUpAt(dir string, fsGroup *int64) error { return nil } +// filterPodMounts only returns mount paths inside the kubelet pod directory +func (m *localVolumeMounter) filterPodMounts(refs []string) []string { + filtered := []string{} + for _, r := range refs { + if strings.HasPrefix(r, m.plugin.host.GetPodsDir()+string(os.PathSeparator)) { + filtered = append(filtered, r) + } + } + return filtered +} + type localVolumeUnmounter struct { *localVolume } @@ -392,7 +406,7 @@ func (u *localVolumeUnmapper) TearDownDevice(mapPath, devicePath string) error { // GetGlobalMapPath returns global map path and error. // path: plugins/kubernetes.io/kubernetes.io/local-volume/volumeDevices/{volumeName} func (lv *localVolume) GetGlobalMapPath(spec *volume.Spec) (string, error) { - return path.Join(lv.plugin.host.GetVolumeDevicePluginDir(strings.EscapeQualifiedNameForDisk(localVolumePluginName)), + return filepath.Join(lv.plugin.host.GetVolumeDevicePluginDir(stringsutil.EscapeQualifiedNameForDisk(localVolumePluginName)), lv.volName), nil } @@ -401,5 +415,5 @@ func (lv *localVolume) GetGlobalMapPath(spec *volume.Spec) (string, error) { // volName: local-pv-ff0d6d4 func (lv *localVolume) GetPodDeviceMapPath() (string, string) { return lv.plugin.host.GetPodVolumeDeviceDir(lv.podUID, - strings.EscapeQualifiedNameForDisk(localVolumePluginName)), lv.volName + stringsutil.EscapeQualifiedNameForDisk(localVolumePluginName)), lv.volName } diff --git a/pkg/volume/local/local_test.go b/pkg/volume/local/local_test.go index 58ab8623bf7..2c1763fb580 100644 --- a/pkg/volume/local/local_test.go +++ b/pkg/volume/local/local_test.go @@ -22,6 +22,8 @@ import ( "fmt" "os" "path" + "path/filepath" + "reflect" "runtime" "testing" @@ -447,3 +449,57 @@ func TestUnsupportedPlugins(t *testing.T) { t.Errorf("Provisionable plugin found, expected none") } } + +func TestFilterPodMounts(t *testing.T) { + tmpDir, plug := getPlugin(t) + defer os.RemoveAll(tmpDir) + + pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}} + mounter, err := plug.NewMounter(getTestVolume(false, tmpDir, false), pod, volume.VolumeOptions{}) + if err != nil { + t.Fatal(err) + } + lvMounter, ok := mounter.(*localVolumeMounter) + if !ok { + t.Fatal("mounter is not localVolumeMounter") + } + + host := volumetest.NewFakeVolumeHost(tmpDir, nil, nil) + podsDir := host.GetPodsDir() + + cases := map[string]struct { + input []string + expected []string + }{ + "empty": { + []string{}, + []string{}, + }, + "not-pod-mount": { + []string{"/mnt/outside"}, + []string{}, + }, + "pod-mount": { + []string{filepath.Join(podsDir, "pod-mount")}, + []string{filepath.Join(podsDir, "pod-mount")}, + }, + "not-directory-prefix": { + []string{podsDir + "pod-mount"}, + []string{}, + }, + "mix": { + []string{"/mnt/outside", + filepath.Join(podsDir, "pod-mount"), + "/another/outside", + filepath.Join(podsDir, "pod-mount2")}, + []string{filepath.Join(podsDir, "pod-mount"), + filepath.Join(podsDir, "pod-mount2")}, + }, + } + for name, test := range cases { + output := lvMounter.filterPodMounts(test.input) + if !reflect.DeepEqual(output, test.expected) { + t.Errorf("%v failed: output %+v doesn't equal expected %+v", name, output, test.expected) + } + } +} diff --git a/pkg/volume/plugins.go b/pkg/volume/plugins.go index 9d49c1f1a57..5ed32f4be92 100644 --- a/pkg/volume/plugins.go +++ b/pkg/volume/plugins.go @@ -249,6 +249,10 @@ type VolumeHost interface { // ex. plugins/kubernetes.io/{PluginName}/{DefaultKubeletVolumeDevicesDirName}/{volumePluginDependentPath}/ GetVolumeDevicePluginDir(pluginName string) string + // GetPodsDir returns the absolute path to a directory where all the pods + // information is stored + GetPodsDir() string + // GetPodVolumeDir returns the absolute path a directory which // represents the named volume under the named plugin for the given // pod. If the specified pod does not exist, the result of this call diff --git a/pkg/volume/testing/testing.go b/pkg/volume/testing/testing.go index 762827a1d16..1cfe6dddc1a 100644 --- a/pkg/volume/testing/testing.go +++ b/pkg/volume/testing/testing.go @@ -95,6 +95,10 @@ func (f *fakeVolumeHost) GetVolumeDevicePluginDir(pluginName string) string { return path.Join(f.rootDir, "plugins", pluginName, "volumeDevices") } +func (f *fakeVolumeHost) GetPodsDir() string { + return path.Join(f.rootDir, "pods") +} + func (f *fakeVolumeHost) GetPodVolumeDir(podUID types.UID, pluginName, volumeName string) string { return path.Join(f.rootDir, "pods", string(podUID), "volumes", pluginName, volumeName) }