@@ -24,6 +24,7 @@ import (
2424 . "github.com/onsi/gomega"
2525
2626 batchv1 "k8s.io/api/batch/v1"
27+ "k8s.io/utils/pointer"
2728 "sigs.k8s.io/controller-runtime/pkg/client"
2829
2930 dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
@@ -60,6 +61,7 @@ var _ = Describe("Backup Deleter Test", func() {
6061 inNS := client .InNamespace (testCtx .DefaultNamespace )
6162 testapps .ClearResourcesWithRemoveFinalizerOption (& testCtx , generics .BackupSignature , true , inNS )
6263 testapps .ClearResourcesWithRemoveFinalizerOption (& testCtx , generics .JobSignature , true , inNS )
64+ testapps .ClearResourcesWithRemoveFinalizerOption (& testCtx , generics .PersistentVolumeClaimSignature , true , inNS )
6365 testapps .ClearResources (& testCtx , generics .VolumeSnapshotSignature , inNS )
6466 }
6567
@@ -107,6 +109,38 @@ var _ = Describe("Backup Deleter Test", func() {
107109 Expect (status ).Should (Equal (DeletionStatusSucceeded ))
108110 })
109111
112+ It ("should ensure worker service account only when creating a deletion job" , func () {
113+ ensureWorkerServiceAccountCalls := 0
114+ deleter .EnsureWorkerServiceAccount = func () (string , error ) {
115+ ensureWorkerServiceAccountCalls ++
116+ return "worker-sa" , nil
117+ }
118+
119+ By ("skipping worker service account creation for snapshot backups" )
120+ backup .Status .BackupMethod = & dpv1alpha1.BackupMethod {
121+ SnapshotVolumes : pointer .Bool (true ),
122+ }
123+ status , err := deleter .DeleteBackupFiles (backup )
124+ Expect (err ).ShouldNot (HaveOccurred ())
125+ Expect (status ).Should (Equal (DeletionStatusSucceeded ))
126+ Expect (ensureWorkerServiceAccountCalls ).Should (Equal (0 ))
127+
128+ By ("creating worker service account when a deletion job is needed" )
129+ backup .Status .BackupMethod = nil
130+ backupRepoPVC := testdp .NewFakePVC (& testCtx , backupRepoPVCName )
131+ backup .Status .PersistentVolumeClaimName = backupRepoPVC .Name
132+ backup .Status .Path = backupPath
133+ status , err = deleter .DeleteBackupFiles (backup )
134+ Expect (err ).ShouldNot (HaveOccurred ())
135+ Expect (status ).Should (Equal (DeletionStatusDeleting ))
136+ Expect (ensureWorkerServiceAccountCalls ).Should (Equal (1 ))
137+
138+ key := BuildDeleteBackupFilesJobKey (backup , false )
139+ Eventually (testapps .CheckObj (& testCtx , key , func (g Gomega , fetched * batchv1.Job ) {
140+ g .Expect (fetched .Spec .Template .Spec .ServiceAccountName ).Should (Equal ("worker-sa" ))
141+ })).Should (Succeed ())
142+ })
143+
110144 It ("should create job to delete backup file" , func () {
111145 By ("mock backup repo PVC" )
112146 backupRepoPVC := testdp .NewFakePVC (& testCtx , backupRepoPVCName )
0 commit comments