Skip to content

Commit 4268fe5

Browse files
committed
fix: separate srcAccount and dstAccount for copyBlobContainer
1 parent fda1e78 commit 4268fe5

File tree

2 files changed

+52
-22
lines changed

2 files changed

+52
-22
lines changed

pkg/blob/controllerserver.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
440440
if err != nil {
441441
return nil, status.Errorf(codes.Internal, "failed to getAzcopyAuth on account(%s) rg(%s), error: %v", accountOptions.Name, accountOptions.ResourceGroup, err)
442442
}
443-
if err := d.copyVolume(req, accountSASToken, authAzcopyEnv, validContainerName, storageEndpointSuffix); err != nil {
443+
if err := d.copyVolume(req, accountSASToken, authAzcopyEnv, accountName, validContainerName, storageEndpointSuffix); err != nil {
444444
return nil, err
445445
}
446446
} else {
@@ -755,23 +755,23 @@ func (d *Driver) DeleteBlobContainer(ctx context.Context, subsID, resourceGroupN
755755
})
756756
}
757757

758-
// CopyBlobContainer copies a blob container in the same storage account
759-
func (d *Driver) copyBlobContainer(req *csi.CreateVolumeRequest, accountSasToken string, authAzcopyEnv []string, dstContainerName, storageEndpointSuffix string) error {
758+
// CopyBlobContainer copies a blob container
759+
func (d *Driver) copyBlobContainer(req *csi.CreateVolumeRequest, accountSasToken string, authAzcopyEnv []string, dstAccountName, dstContainerName, storageEndpointSuffix string) error {
760760
var sourceVolumeID string
761761
if req.GetVolumeContentSource() != nil && req.GetVolumeContentSource().GetVolume() != nil {
762762
sourceVolumeID = req.GetVolumeContentSource().GetVolume().GetVolumeId()
763763

764764
}
765-
resourceGroupName, accountName, srcContainerName, _, _, err := GetContainerInfo(sourceVolumeID) //nolint:dogsled
765+
_, srcAccountName, srcContainerName, _, _, err := GetContainerInfo(sourceVolumeID) //nolint:dogsled
766766
if err != nil {
767767
return status.Error(codes.NotFound, err.Error())
768768
}
769-
if srcContainerName == "" || dstContainerName == "" {
770-
return fmt.Errorf("srcContainerName(%s) or dstContainerName(%s) is empty", srcContainerName, dstContainerName)
769+
if srcAccountName == "" || srcContainerName == "" || dstAccountName == "" || dstContainerName == "" {
770+
return fmt.Errorf("One or more of srcAccountName(%s), srcContainerName(%s), dstAccountName(%s), dstContainerName(%s) are empty", srcAccountName, srcContainerName, dstAccountName, dstContainerName)
771771
}
772772

773-
srcPath := fmt.Sprintf("https://%s.blob.%s/%s%s", accountName, storageEndpointSuffix, srcContainerName, accountSasToken)
774-
dstPath := fmt.Sprintf("https://%s.blob.%s/%s%s", accountName, storageEndpointSuffix, dstContainerName, accountSasToken)
773+
srcPath := fmt.Sprintf("https://%s.blob.%s/%s", srcAccountName, storageEndpointSuffix, srcContainerName)
774+
dstPath := fmt.Sprintf("https://%s.blob.%s/%s", dstAccountName, storageEndpointSuffix, dstContainerName)
775775

776776
jobState, percent, err := d.azcopy.GetAzcopyJob(dstContainerName, authAzcopyEnv)
777777
klog.V(2).Infof("azcopy job status: %s, copy percent: %s%%, error: %v", jobState, percent, err)
@@ -781,9 +781,9 @@ func (d *Driver) copyBlobContainer(req *csi.CreateVolumeRequest, accountSasToken
781781
case util.AzcopyJobRunning:
782782
return fmt.Errorf("wait for the existing AzCopy job to complete, current copy percentage is %s%%", percent)
783783
case util.AzcopyJobNotFound:
784-
klog.V(2).Infof("copy blob container %s to %s", srcContainerName, dstContainerName)
784+
klog.V(2).Infof("copy blob container %s to %s", srcPath, dstPath)
785785
execFunc := func() error {
786-
cmd := exec.Command("azcopy", "copy", srcPath, dstPath, "--recursive", "--check-length=false")
786+
cmd := exec.Command("azcopy", "copy", srcPath+accountSasToken, dstPath+accountSasToken, "--recursive", "--check-length=false")
787787
if len(authAzcopyEnv) > 0 {
788788
cmd.Env = append(os.Environ(), authAzcopyEnv...)
789789
}
@@ -794,11 +794,11 @@ func (d *Driver) copyBlobContainer(req *csi.CreateVolumeRequest, accountSasToken
794794
}
795795
timeoutFunc := func() error {
796796
_, percent, _ := d.azcopy.GetAzcopyJob(dstContainerName, authAzcopyEnv)
797-
return fmt.Errorf("timeout waiting for copy blob container %s to %s complete, current copy percent: %s%%", srcContainerName, dstContainerName, percent)
797+
return fmt.Errorf("timeout waiting for copy blob container %s to %s complete, current copy percent: %s%%", srcPath, dstPath, percent)
798798
}
799799
copyErr := util.WaitUntilTimeout(time.Duration(d.waitForAzCopyTimeoutMinutes)*time.Minute, execFunc, timeoutFunc)
800800
if copyErr != nil {
801-
klog.Warningf("CopyBlobContainer(%s, %s, %s) failed with error: %v", resourceGroupName, accountName, dstPath, copyErr)
801+
klog.Warningf("CopyBlobContainer(%s, %s, %s, %s) failed with error: %v", srcAccountName, srcContainerName, dstAccountName, dstContainerName, copyErr)
802802
} else {
803803
klog.V(2).Infof("copied blob container %s to %s successfully", srcContainerName, dstContainerName)
804804
}
@@ -808,13 +808,13 @@ func (d *Driver) copyBlobContainer(req *csi.CreateVolumeRequest, accountSasToken
808808
}
809809

810810
// copyVolume copies a volume form volume or snapshot, snapshot is not supported now
811-
func (d *Driver) copyVolume(req *csi.CreateVolumeRequest, accountSASToken string, authAzcopyEnv []string, dstContainerName, storageEndpointSuffix string) error {
811+
func (d *Driver) copyVolume(req *csi.CreateVolumeRequest, accountSASToken string, authAzcopyEnv []string, dstAccountName, dstContainerName, storageEndpointSuffix string) error {
812812
vs := req.VolumeContentSource
813813
switch vs.Type.(type) {
814814
case *csi.VolumeContentSource_Snapshot:
815815
return status.Errorf(codes.InvalidArgument, "copy volume from volumeSnapshot is not supported")
816816
case *csi.VolumeContentSource_Volume:
817-
return d.copyBlobContainer(req, accountSASToken, authAzcopyEnv, dstContainerName, storageEndpointSuffix)
817+
return d.copyBlobContainer(req, accountSASToken, authAzcopyEnv, dstAccountName, dstContainerName, storageEndpointSuffix)
818818
default:
819819
return status.Errorf(codes.InvalidArgument, "%v is not a proper volume source", vs)
820820
}

pkg/blob/controllerserver_test.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ func TestCopyVolume(t *testing.T) {
15661566
}
15671567

15681568
expectedErr := status.Errorf(codes.InvalidArgument, "copy volume from volumeSnapshot is not supported")
1569-
err := d.copyVolume(req, "", nil, "", "core.windows.net")
1569+
err := d.copyVolume(req, "", nil, "", "", "core.windows.net")
15701570
if !reflect.DeepEqual(err, expectedErr) {
15711571
t.Errorf("Unexpected error: %v", err)
15721572
}
@@ -1596,7 +1596,37 @@ func TestCopyVolume(t *testing.T) {
15961596
}
15971597

15981598
expectedErr := status.Errorf(codes.NotFound, "error parsing volume id: \"unit-test\", should at least contain two #")
1599-
err := d.copyVolume(req, "", nil, "dstContainer", "core.windows.net")
1599+
err := d.copyVolume(req, "", nil, "dstAccount", "dstContainer", "core.windows.net")
1600+
if !reflect.DeepEqual(err, expectedErr) {
1601+
t.Errorf("Unexpected error: %v", err)
1602+
}
1603+
},
1604+
},
1605+
{
1606+
name: "src account and dst account are different",
1607+
testFunc: func(t *testing.T) {
1608+
d := NewFakeDriver()
1609+
mp := map[string]string{}
1610+
1611+
volumeSource := &csi.VolumeContentSource_VolumeSource{
1612+
VolumeId: "rg#srcAccount##",
1613+
}
1614+
volumeContentSourceVolumeSource := &csi.VolumeContentSource_Volume{
1615+
Volume: volumeSource,
1616+
}
1617+
volumecontensource := csi.VolumeContentSource{
1618+
Type: volumeContentSourceVolumeSource,
1619+
}
1620+
1621+
req := &csi.CreateVolumeRequest{
1622+
Name: "unit-test",
1623+
VolumeCapabilities: stdVolumeCapabilities,
1624+
Parameters: mp,
1625+
VolumeContentSource: &volumecontensource,
1626+
}
1627+
1628+
expectedErr := fmt.Errorf("One or more of srcAccountName(srcAccount), srcContainerName(), dstAccountName(dstAccount), dstContainerName(dstContainer) are empty")
1629+
err := d.copyVolume(req, "", nil, "dstAccount", "dstContainer", "core.windows.net")
16001630
if !reflect.DeepEqual(err, expectedErr) {
16011631
t.Errorf("Unexpected error: %v", err)
16021632
}
@@ -1625,8 +1655,8 @@ func TestCopyVolume(t *testing.T) {
16251655
VolumeContentSource: &volumecontensource,
16261656
}
16271657

1628-
expectedErr := fmt.Errorf("srcContainerName() or dstContainerName(dstContainer) is empty")
1629-
err := d.copyVolume(req, "", nil, "dstContainer", "core.windows.net")
1658+
expectedErr := fmt.Errorf("One or more of srcAccountName(unit-test), srcContainerName(), dstAccountName(dstAccount), dstContainerName(dstContainer) are empty")
1659+
err := d.copyVolume(req, "", nil, "dstAccount", "dstContainer", "core.windows.net")
16301660
if !reflect.DeepEqual(err, expectedErr) {
16311661
t.Errorf("Unexpected error: %v", err)
16321662
}
@@ -1655,8 +1685,8 @@ func TestCopyVolume(t *testing.T) {
16551685
VolumeContentSource: &volumecontensource,
16561686
}
16571687

1658-
expectedErr := fmt.Errorf("srcContainerName(fileshare) or dstContainerName() is empty")
1659-
err := d.copyVolume(req, "", nil, "", "core.windows.net")
1688+
expectedErr := fmt.Errorf("One or more of srcAccountName(f5713de20cde511e8ba4900), srcContainerName(fileshare), dstAccountName(), dstContainerName() are empty")
1689+
err := d.copyVolume(req, "", nil, "", "", "core.windows.net")
16601690
if !reflect.DeepEqual(err, expectedErr) {
16611691
t.Errorf("Unexpected error: %v", err)
16621692
}
@@ -1698,7 +1728,7 @@ func TestCopyVolume(t *testing.T) {
16981728
d.azcopy.ExecCmd = m
16991729

17001730
var expectedErr error
1701-
err := d.copyVolume(req, "sastoken", nil, "dstContainer", "core.windows.net")
1731+
err := d.copyVolume(req, "sastoken", nil, "dstAccount", "dstContainer", "core.windows.net")
17021732
if !reflect.DeepEqual(err, expectedErr) {
17031733
t.Errorf("Unexpected error: %v", err)
17041734
}
@@ -1738,7 +1768,7 @@ func TestCopyVolume(t *testing.T) {
17381768
d.azcopy.ExecCmd = m
17391769

17401770
expectedErr := fmt.Errorf("wait for the existing AzCopy job to complete, current copy percentage is 50.0%%")
1741-
err := d.copyVolume(req, "sastoken", nil, "dstContainer", "core.windows.net")
1771+
err := d.copyVolume(req, "sastoken", nil, "dstAccount", "dstContainer", "core.windows.net")
17421772
if !reflect.DeepEqual(err, expectedErr) {
17431773
t.Errorf("Unexpected error: %v", err)
17441774
}

0 commit comments

Comments
 (0)