@@ -39,7 +39,7 @@ type smbVolume struct {
3939 // Volume id
4040 id string
4141 // Address of the SMB server.
42- sourceField string
42+ source string
4343 // Subdirectory of the SMB server to create volumes under
4444 subDir string
4545 // size of volume
@@ -51,7 +51,7 @@ type smbVolume struct {
5151// Ordering of elements in the CSI volume id.
5252// ID is of the form {server}/{subDir}.
5353const (
54- idsourceField = iota
54+ idSource = iota
5555 idSubDir
5656 idUUID
5757 totalIDElements // Always last
@@ -106,7 +106,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
106106 }()
107107 // Create subdirectory under base-dir
108108 // TODO: revisit permissions
109- internalVolumePath := d . getInternalVolumePath (smbVol )
109+ internalVolumePath := getInternalVolumePath (d . workingMountDir , smbVol )
110110 if err = os .Mkdir (internalVolumePath , 0777 ); err != nil && ! os .IsExist (err ) {
111111 return nil , status .Errorf (codes .Internal , "failed to make subdirectory: %v" , err .Error ())
112112 }
@@ -145,10 +145,6 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
145145
146146 secrets := req .GetSecrets ()
147147 deleteSubDir := len (secrets ) > 0
148- if len (smbVol .uuid ) > 0 {
149- klog .V (2 ).Infof ("existing subDir(%s) is provided, skip subdirectory deletion" , smbVol .subDir )
150- deleteSubDir = false
151- }
152148 if ! deleteSubDir {
153149 options := strings .Split (mountOptions , "," )
154150 if hasGuestMountOptions (options ) {
@@ -169,7 +165,7 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
169165 }()
170166
171167 // Delete subdirectory under base-dir
172- internalVolumePath := d . getInternalVolumePath (smbVol )
168+ internalVolumePath := getInternalVolumePath (d . workingMountDir , smbVol )
173169 klog .V (2 ).Infof ("Removing subdirectory at %v" , internalVolumePath )
174170 if err = os .RemoveAll (internalVolumePath ); err != nil {
175171 return nil , status .Errorf (codes .Internal , "failed to delete subdirectory: %v" , err .Error ())
@@ -244,23 +240,9 @@ func (d *Driver) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsReques
244240 return nil , status .Error (codes .Unimplemented , "" )
245241}
246242
247- // Given a smbVolume, return a CSI volume id
248- func getVolumeIDFromSmbVol (vol * smbVolume ) string {
249- idElements := make ([]string , totalIDElements )
250- idElements [idsourceField ] = strings .Trim (vol .sourceField , "/" )
251- idElements [idSubDir ] = strings .Trim (vol .subDir , "/" )
252- idElements [idUUID ] = vol .uuid
253- return strings .Join (idElements , separator )
254- }
255-
256- // Get working directory for CreateVolume and DeleteVolume
257- func (d * Driver ) getInternalMountPath (vol * smbVolume ) string {
258- return filepath .Join (d .workingMountDir , vol .subDir )
259- }
260-
261243// Mount smb server at base-dir
262244func (d * Driver ) internalMount (ctx context.Context , vol * smbVolume , volCap * csi.VolumeCapability , secrets map [string ]string ) error {
263- stagingPath := d . getInternalMountPath (vol )
245+ stagingPath := getInternalMountPath (d . workingMountDir , vol )
264246
265247 if volCap == nil {
266248 volCap = & csi.VolumeCapability {
@@ -270,11 +252,11 @@ func (d *Driver) internalMount(ctx context.Context, vol *smbVolume, volCap *csi.
270252 }
271253 }
272254
273- klog .V (4 ).Infof ("internally mounting %v at %v" , sourceField , stagingPath )
255+ klog .V (4 ).Infof ("internally mounting %v at %v" , vol . source , stagingPath )
274256 _ , err := d .NodeStageVolume (ctx , & csi.NodeStageVolumeRequest {
275257 StagingTargetPath : stagingPath ,
276258 VolumeContext : map [string ]string {
277- sourceField : vol .sourceField ,
259+ sourceField : vol .source ,
278260 },
279261 VolumeCapability : volCap ,
280262 VolumeId : vol .id ,
@@ -285,17 +267,38 @@ func (d *Driver) internalMount(ctx context.Context, vol *smbVolume, volCap *csi.
285267
286268// Unmount smb server at base-dir
287269func (d * Driver ) internalUnmount (ctx context.Context , vol * smbVolume ) error {
288- targetPath := d . getInternalMountPath (vol )
270+ targetPath := getInternalMountPath (d . workingMountDir , vol )
289271
290272 // Unmount smb server at base-dir
291273 klog .V (4 ).Infof ("internally unmounting %v" , targetPath )
292274 _ , err := d .NodeUnstageVolume (ctx , & csi.NodeUnstageVolumeRequest {
293275 VolumeId : vol .id ,
294- StagingTargetPath : d . getInternalMountPath ( vol ) ,
276+ StagingTargetPath : targetPath ,
295277 })
296278 return err
297279}
298280
281+ // Given a smbVolume, return a CSI volume id
282+ func getVolumeIDFromSmbVol (vol * smbVolume ) string {
283+ idElements := make ([]string , totalIDElements )
284+ idElements [idSource ] = strings .Trim (vol .source , "/" )
285+ idElements [idSubDir ] = strings .Trim (vol .subDir , "/" )
286+ idElements [idUUID ] = vol .uuid
287+ return strings .Join (idElements , separator )
288+ }
289+
290+ // getInternalMountPath: get working directory for CreateVolume and DeleteVolume
291+ func getInternalMountPath (workingMountDir string , vol * smbVolume ) string {
292+ if vol == nil {
293+ return ""
294+ }
295+ mountDir := vol .uuid
296+ if vol .uuid == "" {
297+ mountDir = vol .subDir
298+ }
299+ return filepath .Join (workingMountDir , mountDir )
300+ }
301+
299302// Convert VolumeCreate parameters to an smbVolume
300303func newSMBVolume (name string , size int64 , params map [string ]string ) (* smbVolume , error ) {
301304 var source , subDir string
@@ -317,8 +320,8 @@ func newSMBVolume(name string, size int64, params map[string]string) (*smbVolume
317320 }
318321
319322 vol := & smbVolume {
320- sourceField : source ,
321- size : size ,
323+ source : source ,
324+ size : size ,
322325 }
323326 if subDir == "" {
324327 // use pv name by default if not specified
@@ -339,8 +342,8 @@ func newSMBVolume(name string, size int64, params map[string]string) (*smbVolume
339342// CreateVolume calls in parallel and they may use the same underlying share.
340343// Instead of refcounting how many CreateVolume calls are using the same
341344// share, it's simpler to just do a mount per request.
342- func ( d * Driver ) getInternalVolumePath ( vol * smbVolume ) string {
343- return filepath .Join (d . getInternalMountPath (vol ), vol .subDir )
345+ func getInternalVolumePath ( workingMountDir string , vol * smbVolume ) string {
346+ return filepath .Join (getInternalMountPath (workingMountDir , vol ), vol .subDir )
344347}
345348
346349// Convert into smbVolume into a csi.Volume
@@ -366,9 +369,9 @@ func getSmbVolFromID(id string) (*smbVolume, error) {
366369 source = "//" + source
367370 }
368371 vol := & smbVolume {
369- id : id ,
370- sourceField : source ,
371- subDir : segments [1 ],
372+ id : id ,
373+ source : source ,
374+ subDir : segments [1 ],
372375 }
373376 if len (segments ) >= 3 {
374377 vol .uuid = segments [2 ]
0 commit comments