From c718fc2ca77879e8aced6b80d834681a204b26e6 Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Fri, 27 Oct 2017 13:14:35 +0200 Subject: [PATCH] Don't cache exec and mounter in RBD volume plugin Volume plugin can get a different exec and mounter implementation with every call, it must not be cached. --- pkg/volume/rbd/attacher.go | 13 +++++++++---- pkg/volume/rbd/rbd.go | 12 ++++-------- pkg/volume/rbd/rbd_util.go | 3 ++- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/pkg/volume/rbd/attacher.go b/pkg/volume/rbd/attacher.go index 7d2fab2bbb9..ea752f0943b 100644 --- a/pkg/volume/rbd/attacher.go +++ b/pkg/volume/rbd/attacher.go @@ -27,6 +27,7 @@ import ( "k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/volume" volutil "k8s.io/kubernetes/pkg/volume/util" + "k8s.io/kubernetes/pkg/volume/util/volumehelper" ) // NewAttacher implements AttachableVolumePlugin.NewAttacher. @@ -38,6 +39,7 @@ func (plugin *rbdPlugin) newAttacherInternal(manager diskManager) (volume.Attach return &rbdAttacher{ plugin: plugin, manager: manager, + mounter: volumehelper.NewSafeFormatAndMountFromHost(plugin.GetPluginName(), plugin.host), }, nil } @@ -50,6 +52,7 @@ func (plugin *rbdPlugin) newDetacherInternal(manager diskManager) (volume.Detach return &rbdDetacher{ plugin: plugin, manager: manager, + mounter: volumehelper.NewSafeFormatAndMountFromHost(plugin.GetPluginName(), plugin.host), }, nil } @@ -62,6 +65,7 @@ func (plugin *rbdPlugin) GetDeviceMountRefs(deviceMountPath string) ([]string, e // rbdAttacher implements volume.Attacher interface. type rbdAttacher struct { plugin *rbdPlugin + mounter *mount.SafeFormatAndMount manager diskManager } @@ -124,7 +128,7 @@ func (attacher *rbdAttacher) GetDeviceMountPath(spec *volume.Spec) (string, erro // This method is idempotent, callers are responsible for retrying on failure. func (attacher *rbdAttacher) MountDevice(spec *volume.Spec, devicePath string, deviceMountPath string) error { glog.V(4).Infof("rbd: mouting device %s to %s", devicePath, deviceMountPath) - notMnt, err := attacher.plugin.mounter.IsLikelyNotMountPoint(deviceMountPath) + notMnt, err := attacher.mounter.IsLikelyNotMountPoint(deviceMountPath) if err != nil { if os.IsNotExist(err) { if err := os.MkdirAll(deviceMountPath, 0750); err != nil { @@ -151,7 +155,7 @@ func (attacher *rbdAttacher) MountDevice(spec *volume.Spec, devicePath string, d options = append(options, "ro") } mountOptions := volume.MountOptionFromSpec(spec, options...) - err = attacher.plugin.mounter.FormatAndMount(devicePath, deviceMountPath, fstype, mountOptions) + err = attacher.mounter.FormatAndMount(devicePath, deviceMountPath, fstype, mountOptions) if err != nil { os.Remove(deviceMountPath) return fmt.Errorf("rbd: failed to mount device %s at %s (fstype: %s), error %v", devicePath, deviceMountPath, fstype, err) @@ -164,6 +168,7 @@ func (attacher *rbdAttacher) MountDevice(spec *volume.Spec, devicePath string, d type rbdDetacher struct { plugin *rbdPlugin manager diskManager + mounter *mount.SafeFormatAndMount } var _ volume.Detacher = &rbdDetacher{} @@ -185,7 +190,7 @@ func (detacher *rbdDetacher) UnmountDevice(deviceMountPath string) error { glog.Warningf("Warning: Unmount skipped because path does not exist: %v", deviceMountPath) return nil } - devicePath, cnt, err := mount.GetDeviceNameFromMount(detacher.plugin.mounter, deviceMountPath) + devicePath, cnt, err := mount.GetDeviceNameFromMount(detacher.mounter, deviceMountPath) if err != nil { return err } @@ -195,7 +200,7 @@ func (detacher *rbdDetacher) UnmountDevice(deviceMountPath string) error { if cnt == 1 { // Unmount the device from the device mount point. glog.V(4).Infof("rbd: unmouting device mountpoint %s", deviceMountPath) - if err = detacher.plugin.mounter.Unmount(deviceMountPath); err != nil { + if err = detacher.mounter.Unmount(deviceMountPath); err != nil { return err } glog.V(3).Infof("rbd: successfully umount device mountpath %s", deviceMountPath) diff --git a/pkg/volume/rbd/rbd.go b/pkg/volume/rbd/rbd.go index b3cf958a349..c79653e750a 100644 --- a/pkg/volume/rbd/rbd.go +++ b/pkg/volume/rbd/rbd.go @@ -41,14 +41,12 @@ var ( // This is the primary entrypoint for volume plugins. func ProbeVolumePlugins() []volume.VolumePlugin { - return []volume.VolumePlugin{&rbdPlugin{nil, nil, nil}} + return []volume.VolumePlugin{&rbdPlugin{}} } // rbdPlugin implements Volume.VolumePlugin. type rbdPlugin struct { - host volume.VolumeHost - exec mount.Exec - mounter *mount.SafeFormatAndMount + host volume.VolumeHost } var _ volume.VolumePlugin = &rbdPlugin{} @@ -74,8 +72,6 @@ func getPath(uid types.UID, volName string, host volume.VolumeHost) string { func (plugin *rbdPlugin) Init(host volume.VolumeHost) error { plugin.host = host - plugin.exec = host.GetExec(plugin.GetPluginName()) - plugin.mounter = volumehelper.NewSafeFormatAndMountFromHost(plugin.GetPluginName(), plugin.host) return nil } @@ -505,8 +501,8 @@ func newRBD(podUID types.UID, volName string, image string, pool string, readOnl Pool: pool, ReadOnly: readOnly, plugin: plugin, - mounter: plugin.mounter, - exec: plugin.exec, + mounter: volumehelper.NewSafeFormatAndMountFromHost(plugin.GetPluginName(), plugin.host), + exec: plugin.host.GetExec(plugin.GetPluginName()), manager: manager, MetricsProvider: volume.NewMetricsStatFS(getPath(podUID, volName, plugin.host)), } diff --git a/pkg/volume/rbd/rbd_util.go b/pkg/volume/rbd/rbd_util.go index edf0efbb290..25964ded499 100644 --- a/pkg/volume/rbd/rbd_util.go +++ b/pkg/volume/rbd/rbd_util.go @@ -297,7 +297,8 @@ func (util *RBDUtil) DetachDisk(plugin *rbdPlugin, deviceMountPath string, devic var err error if len(device) > 0 { // rbd unmap - _, err = plugin.exec.Run("rbd", "unmap", device) + exec := plugin.host.GetExec(plugin.GetPluginName()) + _, err = exec.Run("rbd", "unmap", device) if err != nil { return rbdErrors(err, fmt.Errorf("rbd: failed to unmap device %s:Error: %v", device, err)) }