mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
fix(backups): enforce the Velero deletion backoff of failed BackupJobs
The retry backoff of the failed-job sweep only set RequeueAfter. Writing the
retry counter onto the BackupJob triggers a reconcile at once, since the
BackupJob watch has no predicate. When Velero processes a request instantly,
as it does when the backup storage location is missing or read-only, that
reconcile replaced the request again, emitted another event and raised the
counter without any wait. The sweep now records when it last created a
request, in the backups.cozystack.io/velero-deletion-attempted-at annotation,
and replaces a processed request only after the backoff for the current
count has passed since then. Before that, it requeues for the remaining
time.
A request Velero never processes, because Velero is down, and a Velero
backup stuck in New or with no phase were checked at a fixed interval,
each check with an uncached list of Backups. They are now checked again
after as long as they have already waited, from the request's or the
backup's creation time, capped at one hour. Waiting emits no event.
Recording the attempt can fail after the request was created. If the job
was deleted meanwhile, the sweep ends without an error. Any other error is
logged and the computed requeue is kept, because failing the reconcile
would only retry sooner.
Unit tests drive the clock through two back-to-back reconciles of an
instantly processed request (one replacement, one event), then three
retries at 10, 20 and 40 minutes. They also check the waiting intervals for
requests and backups of different ages, and both annotation write errors.
Refs: #5355
Assisted-by: LLM
Signed-off-by: Andrey Kolkov <androndo@gmail.com>
(cherry picked from commit dbc088f862)
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
This commit is contained in:
1 parent
e8cec92626
commit
0bd46bc014
4 files changed
+243
-71
No files matched your search
@@ -234,7 +234,7 @@ func (r *BackupReconciler) cleanupVeleroBackup(ctx context.Context, backup *back
|
||||
return fmt.Errorf("failed to get Velero backup %s/%s: %w", veleroBackupNamespace, veleroBackupName, err)
|
||||
}
|
||||
|
||||
req, err := requestVeleroBackupDeletion(ctx, r.Client, veleroBackupNamespace, veleroBackupName)
|
||||
req, err := requestVeleroBackupDeletion(ctx, r.Client, veleroBackupNamespace, veleroBackupName, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -39,6 +39,9 @@ type BackupJobReconciler struct {
|
||||
// re-reads the BackupJob before dispatching to a driver. Wired in
|
||||
// SetupWithManager.
|
||||
APIReader client.Reader
|
||||
|
||||
// now is a test seam for the clock that paces Velero deletion retries.
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func (r *BackupJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||
|
||||
@@ -20,12 +20,17 @@ import (
|
||||
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
)
|
||||
|
||||
// veleroDeletionRequest is what requestVeleroBackupDeletion did.
|
||||
// veleroDeletionRequest is what requestVeleroBackupDeletion found and did.
|
||||
type veleroDeletionRequest struct {
|
||||
created bool
|
||||
// replaced is a request of ours that Velero had processed and that this
|
||||
// call deleted before creating a new one.
|
||||
replaced *velerov1.DeleteBackupRequest
|
||||
// pending is a request for the backup that Velero has not processed yet.
|
||||
pending *velerov1.DeleteBackupRequest
|
||||
// processed is a request of ours that Velero has processed and that was
|
||||
// left in place because the caller did not allow a replacement.
|
||||
processed *velerov1.DeleteBackupRequest
|
||||
}
|
||||
|
||||
// requestVeleroBackupDeletion asks Velero to delete a backup together with its
|
||||
@@ -35,13 +40,13 @@ type veleroDeletionRequest struct {
|
||||
//
|
||||
// The request is named after the backup, so repeated calls converge on one
|
||||
// request. Nothing is created while any request for the backup is
|
||||
// unprocessed. A request of ours that Velero has processed is deleted and
|
||||
// created again: the backup still exists, so the deletion failed, or the BSL
|
||||
// sync brought the backup back, and Velero keeps a processed request for a
|
||||
// day, during which its name would block a new attempt. Velero does not retry
|
||||
// a processed request, so a caller that needs the deletion to happen must
|
||||
// call again later.
|
||||
func requestVeleroBackupDeletion(ctx context.Context, c client.Client, namespace, name string) (veleroDeletionRequest, error) {
|
||||
// unprocessed. A request of ours that Velero has processed means the backup
|
||||
// survived it: the deletion failed, or the BSL sync brought the backup back.
|
||||
// With replace set, such a request is deleted and created again, because
|
||||
// Velero keeps a processed request for a day and its name would block a new
|
||||
// attempt until then. Velero does not retry a processed request, so a caller
|
||||
// that needs the deletion to happen must call again later.
|
||||
func requestVeleroBackupDeletion(ctx context.Context, c client.Client, namespace, name string, replace bool) (veleroDeletionRequest, error) {
|
||||
var res veleroDeletionRequest
|
||||
requests := &velerov1.DeleteBackupRequestList{}
|
||||
if err := c.List(ctx, requests, client.InNamespace(namespace)); err != nil {
|
||||
@@ -54,6 +59,7 @@ func requestVeleroBackupDeletion(ctx context.Context, c client.Client, namespace
|
||||
continue
|
||||
}
|
||||
if dbr.Status.Phase != velerov1.DeleteBackupRequestPhaseProcessed {
|
||||
res.pending = dbr
|
||||
return res, nil
|
||||
}
|
||||
if dbr.Name == name {
|
||||
@@ -61,6 +67,10 @@ func requestVeleroBackupDeletion(ctx context.Context, c client.Client, namespace
|
||||
}
|
||||
}
|
||||
if own != nil {
|
||||
if !replace {
|
||||
res.processed = own
|
||||
return res, nil
|
||||
}
|
||||
// The UID precondition keeps a stale cache from deleting a request
|
||||
// that has already been replaced.
|
||||
err := c.Delete(ctx, own, client.Preconditions{UID: &own.UID})
|
||||
@@ -93,22 +103,28 @@ func requestVeleroBackupDeletion(ctx context.Context, c client.Client, namespace
|
||||
}
|
||||
|
||||
const (
|
||||
// failedJobSweepRequeue is how often a Failed BackupJob is reconciled
|
||||
// again while one of its Velero backups still waits for deletion. Nothing
|
||||
// watches DeleteBackupRequests, so a request Velero processed is only
|
||||
// noticed, and replaced, on this requeue. Every replacement doubles it, up
|
||||
// to failedJobSweepMaxRequeue, so a deletion Velero can never carry out
|
||||
// (a missing, read-only or unavailable BSL) is retried rarely.
|
||||
// failedJobSweepRequeue is the first interval at which a Failed
|
||||
// BackupJob is reconciled again while one of its Velero backups waits
|
||||
// for deletion; failedJobSweepMaxRequeue caps every interval below.
|
||||
// Nothing watches DeleteBackupRequests, so a request Velero processed is
|
||||
// only noticed on such a requeue. A processed request is replaced once
|
||||
// failedJobSweepBackoff(retries) has passed since the last attempt, so a
|
||||
// deletion Velero can never carry out (a missing, read-only or
|
||||
// unavailable BSL) is attempted at most once an hour, and the annotation
|
||||
// writes that record each attempt cannot cut the wait short.
|
||||
failedJobSweepRequeue = 5 * time.Minute
|
||||
failedJobSweepMaxRequeue = time.Hour
|
||||
// failedJobRunningBackupRequeue is how often a Failed BackupJob is
|
||||
// reconciled again while one of its Velero backups is still running.
|
||||
// Velero rejects a deletion request for a running backup.
|
||||
// failedJobRunningBackupRequeue is the first interval at which a Failed
|
||||
// BackupJob is reconciled again while one of its Velero backups is still
|
||||
// running. Velero rejects a deletion request for a running backup.
|
||||
failedJobRunningBackupRequeue = time.Minute
|
||||
|
||||
// veleroDeletionRetriesAnnotation counts, on a Failed BackupJob, the
|
||||
// deletion requests that Velero processed while the backup survived.
|
||||
veleroDeletionRetriesAnnotation = "backups.cozystack.io/velero-deletion-retries"
|
||||
// veleroDeletionAttemptAnnotation is when the sweep last created a
|
||||
// deletion request for a Failed BackupJob, in RFC 3339.
|
||||
veleroDeletionAttemptAnnotation = "backups.cozystack.io/velero-deletion-attempted-at"
|
||||
)
|
||||
|
||||
// deleteFailedJobVeleroBackups requests deletion of the failed Velero backups
|
||||
@@ -137,9 +153,15 @@ func (r *BackupJobReconciler) deleteFailedJobVeleroBackups(ctx context.Context,
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
now := r.clock()
|
||||
retries := veleroDeletionRetries(j)
|
||||
lastAttempt := veleroDeletionAttempt(j)
|
||||
nextAttempt := lastAttempt.Add(failedJobSweepBackoff(retries))
|
||||
replaceDue := lastAttempt.IsZero() || !now.Before(nextAttempt)
|
||||
|
||||
var referenced map[client.ObjectKey]bool
|
||||
var requeueAfter time.Duration
|
||||
var waiting, replaced bool
|
||||
var attempted, replaced bool
|
||||
for i := range veleroBackups.Items {
|
||||
vb := &veleroBackups.Items[i]
|
||||
switch vb.Status.Phase {
|
||||
@@ -155,7 +177,8 @@ func (r *BackupJobReconciler) deleteFailedJobVeleroBackups(ctx context.Context,
|
||||
// that no Backup references stays until Velero's TTL expires it.
|
||||
continue
|
||||
default:
|
||||
requeueAfter = shorterRequeue(requeueAfter, failedJobRunningBackupRequeue)
|
||||
requeueAfter = shorterRequeue(requeueAfter,
|
||||
waitingRequeue(now, vb.CreationTimestamp.Time, failedJobRunningBackupRequeue))
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -169,36 +192,60 @@ func (r *BackupJobReconciler) deleteFailedJobVeleroBackups(ctx context.Context,
|
||||
continue
|
||||
}
|
||||
|
||||
req, err := requestVeleroBackupDeletion(ctx, r.Client, vb.Namespace, vb.Name)
|
||||
req, err := requestVeleroBackupDeletion(ctx, r.Client, vb.Namespace, vb.Name, replaceDue)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
waiting = true
|
||||
if req.replaced != nil {
|
||||
replaced = true
|
||||
r.warnVeleroDeletionRetried(j, vb.Name, req.replaced)
|
||||
} else if req.created {
|
||||
logger.Info("requested deletion of the Velero Backup of a failed BackupJob",
|
||||
"backupjob", j.Name, "namespace", j.Namespace, "veleroBackup", vb.Name)
|
||||
switch {
|
||||
case req.pending != nil:
|
||||
requeueAfter = shorterRequeue(requeueAfter,
|
||||
waitingRequeue(now, req.pending.CreationTimestamp.Time, failedJobSweepRequeue))
|
||||
case req.processed != nil:
|
||||
requeueAfter = shorterRequeue(requeueAfter, nextAttempt.Sub(now))
|
||||
case req.created:
|
||||
attempted = true
|
||||
if req.replaced != nil {
|
||||
replaced = true
|
||||
r.warnVeleroDeletionRetried(j, vb.Name, req.replaced)
|
||||
} else {
|
||||
logger.Info("requested deletion of the Velero Backup of a failed BackupJob",
|
||||
"backupjob", j.Name, "namespace", j.Namespace, "veleroBackup", vb.Name)
|
||||
}
|
||||
default:
|
||||
requeueAfter = shorterRequeue(requeueAfter, failedJobSweepRequeue)
|
||||
}
|
||||
}
|
||||
if !waiting {
|
||||
if !attempted {
|
||||
return ctrl.Result{RequeueAfter: requeueAfter}, nil
|
||||
}
|
||||
|
||||
retries := veleroDeletionRetries(j)
|
||||
if replaced {
|
||||
retries++
|
||||
patch := client.MergeFrom(j.DeepCopy())
|
||||
if j.Annotations == nil {
|
||||
j.Annotations = map[string]string{}
|
||||
}
|
||||
j.Annotations[veleroDeletionRetriesAnnotation] = strconv.Itoa(retries)
|
||||
if err := r.Patch(ctx, j, patch); err != nil {
|
||||
return ctrl.Result{}, fmt.Errorf("failed to record Velero deletion retries on BackupJob %s/%s: %w", j.Namespace, j.Name, err)
|
||||
}
|
||||
}
|
||||
return ctrl.Result{RequeueAfter: shorterRequeue(requeueAfter, failedJobSweepBackoff(retries))}, nil
|
||||
requeueAfter = shorterRequeue(requeueAfter, failedJobSweepBackoff(retries))
|
||||
patch := client.MergeFrom(j.DeepCopy())
|
||||
if j.Annotations == nil {
|
||||
j.Annotations = map[string]string{}
|
||||
}
|
||||
j.Annotations[veleroDeletionRetriesAnnotation] = strconv.Itoa(retries)
|
||||
j.Annotations[veleroDeletionAttemptAnnotation] = now.UTC().Format(time.RFC3339)
|
||||
if err := r.Patch(ctx, j, patch); err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
// The request is already in place; without the record the next
|
||||
// attempt only comes sooner than the backoff asks for.
|
||||
logger.Error(err, "failed to record the Velero deletion attempt on the BackupJob",
|
||||
"backupjob", j.Name, "namespace", j.Namespace)
|
||||
}
|
||||
return ctrl.Result{RequeueAfter: requeueAfter}, nil
|
||||
}
|
||||
|
||||
func (r *BackupJobReconciler) clock() time.Time {
|
||||
if r.now != nil {
|
||||
return r.now()
|
||||
}
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
func (r *BackupJobReconciler) warnVeleroDeletionRetried(j *backupsv1alpha1.BackupJob, veleroBackup string, processed *velerov1.DeleteBackupRequest) {
|
||||
@@ -222,6 +269,14 @@ func veleroDeletionRetries(j *backupsv1alpha1.BackupJob) int {
|
||||
return n
|
||||
}
|
||||
|
||||
func veleroDeletionAttempt(j *backupsv1alpha1.BackupJob) time.Time {
|
||||
t, err := time.Parse(time.RFC3339, j.Annotations[veleroDeletionAttemptAnnotation])
|
||||
if err != nil {
|
||||
return time.Time{}
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func failedJobSweepBackoff(retries int) time.Duration {
|
||||
d := failedJobSweepRequeue
|
||||
for i := 0; i < retries && d < failedJobSweepMaxRequeue; i++ {
|
||||
@@ -230,6 +285,17 @@ func failedJobSweepBackoff(retries int) time.Duration {
|
||||
return min(d, failedJobSweepMaxRequeue)
|
||||
}
|
||||
|
||||
// waitingRequeue paces the checks on something Velero has not acted on yet:
|
||||
// the next check comes after as long as it has already waited, between floor
|
||||
// and failedJobSweepMaxRequeue, so a Velero that is down is not polled at a
|
||||
// fixed rate.
|
||||
func waitingRequeue(now, since time.Time, floor time.Duration) time.Duration {
|
||||
if since.IsZero() {
|
||||
return floor
|
||||
}
|
||||
return min(max(now.Sub(since), floor), failedJobSweepMaxRequeue)
|
||||
}
|
||||
|
||||
func shorterRequeue(current, next time.Duration) time.Duration {
|
||||
if current == 0 || next < current {
|
||||
return next
|
||||
|
||||
@@ -3,6 +3,7 @@ package backupcontroller
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -384,57 +385,159 @@ func TestReconcile_FailedJobVeleroBackupSweep(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A deletion Velero keeps failing is retried with a growing interval, and
|
||||
// every failed attempt is reported on the BackupJob with Velero's errors.
|
||||
func markOwnRequestProcessed(t *testing.T, c client.Client, errs ...string) {
|
||||
t.Helper()
|
||||
dbr := &velerov1.DeleteBackupRequest{}
|
||||
if err := c.Get(context.Background(), types.NamespacedName{Namespace: veleroNamespace, Name: failedVeleroBackup}, dbr); err != nil {
|
||||
t.Fatalf("get request: %v", err)
|
||||
}
|
||||
dbr.Status = velerov1.DeleteBackupRequestStatus{Phase: velerov1.DeleteBackupRequestPhaseProcessed, Errors: errs}
|
||||
if err := c.Update(context.Background(), dbr); err != nil {
|
||||
t.Fatalf("mark request processed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A deletion Velero keeps failing at once is retried only after a growing
|
||||
// wait, even though recording each attempt triggers a new reconcile, and
|
||||
// every retry is reported on the BackupJob with Velero's errors.
|
||||
func TestReconcile_FailedJobVeleroDeletionBacksOff(t *testing.T) {
|
||||
const readOnly = "backup storage location default is currently in read-only mode"
|
||||
r, c := newVeleroCleanupReconciler(t,
|
||||
veleroCleanupJob(backupsv1alpha1.BackupJobPhaseFailed),
|
||||
ownedVeleroBackup(failedVeleroBackup, velerov1.BackupPhaseFailed))
|
||||
now := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
r.now = func() time.Time { return now }
|
||||
events := r.Recorder.(*record.FakeRecorder).Events
|
||||
expectEvent := func(step string, want bool) {
|
||||
t.Helper()
|
||||
select {
|
||||
case ev := <-events:
|
||||
if !want {
|
||||
t.Fatalf("%s: unexpected event %q", step, ev)
|
||||
}
|
||||
if !strings.Contains(ev, "Warning VeleroBackupDeletionFailed") || !strings.Contains(ev, readOnly) {
|
||||
t.Errorf("%s: unexpected event %q", step, ev)
|
||||
}
|
||||
default:
|
||||
if want {
|
||||
t.Fatalf("%s: no Warning event", step)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if res := reconcileCleanupJob(t, r); res.RequeueAfter != failedJobSweepRequeue {
|
||||
t.Fatalf("first request: RequeueAfter = %v, want %v", res.RequeueAfter, failedJobSweepRequeue)
|
||||
}
|
||||
for i, want := range []time.Duration{10 * time.Minute, 20 * time.Minute, 40 * time.Minute} {
|
||||
dbr := &velerov1.DeleteBackupRequest{}
|
||||
if err := c.Get(context.Background(), types.NamespacedName{Namespace: veleroNamespace, Name: failedVeleroBackup}, dbr); err != nil {
|
||||
t.Fatalf("get request: %v", err)
|
||||
}
|
||||
dbr.Status = velerov1.DeleteBackupRequestStatus{
|
||||
Phase: velerov1.DeleteBackupRequestPhaseProcessed,
|
||||
Errors: []string{"backup storage location default is currently in read-only mode"},
|
||||
}
|
||||
if err := c.Update(context.Background(), dbr); err != nil {
|
||||
t.Fatalf("mark request processed: %v", err)
|
||||
}
|
||||
expectEvent("first request", false)
|
||||
|
||||
if res := reconcileCleanupJob(t, r); res.RequeueAfter != want {
|
||||
t.Errorf("retry %d: RequeueAfter = %v, want %v", i+1, res.RequeueAfter, want)
|
||||
markOwnRequestProcessed(t, c, readOnly)
|
||||
if res := reconcileCleanupJob(t, r); res.RequeueAfter != failedJobSweepRequeue {
|
||||
t.Errorf("before the backoff: RequeueAfter = %v, want %v", res.RequeueAfter, failedJobSweepRequeue)
|
||||
}
|
||||
expectEvent("before the backoff", false)
|
||||
|
||||
for i, wait := range []time.Duration{5 * time.Minute, 10 * time.Minute, 20 * time.Minute} {
|
||||
now = now.Add(wait)
|
||||
step := fmt.Sprintf("retry %d", i+1)
|
||||
if res := reconcileCleanupJob(t, r); res.RequeueAfter != 2*wait {
|
||||
t.Errorf("%s: RequeueAfter = %v, want %v", step, res.RequeueAfter, 2*wait)
|
||||
}
|
||||
select {
|
||||
case ev := <-events:
|
||||
if !strings.Contains(ev, "Warning VeleroBackupDeletionFailed") || !strings.Contains(ev, "read-only mode") {
|
||||
t.Errorf("retry %d: unexpected event %q", i+1, ev)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("retry %d: no Warning event", i+1)
|
||||
expectEvent(step, true)
|
||||
|
||||
markOwnRequestProcessed(t, c, readOnly)
|
||||
if res := reconcileCleanupJob(t, r); res.RequeueAfter != 2*wait {
|
||||
t.Errorf("%s, reconcile right after: RequeueAfter = %v, want %v", step, res.RequeueAfter, 2*wait)
|
||||
}
|
||||
expectEvent(step+", reconcile right after", false)
|
||||
if n := len(listDeleteBackupRequests(t, c)); n != 1 {
|
||||
t.Fatalf("retry %d: expected one request, got %d", i+1, n)
|
||||
t.Fatalf("%s: expected one request, got %d", step, n)
|
||||
}
|
||||
}
|
||||
if res := reconcileCleanupJob(t, r); res.RequeueAfter != 40*time.Minute {
|
||||
t.Errorf("a pending request must keep the backoff, got %v", res.RequeueAfter)
|
||||
}
|
||||
if len(events) != 0 {
|
||||
t.Errorf("a pending request must not emit an event, got %q", <-events)
|
||||
}
|
||||
|
||||
if got := failedJobSweepBackoff(10); got != failedJobSweepMaxRequeue {
|
||||
t.Errorf("backoff after 10 retries = %v, want the %v cap", got, failedJobSweepMaxRequeue)
|
||||
}
|
||||
}
|
||||
|
||||
// Requests and backups Velero has not acted on yet are checked less often the
|
||||
// longer they wait, and waiting is not reported.
|
||||
func TestReconcile_FailedJobWaitingOnVeleroBacksOff(t *testing.T) {
|
||||
now := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
|
||||
cases := []struct {
|
||||
name string
|
||||
veleroPhase velerov1.BackupPhase
|
||||
existing []client.Object
|
||||
backupAge time.Duration
|
||||
want time.Duration
|
||||
}{
|
||||
{name: "request waiting 20m", veleroPhase: velerov1.BackupPhaseFailed, want: 20 * time.Minute,
|
||||
existing: []client.Object{agedRequest(now.Add(-20 * time.Minute))}},
|
||||
{name: "request waiting 3h", veleroPhase: velerov1.BackupPhaseFailed, want: failedJobSweepMaxRequeue,
|
||||
existing: []client.Object{agedRequest(now.Add(-3 * time.Hour))}},
|
||||
{name: "request just created", veleroPhase: velerov1.BackupPhaseFailed, want: failedJobSweepRequeue,
|
||||
existing: []client.Object{agedRequest(now)}},
|
||||
{name: "backup new for 3h", veleroPhase: velerov1.BackupPhaseNew, backupAge: 3 * time.Hour, want: failedJobSweepMaxRequeue},
|
||||
{name: "backup new for 10m", veleroPhase: "", backupAge: 10 * time.Minute, want: 10 * time.Minute},
|
||||
{name: "backup just started", veleroPhase: velerov1.BackupPhaseInProgress, want: failedJobRunningBackupRequeue},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
vb := ownedVeleroBackup(failedVeleroBackup, tc.veleroPhase)
|
||||
vb.CreationTimestamp = metav1.NewTime(now.Add(-tc.backupAge))
|
||||
r, c := newVeleroCleanupReconciler(t, append([]client.Object{veleroCleanupJob(backupsv1alpha1.BackupJobPhaseFailed), vb}, tc.existing...)...)
|
||||
r.now = func() time.Time { return now }
|
||||
|
||||
if res := reconcileCleanupJob(t, r); res.RequeueAfter != tc.want {
|
||||
t.Errorf("RequeueAfter = %v, want %v", res.RequeueAfter, tc.want)
|
||||
}
|
||||
if n := len(listDeleteBackupRequests(t, c)); n != len(tc.existing) {
|
||||
t.Errorf("waiting must not create requests, got %d", n)
|
||||
}
|
||||
if events := r.Recorder.(*record.FakeRecorder).Events; len(events) != 0 {
|
||||
t.Errorf("waiting must not be reported, got %q", <-events)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func agedRequest(created time.Time) *velerov1.DeleteBackupRequest {
|
||||
dbr := deleteRequest(failedVeleroBackup, velerov1.DeleteBackupRequestPhaseInProgress)
|
||||
dbr.CreationTimestamp = metav1.NewTime(created)
|
||||
return dbr
|
||||
}
|
||||
|
||||
// Recording the attempt can fail after the request was created: a job deleted
|
||||
// meanwhile ends the sweep, any other error still keeps the backoff requeue.
|
||||
func TestReconcile_FailedJobAttemptRecordErrors(t *testing.T) {
|
||||
for name, patchErr := range map[string]error{
|
||||
"job deleted": apierrors.NewNotFound(schema.GroupResource{Group: "backups.cozystack.io", Resource: "backupjobs"}, failedJobName),
|
||||
"apiserver": apierrors.NewServiceUnavailable("etcd timeout"),
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
funcs := &interceptor.Funcs{
|
||||
Patch: func(context.Context, client.WithWatch, client.Object, client.Patch, ...client.PatchOption) error {
|
||||
return patchErr
|
||||
},
|
||||
}
|
||||
c := newVeleroCleanupClient(t, funcs,
|
||||
veleroCleanupJob(backupsv1alpha1.BackupJobPhaseFailed),
|
||||
ownedVeleroBackup(failedVeleroBackup, velerov1.BackupPhaseFailed))
|
||||
r := &BackupJobReconciler{Client: c, Recorder: record.NewFakeRecorder(16)}
|
||||
|
||||
want := failedJobSweepRequeue
|
||||
if apierrors.IsNotFound(patchErr) {
|
||||
want = 0
|
||||
}
|
||||
if res := reconcileCleanupJob(t, r); res.RequeueAfter != want {
|
||||
t.Errorf("RequeueAfter = %v, want %v", res.RequeueAfter, want)
|
||||
}
|
||||
if n := len(listDeleteBackupRequests(t, c)); n != 1 {
|
||||
t.Errorf("expected the request to stay, got %d", n)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcile_FailedJobWithoutVeleroCRDs(t *testing.T) {
|
||||
funcs := &interceptor.Funcs{
|
||||
List: func(ctx context.Context, c client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
|
||||
@@ -465,7 +568,7 @@ func TestRequestVeleroBackupDeletion_ReplacesByUID(t *testing.T) {
|
||||
},
|
||||
}
|
||||
c := newVeleroCleanupClient(t, funcs, stale)
|
||||
req, err := requestVeleroBackupDeletion(context.Background(), c, veleroNamespace, failedVeleroBackup)
|
||||
req, err := requestVeleroBackupDeletion(context.Background(), c, veleroNamespace, failedVeleroBackup, true)
|
||||
if err != nil || !req.created || req.replaced == nil {
|
||||
t.Fatalf("requestVeleroBackupDeletion = (%+v, %v), want a replaced and a created request", req, err)
|
||||
}
|
||||
@@ -491,7 +594,7 @@ func TestRequestVeleroBackupDeletion_ReturnsAPIErrors(t *testing.T) {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
c := newVeleroCleanupClient(t, &funcs,
|
||||
deleteRequest(failedVeleroBackup, velerov1.DeleteBackupRequestPhaseProcessed, "backup storage location unavailable"))
|
||||
req, err := requestVeleroBackupDeletion(context.Background(), c, veleroNamespace, failedVeleroBackup)
|
||||
req, err := requestVeleroBackupDeletion(context.Background(), c, veleroNamespace, failedVeleroBackup, true)
|
||||
if req.created || !apierrors.IsForbidden(err) {
|
||||
t.Fatalf("requestVeleroBackupDeletion = (%+v, %v), want a Forbidden error", req, err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user