@@ -397,9 +397,17 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
397397 }
398398 if folderName != "" {
399399 if createFolderIfNotExist {
400- if err := d .createFolderIfNotExists (ctx , accountName , accountKey , fileShareName , folderName , storageEndpointSuffix ); err != nil {
401- klog .Warningf ("Failed to create folder %s in share %s: %v" , folderName , fileShareName , err )
402- // Continue with mounting - folder might already exist or be created by other means
400+ if protocol == nfs {
401+ // For NFS, account key is not available. Mount the share root
402+ // to a temporary path, create the folder, then unmount.
403+ if err := d .createFolderOnNfsShare (ctx , source , folderName , mountFlags ); err != nil {
404+ klog .Warningf ("Failed to create folder %s on NFS share: %v" , folderName , err )
405+ }
406+ } else {
407+ if err := d .createFolderIfNotExists (ctx , accountName , accountKey , fileShareName , folderName , storageEndpointSuffix ); err != nil {
408+ klog .Warningf ("Failed to create folder %s in share %s: %v" , folderName , fileShareName , err )
409+ // Continue with mounting - folder might already exist or be created by other means
410+ }
403411 }
404412 }
405413 source = fmt .Sprintf ("%s%s%s" , source , osSeparator , folderName )
@@ -886,3 +894,34 @@ func shouldUseServiceAccountToken(attrib map[string]string) bool {
886894 }
887895 return false
888896}
897+
898+ // createFolderOnNfsShare mounts the NFS share root to a temporary directory,
899+ // creates the folder path, then unmounts. This avoids the need for an account
900+ // key which is not available in NFS mode.
901+ func (d * Driver ) createFolderOnNfsShare (ctx context.Context , nfsSource , folderName string , mountFlags []string ) error {
902+ tempDir , err := os .MkdirTemp ("" , "nfs-folder-create-" )
903+ if err != nil {
904+ return fmt .Errorf ("failed to create temp dir: %v" , err )
905+ }
906+ defer os .Remove (tempDir )
907+
908+ mountOptions := util .JoinMountOptions (mountFlags , []string {"vers=4,minorversion=1,sec=sys" })
909+ mountOptions = appendDefaultNfsMountOptions (mountOptions , d .appendNoResvPortOption , d .appendActimeoOption )
910+
911+ klog .V (2 ).Infof ("Mounting NFS share %s to temp dir %s to create folder %s" , nfsSource , tempDir , folderName )
912+ if err := d .mounter .Mount (nfsSource , tempDir , nfs , mountOptions ); err != nil {
913+ return fmt .Errorf ("failed to mount NFS share %s to %s: %v" , nfsSource , tempDir , err )
914+ }
915+ defer func () {
916+ if err := d .mounter .Unmount (tempDir ); err != nil {
917+ klog .Warningf ("Failed to unmount temp dir %s: %v" , tempDir , err )
918+ }
919+ }()
920+
921+ folderPath := filepath .Join (tempDir , folderName )
922+ if err := os .MkdirAll (folderPath , 0755 ); err != nil {
923+ return fmt .Errorf ("failed to create folder %s: %v" , folderPath , err )
924+ }
925+ klog .V (2 ).Infof ("Successfully created folder %s on NFS share" , folderName )
926+ return nil
927+ }
0 commit comments