Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when unpublishing already unmounted file system #106

Merged
merged 1 commit into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ replace k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.0.0-20191003001538
replace k8s.io/sample-cli-plugin => k8s.io/sample-cli-plugin v0.0.0-20191003002540-40951731b79f

replace k8s.io/sample-controller => k8s.io/sample-controller v0.0.0-20191003001734-27680fba8268

go 1.13
16 changes: 16 additions & 0 deletions pkg/driver/mocks/mock_mount.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/driver/mounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
type Mounter interface {
mount.Interface
MakeDir(pathname string) error
GetDeviceName(mountPath string) (string, int, error)
}

type NodeMounter struct {
Expand All @@ -44,3 +45,7 @@ func (m *NodeMounter) MakeDir(pathname string) error {
}
return nil
}

func (m *NodeMounter) GetDeviceName(mountPath string) (string, int, error) {
return mount.GetDeviceNameFromMount(m, mountPath)
}
19 changes: 18 additions & 1 deletion pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,25 @@ func (d *Driver) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublish
return nil, status.Error(codes.InvalidArgument, "Target path not provided")
}

// Check if target directory is a mount point. GetDeviceNameFromMount
// given a mnt point, finds the device from /proc/mounts
// returns the device name, reference count, and error code
_, refCount, err := d.mounter.GetDeviceName(target)
if err != nil {
msg := fmt.Sprintf("failed to check if volume is mounted: %v", err)
return nil, status.Error(codes.Internal, msg)
}

// From the spec: If the volume corresponding to the volume_id
// is not staged to the staging_target_path, the Plugin MUST
// reply 0 OK.
if refCount == 0 {
klog.V(5).Infof("NodeUnpublishVolume: %s target not mounted", target)
return &csi.NodeUnpublishVolumeResponse{}, nil
}

klog.V(5).Infof("NodeUnpublishVolume: unmounting %s", target)
err := d.mounter.Unmount(target)
err = d.mounter.Unmount(target)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not unmount %q: %v", target, err)
}
Expand Down
59 changes: 57 additions & 2 deletions pkg/driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,33 @@ func TestNodeUnpublishVolume(t *testing.T) {
TargetPath: targetPath,
}

mockMounter.EXPECT().GetDeviceName(gomock.Eq(targetPath)).Return("", 1, nil)
mockMounter.EXPECT().Unmount(gomock.Eq(targetPath)).Return(nil)

_, err := driver.NodeUnpublishVolume(ctx, req)
if err != nil {
t.Fatalf("NodeUnpublishVolume is failed: %v", err)
}
},
},
{
name: "success: unpublish with already unmounted target",
testFunc: func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockMounter := mocks.NewMockMounter(mockCtrl)
driver := &Driver{
endpoint: endpoint,
nodeID: nodeID,
mounter: mockMounter,
}

ctx := context.Background()
req := &csi.NodeUnpublishVolumeRequest{
VolumeId: volumeId,
TargetPath: targetPath,
}

mockMounter.EXPECT().GetDeviceName(gomock.Eq(targetPath)).Return("", 0, nil)
mockMounter.EXPECT().Unmount(gomock.Eq(targetPath)).Return(nil)

_, err := driver.NodeUnpublishVolume(ctx, req)
Expand All @@ -489,7 +516,7 @@ func TestNodeUnpublishVolume(t *testing.T) {

_, err := driver.NodeUnpublishVolume(ctx, req)
if err == nil {
t.Fatalf("NodeUnpublishVolume is not failed: %v", err)
t.Fatalf("NodeUnpublishVolume is not failed")
}
},
},
Expand All @@ -511,11 +538,39 @@ func TestNodeUnpublishVolume(t *testing.T) {
}

mountErr := fmt.Errorf("Unmount failed")
mockMounter.EXPECT().GetDeviceName(gomock.Eq(targetPath)).Return("", 1, nil)
mockMounter.EXPECT().Unmount(gomock.Eq(targetPath)).Return(mountErr)

_, err := driver.NodeUnpublishVolume(ctx, req)
if err == nil {
t.Fatalf("NodeUnpublishVolume is not failed: %v", err)
t.Fatalf("NodeUnpublishVolume is not failed")
}
},
},
{
name: "fail: mounter failed to GetDeviceName",
testFunc: func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockMounter := mocks.NewMockMounter(mockCtrl)
driver := &Driver{
endpoint: endpoint,
nodeID: nodeID,
mounter: mockMounter,
}

ctx := context.Background()
req := &csi.NodeUnpublishVolumeRequest{
VolumeId: volumeId,
TargetPath: targetPath,
}

mounterErr := fmt.Errorf("Unmount failed")
mockMounter.EXPECT().GetDeviceName(gomock.Eq(targetPath)).Return("", 1, mounterErr)
mockMounter.EXPECT().Unmount(gomock.Eq(targetPath)).Return(nil)

_, err := driver.NodeUnpublishVolume(ctx, req)
if err == nil {
t.Fatalf("NodeUnpublishVolume is not failed")
}
},
},
Expand Down