4
4
"context"
5
5
"path"
6
6
"slices"
7
- "strings"
8
7
"time"
9
8
10
9
"github.com/pkg/errors"
@@ -514,10 +513,17 @@ func finishBackup(ctx context.Context, c client.Client, pgBackup *v2.PerconaPGBa
514
513
if err != nil {
515
514
return nil , errors .Wrap (err , "get backup in progress" )
516
515
}
516
+
517
517
if runningBackup != pgBackup .Name {
518
+ // This block only runs after all finalizer operations are complete.
519
+ // Or, it runs when the user deletes a backup object that never started.
520
+ // In both cases, treat this as the function's exit point.
521
+
518
522
return nil , nil
519
523
}
520
524
525
+ // deleteAnnotation deletes the annotation from the PerconaPGCluster
526
+ // and returns true when it's deleted from crunchy PostgresCluster.
521
527
deleteAnnotation := func (annotation string ) (bool , error ) {
522
528
pgCluster := new (v1beta1.PostgresCluster )
523
529
if err := c .Get (ctx , types.NamespacedName {Name : pgBackup .Spec .PGCluster , Namespace : pgBackup .Namespace }, pgCluster ); err != nil {
@@ -555,24 +561,20 @@ func finishBackup(ctx context.Context, c client.Client, pgBackup *v2.PerconaPGBa
555
561
return nil , errors .Wrapf (err , "delete %s annotation" , naming .PGBackRestBackup )
556
562
}
557
563
if ! deleted {
558
- // We should wait until the crunchy reconciler is finished.
559
- // If we delete the job labels without waiting for the reconcile to finish, the Crunchy reconciler will
560
- // receive the pgcluster with the "naming.PGBackRestBackup" annotation, but will not find the manual backup job.
561
- // It will attempt to create a new job with the same name, failing and resulting in a scary error in the logs.
564
+ // We should wait until the annotation is deleted from crunchy cluster.
562
565
return & reconcile.Result {RequeueAfter : time .Second * 5 }, nil
563
566
}
564
567
565
568
// Remove PGBackRest labels to prevent the job from being
566
- // deleted by the cleanupRepoResources method.
569
+ // deleted by the cleanupRepoResources method and included in
570
+ // repoResources.manualBackupJobs used in reconcileManualBackup method
567
571
if job != nil {
568
572
if err := retry .RetryOnConflict (retry .DefaultBackoff , func () error {
569
573
j := new (batchv1.Job )
570
574
if err := c .Get (ctx , client .ObjectKeyFromObject (job ), j ); err != nil {
571
- if k8serrors .IsNotFound (err ) {
572
- return nil
573
- }
574
575
return errors .Wrap (err , "get job" )
575
576
}
577
+
576
578
for k := range naming .PGBackRestLabels (pgBackup .Spec .PGCluster ) {
577
579
delete (j .Labels , k )
578
580
}
@@ -583,6 +585,7 @@ func finishBackup(ctx context.Context, c client.Client, pgBackup *v2.PerconaPGBa
583
585
}
584
586
}
585
587
588
+ // We should delete the pgbackrest status to be able to recreate backups with the same name.
586
589
if err := retry .RetryOnConflict (retry .DefaultBackoff , func () error {
587
590
pgCluster := new (v1beta1.PostgresCluster )
588
591
if err := c .Get (ctx , types.NamespacedName {Name : pgBackup .Spec .PGCluster , Namespace : pgBackup .Namespace }, pgCluster ); err != nil {
@@ -595,39 +598,19 @@ func finishBackup(ctx context.Context, c client.Client, pgBackup *v2.PerconaPGBa
595
598
return nil , errors .Wrap (err , "update postgrescluster" )
596
599
}
597
600
598
- deleted , err = deleteAnnotation (pNaming .AnnotationBackupInProgress )
601
+ _ , err = deleteAnnotation (pNaming .AnnotationBackupInProgress )
599
602
if err != nil {
600
603
return nil , errors .Wrapf (err , "delete %s annotation" , pNaming .AnnotationBackupInProgress )
601
604
}
602
- if ! deleted {
603
- return & reconcile.Result {RequeueAfter : time .Second * 5 }, nil
604
- }
605
-
606
- if job != nil && checkBackupJob (job ) != v2 .BackupSucceeded {
607
- // Remove all crunchy labels to prevent the job from being included in
608
- // repoResources.manualBackupJobs used in reconcileManualBackup method.
609
- if err := retry .RetryOnConflict (retry .DefaultBackoff , func () error {
610
- j := new (batchv1.Job )
611
- if err := c .Get (ctx , client .ObjectKeyFromObject (job ), j ); err != nil {
612
- if k8serrors .IsNotFound (err ) {
613
- return nil
614
- }
615
- return errors .Wrap (err , "get job" )
616
- }
617
-
618
- for k := range j .Labels {
619
- if strings .HasPrefix (k , pNaming .PrefixCrunchy ) {
620
- delete (j .Labels , k )
621
- }
622
- }
623
-
624
- return c .Update (ctx , j )
625
- }); err != nil {
626
- return nil , errors .Wrap (err , "delete backup job labels" )
627
- }
628
- }
605
+ // Do not add any code after this comment.
606
+ //
607
+ // The code after the comment may or may not execute, depending on whether the
608
+ // crunchy cluster removes the AnnotationBackupInProgress annotation in time.
609
+ //
610
+ // Once AnnotationBackupInProgress is deleted, the successful return of finalizer must happen inside the
611
+ // `if runningBackup != pgBackup.Name { ... }` block.
629
612
630
- return nil , nil
613
+ return & reconcile. Result { RequeueAfter : time . Second * 5 } , nil
631
614
}
632
615
633
616
func startBackup (ctx context.Context , c client.Client , pb * v2.PerconaPGBackup ) error {
0 commit comments