@@ -187,8 +187,6 @@ const (
187187	SnapshotID        =  "snapshot_id" 
188188
189189	FSGroupChangeNone  =  "None" 
190- 
191- 	waitForAzCopyInterval  =  2  *  time .Second 
192190)
193191
194192var  (
@@ -1011,55 +1009,59 @@ func (d *Driver) copyFileShare(ctx context.Context, req *csi.CreateVolumeRequest
10111009		return  fmt .Errorf ("srcFileShareName(%s) or dstFileShareName(%s) is empty" , srcFileShareName , dstFileShareName )
10121010	}
10131011
1014- 	timeAfter  :=  time .After (time .Duration (d .waitForAzCopyTimeoutMinutes ) *  time .Minute )
1015- 	timeTick  :=  time .Tick (waitForAzCopyInterval )
10161012	srcPath  :=  fmt .Sprintf ("https://%s.file.%s/%s%s" , accountName , storageEndpointSuffix , srcFileShareName , accountSASToken )
10171013	dstPath  :=  fmt .Sprintf ("https://%s.file.%s/%s%s" , accountName , storageEndpointSuffix , dstFileShareName , accountSASToken )
10181014
10191015	jobState , percent , err  :=  d .azcopy .GetAzcopyJob (dstFileShareName , authAzcopyEnv )
10201016	klog .V (2 ).Infof ("azcopy job status: %s, copy percent: %s%%, error: %v" , jobState , percent , err )
1021- 	if  jobState  ==  fileutil .AzcopyJobError  ||  jobState  ==  fileutil .AzcopyJobCompleted  {
1017+ 	switch  jobState  {
1018+ 	case  fileutil .AzcopyJobError , fileutil .AzcopyJobCompleted :
10221019		return  err 
1023- 	}
1024- 	klog .V (2 ).Infof ("begin to copy fileshare %s to %s" , srcFileShareName , dstFileShareName )
1025- 	for  {
1026- 		select  {
1027- 		case  <- timeTick :
1028- 			jobState , percent , err  :=  d .azcopy .GetAzcopyJob (dstFileShareName , authAzcopyEnv )
1029- 			klog .V (2 ).Infof ("azcopy job status: %s, copy percent: %s%%, error: %v" , jobState , percent , err )
1030- 			switch  jobState  {
1031- 			case  fileutil .AzcopyJobError , fileutil .AzcopyJobCompleted :
1020+ 	case  fileutil .AzcopyJobRunning :
1021+ 		return  fmt .Errorf ("wait for the existing AzCopy job to complete, current copy percentage is %s%%" , percent )
1022+ 	case  fileutil .AzcopyJobNotFound :
1023+ 		klog .V (2 ).Infof ("copy fileshare %s to %s" , srcFileShareName , dstFileShareName )
1024+ 		execFuncWithAuth  :=  func () error  {
1025+ 			cmd  :=  exec .Command ("azcopy" , "copy" , srcPath , dstPath )
1026+ 			cmd .Args  =  append (cmd .Args , defaultAzcopyCopyOptions ... )
1027+ 			if  len (authAzcopyEnv ) >  0  {
1028+ 				cmd .Env  =  append (os .Environ (), authAzcopyEnv ... )
1029+ 			}
1030+ 			if  out , err  :=  cmd .CombinedOutput (); err  !=  nil  {
1031+ 				return  fmt .Errorf ("exec error: %v, output: %v" , err , string (out ))
1032+ 			}
1033+ 			return  nil 
1034+ 		}
1035+ 		timeoutFunc  :=  func () error  {
1036+ 			_ , percent , _  :=  d .azcopy .GetAzcopyJob (dstFileShareName , authAzcopyEnv )
1037+ 			return  fmt .Errorf ("timeout waiting for copy blob container %s to %s complete, current copy percent: %s%%" , srcFileShareName , dstFileShareName , percent )
1038+ 		}
1039+ 		copyErr  :=  fileutil .WaitForExecCompletion (time .Duration (d .waitForAzCopyTimeoutMinutes )* time .Minute , execFuncWithAuth , timeoutFunc )
1040+ 		if  accountSASToken  ==  ""  &&  copyErr  !=  nil  &&  strings .Contains (copyErr .Error (), authorizationPermissionMismatch ) {
1041+ 			klog .Warningf ("azcopy list failed with AuthorizationPermissionMismatch error, should assign \" Storage File Data SMB Share Elevated Contributor\"  role to controller identity, fall back to use sas token, original error: %v" , copyErr )
1042+ 			d .azcopySasTokenCache .Set (accountName , "" )
1043+ 			var  sasToken  string 
1044+ 			if  sasToken , _ , err  =  d .getAzcopyAuth (ctx , accountName , "" , storageEndpointSuffix , accountOptions , secrets , secretName , secretNamespace , true ); err  !=  nil  {
10321045				return  err 
1033- 			case   fileutil . AzcopyJobNotFound : 
1034- 				 klog . V ( 2 ). Infof ( "copy fileshare %s to %s" ,  srcFileShareName ,  dstFileShareName ) 
1035- 				cmd  :=  exec .Command ("azcopy" , "copy" , srcPath , dstPath )
1046+ 			} 
1047+ 			execFuncWithSasToken   :=   func ()  error  { 
1048+ 				cmd  :=  exec .Command ("azcopy" , "copy" , srcPath + sasToken , dstPath + sasToken )
10361049				cmd .Args  =  append (cmd .Args , defaultAzcopyCopyOptions ... )
1037- 				if  len ( authAzcopyEnv )  >   0  {
1038- 					cmd . Env   =   append ( os . Environ (),  authAzcopyEnv ... )
1050+ 				if  out ,  err   :=   cmd . CombinedOutput ();  err   !=   nil  {
1051+ 					return   fmt . Errorf ( "exec error: %v, output: %v" ,  err ,  string ( out ) )
10391052				}
1040- 				out , copyErr  :=  cmd .CombinedOutput ()
1041- 				if  accountSASToken  ==  ""  &&  strings .Contains (string (out ), authorizationPermissionMismatch ) &&  copyErr  !=  nil  {
1042- 					klog .Warningf ("azcopy list failed with AuthorizationPermissionMismatch error, should assign \" Storage File Data SMB Share Elevated Contributor\"  role to controller identity, fall back to use sas token, original output: %v" , string (out ))
1043- 					d .azcopySasTokenCache .Set (accountName , "" )
1044- 					var  sasToken  string 
1045- 					if  sasToken , _ , err  =  d .getAzcopyAuth (ctx , accountName , "" , storageEndpointSuffix , accountOptions , secrets , secretName , secretNamespace , true ); err  !=  nil  {
1046- 						return  err 
1047- 					}
1048- 					cmd  :=  exec .Command ("azcopy" , "copy" , srcPath + sasToken , dstPath + sasToken )
1049- 					cmd .Args  =  append (cmd .Args , defaultAzcopyCopyOptions ... )
1050- 					out , copyErr  =  cmd .CombinedOutput ()
1051- 				}
1052- 				if  copyErr  !=  nil  {
1053- 					klog .Warningf ("CopyFileShare(%s, %s, %s) failed with error(%v): %v" , resourceGroupName , accountName , dstFileShareName , copyErr , string (out ))
1054- 				} else  {
1055- 					klog .V (2 ).Infof ("copied fileshare %s to %s successfully" , srcFileShareName , dstFileShareName )
1056- 				}
1057- 				return  copyErr 
1053+ 				return  nil 
10581054			}
1059- 		case  <- timeAfter :
1060- 			return  fmt .Errorf ("timeout waiting for copy fileshare %s to %s succeed" , srcFileShareName , dstFileShareName )
1055+ 			copyErr  =  fileutil .WaitForExecCompletion (time .Duration (d .waitForAzCopyTimeoutMinutes )* time .Minute , execFuncWithSasToken , timeoutFunc )
10611056		}
1057+ 		if  copyErr  !=  nil  {
1058+ 			klog .Warningf ("CopyFileShare(%s, %s, %s) failed with error: %v" , resourceGroupName , accountName , dstFileShareName , copyErr )
1059+ 		} else  {
1060+ 			klog .V (2 ).Infof ("copied fileshare %s to %s successfully" , srcFileShareName , dstFileShareName )
1061+ 		}
1062+ 		return  copyErr 
10621063	}
1064+ 	return  err 
10631065}
10641066
10651067// GetTotalAccountQuota returns the total quota in GB of all file shares in the storage account and the number of file shares 
0 commit comments