Skip to content

Commit

Permalink
remove DeleteVolume related code
Browse files Browse the repository at this point in the history
  • Loading branch information
boddumanohar committed May 10, 2021
1 parent 18042f2 commit 1db8d65
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 79 deletions.
49 changes: 2 additions & 47 deletions pkg/smb/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
// "github.com/hirochachacha/go-smb2"
)

// smbVolume is an internal representation of a volume
Expand Down Expand Up @@ -117,36 +115,9 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)

// DeleteVolume only supports static provisioning, no delete volume action
func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
volumeID := req.GetVolumeId()
if volumeID == "" {
return nil, status.Error(codes.InvalidArgument, "volume id is empty")
}

smbVol, err := d.getSmbVolFromID(volumeID)
if err != nil {
// An invalid ID should be treated as doesn't exist
klog.Warningf("failed to get smb volume for volume id %v deletion: %v", volumeID, err)
return &csi.DeleteVolumeResponse{}, nil
}

// Mount smb base share so we can delete the subdirectory
if err = d.internalMount(ctx, smbVol, nil); err != nil {
return nil, status.Errorf(codes.Internal, "failed to mount smb server: %v", err.Error())
}
defer func() {
if err = d.internalUnmount(ctx, smbVol); err != nil {
klog.Warningf("failed to unmount smb server: %v", err.Error())
}
}()

// Delete subdirectory under base-dir
internalVolumePath := d.getInternalVolumePath(smbVol)

klog.V(2).Infof("Removing subdirectory at %v", internalVolumePath)
if err = os.RemoveAll(internalVolumePath); err != nil {
return nil, status.Errorf(codes.Internal, "failed to delete subdirectory: %v", err.Error())
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}

return &csi.DeleteVolumeResponse{}, nil
}

Expand Down Expand Up @@ -326,19 +297,3 @@ func (d *Driver) smbVolToCSI(vol *smbVolume) *csi.Volume {
},
}
}

// Given a CSI volume id, return a smbVolume
func (d *Driver) getSmbVolFromID(id string) (*smbVolume, error) {
volRegex := regexp.MustCompile("^([^/]+)/(.*)/([^/]+)$")
tokens := volRegex.FindStringSubmatch(id)
if tokens == nil {
return nil, fmt.Errorf("Could not split %q into server, baseDir and subDir", id)
}

return &smbVolume{
id: id,
sourceField: tokens[1],
baseDir: tokens[2],
subDir: tokens[3],
}, nil
}
42 changes: 10 additions & 32 deletions pkg/smb/controllerserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ func TestCreateVolume(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
// Setup
_ = os.MkdirAll(filepath.Join(workingMountDir, testCSIVolume), os.ModePerm)
_, _ = os.Create(filepath.Join(workingMountDir, testCSIVolume, testCSIVolume))

// Run
resp, err := d.CreateVolume(context.TODO(), test.req)
Expand Down Expand Up @@ -171,50 +170,29 @@ func TestCreateVolume(t *testing.T) {

func TestDeleteVolume(t *testing.T) {
d := NewFakeDriver()
cases := []struct {

tests := []struct {
desc string
req *csi.DeleteVolumeRequest
resp *csi.DeleteVolumeResponse
req csi.DeleteVolumeRequest
expectedErr error
}{
{
desc: "Volume ID missing",
req: &csi.DeleteVolumeRequest{},
resp: nil,
req: csi.DeleteVolumeRequest{},
expectedErr: status.Error(codes.InvalidArgument, "Volume ID missing in request"),
},
{
desc: "Valid request",
req: &csi.DeleteVolumeRequest{VolumeId: testVolumeID},
resp: &csi.DeleteVolumeResponse{},
req: csi.DeleteVolumeRequest{VolumeId: "vol_1"},
expectedErr: nil,
},
}

for _, test := range cases {
test := test //pin
t.Run(test.desc, func(t *testing.T) {
// Setup
_ = os.MkdirAll(filepath.Join(workingMountDir, testCSIVolume), os.ModePerm)
_, _ = os.Create(filepath.Join(workingMountDir, testCSIVolume, testCSIVolume))

// Run
resp, err := d.DeleteVolume(context.TODO(), test.req)

// Verify
if test.expectedErr == nil && err != nil {
t.Errorf("test %q failed: %v", test.desc, err)
}
if test.expectedErr != nil && err == nil {
t.Errorf("test %q failed; expected error %v, got success", test.desc, test.expectedErr)
}
if !reflect.DeepEqual(resp, test.resp) {
t.Errorf("test %q failed: got resp %+v, expected %+v", test.desc, resp, test.resp)
}
if _, err := os.Stat(filepath.Join(workingMountDir, testCSIVolume, testCSIVolume)); test.expectedErr == nil && !os.IsNotExist(err) {
t.Errorf("test %q failed: expected volume subdirectory deleted, it still exists", test.desc)
}
})
for _, test := range tests {
_, err := d.DeleteVolume(context.Background(), &test.req)
if !reflect.DeepEqual(err, test.expectedErr) {
t.Errorf("Unexpected error: %v", err)
}
}
}

Expand Down

0 comments on commit 1db8d65

Please sign in to comment.