@@ -3,6 +3,7 @@ package pgbackup
33import (
44 "context"
55 "path"
6+ "slices"
67 "strings"
78 "time"
89
@@ -131,7 +132,7 @@ func (r *PGBackupReconciler) Reconcile(ctx context.Context, request reconcile.Re
131132 }
132133
133134 // start backup only if backup job doesn't exist
134- _ , err := findBackupJob (ctx , r .Client , pgCluster , pgBackup )
135+ _ , err := findBackupJob (ctx , r .Client , pgBackup )
135136 if err != nil {
136137 if ! errors .Is (err , ErrBackupJobNotFound ) {
137138 return reconcile.Result {}, errors .Wrap (err , "find backup job" )
@@ -184,7 +185,7 @@ func (r *PGBackupReconciler) Reconcile(ctx context.Context, request reconcile.Re
184185 return reconcile.Result {}, errors .Errorf ("PostgresCluster %s is not found" , pgBackup .Spec .PGCluster )
185186 }
186187
187- job , err := findBackupJob (ctx , r .Client , pgCluster , pgBackup )
188+ job , err := findBackupJob (ctx , r .Client , pgBackup )
188189 if err != nil {
189190 if errors .Is (err , ErrBackupJobNotFound ) {
190191 log .Info ("Waiting for backup to start" )
@@ -231,6 +232,15 @@ func (r *PGBackupReconciler) Reconcile(ctx context.Context, request reconcile.Re
231232 job := & batchv1.Job {}
232233 err := r .Client .Get (ctx , types.NamespacedName {Name : pgBackup .Status .JobName , Namespace : pgBackup .Namespace }, job )
233234 if err != nil {
235+ // If something has deleted the job even with the finalizer, we should fail the backup.
236+ if k8serrors .IsNotFound (err ) {
237+ if err := updateStatus (ctx , r .Client , pgBackup , func (bcp * v2.PerconaPGBackup ) {
238+ bcp .Status .State = v2 .BackupFailed
239+ }); err != nil {
240+ return reconcile.Result {}, errors .Wrap (err , "update PGBackup status" )
241+ }
242+ return reconcile.Result {}, nil
243+ }
234244 return reconcile.Result {}, errors .Wrap (err , "get backup job" )
235245 }
236246
@@ -265,6 +275,21 @@ func (r *PGBackupReconciler) Reconcile(ctx context.Context, request reconcile.Re
265275
266276 return reconcile.Result {}, nil
267277 case v2 .BackupSucceeded :
278+ job , err := findBackupJob (ctx , r .Client , pgBackup )
279+ if err == nil && slices .Contains (job .Finalizers , pNaming .FinalizerKeepJob ) {
280+ if err := retry .RetryOnConflict (retry .DefaultBackoff , func () error {
281+ j := new (batchv1.Job )
282+ if err := r .Client .Get (ctx , client .ObjectKeyFromObject (job ), j ); err != nil {
283+ return errors .Wrap (err , "get job" )
284+ }
285+ j .Finalizers = slices .DeleteFunc (j .Finalizers , func (s string ) bool { return s == pNaming .FinalizerKeepJob })
286+
287+ return r .Client .Update (ctx , j )
288+ }); err != nil {
289+ return reconcile.Result {}, errors .Wrap (err , "update PGBackup status" )
290+ }
291+ }
292+
268293 if pgCluster == nil {
269294 return reconcile.Result {}, nil
270295 }
@@ -636,8 +661,11 @@ func startBackup(ctx context.Context, c client.Client, pb *v2.PerconaPGBackup) e
636661 return nil
637662}
638663
639- func findBackupJob (ctx context.Context , c client.Client , pg * v2.PerconaPGCluster , pb * v2.PerconaPGBackup ) (* batchv1.Job , error ) {
640- if jobName := pb .GetAnnotations ()[pNaming .AnnotationPGBackrestBackupJobName ]; jobName != "" {
664+ func findBackupJob (ctx context.Context , c client.Client , pb * v2.PerconaPGBackup ) (* batchv1.Job , error ) {
665+ if jobName := pb .GetAnnotations ()[pNaming .AnnotationPGBackrestBackupJobName ]; jobName != "" || pb .Status .JobName != "" {
666+ if jobName == "" {
667+ jobName = pb .Status .JobName
668+ }
641669 job := new (batchv1.Job )
642670 err := c .Get (ctx , types.NamespacedName {Name : jobName , Namespace : pb .Namespace }, job )
643671 if err != nil {
@@ -648,9 +676,9 @@ func findBackupJob(ctx context.Context, c client.Client, pg *v2.PerconaPGCluster
648676
649677 jobList := & batchv1.JobList {}
650678 err := c .List (ctx , jobList ,
651- client .InNamespace (pg .Namespace ),
679+ client .InNamespace (pb .Namespace ),
652680 client.MatchingLabelsSelector {
653- Selector : naming .PGBackRestBackupJobSelector (pg . Name , pb .Spec .RepoName , naming .BackupManual ),
681+ Selector : naming .PGBackRestBackupJobSelector (pb . Spec . PGCluster , pb .Spec .RepoName , naming .BackupManual ),
654682 })
655683 if err != nil {
656684 return nil , errors .Wrap (err , "get backup jobs" )
0 commit comments