@@ -79,29 +79,40 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
7979 }
8080
8181 secrets := req .GetSecrets ()
82- if len (secrets ) > 0 {
83- if len (smbVol .uuid ) > 0 {
84- klog .V (2 ).Infof ("existing subDir(%s) is provided, skip subdirectory creation" , smbVol .subDir )
85- } else {
86- // Mount smb base share so we can create a subdirectory
87- if err := d .internalMount (ctx , smbVol , volumeCapabilities [0 ], secrets ); err != nil {
88- return nil , status .Errorf (codes .Internal , "failed to mount smb server: %v" , err .Error ())
89- }
90- defer func () {
91- if err = d .internalUnmount (ctx , smbVol ); err != nil {
92- klog .Warningf ("failed to unmount smb server: %v" , err .Error ())
93- }
94- }()
95- // Create subdirectory under base-dir
96- // TODO: revisit permissions
97- internalVolumePath := d .getInternalVolumePath (smbVol )
98- if err = os .Mkdir (internalVolumePath , 0777 ); err != nil && ! os .IsExist (err ) {
99- return nil , status .Errorf (codes .Internal , "failed to make subdirectory: %v" , err .Error ())
82+ createSubDir := len (secrets ) > 0
83+ if len (smbVol .uuid ) > 0 {
84+ klog .V (2 ).Infof ("existing subDir(%s) is provided, skip subdirectory creation" , smbVol .subDir )
85+ createSubDir = false
86+ }
87+
88+ volCap := volumeCapabilities [0 ]
89+ if volCap .GetMount () != nil && ! createSubDir {
90+ options := volCap .GetMount ().GetMountFlags ()
91+ if hasGuestMountOptions (options ) {
92+ klog .V (2 ).Infof ("guest mount option(%v) is provided, create subdirectory" , options )
93+ createSubDir = true
94+ }
95+ }
96+
97+ if createSubDir {
98+ // Mount smb base share so we can create a subdirectory
99+ if err := d .internalMount (ctx , smbVol , volCap , secrets ); err != nil {
100+ return nil , status .Errorf (codes .Internal , "failed to mount smb server: %v" , err .Error ())
101+ }
102+ defer func () {
103+ if err = d .internalUnmount (ctx , smbVol ); err != nil {
104+ klog .Warningf ("failed to unmount smb server: %v" , err .Error ())
100105 }
101- parameters [subDirField ] = smbVol .subDir
106+ }()
107+ // Create subdirectory under base-dir
108+ // TODO: revisit permissions
109+ internalVolumePath := d .getInternalVolumePath (smbVol )
110+ if err = os .Mkdir (internalVolumePath , 0777 ); err != nil && ! os .IsExist (err ) {
111+ return nil , status .Errorf (codes .Internal , "failed to make subdirectory: %v" , err .Error ())
102112 }
113+ parameters [subDirField ] = smbVol .subDir
103114 } else {
104- klog .V (2 ).Infof ("CreateVolume(%s) does not provide secrets " , name )
115+ klog .V (2 ).Infof ("CreateVolume(%s) does not create subdirectory " , name )
105116 }
106117 return & csi.CreateVolumeResponse {Volume : d .smbVolToCSI (smbVol , parameters )}, nil
107118}
@@ -133,29 +144,38 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
133144 }
134145
135146 secrets := req .GetSecrets ()
136- if len (secrets ) > 0 {
137- if len (smbVol .uuid ) > 0 {
138- klog .V (2 ).Infof ("existing subDir(%s) is provided, skip subdirectory creation" , smbVol .subDir )
139- } else {
140- // Mount smb base share so we can delete the subdirectory
141- if err = d .internalMount (ctx , smbVol , volCap , secrets ); err != nil {
142- return nil , status .Errorf (codes .Internal , "failed to mount smb server: %v" , err .Error ())
143- }
144- defer func () {
145- if err = d .internalUnmount (ctx , smbVol ); err != nil {
146- klog .Warningf ("failed to unmount smb server: %v" , err .Error ())
147- }
148- }()
149-
150- // Delete subdirectory under base-dir
151- internalVolumePath := d .getInternalVolumePath (smbVol )
152- klog .V (2 ).Infof ("Removing subdirectory at %v" , internalVolumePath )
153- if err = os .RemoveAll (internalVolumePath ); err != nil {
154- return nil , status .Errorf (codes .Internal , "failed to delete subdirectory: %v" , err .Error ())
147+ 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+ }
152+ if ! deleteSubDir {
153+ options := strings .Split (mountOptions , "," )
154+ if hasGuestMountOptions (options ) {
155+ klog .V (2 ).Infof ("guest mount option(%v) is provided, delete subdirectory" , options )
156+ deleteSubDir = true
157+ }
158+ }
159+
160+ if deleteSubDir {
161+ // Mount smb base share so we can delete the subdirectory
162+ if err = d .internalMount (ctx , smbVol , volCap , secrets ); err != nil {
163+ return nil , status .Errorf (codes .Internal , "failed to mount smb server: %v" , err .Error ())
164+ }
165+ defer func () {
166+ if err = d .internalUnmount (ctx , smbVol ); err != nil {
167+ klog .Warningf ("failed to unmount smb server: %v" , err .Error ())
155168 }
169+ }()
170+
171+ // Delete subdirectory under base-dir
172+ internalVolumePath := d .getInternalVolumePath (smbVol )
173+ klog .V (2 ).Infof ("Removing subdirectory at %v" , internalVolumePath )
174+ if err = os .RemoveAll (internalVolumePath ); err != nil {
175+ return nil , status .Errorf (codes .Internal , "failed to delete subdirectory: %v" , err .Error ())
156176 }
157177 } else {
158- klog .V (2 ).Infof ("DeleteVolume(%s) does not provide secrets " , volumeID )
178+ klog .V (2 ).Infof ("DeleteVolume(%s) does not delete subdirectory " , volumeID )
159179 }
160180
161181 return & csi.DeleteVolumeResponse {}, nil
0 commit comments