Skip to content

Commit 34ff7a7

Browse files
committed
fix: separate srcAccount and dstAccount for copyBlobContainer
1 parent fda1e78 commit 34ff7a7

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

pkg/blob/controllerserver.go

Lines changed: 15 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,24 @@ 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)
775+
775776

776777
jobState, percent, err := d.azcopy.GetAzcopyJob(dstContainerName, authAzcopyEnv)
777778
klog.V(2).Infof("azcopy job status: %s, copy percent: %s%%, error: %v", jobState, percent, err)
@@ -781,9 +782,9 @@ func (d *Driver) copyBlobContainer(req *csi.CreateVolumeRequest, accountSasToken
781782
case util.AzcopyJobRunning:
782783
return fmt.Errorf("wait for the existing AzCopy job to complete, current copy percentage is %s%%", percent)
783784
case util.AzcopyJobNotFound:
784-
klog.V(2).Infof("copy blob container %s to %s", srcContainerName, dstContainerName)
785+
klog.V(2).Infof("copy blob container %s to %s", srcPath, dstPath)
785786
execFunc := func() error {
786-
cmd := exec.Command("azcopy", "copy", srcPath, dstPath, "--recursive", "--check-length=false")
787+
cmd := exec.Command("azcopy", "copy", srcPath + accountSasToken, dstPath + accountSasToken, "--recursive", "--check-length=false")
787788
if len(authAzcopyEnv) > 0 {
788789
cmd.Env = append(os.Environ(), authAzcopyEnv...)
789790
}
@@ -794,11 +795,11 @@ func (d *Driver) copyBlobContainer(req *csi.CreateVolumeRequest, accountSasToken
794795
}
795796
timeoutFunc := func() error {
796797
_, 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)
798+
return fmt.Errorf("timeout waiting for copy blob container %s to %s complete, current copy percent: %s%%", srcPath, dstPath, percent)
798799
}
799800
copyErr := util.WaitUntilTimeout(time.Duration(d.waitForAzCopyTimeoutMinutes)*time.Minute, execFunc, timeoutFunc)
800801
if copyErr != nil {
801-
klog.Warningf("CopyBlobContainer(%s, %s, %s) failed with error: %v", resourceGroupName, accountName, dstPath, copyErr)
802+
klog.Warningf("CopyBlobContainer(%s, %s, %s, %s) failed with error: %v", srcAccountName, srcContainerName, dstAccountName, dstContainerName, copyErr)
802803
} else {
803804
klog.V(2).Infof("copied blob container %s to %s successfully", srcContainerName, dstContainerName)
804805
}
@@ -808,13 +809,13 @@ func (d *Driver) copyBlobContainer(req *csi.CreateVolumeRequest, accountSasToken
808809
}
809810

810811
// 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 {
812+
func (d *Driver) copyVolume(req *csi.CreateVolumeRequest, accountSASToken string, authAzcopyEnv []string, dstAccountName, dstContainerName, storageEndpointSuffix string) error {
812813
vs := req.VolumeContentSource
813814
switch vs.Type.(type) {
814815
case *csi.VolumeContentSource_Snapshot:
815816
return status.Errorf(codes.InvalidArgument, "copy volume from volumeSnapshot is not supported")
816817
case *csi.VolumeContentSource_Volume:
817-
return d.copyBlobContainer(req, accountSASToken, authAzcopyEnv, dstContainerName, storageEndpointSuffix)
818+
return d.copyBlobContainer(req, accountSASToken, authAzcopyEnv, dstAccountName, dstContainerName, storageEndpointSuffix)
818819
default:
819820
return status.Errorf(codes.InvalidArgument, "%v is not a proper volume source", vs)
820821
}

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)