Skip to content

Commit

Permalink
Copy pkg/mounter and refactor to use k8s.io/mount-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
wongma7 committed Mar 9, 2021
1 parent ee3b46c commit 2874b2d
Show file tree
Hide file tree
Showing 10 changed files with 792 additions and 540 deletions.
63 changes: 32 additions & 31 deletions pkg/driver/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,50 @@ package driver
import (
"os"

"k8s.io/utils/exec"
"k8s.io/utils/mount"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/mounter"
mountutils "k8s.io/mount-utils"
utilexec "k8s.io/utils/exec"
)

// Mounter is an interface for mount operations
type Mounter interface {
mount.Interface
exec.Interface
// Implemented by NodeMounter.SafeFormatAndMount
mountutils.Interface
FormatAndMount(source string, target string, fstype string, options []string) error
GetDeviceName(mountPath string) (string, int, error)
MakeFile(pathname string) error
MakeDir(pathname string) error
ExistsPath(filename string) (bool, error)

// Implemented by NodeMounter.SafeFormatAndMount.Exec
// TODO this won't make sense on Windows with csi-proxy
utilexec.Interface

// Implemented by NodeMounter below
GetDeviceNameFromMount(mountPath string) (string, int, error)
// TODO this won't make sense on Windows with csi-proxy
MakeFile(path string) error
MakeDir(path string) error
PathExists(path string) (bool, error)
}

type NodeMounter struct {
mount.SafeFormatAndMount
exec.Interface
mountutils.SafeFormatAndMount
utilexec.Interface
}

func newNodeMounter() Mounter {
return &NodeMounter{
mount.SafeFormatAndMount{
Interface: mount.New(""),
Exec: exec.New(),
},
exec.New(),
func newNodeMounter() (Mounter, error) {
safeMounter, err := mounter.NewSafeMounter()
if err != nil {
return nil, err
}
return &NodeMounter{*safeMounter, safeMounter.Exec}, nil
}

func (m *NodeMounter) GetDeviceName(mountPath string) (string, int, error) {
return mount.GetDeviceNameFromMount(m, mountPath)
// GetDeviceNameFromMount returns the volume ID for a mount path.
func (m NodeMounter) GetDeviceNameFromMount(mountPath string) (string, int, error) {
return mountutils.GetDeviceNameFromMount(m, mountPath)
}

// This function is mirrored in ./sanity_test.go to make sure sanity test covered this block of code
// Please mirror the change to func MakeFile in ./sanity_test.go
func (m *NodeMounter) MakeFile(pathname string) error {
f, err := os.OpenFile(pathname, os.O_CREATE, os.FileMode(0644))
func (m *NodeMounter) MakeFile(path string) error {
f, err := os.OpenFile(path, os.O_CREATE, os.FileMode(0644))
if err != nil {
if !os.IsExist(err) {
return err
Expand All @@ -70,8 +76,8 @@ func (m *NodeMounter) MakeFile(pathname string) error {

// This function is mirrored in ./sanity_test.go to make sure sanity test covered this block of code
// Please mirror the change to func MakeFile in ./sanity_test.go
func (m *NodeMounter) MakeDir(pathname string) error {
err := os.MkdirAll(pathname, os.FileMode(0755))
func (m *NodeMounter) MakeDir(path string) error {
err := os.MkdirAll(path, os.FileMode(0755))
if err != nil {
if !os.IsExist(err) {
return err
Expand All @@ -82,11 +88,6 @@ func (m *NodeMounter) MakeDir(pathname string) error {

// This function is mirrored in ./sanity_test.go to make sure sanity test covered this block of code
// Please mirror the change to func MakeFile in ./sanity_test.go
func (m *NodeMounter) ExistsPath(filename string) (bool, error) {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
func (m *NodeMounter) PathExists(path string) (bool, error) {
return mountutils.PathExists(path)
}
38 changes: 21 additions & 17 deletions pkg/driver/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ func TestMakeDir(t *testing.T) {

targetPath := filepath.Join(dir, "targetdir")

var (
mountObj = newNodeMounter()
)
mountObj, err := newNodeMounter()
if err != nil {
t.Fatalf("error creating mounter %v", err)
}

if mountObj.MakeDir(targetPath) != nil {
t.Fatalf("Expect no error but got: %v", err)
Expand All @@ -45,7 +46,7 @@ func TestMakeDir(t *testing.T) {
t.Fatalf("Expect no error but got: %v", err)
}

if exists, err := mountObj.ExistsPath(targetPath); !exists {
if exists, err := mountObj.PathExists(targetPath); !exists {
t.Fatalf("Expect no error but got: %v", err)
}
}
Expand All @@ -60,9 +61,10 @@ func TestMakeFile(t *testing.T) {

targetPath := filepath.Join(dir, "targetfile")

var (
mountObj = newNodeMounter()
)
mountObj, err := newNodeMounter()
if err != nil {
t.Fatalf("error creating mounter %v", err)
}

if mountObj.MakeFile(targetPath) != nil {
t.Fatalf("Expect no error but got: %v", err)
Expand All @@ -72,13 +74,13 @@ func TestMakeFile(t *testing.T) {
t.Fatalf("Expect no error but got: %v", err)
}

if exists, err := mountObj.ExistsPath(targetPath); !exists {
if exists, err := mountObj.PathExists(targetPath); !exists {
t.Fatalf("Expect no error but got: %v", err)
}

}

func TestExistsPath(t *testing.T) {
func TestPathExists(t *testing.T) {
// Setup the full driver and its environment
dir, err := ioutil.TempDir("", "mount-ebs-csi")
if err != nil {
Expand All @@ -88,11 +90,12 @@ func TestExistsPath(t *testing.T) {

targetPath := filepath.Join(dir, "notafile")

var (
mountObj = newNodeMounter()
)
mountObj, err := newNodeMounter()
if err != nil {
t.Fatalf("error creating mounter %v", err)
}

exists, err := mountObj.ExistsPath(targetPath)
exists, err := mountObj.PathExists(targetPath)

if err != nil {
t.Fatalf("Expect no error but got: %v", err)
Expand All @@ -114,11 +117,12 @@ func TestGetDeviceName(t *testing.T) {

targetPath := filepath.Join(dir, "notafile")

var (
mountObj = newNodeMounter()
)
mountObj, err := newNodeMounter()
if err != nil {
t.Fatalf("error creating mounter %v", err)
}

if _, _, err := mountObj.GetDeviceName(targetPath); err != nil {
if _, _, err := mountObj.GetDeviceNameFromMount(targetPath); err != nil {
t.Fatalf("Expect no error but got: %v", err)
}

Expand Down
Loading

0 comments on commit 2874b2d

Please sign in to comment.