From 0e547bae225b8d019d31054c4cf66670abf9ec17 Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Mon, 14 Aug 2017 12:16:27 +0200 Subject: [PATCH] SafeFormatAndMount should use volume.Exec provided by VolumeHost We need to execute mkfs / fsck where the utilities are. --- pkg/util/mount/BUILD | 5 +-- pkg/util/mount/mount.go | 3 +- pkg/util/mount/mount_linux.go | 9 ++-- pkg/util/mount/safe_format_and_mount_test.go | 47 ++++++++------------ pkg/volume/aws_ebs/BUILD | 1 - pkg/volume/aws_ebs/attacher.go | 4 +- pkg/volume/aws_ebs/aws_ebs.go | 3 +- pkg/volume/azure_dd/BUILD | 1 + pkg/volume/azure_dd/attacher.go | 3 +- pkg/volume/cinder/attacher.go | 4 +- pkg/volume/cinder/cinder.go | 3 +- pkg/volume/fc/BUILD | 1 + pkg/volume/fc/attacher.go | 5 ++- pkg/volume/fc/fc.go | 6 +-- pkg/volume/fc/fc_test.go | 6 ++- pkg/volume/flexvolume/attacher-defaults.go | 3 +- pkg/volume/flexvolume/mounter.go | 4 -- pkg/volume/flexvolume/plugin.go | 7 ++- pkg/volume/gce_pd/attacher.go | 4 +- pkg/volume/iscsi/iscsi.go | 10 ++--- pkg/volume/iscsi/iscsi_test.go | 5 ++- pkg/volume/photon_pd/BUILD | 1 - pkg/volume/photon_pd/attacher.go | 4 +- pkg/volume/photon_pd/photon_pd.go | 3 +- pkg/volume/portworx/BUILD | 1 - pkg/volume/portworx/portworx.go | 3 +- pkg/volume/rbd/rbd.go | 4 +- pkg/volume/scaleio/BUILD | 1 - pkg/volume/scaleio/sio_volume.go | 6 +-- pkg/volume/storageos/storageos.go | 3 +- pkg/volume/util/volumehelper/BUILD | 1 + pkg/volume/util/volumehelper/volumehelper.go | 9 ++++ pkg/volume/vsphere_volume/BUILD | 1 - pkg/volume/vsphere_volume/attacher.go | 4 +- pkg/volume/vsphere_volume/vsphere_volume.go | 3 +- 35 files changed, 78 insertions(+), 100 deletions(-) diff --git a/pkg/util/mount/BUILD b/pkg/util/mount/BUILD index 6087221857d..3bfcded823f 100644 --- a/pkg/util/mount/BUILD +++ b/pkg/util/mount/BUILD @@ -46,10 +46,7 @@ go_test( "//conditions:default": [], }), library = ":go_default_library", - deps = [ - "//vendor/k8s.io/utils/exec:go_default_library", - "//vendor/k8s.io/utils/exec/testing:go_default_library", - ], + deps = ["//vendor/k8s.io/utils/exec/testing:go_default_library"], ) filegroup( diff --git a/pkg/util/mount/mount.go b/pkg/util/mount/mount.go index 7552bbb0949..446e1f51f7f 100644 --- a/pkg/util/mount/mount.go +++ b/pkg/util/mount/mount.go @@ -25,7 +25,6 @@ import ( "strings" "github.com/golang/glog" - "k8s.io/utils/exec" ) const ( @@ -99,7 +98,7 @@ type MountPoint struct { // mounts it otherwise the device is formatted first then mounted. type SafeFormatAndMount struct { Interface - Runner exec.Interface + Exec } // FormatAndMount formats the given disk, if needed, and mounts it. diff --git a/pkg/util/mount/mount_linux.go b/pkg/util/mount/mount_linux.go index 65715db6cc2..88396708a4f 100644 --- a/pkg/util/mount/mount_linux.go +++ b/pkg/util/mount/mount_linux.go @@ -412,8 +412,7 @@ func (mounter *SafeFormatAndMount) formatAndMount(source string, target string, // Run fsck on the disk to fix repairable issues glog.V(4).Infof("Checking for issues with fsck on disk: %s", source) args := []string{"-a", source} - cmd := mounter.Runner.Command("fsck", args...) - out, err := cmd.CombinedOutput() + out, err := mounter.Exec.Run("fsck", args...) if err != nil { ee, isExitError := err.(utilexec.ExitError) switch { @@ -450,8 +449,7 @@ func (mounter *SafeFormatAndMount) formatAndMount(source string, target string, args = []string{"-F", source} } glog.Infof("Disk %q appears to be unformatted, attempting to format as type: %q with options: %v", source, fstype, args) - cmd := mounter.Runner.Command("mkfs."+fstype, args...) - _, err := cmd.CombinedOutput() + _, err := mounter.Exec.Run("mkfs."+fstype, args...) if err == nil { // the disk has been formatted successfully try to mount it again. glog.Infof("Disk successfully formatted (mkfs): %s - %s %s", fstype, source, target) @@ -476,9 +474,8 @@ func (mounter *SafeFormatAndMount) formatAndMount(source string, target string, // diskLooksUnformatted uses 'lsblk' to see if the given disk is unformated func (mounter *SafeFormatAndMount) getDiskFormat(disk string) (string, error) { args := []string{"-n", "-o", "FSTYPE", disk} - cmd := mounter.Runner.Command("lsblk", args...) glog.V(4).Infof("Attempting to determine if disk %q is formatted using lsblk with args: (%v)", disk, args) - dataOut, err := cmd.CombinedOutput() + dataOut, err := mounter.Exec.Run("lsblk", args...) output := string(dataOut) glog.V(4).Infof("Output: %q", output) diff --git a/pkg/util/mount/safe_format_and_mount_test.go b/pkg/util/mount/safe_format_and_mount_test.go index 3ea9d575b1c..72b768f3bf4 100644 --- a/pkg/util/mount/safe_format_and_mount_test.go +++ b/pkg/util/mount/safe_format_and_mount_test.go @@ -21,7 +21,6 @@ import ( "runtime" "testing" - "k8s.io/utils/exec" fakeexec "k8s.io/utils/exec/testing" ) @@ -181,40 +180,30 @@ func TestSafeFormatAndMount(t *testing.T) { } for _, test := range tests { - commandScripts := []fakeexec.FakeCommandAction{} - for _, expected := range test.execScripts { - ecmd := expected.command - eargs := expected.args - output := expected.output - err := expected.err - commandScript := func(cmd string, args ...string) exec.Cmd { - if cmd != ecmd { - t.Errorf("Unexpected command %s. Expecting %s", cmd, ecmd) - } - - for j := range args { - if args[j] != eargs[j] { - t.Errorf("Unexpected args %v. Expecting %v", args, eargs) - } - } - fake := fakeexec.FakeCmd{ - CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{ - func() ([]byte, error) { return []byte(output), err }, - }, - } - return fakeexec.InitFakeCmd(&fake, cmd, args...) + execCallCount := 0 + execCallback := func(cmd string, args ...string) ([]byte, error) { + if len(test.execScripts) <= execCallCount { + t.Errorf("Unexpected command: %s %v", cmd, args) + return nil, nil } - commandScripts = append(commandScripts, commandScript) - } - - fake := fakeexec.FakeExec{ - CommandScript: commandScripts, + script := test.execScripts[execCallCount] + execCallCount++ + if script.command != cmd { + t.Errorf("Unexpected command %s. Expecting %s", cmd, script.command) + } + for j := range args { + if args[j] != script.args[j] { + t.Errorf("Unexpected args %v. Expecting %v", args, script.args) + } + } + return []byte(script.output), script.err } fakeMounter := ErrorMounter{&FakeMounter{}, 0, test.mountErrs} + fakeExec := NewFakeExec(execCallback) mounter := SafeFormatAndMount{ Interface: &fakeMounter, - Runner: &fake, + Exec: fakeExec, } device := "/dev/foo" diff --git a/pkg/volume/aws_ebs/BUILD b/pkg/volume/aws_ebs/BUILD index 7b623b5f43a..a8d96f4b4ba 100644 --- a/pkg/volume/aws_ebs/BUILD +++ b/pkg/volume/aws_ebs/BUILD @@ -27,7 +27,6 @@ go_library( "//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/types:go_default_library", - "//vendor/k8s.io/utils/exec:go_default_library", ], ) diff --git a/pkg/volume/aws_ebs/attacher.go b/pkg/volume/aws_ebs/attacher.go index 1419864127f..2ffa14f4122 100644 --- a/pkg/volume/aws_ebs/attacher.go +++ b/pkg/volume/aws_ebs/attacher.go @@ -29,7 +29,7 @@ import ( "k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/volume" volumeutil "k8s.io/kubernetes/pkg/volume/util" - "k8s.io/utils/exec" + "k8s.io/kubernetes/pkg/volume/util/volumehelper" ) type awsElasticBlockStoreAttacher struct { @@ -222,7 +222,7 @@ func (attacher *awsElasticBlockStoreAttacher) MountDevice(spec *volume.Spec, dev options = append(options, "ro") } if notMnt { - diskMounter := &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()} + diskMounter := volumehelper.NewSafeFormatAndMountFromHost(awsElasticBlockStorePluginName, attacher.host) mountOptions := volume.MountOptionFromSpec(spec, options...) err = diskMounter.FormatAndMount(devicePath, deviceMountPath, volumeSource.FSType, mountOptions) if err != nil { diff --git a/pkg/volume/aws_ebs/aws_ebs.go b/pkg/volume/aws_ebs/aws_ebs.go index 433d9325be8..7a81f7605e7 100644 --- a/pkg/volume/aws_ebs/aws_ebs.go +++ b/pkg/volume/aws_ebs/aws_ebs.go @@ -35,7 +35,6 @@ import ( "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/util" "k8s.io/kubernetes/pkg/volume/util/volumehelper" - "k8s.io/utils/exec" ) // This is the primary entrypoint for volume plugins. @@ -135,7 +134,7 @@ func (plugin *awsElasticBlockStorePlugin) newMounterInternal(spec *volume.Spec, }, fsType: fsType, readOnly: readOnly, - diskMounter: &mount.SafeFormatAndMount{Interface: plugin.host.GetMounter(plugin.GetPluginName()), Runner: exec.New()}}, nil + diskMounter: volumehelper.NewSafeFormatAndMountFromHost(plugin.GetPluginName(), plugin.host)}, nil } func (plugin *awsElasticBlockStorePlugin) NewUnmounter(volName string, podUID types.UID) (volume.Unmounter, error) { diff --git a/pkg/volume/azure_dd/BUILD b/pkg/volume/azure_dd/BUILD index db2313fe564..04688265332 100644 --- a/pkg/volume/azure_dd/BUILD +++ b/pkg/volume/azure_dd/BUILD @@ -24,6 +24,7 @@ go_library( "//pkg/util/strings:go_default_library", "//pkg/volume:go_default_library", "//pkg/volume/util:go_default_library", + "//pkg/volume/util/volumehelper:go_default_library", "//vendor/github.com/Azure/azure-sdk-for-go/arm/compute:go_default_library", "//vendor/github.com/Azure/azure-sdk-for-go/arm/storage:go_default_library", "//vendor/github.com/golang/glog:go_default_library", diff --git a/pkg/volume/azure_dd/attacher.go b/pkg/volume/azure_dd/attacher.go index 43e300fd16c..9c6822d2a87 100644 --- a/pkg/volume/azure_dd/attacher.go +++ b/pkg/volume/azure_dd/attacher.go @@ -35,6 +35,7 @@ import ( "k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/volume" volumeutil "k8s.io/kubernetes/pkg/volume/util" + "k8s.io/kubernetes/pkg/volume/util/volumehelper" "k8s.io/utils/exec" ) @@ -232,7 +233,7 @@ func (attacher *azureDiskAttacher) MountDevice(spec *volume.Spec, devicePath str options := []string{} if notMnt { - diskMounter := &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()} + diskMounter := volumehelper.NewSafeFormatAndMountFromHost(azureDataDiskPluginName, attacher.plugin.host) mountOptions := volume.MountOptionFromSpec(spec, options...) err = diskMounter.FormatAndMount(devicePath, deviceMountPath, *volumeSource.FSType, mountOptions) if err != nil { diff --git a/pkg/volume/cinder/attacher.go b/pkg/volume/cinder/attacher.go index 024af41c73c..467e6c89420 100644 --- a/pkg/volume/cinder/attacher.go +++ b/pkg/volume/cinder/attacher.go @@ -29,7 +29,7 @@ import ( "k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/volume" volumeutil "k8s.io/kubernetes/pkg/volume/util" - "k8s.io/utils/exec" + "k8s.io/kubernetes/pkg/volume/util/volumehelper" ) type cinderDiskAttacher struct { @@ -285,7 +285,7 @@ func (attacher *cinderDiskAttacher) MountDevice(spec *volume.Spec, devicePath st options = append(options, "ro") } if notMnt { - diskMounter := &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()} + diskMounter := volumehelper.NewSafeFormatAndMountFromHost(cinderVolumePluginName, attacher.host) mountOptions := volume.MountOptionFromSpec(spec, options...) err = diskMounter.FormatAndMount(devicePath, deviceMountPath, volumeSource.FSType, mountOptions) if err != nil { diff --git a/pkg/volume/cinder/cinder.go b/pkg/volume/cinder/cinder.go index d045a7128b9..be6572dcd26 100644 --- a/pkg/volume/cinder/cinder.go +++ b/pkg/volume/cinder/cinder.go @@ -36,7 +36,6 @@ import ( "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/util" "k8s.io/kubernetes/pkg/volume/util/volumehelper" - "k8s.io/utils/exec" ) // This is the primary entrypoint for volume plugins. @@ -139,7 +138,7 @@ func (plugin *cinderPlugin) newMounterInternal(spec *volume.Spec, podUID types.U }, fsType: fsType, readOnly: readOnly, - blockDeviceMounter: &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()}}, nil + blockDeviceMounter: volumehelper.NewSafeFormatAndMountFromHost(plugin.GetPluginName(), plugin.host)}, nil } func (plugin *cinderPlugin) NewUnmounter(volName string, podUID types.UID) (volume.Unmounter, error) { diff --git a/pkg/volume/fc/BUILD b/pkg/volume/fc/BUILD index df6f13d053f..0fe87cbc121 100644 --- a/pkg/volume/fc/BUILD +++ b/pkg/volume/fc/BUILD @@ -20,6 +20,7 @@ go_library( "//pkg/util/strings:go_default_library", "//pkg/volume:go_default_library", "//pkg/volume/util:go_default_library", + "//pkg/volume/util/volumehelper:go_default_library", "//vendor/github.com/golang/glog:go_default_library", "//vendor/k8s.io/api/core/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/types:go_default_library", diff --git a/pkg/volume/fc/attacher.go b/pkg/volume/fc/attacher.go index 52f4a26d287..710716faa6c 100644 --- a/pkg/volume/fc/attacher.go +++ b/pkg/volume/fc/attacher.go @@ -28,6 +28,7 @@ import ( "k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/volume" volumeutil "k8s.io/kubernetes/pkg/volume/util" + "k8s.io/kubernetes/pkg/volume/util/volumehelper" "k8s.io/utils/exec" ) @@ -111,7 +112,7 @@ func (attacher *fcAttacher) MountDevice(spec *volume.Spec, devicePath string, de options = append(options, "ro") } if notMnt { - diskMounter := &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()} + diskMounter := &mount.SafeFormatAndMount{Interface: mounter, Exec: attacher.host.GetExec(fcPluginName)} mountOptions := volume.MountOptionFromSpec(spec, options...) err = diskMounter.FormatAndMount(devicePath, deviceMountPath, volumeSource.FSType, mountOptions) if err != nil { @@ -192,7 +193,7 @@ func volumeSpecToMounter(spec *volume.Spec, host volume.VolumeHost) (*fcDiskMoun }, fsType: fc.FSType, readOnly: readOnly, - mounter: &mount.SafeFormatAndMount{Interface: host.GetMounter(fcPluginName), Runner: exec.New()}, + mounter: volumehelper.NewSafeFormatAndMountFromHost(fcPluginName, host), }, nil } diff --git a/pkg/volume/fc/fc.go b/pkg/volume/fc/fc.go index 079187320eb..7d4392fa3ef 100644 --- a/pkg/volume/fc/fc.go +++ b/pkg/volume/fc/fc.go @@ -103,10 +103,10 @@ func (plugin *fcPlugin) GetAccessModes() []v1.PersistentVolumeAccessMode { func (plugin *fcPlugin) NewMounter(spec *volume.Spec, pod *v1.Pod, _ volume.VolumeOptions) (volume.Mounter, error) { // Inject real implementations here, test through the internal function. - return plugin.newMounterInternal(spec, pod.UID, &FCUtil{}, plugin.host.GetMounter(plugin.GetPluginName())) + return plugin.newMounterInternal(spec, pod.UID, &FCUtil{}, plugin.host.GetMounter(plugin.GetPluginName()), plugin.host.GetExec(plugin.GetPluginName())) } -func (plugin *fcPlugin) newMounterInternal(spec *volume.Spec, podUID types.UID, manager diskManager, mounter mount.Interface) (volume.Mounter, error) { +func (plugin *fcPlugin) newMounterInternal(spec *volume.Spec, podUID types.UID, manager diskManager, mounter mount.Interface, exec mount.Exec) (volume.Mounter, error) { // fc volumes used directly in a pod have a ReadOnly flag set by the pod author. // fc volumes used as a PersistentVolume gets the ReadOnly flag indirectly through the persistent-claim volume used to mount the PV fc, readOnly, err := getVolumeSource(spec) @@ -138,7 +138,7 @@ func (plugin *fcPlugin) newMounterInternal(spec *volume.Spec, podUID types.UID, plugin: plugin}, fsType: fc.FSType, readOnly: readOnly, - mounter: &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()}, + mounter: &mount.SafeFormatAndMount{Interface: mounter, Exec: exec}, }, nil } diff --git a/pkg/volume/fc/fc_test.go b/pkg/volume/fc/fc_test.go index e2662d2c909..c65ff800f95 100644 --- a/pkg/volume/fc/fc_test.go +++ b/pkg/volume/fc/fc_test.go @@ -141,7 +141,8 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { fakeManager := NewFakeDiskManager() defer fakeManager.Cleanup() fakeMounter := &mount.FakeMounter{} - mounter, err := plug.(*fcPlugin).newMounterInternal(spec, types.UID("poduid"), fakeManager, fakeMounter) + fakeExec := mount.NewFakeExec(nil) + mounter, err := plug.(*fcPlugin).newMounterInternal(spec, types.UID("poduid"), fakeManager, fakeMounter, fakeExec) if err != nil { t.Errorf("Failed to make a new Mounter: %v", err) } @@ -210,7 +211,8 @@ func doTestPluginNilMounter(t *testing.T, spec *volume.Spec) { fakeManager := NewFakeDiskManager() defer fakeManager.Cleanup() fakeMounter := &mount.FakeMounter{} - mounter, err := plug.(*fcPlugin).newMounterInternal(spec, types.UID("poduid"), fakeManager, fakeMounter) + fakeExec := mount.NewFakeExec(nil) + mounter, err := plug.(*fcPlugin).newMounterInternal(spec, types.UID("poduid"), fakeManager, fakeMounter, fakeExec) if err == nil { t.Errorf("Error failed to make a new Mounter is expected: %v", err) } diff --git a/pkg/volume/flexvolume/attacher-defaults.go b/pkg/volume/flexvolume/attacher-defaults.go index 3073f4def05..bf8dcfe8254 100644 --- a/pkg/volume/flexvolume/attacher-defaults.go +++ b/pkg/volume/flexvolume/attacher-defaults.go @@ -24,7 +24,6 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/volume" - "k8s.io/utils/exec" ) type attacherDefaults flexVolumeAttacher @@ -59,7 +58,7 @@ func (a *attacherDefaults) MountDevice(spec *volume.Spec, devicePath string, dev options = append(options, "rw") } - diskMounter := &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()} + diskMounter := &mount.SafeFormatAndMount{Interface: mounter, Exec: a.plugin.host.GetExec(a.plugin.GetPluginName())} return diskMounter.FormatAndMount(devicePath, deviceMountPath, volSource.FSType, options) } diff --git a/pkg/volume/flexvolume/mounter.go b/pkg/volume/flexvolume/mounter.go index 316928c429b..94b2ffd7488 100644 --- a/pkg/volume/flexvolume/mounter.go +++ b/pkg/volume/flexvolume/mounter.go @@ -19,7 +19,6 @@ package flexvolume import ( "strconv" - "k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/volume" "k8s.io/utils/exec" ) @@ -29,9 +28,6 @@ type flexVolumeMounter struct { *flexVolume // Runner used to setup the volume. runner exec.Interface - // blockDeviceMounter provides the interface to create filesystem if the - // filesystem doesn't exist. - blockDeviceMounter mount.Interface // the considered volume spec spec *volume.Spec readOnly bool diff --git a/pkg/volume/flexvolume/plugin.go b/pkg/volume/flexvolume/plugin.go index cd1de0a6be4..955a47c3c00 100644 --- a/pkg/volume/flexvolume/plugin.go +++ b/pkg/volume/flexvolume/plugin.go @@ -176,10 +176,9 @@ func (plugin *flexVolumePlugin) newMounterInternal(spec *volume.Spec, pod *api.P podServiceAccountName: pod.Spec.ServiceAccountName, volName: spec.Name(), }, - runner: runner, - spec: spec, - readOnly: readOnly, - blockDeviceMounter: &mount.SafeFormatAndMount{Interface: mounter, Runner: runner}, + runner: runner, + spec: spec, + readOnly: readOnly, }, nil } diff --git a/pkg/volume/gce_pd/attacher.go b/pkg/volume/gce_pd/attacher.go index 4801b80101c..e60f3c6c2f0 100644 --- a/pkg/volume/gce_pd/attacher.go +++ b/pkg/volume/gce_pd/attacher.go @@ -31,7 +31,7 @@ import ( "k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/volume" volumeutil "k8s.io/kubernetes/pkg/volume/util" - "k8s.io/utils/exec" + "k8s.io/kubernetes/pkg/volume/util/volumehelper" ) type gcePersistentDiskAttacher struct { @@ -208,7 +208,7 @@ func (attacher *gcePersistentDiskAttacher) MountDevice(spec *volume.Spec, device options = append(options, "ro") } if notMnt { - diskMounter := &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()} + diskMounter := volumehelper.NewSafeFormatAndMountFromHost(gcePersistentDiskPluginName, attacher.host) mountOptions := volume.MountOptionFromSpec(spec, options...) err = diskMounter.FormatAndMount(devicePath, deviceMountPath, volumeSource.FSType, mountOptions) if err != nil { diff --git a/pkg/volume/iscsi/iscsi.go b/pkg/volume/iscsi/iscsi.go index d21295ede64..d4b09f00f00 100644 --- a/pkg/volume/iscsi/iscsi.go +++ b/pkg/volume/iscsi/iscsi.go @@ -112,10 +112,10 @@ func (plugin *iscsiPlugin) NewMounter(spec *volume.Spec, pod *v1.Pod, _ volume.V } } - return plugin.newMounterInternal(spec, pod.UID, &ISCSIUtil{}, plugin.host.GetMounter(plugin.GetPluginName()), secret) + return plugin.newMounterInternal(spec, pod.UID, &ISCSIUtil{}, plugin.host.GetMounter(plugin.GetPluginName()), plugin.host.GetExec(plugin.GetPluginName()), secret) } -func (plugin *iscsiPlugin) newMounterInternal(spec *volume.Spec, podUID types.UID, manager diskManager, mounter mount.Interface, secret map[string]string) (volume.Mounter, error) { +func (plugin *iscsiPlugin) newMounterInternal(spec *volume.Spec, podUID types.UID, manager diskManager, mounter mount.Interface, exec mount.Exec, secret map[string]string) (volume.Mounter, error) { // iscsi volumes used directly in a pod have a ReadOnly flag set by the pod author. // iscsi volumes used as a PersistentVolume gets the ReadOnly flag indirectly through the persistent-claim volume used to mount the PV iscsi, readOnly, err := getVolumeSource(spec) @@ -147,7 +147,7 @@ func (plugin *iscsiPlugin) newMounterInternal(spec *volume.Spec, podUID types.UI plugin: plugin}, fsType: iscsi.FSType, readOnly: readOnly, - mounter: &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()}, + mounter: &mount.SafeFormatAndMount{Interface: mounter, Exec: exec}, deviceUtil: ioutil.NewDeviceHandler(ioutil.NewIOHandler()), mountOptions: volume.MountOptionFromSpec(spec), }, nil @@ -155,10 +155,10 @@ func (plugin *iscsiPlugin) newMounterInternal(spec *volume.Spec, podUID types.UI func (plugin *iscsiPlugin) NewUnmounter(volName string, podUID types.UID) (volume.Unmounter, error) { // Inject real implementations here, test through the internal function. - return plugin.newUnmounterInternal(volName, podUID, &ISCSIUtil{}, plugin.host.GetMounter(plugin.GetPluginName())) + return plugin.newUnmounterInternal(volName, podUID, &ISCSIUtil{}, plugin.host.GetMounter(plugin.GetPluginName()), plugin.host.GetExec(plugin.GetPluginName())) } -func (plugin *iscsiPlugin) newUnmounterInternal(volName string, podUID types.UID, manager diskManager, mounter mount.Interface) (volume.Unmounter, error) { +func (plugin *iscsiPlugin) newUnmounterInternal(volName string, podUID types.UID, manager diskManager, mounter mount.Interface, exec mount.Exec) (volume.Unmounter, error) { return &iscsiDiskUnmounter{ iscsiDisk: &iscsiDisk{ podUID: podUID, diff --git a/pkg/volume/iscsi/iscsi_test.go b/pkg/volume/iscsi/iscsi_test.go index 348b1994b3f..1c88d0bf7f7 100644 --- a/pkg/volume/iscsi/iscsi_test.go +++ b/pkg/volume/iscsi/iscsi_test.go @@ -141,7 +141,8 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { fakeManager := NewFakeDiskManager() defer fakeManager.Cleanup() fakeMounter := &mount.FakeMounter{} - mounter, err := plug.(*iscsiPlugin).newMounterInternal(spec, types.UID("poduid"), fakeManager, fakeMounter, nil) + fakeExec := mount.NewFakeExec(nil) + mounter, err := plug.(*iscsiPlugin).newMounterInternal(spec, types.UID("poduid"), fakeManager, fakeMounter, fakeExec, nil) if err != nil { t.Errorf("Failed to make a new Mounter: %v", err) } @@ -178,7 +179,7 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { fakeManager2 := NewFakeDiskManager() defer fakeManager2.Cleanup() - unmounter, err := plug.(*iscsiPlugin).newUnmounterInternal("vol1", types.UID("poduid"), fakeManager2, fakeMounter) + unmounter, err := plug.(*iscsiPlugin).newUnmounterInternal("vol1", types.UID("poduid"), fakeManager2, fakeMounter, fakeExec) if err != nil { t.Errorf("Failed to make a new Unmounter: %v", err) } diff --git a/pkg/volume/photon_pd/BUILD b/pkg/volume/photon_pd/BUILD index d35face84e1..a57d2b6cefd 100644 --- a/pkg/volume/photon_pd/BUILD +++ b/pkg/volume/photon_pd/BUILD @@ -26,7 +26,6 @@ go_library( "//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/types:go_default_library", - "//vendor/k8s.io/utils/exec:go_default_library", ], ) diff --git a/pkg/volume/photon_pd/attacher.go b/pkg/volume/photon_pd/attacher.go index 0c3e59297ab..8bdaba3c10f 100644 --- a/pkg/volume/photon_pd/attacher.go +++ b/pkg/volume/photon_pd/attacher.go @@ -30,7 +30,7 @@ import ( "k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/volume" volumeutil "k8s.io/kubernetes/pkg/volume/util" - "k8s.io/utils/exec" + "k8s.io/kubernetes/pkg/volume/util/volumehelper" ) type photonPersistentDiskAttacher struct { @@ -209,7 +209,7 @@ func (attacher *photonPersistentDiskAttacher) MountDevice(spec *volume.Spec, dev options := []string{} if notMnt { - diskMounter := &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()} + diskMounter := volumehelper.NewSafeFormatAndMountFromHost(photonPersistentDiskPluginName, attacher.host) mountOptions := volume.MountOptionFromSpec(spec) err = diskMounter.FormatAndMount(devicePath, deviceMountPath, volumeSource.FSType, mountOptions) if err != nil { diff --git a/pkg/volume/photon_pd/photon_pd.go b/pkg/volume/photon_pd/photon_pd.go index 2a6bac269fd..daa0470893e 100644 --- a/pkg/volume/photon_pd/photon_pd.go +++ b/pkg/volume/photon_pd/photon_pd.go @@ -31,7 +31,6 @@ import ( "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/util" "k8s.io/kubernetes/pkg/volume/util/volumehelper" - "k8s.io/utils/exec" ) // This is the primary entrypoint for volume plugins. @@ -116,7 +115,7 @@ func (plugin *photonPersistentDiskPlugin) newMounterInternal(spec *volume.Spec, plugin: plugin, }, fsType: fsType, - diskMounter: &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()}}, nil + diskMounter: volumehelper.NewSafeFormatAndMountFromHost(plugin.GetPluginName(), plugin.host)}, nil } func (plugin *photonPersistentDiskPlugin) newUnmounterInternal(volName string, podUID types.UID, manager pdManager, mounter mount.Interface) (volume.Unmounter, error) { diff --git a/pkg/volume/portworx/BUILD b/pkg/volume/portworx/BUILD index 5f7d8abe3ce..155eddc4ed3 100644 --- a/pkg/volume/portworx/BUILD +++ b/pkg/volume/portworx/BUILD @@ -43,7 +43,6 @@ go_library( "//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/types:go_default_library", - "//vendor/k8s.io/utils/exec:go_default_library", ], ) diff --git a/pkg/volume/portworx/portworx.go b/pkg/volume/portworx/portworx.go index bcac7ddcb6e..165e9206e24 100644 --- a/pkg/volume/portworx/portworx.go +++ b/pkg/volume/portworx/portworx.go @@ -29,7 +29,6 @@ import ( kstrings "k8s.io/kubernetes/pkg/util/strings" "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/util/volumehelper" - "k8s.io/utils/exec" ) // This is the primary entrypoint for volume plugins. @@ -115,7 +114,7 @@ func (plugin *portworxVolumePlugin) newMounterInternal(spec *volume.Spec, podUID }, fsType: fsType, readOnly: readOnly, - diskMounter: &mount.SafeFormatAndMount{Interface: plugin.host.GetMounter(plugin.GetPluginName()), Runner: exec.New()}}, nil + diskMounter: volumehelper.NewSafeFormatAndMountFromHost(plugin.GetPluginName(), plugin.host)}, nil } func (plugin *portworxVolumePlugin) NewUnmounter(volName string, podUID types.UID) (volume.Unmounter, error) { diff --git a/pkg/volume/rbd/rbd.go b/pkg/volume/rbd/rbd.go index 41cc28f8198..4b58a10bb27 100644 --- a/pkg/volume/rbd/rbd.go +++ b/pkg/volume/rbd/rbd.go @@ -158,7 +158,7 @@ func (plugin *rbdPlugin) newMounterInternal(spec *volume.Spec, podUID types.UID, Pool: pool, ReadOnly: readOnly, manager: manager, - mounter: &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()}, + mounter: volumehelper.NewSafeFormatAndMountFromHost(plugin.GetPluginName(), plugin.host), plugin: plugin, MetricsProvider: volume.NewMetricsStatFS(getPath(podUID, spec.Name(), plugin.host)), }, @@ -183,7 +183,7 @@ func (plugin *rbdPlugin) newUnmounterInternal(volName string, podUID types.UID, podUID: podUID, volName: volName, manager: manager, - mounter: &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()}, + mounter: volumehelper.NewSafeFormatAndMountFromHost(plugin.GetPluginName(), plugin.host), plugin: plugin, MetricsProvider: volume.NewMetricsStatFS(getPath(podUID, volName, plugin.host)), }, diff --git a/pkg/volume/scaleio/BUILD b/pkg/volume/scaleio/BUILD index cdd168fd922..6374c05205c 100644 --- a/pkg/volume/scaleio/BUILD +++ b/pkg/volume/scaleio/BUILD @@ -52,7 +52,6 @@ go_library( "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/types:go_default_library", "//vendor/k8s.io/apimachinery/pkg/util/uuid:go_default_library", - "//vendor/k8s.io/utils/exec:go_default_library", ], ) diff --git a/pkg/volume/scaleio/sio_volume.go b/pkg/volume/scaleio/sio_volume.go index 1897ea449e9..654d8fd58a6 100644 --- a/pkg/volume/scaleio/sio_volume.go +++ b/pkg/volume/scaleio/sio_volume.go @@ -34,7 +34,6 @@ import ( "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/util" "k8s.io/kubernetes/pkg/volume/util/volumehelper" - "k8s.io/utils/exec" ) type sioVolume struct { @@ -143,10 +142,7 @@ func (v *sioVolume) SetUpAt(dir string, fsGroup *int64) error { } glog.V(4).Info(log("setup created mount point directory %s", dir)) - diskMounter := &mount.SafeFormatAndMount{ - Interface: v.plugin.mounter, - Runner: exec.New(), - } + diskMounter := volumehelper.NewSafeFormatAndMountFromHost(v.plugin.GetPluginName(), v.plugin.host) err = diskMounter.FormatAndMount(devicePath, dir, v.fsType, options) if err != nil { diff --git a/pkg/volume/storageos/storageos.go b/pkg/volume/storageos/storageos.go index cc575f5a1f6..af76fb7fac9 100644 --- a/pkg/volume/storageos/storageos.go +++ b/pkg/volume/storageos/storageos.go @@ -36,7 +36,6 @@ import ( "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/util" "k8s.io/kubernetes/pkg/volume/util/volumehelper" - "k8s.io/utils/exec" ) // ProbeVolumePlugins is the primary entrypoint for volume plugins. @@ -137,7 +136,7 @@ func (plugin *storageosPlugin) newMounterInternal(spec *volume.Spec, pod *v1.Pod MetricsProvider: volume.NewMetricsStatFS(getPath(pod.UID, volNamespace, volName, spec.Name(), plugin.host)), }, devicePath: storageosDevicePath, - diskMounter: &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()}, + diskMounter: &mount.SafeFormatAndMount{Interface: mounter, Exec: plugin.host.GetExec(plugin.GetPluginName())}, }, nil } diff --git a/pkg/volume/util/volumehelper/BUILD b/pkg/volume/util/volumehelper/BUILD index 1880d21c058..849d3ee4e2c 100644 --- a/pkg/volume/util/volumehelper/BUILD +++ b/pkg/volume/util/volumehelper/BUILD @@ -9,6 +9,7 @@ go_library( name = "go_default_library", srcs = ["volumehelper.go"], deps = [ + "//pkg/util/mount:go_default_library", "//pkg/volume:go_default_library", "//pkg/volume/util/types:go_default_library", "//vendor/k8s.io/api/core/v1:go_default_library", diff --git a/pkg/volume/util/volumehelper/volumehelper.go b/pkg/volume/util/volumehelper/volumehelper.go index bcde93f71ed..b0734601d0a 100644 --- a/pkg/volume/util/volumehelper/volumehelper.go +++ b/pkg/volume/util/volumehelper/volumehelper.go @@ -23,6 +23,7 @@ import ( "strings" "k8s.io/api/core/v1" + "k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/util/types" ) @@ -127,3 +128,11 @@ func SplitUniqueName(uniqueName v1.UniqueVolumeName) (string, string, error) { pluginName := fmt.Sprintf("%s/%s", components[0], components[1]) return pluginName, components[2], nil } + +// NewSafeFormatAndMountFromHost creates a new SafeFormatAndMount with Mounter +// and Exec taken from given VolumeHost. +func NewSafeFormatAndMountFromHost(pluginName string, host volume.VolumeHost) *mount.SafeFormatAndMount { + mounter := host.GetMounter(pluginName) + exec := host.GetExec(pluginName) + return &mount.SafeFormatAndMount{Interface: mounter, Exec: exec} +} diff --git a/pkg/volume/vsphere_volume/BUILD b/pkg/volume/vsphere_volume/BUILD index e118362772b..9f5a9ac63a6 100644 --- a/pkg/volume/vsphere_volume/BUILD +++ b/pkg/volume/vsphere_volume/BUILD @@ -28,7 +28,6 @@ go_library( "//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/types:go_default_library", - "//vendor/k8s.io/utils/exec:go_default_library", ], ) diff --git a/pkg/volume/vsphere_volume/attacher.go b/pkg/volume/vsphere_volume/attacher.go index 2cf90ab883b..e87b07d50c5 100644 --- a/pkg/volume/vsphere_volume/attacher.go +++ b/pkg/volume/vsphere_volume/attacher.go @@ -29,7 +29,7 @@ import ( "k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/volume" volumeutil "k8s.io/kubernetes/pkg/volume/util" - "k8s.io/utils/exec" + "k8s.io/kubernetes/pkg/volume/util/volumehelper" ) type vsphereVMDKAttacher struct { @@ -195,7 +195,7 @@ func (attacher *vsphereVMDKAttacher) MountDevice(spec *volume.Spec, devicePath s options := []string{} if notMnt { - diskMounter := &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()} + diskMounter := volumehelper.NewSafeFormatAndMountFromHost(vsphereVolumePluginName, attacher.host) mountOptions := volume.MountOptionFromSpec(spec, options...) err = diskMounter.FormatAndMount(devicePath, deviceMountPath, volumeSource.FSType, mountOptions) if err != nil { diff --git a/pkg/volume/vsphere_volume/vsphere_volume.go b/pkg/volume/vsphere_volume/vsphere_volume.go index 36043df28f8..92322fa2ace 100644 --- a/pkg/volume/vsphere_volume/vsphere_volume.go +++ b/pkg/volume/vsphere_volume/vsphere_volume.go @@ -32,7 +32,6 @@ import ( "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/util" "k8s.io/kubernetes/pkg/volume/util/volumehelper" - "k8s.io/utils/exec" ) // This is the primary entrypoint for volume plugins. @@ -116,7 +115,7 @@ func (plugin *vsphereVolumePlugin) newMounterInternal(spec *volume.Spec, podUID plugin: plugin, }, fsType: fsType, - diskMounter: &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()}}, nil + diskMounter: volumehelper.NewSafeFormatAndMountFromHost(plugin.GetPluginName(), plugin.host)}, nil } func (plugin *vsphereVolumePlugin) newUnmounterInternal(volName string, podUID types.UID, manager vdManager, mounter mount.Interface) (volume.Unmounter, error) {