From f38d4938bea48b0d04ff5f43778deaf3ad5a29f6 Mon Sep 17 00:00:00 2001 From: Travis Rhoden Date: Thu, 11 Apr 2019 12:19:55 -0600 Subject: [PATCH 1/2] Move ExecMount to pkg/volume/util/exec This patch moves the ExecMounter found in pkg/util/mount to pkg/volume/util/exec. This is done in preparation for pkg/util/mount to move out of tree. This specific implemention of mount.Interface is only used internally to K8s and does not need to move out of tree. --- pkg/kubelet/BUILD | 1 + pkg/kubelet/volume_host.go | 3 +- pkg/util/mount/BUILD | 3 - pkg/volume/util/BUILD | 1 + pkg/volume/util/exec/BUILD | 74 +++++++++++++++++++ .../mount => volume/util/exec}/exec_mount.go | 22 +++--- .../util/exec}/exec_mount_test.go | 18 +++-- .../util/exec}/exec_mount_unsupported.go | 16 ++-- 8 files changed, 109 insertions(+), 29 deletions(-) create mode 100644 pkg/volume/util/exec/BUILD rename pkg/{util/mount => volume/util/exec}/exec_mount.go (88%) rename pkg/{util/mount => volume/util/exec}/exec_mount_test.go (86%) rename pkg/{util/mount => volume/util/exec}/exec_mount_unsupported.go (83%) diff --git a/pkg/kubelet/BUILD b/pkg/kubelet/BUILD index 448aeb9f19e..182fbb40152 100644 --- a/pkg/kubelet/BUILD +++ b/pkg/kubelet/BUILD @@ -110,6 +110,7 @@ go_library( "//pkg/volume:go_default_library", "//pkg/volume/csi:go_default_library", "//pkg/volume/util:go_default_library", + "//pkg/volume/util/exec:go_default_library", "//pkg/volume/util/subpath:go_default_library", "//pkg/volume/util/types:go_default_library", "//pkg/volume/util/volumepathhandler:go_default_library", diff --git a/pkg/kubelet/volume_host.go b/pkg/kubelet/volume_host.go index 4b2e51844cf..6cf1a56e549 100644 --- a/pkg/kubelet/volume_host.go +++ b/pkg/kubelet/volume_host.go @@ -43,6 +43,7 @@ import ( "k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/util" + execmnt "k8s.io/kubernetes/pkg/volume/util/exec" "k8s.io/kubernetes/pkg/volume/util/subpath" ) @@ -230,7 +231,7 @@ func (kvh *kubeletVolumeHost) GetMounter(pluginName string) mount.Interface { if exec == nil { return kvh.kubelet.mounter } - return mount.NewExecMounter(exec, kvh.kubelet.mounter) + return execmnt.NewExecMounter(exec, kvh.kubelet.mounter) } func (kvh *kubeletVolumeHost) GetHostName() string { diff --git a/pkg/util/mount/BUILD b/pkg/util/mount/BUILD index e665748dbe8..f4e40b669f9 100644 --- a/pkg/util/mount/BUILD +++ b/pkg/util/mount/BUILD @@ -5,8 +5,6 @@ go_library( srcs = [ "doc.go", "exec.go", - "exec_mount.go", - "exec_mount_unsupported.go", "fake.go", "mount.go", "mount_helper_common.go", @@ -38,7 +36,6 @@ go_library( go_test( name = "go_default_test", srcs = [ - "exec_mount_test.go", "mount_helper_test.go", "mount_linux_test.go", "mount_test.go", diff --git a/pkg/volume/util/BUILD b/pkg/volume/util/BUILD index dac92b53ea1..d6200ddc6e3 100644 --- a/pkg/volume/util/BUILD +++ b/pkg/volume/util/BUILD @@ -84,6 +84,7 @@ filegroup( name = "all-srcs", srcs = [ ":package-srcs", + "//pkg/volume/util/exec:all-srcs", "//pkg/volume/util/fs:all-srcs", "//pkg/volume/util/nestedpendingoperations:all-srcs", "//pkg/volume/util/nsenter:all-srcs", diff --git a/pkg/volume/util/exec/BUILD b/pkg/volume/util/exec/BUILD new file mode 100644 index 00000000000..5f4fd655ac6 --- /dev/null +++ b/pkg/volume/util/exec/BUILD @@ -0,0 +1,74 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = [ + "exec_mount.go", + "exec_mount_unsupported.go", + ], + importpath = "k8s.io/kubernetes/pkg/volume/util/exec", + visibility = ["//visibility:public"], + deps = select({ + "@io_bazel_rules_go//go/platform:android": [ + "//pkg/util/mount:go_default_library", + ], + "@io_bazel_rules_go//go/platform:darwin": [ + "//pkg/util/mount:go_default_library", + ], + "@io_bazel_rules_go//go/platform:dragonfly": [ + "//pkg/util/mount:go_default_library", + ], + "@io_bazel_rules_go//go/platform:freebsd": [ + "//pkg/util/mount:go_default_library", + ], + "@io_bazel_rules_go//go/platform:linux": [ + "//pkg/util/mount:go_default_library", + "//vendor/k8s.io/klog:go_default_library", + ], + "@io_bazel_rules_go//go/platform:nacl": [ + "//pkg/util/mount:go_default_library", + ], + "@io_bazel_rules_go//go/platform:netbsd": [ + "//pkg/util/mount:go_default_library", + ], + "@io_bazel_rules_go//go/platform:openbsd": [ + "//pkg/util/mount:go_default_library", + ], + "@io_bazel_rules_go//go/platform:plan9": [ + "//pkg/util/mount:go_default_library", + ], + "@io_bazel_rules_go//go/platform:solaris": [ + "//pkg/util/mount:go_default_library", + ], + "@io_bazel_rules_go//go/platform:windows": [ + "//pkg/util/mount:go_default_library", + ], + "//conditions:default": [], + }), +) + +go_test( + name = "go_default_test", + srcs = ["exec_mount_test.go"], + embed = [":go_default_library"], + deps = select({ + "@io_bazel_rules_go//go/platform:linux": [ + "//pkg/util/mount:go_default_library", + ], + "//conditions:default": [], + }), +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/pkg/util/mount/exec_mount.go b/pkg/volume/util/exec/exec_mount.go similarity index 88% rename from pkg/util/mount/exec_mount.go rename to pkg/volume/util/exec/exec_mount.go index cdddb635989..9171085418a 100644 --- a/pkg/util/mount/exec_mount.go +++ b/pkg/volume/util/exec/exec_mount.go @@ -16,23 +16,25 @@ See the License for the specific language governing permissions and limitations under the License. */ -package mount +package exec import ( "fmt" "os" "k8s.io/klog" + + "k8s.io/kubernetes/pkg/util/mount" ) // ExecMounter is a mounter that uses provided Exec interface to mount and // unmount a filesystem. For all other calls it uses a wrapped mounter. type execMounter struct { - wrappedMounter Interface - exec Exec + wrappedMounter mount.Interface + exec mount.Exec } -func NewExecMounter(exec Exec, wrapped Interface) Interface { +func NewExecMounter(exec mount.Exec, wrapped mount.Interface) mount.Interface { return &execMounter{ wrappedMounter: wrapped, exec: exec, @@ -40,11 +42,11 @@ func NewExecMounter(exec Exec, wrapped Interface) Interface { } // execMounter implements mount.Interface -var _ Interface = &execMounter{} +var _ mount.Interface = &execMounter{} // Mount runs mount(8) using given exec interface. func (m *execMounter) Mount(source string, target string, fstype string, options []string) error { - bind, bindOpts, bindRemountOpts := IsBind(options) + bind, bindOpts, bindRemountOpts := mount.IsBind(options) if bind { err := m.doExecMount(source, target, fstype, bindOpts) @@ -60,7 +62,7 @@ func (m *execMounter) Mount(source string, target string, fstype string, options // doExecMount calls exec(mount ) using given exec interface. func (m *execMounter) doExecMount(source, target, fstype string, options []string) error { klog.V(5).Infof("Exec Mounting %s %s %s %v", source, target, fstype, options) - mountArgs := MakeMountArgs(source, target, fstype, options) + mountArgs := mount.MakeMountArgs(source, target, fstype, options) output, err := m.exec.Run("mount", mountArgs...) klog.V(5).Infof("Exec mounted %v: %v: %s", mountArgs, err, string(output)) if err != nil { @@ -84,7 +86,7 @@ func (m *execMounter) Unmount(target string) error { } // List returns a list of all mounted filesystems. -func (m *execMounter) List() ([]MountPoint, error) { +func (m *execMounter) List() ([]mount.MountPoint, error) { return m.wrappedMounter.List() } @@ -112,7 +114,7 @@ func (m *execMounter) GetDeviceNameFromMount(mountPath, pluginDir string) (strin return m.wrappedMounter.GetDeviceNameFromMount(mountPath, pluginDir) } -func (m *execMounter) IsMountPointMatch(mp MountPoint, dir string) bool { +func (m *execMounter) IsMountPointMatch(mp mount.MountPoint, dir string) bool { return m.wrappedMounter.IsMountPointMatch(mp, dir) } @@ -120,7 +122,7 @@ func (m *execMounter) MakeRShared(path string) error { return m.wrappedMounter.MakeRShared(path) } -func (m *execMounter) GetFileType(pathname string) (FileType, error) { +func (m *execMounter) GetFileType(pathname string) (mount.FileType, error) { return m.wrappedMounter.GetFileType(pathname) } diff --git a/pkg/util/mount/exec_mount_test.go b/pkg/volume/util/exec/exec_mount_test.go similarity index 86% rename from pkg/util/mount/exec_mount_test.go rename to pkg/volume/util/exec/exec_mount_test.go index d6ccd844895..f25485e0f25 100644 --- a/pkg/util/mount/exec_mount_test.go +++ b/pkg/volume/util/exec/exec_mount_test.go @@ -16,13 +16,15 @@ See the License for the specific language governing permissions and limitations under the License. */ -package mount +package exec import ( "fmt" "reflect" "strings" "testing" + + "k8s.io/kubernetes/pkg/util/mount" ) var ( @@ -33,7 +35,7 @@ var ( ) func TestMount(t *testing.T) { - exec := NewFakeExec(func(cmd string, args ...string) ([]byte, error) { + exec := mount.NewFakeExec(func(cmd string, args ...string) ([]byte, error) { if cmd != "mount" { t.Errorf("expected mount command, got %q", cmd) } @@ -45,7 +47,7 @@ func TestMount(t *testing.T) { return nil, nil }) - wrappedMounter := &fakeMounter{FakeMounter: &FakeMounter{}, t: t} + wrappedMounter := &fakeMounter{FakeMounter: &mount.FakeMounter{}, t: t} mounter := NewExecMounter(exec, wrappedMounter) mounter.Mount(sourcePath, destinationPath, fsType, mountOptions) @@ -53,7 +55,7 @@ func TestMount(t *testing.T) { func TestBindMount(t *testing.T) { cmdCount := 0 - exec := NewFakeExec(func(cmd string, args ...string) ([]byte, error) { + exec := mount.NewFakeExec(func(cmd string, args ...string) ([]byte, error) { cmdCount++ if cmd != "mount" { t.Errorf("expected mount command, got %q", cmd) @@ -73,14 +75,14 @@ func TestBindMount(t *testing.T) { return nil, nil }) - wrappedMounter := &fakeMounter{FakeMounter: &FakeMounter{}, t: t} + wrappedMounter := &fakeMounter{FakeMounter: &mount.FakeMounter{}, t: t} mounter := NewExecMounter(exec, wrappedMounter) bindOptions := append(mountOptions, "bind") mounter.Mount(sourcePath, destinationPath, fsType, bindOptions) } func TestUnmount(t *testing.T) { - exec := NewFakeExec(func(cmd string, args ...string) ([]byte, error) { + exec := mount.NewFakeExec(func(cmd string, args ...string) ([]byte, error) { if cmd != "umount" { t.Errorf("expected unmount command, got %q", cmd) } @@ -92,7 +94,7 @@ func TestUnmount(t *testing.T) { return nil, nil }) - wrappedMounter := &fakeMounter{&FakeMounter{}, t} + wrappedMounter := &fakeMounter{&mount.FakeMounter{}, t} mounter := NewExecMounter(exec, wrappedMounter) mounter.Unmount(destinationPath) @@ -100,7 +102,7 @@ func TestUnmount(t *testing.T) { /* Fake wrapped mounter */ type fakeMounter struct { - *FakeMounter + *mount.FakeMounter t *testing.T } diff --git a/pkg/util/mount/exec_mount_unsupported.go b/pkg/volume/util/exec/exec_mount_unsupported.go similarity index 83% rename from pkg/util/mount/exec_mount_unsupported.go rename to pkg/volume/util/exec/exec_mount_unsupported.go index 328b383fd4d..09667c070fc 100644 --- a/pkg/util/mount/exec_mount_unsupported.go +++ b/pkg/volume/util/exec/exec_mount_unsupported.go @@ -16,18 +16,20 @@ See the License for the specific language governing permissions and limitations under the License. */ -package mount +package exec import ( "errors" "os" + + "k8s.io/kubernetes/pkg/util/mount" ) type execMounter struct{} // ExecMounter is a mounter that uses provided Exec interface to mount and // unmount a filesystem. For all other calls it uses a wrapped mounter. -func NewExecMounter(exec Exec, wrapped Interface) Interface { +func NewExecMounter(exec mount.Exec, wrapped mount.Interface) mount.Interface { return &execMounter{} } @@ -39,11 +41,11 @@ func (mounter *execMounter) Unmount(target string) error { return nil } -func (mounter *execMounter) List() ([]MountPoint, error) { - return []MountPoint{}, nil +func (mounter *execMounter) List() ([]mount.MountPoint, error) { + return []mount.MountPoint{}, nil } -func (mounter *execMounter) IsMountPointMatch(mp MountPoint, dir string) bool { +func (mounter *execMounter) IsMountPointMatch(mp mount.MountPoint, dir string) bool { return (mp.Path == dir) } @@ -67,8 +69,8 @@ func (mounter *execMounter) MakeRShared(path string) error { return nil } -func (mounter *execMounter) GetFileType(pathname string) (FileType, error) { - return FileType("fake"), errors.New("not implemented") +func (mounter *execMounter) GetFileType(pathname string) (mount.FileType, error) { + return mount.FileType("fake"), errors.New("not implemented") } func (mounter *execMounter) MakeDir(pathname string) error { From 96476fd054b3b0253c6e3cd8d92637d7bec2e1a7 Mon Sep 17 00:00:00 2001 From: Travis Rhoden Date: Thu, 11 Apr 2019 13:22:14 -0600 Subject: [PATCH 2/2] Fix linting issues for exec mounter --- pkg/volume/util/exec/exec_mount.go | 4 +++- pkg/volume/util/exec/exec_mount_unsupported.go | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/volume/util/exec/exec_mount.go b/pkg/volume/util/exec/exec_mount.go index 9171085418a..a5ab848a85d 100644 --- a/pkg/volume/util/exec/exec_mount.go +++ b/pkg/volume/util/exec/exec_mount.go @@ -34,6 +34,8 @@ type execMounter struct { exec mount.Exec } +// NewExecMounter returns a mounter that uses provided Exec interface to mount and +// unmount a filesystem. For all other calls it uses a wrapped mounter. func NewExecMounter(exec mount.Exec, wrapped mount.Interface) mount.Interface { return &execMounter{ wrappedMounter: wrapped, @@ -66,7 +68,7 @@ func (m *execMounter) doExecMount(source, target, fstype string, options []strin output, err := m.exec.Run("mount", mountArgs...) klog.V(5).Infof("Exec mounted %v: %v: %s", mountArgs, err, string(output)) if err != nil { - return fmt.Errorf("mount failed: %v\nMounting command: %s\nMounting arguments: %s %s %s %v\nOutput: %s\n", + return fmt.Errorf("mount failed: %v\nMounting command: %s\nMounting arguments: %s %s %s %v\nOutput: %s", err, "mount", source, target, fstype, options, string(output)) } diff --git a/pkg/volume/util/exec/exec_mount_unsupported.go b/pkg/volume/util/exec/exec_mount_unsupported.go index 09667c070fc..fa02c1fa257 100644 --- a/pkg/volume/util/exec/exec_mount_unsupported.go +++ b/pkg/volume/util/exec/exec_mount_unsupported.go @@ -27,7 +27,7 @@ import ( type execMounter struct{} -// ExecMounter is a mounter that uses provided Exec interface to mount and +// NewExecMounter returns a mounter that uses provided Exec interface to mount and // unmount a filesystem. For all other calls it uses a wrapped mounter. func NewExecMounter(exec mount.Exec, wrapped mount.Interface) mount.Interface { return &execMounter{} @@ -85,7 +85,7 @@ func (mounter *execMounter) ExistsPath(pathname string) (bool, error) { return true, errors.New("not implemented") } -func (m *execMounter) EvalHostSymlinks(pathname string) (string, error) { +func (mounter *execMounter) EvalHostSymlinks(pathname string) (string, error) { return "", errors.New("not implemented") }