@@ -61,8 +61,6 @@ const (
6161 MSI = "MSI"
6262 SPN = "SPN"
6363 authorizationPermissionMismatch = "AuthorizationPermissionMismatch"
64-
65- waitForAzCopyInterval = 2 * time .Second
6664)
6765
6866// CreateVolume provisions a volume
@@ -85,7 +83,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
8583 // logging the job status if it's volume cloning
8684 if req .GetVolumeContentSource () != nil {
8785 jobState , percent , err := d .azcopy .GetAzcopyJob (volName , []string {})
88- klog . V ( 2 ). Infof ( "azcopy job status: %s, copy percent: %s%%, error: %v" , jobState , percent , err )
86+ return nil , status . Errorf ( codes . Aborted , volumeOperationAlreadyExistsWithAzcopyFmt , volName , jobState , percent , err )
8987 }
9088 return nil , status .Errorf (codes .Aborted , volumeOperationAlreadyExistsFmt , volName )
9189 }
@@ -759,43 +757,41 @@ func (d *Driver) copyBlobContainer(req *csi.CreateVolumeRequest, accountSasToken
759757 return fmt .Errorf ("srcContainerName(%s) or dstContainerName(%s) is empty" , srcContainerName , dstContainerName )
760758 }
761759
762- timeAfter := time .After (time .Duration (d .waitForAzCopyTimeoutMinutes ) * time .Minute )
763- timeTick := time .Tick (waitForAzCopyInterval )
764760 srcPath := fmt .Sprintf ("https://%s.blob.%s/%s%s" , accountName , storageEndpointSuffix , srcContainerName , accountSasToken )
765761 dstPath := fmt .Sprintf ("https://%s.blob.%s/%s%s" , accountName , storageEndpointSuffix , dstContainerName , accountSasToken )
766762
767763 jobState , percent , err := d .azcopy .GetAzcopyJob (dstContainerName , authAzcopyEnv )
768764 klog .V (2 ).Infof ("azcopy job status: %s, copy percent: %s%%, error: %v" , jobState , percent , err )
769- if jobState == util .AzcopyJobError || jobState == util .AzcopyJobCompleted {
765+ switch jobState {
766+ case util .AzcopyJobError , util .AzcopyJobCompleted :
770767 return err
771- }
772- klog .V (2 ).Infof ("begin to copy blob container %s to %s" , srcContainerName , dstContainerName )
773- for {
774- select {
775- case <- timeTick :
776- jobState , percent , err := d .azcopy .GetAzcopyJob (dstContainerName , authAzcopyEnv )
777- klog .V (2 ).Infof ("azcopy job status: %s, copy percent: %s%%, error: %v" , jobState , percent , err )
778- switch jobState {
779- case util .AzcopyJobError , util .AzcopyJobCompleted :
780- return err
781- case util .AzcopyJobNotFound :
782- klog .V (2 ).Infof ("copy blob container %s to %s" , srcContainerName , dstContainerName )
783- cmd := exec .Command ("azcopy" , "copy" , srcPath , dstPath , "--recursive" , "--check-length=false" )
784- if len (authAzcopyEnv ) > 0 {
785- cmd .Env = append (os .Environ (), authAzcopyEnv ... )
786- }
787- out , copyErr := cmd .CombinedOutput ()
788- if copyErr != nil {
789- klog .Warningf ("CopyBlobContainer(%s, %s, %s) failed with error(%v): %v" , resourceGroupName , accountName , dstPath , copyErr , string (out ))
790- } else {
791- klog .V (2 ).Infof ("copied blob container %s to %s successfully" , srcContainerName , dstContainerName )
792- }
793- return copyErr
768+ case util .AzcopyJobRunning :
769+ return fmt .Errorf ("wait for the existing AzCopy job to complete, current copy percentage is %s%%" , percent )
770+ case util .AzcopyJobNotFound :
771+ klog .V (2 ).Infof ("copy blob container %s to %s" , srcContainerName , dstContainerName )
772+ execFunc := func () error {
773+ cmd := exec .Command ("azcopy" , "copy" , srcPath , dstPath , "--recursive" , "--check-length=false" )
774+ if len (authAzcopyEnv ) > 0 {
775+ cmd .Env = append (os .Environ (), authAzcopyEnv ... )
776+ }
777+ if out , err := cmd .CombinedOutput (); err != nil {
778+ return fmt .Errorf ("exec error: %v, output: %v" , err , string (out ))
794779 }
795- case <- timeAfter :
796- return fmt .Errorf ("timeout waiting for copy blob container %s to %s succeed" , srcContainerName , dstContainerName )
780+ return nil
781+ }
782+ timeoutFunc := func () error {
783+ _ , percent , _ := d .azcopy .GetAzcopyJob (dstContainerName , authAzcopyEnv )
784+ return fmt .Errorf ("timeout waiting for copy blob container %s to %s complete, current copy percent: %s%%" , srcContainerName , dstContainerName , percent )
785+ }
786+ copyErr := util .WaitForExecCompletion (time .Duration (d .waitForAzCopyTimeoutMinutes )* time .Minute , execFunc , timeoutFunc )
787+ if copyErr != nil {
788+ klog .Warningf ("CopyBlobContainer(%s, %s, %s) failed with error: %v" , resourceGroupName , accountName , dstPath , copyErr )
789+ } else {
790+ klog .V (2 ).Infof ("copied blob container %s to %s successfully" , srcContainerName , dstContainerName )
797791 }
792+ return copyErr
798793 }
794+ return err
799795}
800796
801797// copyVolume copies a volume form volume or snapshot, snapshot is not supported now
0 commit comments