Skip to content

Commit e39171d

Browse files
authored
Merge pull request #1850 from k8s-infra-cherrypick-robot/cherry-pick-1849-to-release-1.25
[release-1.25] fix: cleanup azcopy jobs after job complete
2 parents 97814a5 + ebaf29f commit e39171d

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

pkg/blob/blob.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func NewDriver(options *DriverOptions, kubeClient kubernetes.Interface, cloud *p
282282
sasTokenExpirationMinutes: options.SasTokenExpirationMinutes,
283283
waitForAzCopyTimeoutMinutes: options.WaitForAzCopyTimeoutMinutes,
284284
fsGroupChangePolicy: options.FSGroupChangePolicy,
285-
azcopy: &util.Azcopy{},
285+
azcopy: &util.Azcopy{ExecCmd: &util.ExecCommand{}},
286286
KubeClient: kubeClient,
287287
cloud: cloud,
288288
}

pkg/blob/controllerserver.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ func (d *Driver) copyBlobContainer(ctx context.Context, req *csi.CreateVolumeReq
803803
jobState, percent, err := d.azcopy.GetAzcopyJob(dstContainerName, authAzcopyEnv)
804804
klog.V(2).Infof("azcopy job status: %s, copy percent: %s%%, error: %v", jobState, percent, err)
805805
switch jobState {
806-
case util.AzcopyJobError, util.AzcopyJobCompleted:
806+
case util.AzcopyJobError, util.AzcopyJobCompleted, util.AzcopyJobCompletedWithErrors, util.AzcopyJobCompletedWithSkipped, util.AzcopyJobCompletedWithErrorsAndSkipped:
807807
return err
808808
case util.AzcopyJobRunning:
809809
err = wait.PollImmediate(20*time.Second, time.Duration(d.waitForAzCopyTimeoutMinutes)*time.Minute, func() (bool, error) {
@@ -835,6 +835,9 @@ func (d *Driver) copyBlobContainer(ctx context.Context, req *csi.CreateVolumeReq
835835
klog.Warningf("CopyBlobContainer(%s, %s, %s) failed with error: %v", accountOptions.ResourceGroup, dstAccountName, dstContainerName, err)
836836
} else {
837837
klog.V(2).Infof("copied blob container %s to %s successfully", srcContainerName, dstContainerName)
838+
if out, err := d.azcopy.CleanJobs(); err != nil {
839+
klog.Warningf("clean azcopy jobs failed with error: %v, output: %s", err, string(out))
840+
}
838841
}
839842
return err
840843
}

pkg/util/util.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ const (
4545
type AzcopyJobState string
4646

4747
const (
48-
AzcopyJobError AzcopyJobState = "Error"
49-
AzcopyJobNotFound AzcopyJobState = "NotFound"
50-
AzcopyJobRunning AzcopyJobState = "Running"
51-
AzcopyJobCompleted AzcopyJobState = "Completed"
48+
AzcopyJobError AzcopyJobState = "Error"
49+
AzcopyJobNotFound AzcopyJobState = "NotFound"
50+
AzcopyJobRunning AzcopyJobState = "Running"
51+
AzcopyJobCompleted AzcopyJobState = "Completed"
52+
AzcopyJobCompletedWithErrors AzcopyJobState = "CompletedWithErrors"
53+
AzcopyJobCompletedWithSkipped AzcopyJobState = "CompletedWithSkipped"
54+
AzcopyJobCompletedWithErrorsAndSkipped AzcopyJobState = "CompletedWithErrorsAndSkipped"
5255
)
5356

5457
// RoundUpBytes rounds up the volume size in bytes up to multiplications of GiB
@@ -243,9 +246,6 @@ func (ac *Azcopy) GetAzcopyJob(dstBlobContainer string, authAzcopyEnv []string)
243246
// Start Time: Wednesday, 09-Aug-23 09:09:03 UTC
244247
// Status: Cancelled
245248
// Command: copy https://{accountName}.file.core.windows.net/{srcBlobContainer}{SAStoken} https://{accountName}.file.core.windows.net/{dstBlobContainer}{SAStoken} --recursive --check-length=false
246-
if ac.ExecCmd == nil {
247-
ac.ExecCmd = &ExecCommand{}
248-
}
249249
out, err := ac.ExecCmd.RunCommand(cmdStr, authAzcopyEnv)
250250
// if grep command returns nothing, the exec will return exit status 1 error, so filter this error
251251
if err != nil && err.Error() != "exit status 1" {
@@ -279,13 +279,8 @@ func (ac *Azcopy) GetAzcopyJob(dstBlobContainer string, authAzcopyEnv []string)
279279
return jobState, percent, nil
280280
}
281281

282-
// TestListJobs test azcopy jobs list command with authAzcopyEnv
283-
func (ac *Azcopy) TestListJobs(accountName, storageEndpointSuffix string, authAzcopyEnv []string) (string, error) {
284-
cmdStr := fmt.Sprintf("azcopy list %s", fmt.Sprintf("https://%s.blob.%s", accountName, storageEndpointSuffix))
285-
if ac.ExecCmd == nil {
286-
ac.ExecCmd = &ExecCommand{}
287-
}
288-
return ac.ExecCmd.RunCommand(cmdStr, authAzcopyEnv)
282+
func (ac *Azcopy) CleanJobs() (string, error) {
283+
return ac.ExecCmd.RunCommand("azcopy jobs clean", nil)
289284
}
290285

291286
// parseAzcopyJobList parse command azcopy jobs list, get jobid and state from joblist

pkg/util/util_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,41 @@ func TestTrimDuplicatedSpace(t *testing.T) {
346346
}
347347
}
348348

349+
func TestCleanJobs(t *testing.T) {
350+
tests := []struct {
351+
desc string
352+
execStr string
353+
execErr error
354+
expectedErr error
355+
}{
356+
{
357+
desc: "run exec get error",
358+
execStr: "",
359+
execErr: fmt.Errorf("error"),
360+
expectedErr: fmt.Errorf("error"),
361+
},
362+
{
363+
desc: "run exec succeed",
364+
execStr: "cleaned",
365+
execErr: nil,
366+
expectedErr: nil,
367+
},
368+
}
369+
for _, test := range tests {
370+
ctrl := gomock.NewController(t)
371+
defer ctrl.Finish()
372+
373+
m := NewMockEXEC(ctrl)
374+
m.EXPECT().RunCommand(gomock.Eq("azcopy jobs clean"), nil).Return(test.execStr, test.execErr)
375+
376+
azcopyFunc := &Azcopy{ExecCmd: m}
377+
_, err := azcopyFunc.CleanJobs()
378+
if !reflect.DeepEqual(err, test.expectedErr) {
379+
t.Errorf("test[%s]: unexpected err: %v, expected err: %v", test.desc, err, test.expectedErr)
380+
}
381+
}
382+
}
383+
349384
func TestGetAzcopyJob(t *testing.T) {
350385
tests := []struct {
351386
desc string

0 commit comments

Comments
 (0)