mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
fix(backups): retry stuck Velero deletions of failed BackupJobs and report them
Velero marks a backup Deleting before it removes the data and leaves it in that phase when removing the data fails, with the deletion request processed with errors. The failed-job sweep skipped Deleting backups and did not requeue for them, so their data stayed in the bucket until Velero's TTL. Deleting backups now go through the same request path: a pending request is left alone, and a processed one of ours is replaced. A request of ours that Velero processed without errors is also replaced when the backup still exists, for example after the BSL sync brought it back. Otherwise the request name blocked a new attempt until Velero garbage-collects processed requests a day later. When Velero can never carry out the deletion, because the backup storage location is missing, read-only or unavailable, the sweep replaced the request every 5 minutes without any sign of it. Each replacement now emits a VeleroBackupDeletionFailed Warning event on the BackupJob with Velero's status.errors, and doubles the requeue interval from 5 minutes up to 1 hour. The count is kept in the backups.cozystack.io/velero-deletion-retries annotation, because the replaced request is gone and a requeue carries no state. The controller's ClusterRole already allows patching BackupJobs. Unit tests cover a Deleting backup with a pending request and with an errored one, a cleanly processed request whose backup still exists, an errored request under a foreign name that is left alone, three consecutive failed deletions (three events, 10m, 20m and 40m requeues, the 1h cap), and a cluster without the Velero CRDs. Refs: #5355 Assisted-by: LLM Signed-off-by: Andrey Kolkov <androndo@gmail.com>
This commit is contained in:
1 parent
887453c008
commit
73df99b77d
4 files changed
+273
-92
No files matched your search
@@ -248,11 +248,11 @@ func (r *BackupReconciler) cleanupVeleroBackup(ctx context.Context, backup *back
|
||||
return fmt.Errorf("failed to get Velero backup %s/%s: %w", veleroBackupNamespace, veleroBackupName, err)
|
||||
}
|
||||
|
||||
created, err := requestVeleroBackupDeletion(ctx, r.Client, veleroBackupNamespace, veleroBackupName)
|
||||
req, err := requestVeleroBackupDeletion(ctx, r.Client, veleroBackupNamespace, veleroBackupName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if created {
|
||||
if req.created {
|
||||
logger.Info("created DeleteBackupRequest for Velero backup",
|
||||
"name", veleroBackupName, "namespace", veleroBackupNamespace)
|
||||
} else {
|
||||
|
||||
@@ -60,11 +60,12 @@ func (r *BackupJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
// Skip terminal BackupJobs: a Succeeded/Failed run must not keep
|
||||
// projecting Secrets or re-running dispatch on every requeue, which
|
||||
// would otherwise materialise cozy-backups-creds in tenant namespaces
|
||||
// long after the BackupJob is done and pin needless work on the
|
||||
// apiserver.
|
||||
// A terminal BackupJob never reaches credentials projection or strategy
|
||||
// dispatch: a finished run must not keep projecting Secrets or
|
||||
// re-running dispatch on every requeue, which would otherwise
|
||||
// materialise cozy-backups-creds in tenant namespaces long after the
|
||||
// BackupJob is done and pin needless work on the apiserver. A Failed
|
||||
// one only has the failed Velero backups it left behind deleted.
|
||||
if j.Status.Phase == backupsv1alpha1.BackupJobPhaseFailed {
|
||||
logger.V(1).Info("BackupJob already terminal, cleaning up its Velero Backups", "phase", j.Status.Phase)
|
||||
return r.deleteFailedJobVeleroBackups(ctx, j)
|
||||
|
||||
@@ -3,8 +3,12 @@ package backupcontroller
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -16,42 +20,56 @@ import (
|
||||
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
)
|
||||
|
||||
// veleroDeletionRequest is what requestVeleroBackupDeletion 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
|
||||
}
|
||||
|
||||
// requestVeleroBackupDeletion asks Velero to delete a backup together with its
|
||||
// data in the object store. Deleting the backup.velero.io object directly only
|
||||
// removes the Kubernetes object, and the BSL sync recreates it from the object
|
||||
// store.
|
||||
// store. Callers call it only for a backup that still exists.
|
||||
//
|
||||
// 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,
|
||||
// or once one was processed without errors (Velero then removes the backup and
|
||||
// the remaining requests). A request of ours that Velero processed with errors
|
||||
// is deleted and created again: Velero keeps it for a day, and until then 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) (bool, error) {
|
||||
// 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) {
|
||||
var res veleroDeletionRequest
|
||||
requests := &velerov1.DeleteBackupRequestList{}
|
||||
if err := c.List(ctx, requests, client.InNamespace(namespace)); err != nil {
|
||||
return false, fmt.Errorf("failed to list DeleteBackupRequests in %s: %w", namespace, err)
|
||||
return res, fmt.Errorf("failed to list DeleteBackupRequests in %s: %w", namespace, err)
|
||||
}
|
||||
var failed *velerov1.DeleteBackupRequest
|
||||
var own *velerov1.DeleteBackupRequest
|
||||
for i := range requests.Items {
|
||||
dbr := &requests.Items[i]
|
||||
if dbr.Spec.BackupName != name {
|
||||
continue
|
||||
}
|
||||
if dbr.Status.Phase != velerov1.DeleteBackupRequestPhaseProcessed || len(dbr.Status.Errors) == 0 {
|
||||
return false, nil
|
||||
if dbr.Status.Phase != velerov1.DeleteBackupRequestPhaseProcessed {
|
||||
return res, nil
|
||||
}
|
||||
if dbr.Name == name {
|
||||
failed = dbr
|
||||
own = dbr
|
||||
}
|
||||
}
|
||||
if failed != nil {
|
||||
if own != nil {
|
||||
// The UID precondition keeps a stale cache from deleting a request
|
||||
// that has already been replaced.
|
||||
err := c.Delete(ctx, failed, client.Preconditions{UID: &failed.UID})
|
||||
if err != nil && !apierrors.IsNotFound(err) && !apierrors.IsConflict(err) {
|
||||
return false, fmt.Errorf("failed to delete failed DeleteBackupRequest %s/%s: %w", namespace, name, err)
|
||||
err := c.Delete(ctx, own, client.Preconditions{UID: &own.UID})
|
||||
switch {
|
||||
case err == nil:
|
||||
res.replaced = own
|
||||
case apierrors.IsNotFound(err), apierrors.IsConflict(err):
|
||||
default:
|
||||
return res, fmt.Errorf("failed to delete processed DeleteBackupRequest %s/%s: %w", namespace, name, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,23 +84,31 @@ func requestVeleroBackupDeletion(ctx context.Context, c client.Client, namespace
|
||||
}
|
||||
if err := c.Create(ctx, dbr); err != nil {
|
||||
if apierrors.IsAlreadyExists(err) {
|
||||
return false, nil
|
||||
return res, nil
|
||||
}
|
||||
return false, fmt.Errorf("failed to create DeleteBackupRequest for %s/%s: %w", namespace, name, err)
|
||||
return res, fmt.Errorf("failed to create DeleteBackupRequest for %s/%s: %w", namespace, name, err)
|
||||
}
|
||||
return true, nil
|
||||
res.created = true
|
||||
return res, nil
|
||||
}
|
||||
|
||||
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 with errors
|
||||
// is only noticed, and replaced, on this requeue.
|
||||
failedJobSweepRequeue = 5 * time.Minute
|
||||
// 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 = 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 = 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"
|
||||
)
|
||||
|
||||
// deleteFailedJobVeleroBackups requests deletion of the failed Velero backups
|
||||
@@ -113,15 +139,20 @@ func (r *BackupJobReconciler) deleteFailedJobVeleroBackups(ctx context.Context,
|
||||
|
||||
var referenced map[client.ObjectKey]bool
|
||||
var requeueAfter time.Duration
|
||||
var waiting, replaced bool
|
||||
for i := range veleroBackups.Items {
|
||||
vb := &veleroBackups.Items[i]
|
||||
switch vb.Status.Phase {
|
||||
case velerov1.BackupPhasePartiallyFailed, velerov1.BackupPhaseFailed, velerov1.BackupPhaseFailedValidation:
|
||||
case velerov1.BackupPhaseCompleted, velerov1.BackupPhaseDeleting:
|
||||
case velerov1.BackupPhasePartiallyFailed, velerov1.BackupPhaseFailed, velerov1.BackupPhaseFailedValidation,
|
||||
// Velero marks the backup Deleting before it removes the data and
|
||||
// leaves it so when that fails, so the request may need replacing.
|
||||
velerov1.BackupPhaseDeleting:
|
||||
case velerov1.BackupPhaseCompleted:
|
||||
// A job can fail after Velero completed, when creating or
|
||||
// recording its Backup fails. That Velero backup may be the only
|
||||
// copy of good data, and a Backup that already references it
|
||||
// deletes it itself, so a job-level failure never removes it.
|
||||
// copy of good data, so a job-level failure never removes it: a
|
||||
// Backup that references it deletes it with the Backup, and one
|
||||
// that no Backup references stays until Velero's TTL expires it.
|
||||
continue
|
||||
default:
|
||||
requeueAfter = shorterRequeue(requeueAfter, failedJobRunningBackupRequeue)
|
||||
@@ -138,17 +169,65 @@ func (r *BackupJobReconciler) deleteFailedJobVeleroBackups(ctx context.Context,
|
||||
continue
|
||||
}
|
||||
|
||||
created, err := requestVeleroBackupDeletion(ctx, r.Client, vb.Namespace, vb.Name)
|
||||
req, err := requestVeleroBackupDeletion(ctx, r.Client, vb.Namespace, vb.Name)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
if created {
|
||||
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)
|
||||
}
|
||||
requeueAfter = shorterRequeue(requeueAfter, failedJobSweepRequeue)
|
||||
}
|
||||
return ctrl.Result{RequeueAfter: requeueAfter}, nil
|
||||
if !waiting {
|
||||
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
|
||||
}
|
||||
|
||||
func (r *BackupJobReconciler) warnVeleroDeletionRetried(j *backupsv1alpha1.BackupJob, veleroBackup string, processed *velerov1.DeleteBackupRequest) {
|
||||
if r.Recorder == nil {
|
||||
return
|
||||
}
|
||||
reason := "Velero reported no error, but the backup still exists"
|
||||
if len(processed.Status.Errors) > 0 {
|
||||
reason = strings.Join(processed.Status.Errors, "; ")
|
||||
}
|
||||
r.Recorder.Eventf(j, corev1.EventTypeWarning, "VeleroBackupDeletionFailed",
|
||||
"Velero did not delete backup %s/%s: %s; requesting the deletion again",
|
||||
veleroNamespace, veleroBackup, reason)
|
||||
}
|
||||
|
||||
func veleroDeletionRetries(j *backupsv1alpha1.BackupJob) int {
|
||||
n, err := strconv.Atoi(j.Annotations[veleroDeletionRetriesAnnotation])
|
||||
if err != nil || n < 0 {
|
||||
return 0
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func failedJobSweepBackoff(retries int) time.Duration {
|
||||
d := failedJobSweepRequeue
|
||||
for i := 0; i < retries && d < failedJobSweepMaxRequeue; i++ {
|
||||
d *= 2
|
||||
}
|
||||
return min(d, failedJobSweepMaxRequeue)
|
||||
}
|
||||
|
||||
func shorterRequeue(current, next time.Duration) time.Duration {
|
||||
|
||||
@@ -3,10 +3,13 @@ package backupcontroller
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
@@ -237,67 +240,91 @@ func TestReconcileVelero_SeveralVeleroBackupsKeepCompletedOne(t *testing.T) {
|
||||
// Reconciling a Failed BackupJob, including one that failed under a version
|
||||
// without the cleanup, requests deletion of its failed Velero backups only.
|
||||
func TestReconcile_FailedJobVeleroBackupSweep(t *testing.T) {
|
||||
foreign := failedVeleroBackup + "-abcde"
|
||||
cases := []struct {
|
||||
name string
|
||||
veleroPhase velerov1.BackupPhase
|
||||
jobPhase backupsv1alpha1.BackupJobPhase
|
||||
existing []client.Object
|
||||
// Backups visible to the uncached reader only.
|
||||
uncached []client.Object
|
||||
wantNames []string
|
||||
wantPhase velerov1.DeleteBackupRequestPhase
|
||||
wantRequeue ctrl.Result
|
||||
uncached []client.Object
|
||||
wantRequests map[string]velerov1.DeleteBackupRequestPhase
|
||||
wantRequeue time.Duration
|
||||
wantEvent bool
|
||||
}{
|
||||
{
|
||||
name: "PartiallyFailed without a request",
|
||||
veleroPhase: velerov1.BackupPhasePartiallyFailed,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
wantNames: []string{failedVeleroBackup},
|
||||
wantRequeue: ctrl.Result{RequeueAfter: failedJobSweepRequeue},
|
||||
name: "PartiallyFailed without a request",
|
||||
veleroPhase: velerov1.BackupPhasePartiallyFailed,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
wantRequests: map[string]velerov1.DeleteBackupRequestPhase{failedVeleroBackup: ""},
|
||||
wantRequeue: failedJobSweepRequeue,
|
||||
},
|
||||
{
|
||||
name: "FailedValidation is deleted",
|
||||
veleroPhase: velerov1.BackupPhaseFailedValidation,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
wantNames: []string{failedVeleroBackup},
|
||||
wantRequeue: ctrl.Result{RequeueAfter: failedJobSweepRequeue},
|
||||
name: "FailedValidation is deleted",
|
||||
veleroPhase: velerov1.BackupPhaseFailedValidation,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
wantRequests: map[string]velerov1.DeleteBackupRequestPhase{failedVeleroBackup: ""},
|
||||
wantRequeue: failedJobSweepRequeue,
|
||||
},
|
||||
{
|
||||
name: "pending request from elsewhere",
|
||||
name: "pending request from elsewhere",
|
||||
veleroPhase: velerov1.BackupPhaseFailed,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
existing: []client.Object{deleteRequest(foreign, velerov1.DeleteBackupRequestPhaseInProgress)},
|
||||
wantRequests: map[string]velerov1.DeleteBackupRequestPhase{foreign: velerov1.DeleteBackupRequestPhaseInProgress},
|
||||
wantRequeue: failedJobSweepRequeue,
|
||||
},
|
||||
{
|
||||
name: "errored request from elsewhere is left alone",
|
||||
veleroPhase: velerov1.BackupPhaseFailed,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
existing: []client.Object{deleteRequest(failedVeleroBackup+"-abcde", velerov1.DeleteBackupRequestPhaseInProgress)},
|
||||
wantNames: []string{failedVeleroBackup + "-abcde"},
|
||||
wantPhase: velerov1.DeleteBackupRequestPhaseInProgress,
|
||||
wantRequeue: ctrl.Result{RequeueAfter: failedJobSweepRequeue},
|
||||
existing: []client.Object{deleteRequest(foreign, velerov1.DeleteBackupRequestPhaseProcessed, "backup storage location unavailable")},
|
||||
wantRequests: map[string]velerov1.DeleteBackupRequestPhase{
|
||||
failedVeleroBackup: "",
|
||||
foreign: velerov1.DeleteBackupRequestPhaseProcessed,
|
||||
},
|
||||
wantRequeue: failedJobSweepRequeue,
|
||||
},
|
||||
{
|
||||
name: "own request processed with errors is replaced",
|
||||
veleroPhase: velerov1.BackupPhaseFailed,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
existing: []client.Object{deleteRequest(failedVeleroBackup, velerov1.DeleteBackupRequestPhaseProcessed, "backup is still in progress")},
|
||||
wantNames: []string{failedVeleroBackup},
|
||||
wantRequeue: ctrl.Result{RequeueAfter: failedJobSweepRequeue},
|
||||
name: "own request processed with errors is replaced",
|
||||
veleroPhase: velerov1.BackupPhaseFailed,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
existing: []client.Object{deleteRequest(failedVeleroBackup, velerov1.DeleteBackupRequestPhaseProcessed, "backup is still in progress")},
|
||||
wantRequests: map[string]velerov1.DeleteBackupRequestPhase{failedVeleroBackup: ""},
|
||||
wantRequeue: 2 * failedJobSweepRequeue,
|
||||
wantEvent: true,
|
||||
},
|
||||
{
|
||||
name: "request processed without errors is kept",
|
||||
veleroPhase: velerov1.BackupPhaseFailed,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
existing: []client.Object{deleteRequest(failedVeleroBackup, velerov1.DeleteBackupRequestPhaseProcessed)},
|
||||
wantNames: []string{failedVeleroBackup},
|
||||
wantPhase: velerov1.DeleteBackupRequestPhaseProcessed,
|
||||
wantRequeue: ctrl.Result{RequeueAfter: failedJobSweepRequeue},
|
||||
name: "own request processed without errors while the backup exists is replaced",
|
||||
veleroPhase: velerov1.BackupPhaseFailed,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
existing: []client.Object{deleteRequest(failedVeleroBackup, velerov1.DeleteBackupRequestPhaseProcessed)},
|
||||
wantRequests: map[string]velerov1.DeleteBackupRequestPhase{failedVeleroBackup: ""},
|
||||
wantRequeue: 2 * failedJobSweepRequeue,
|
||||
wantEvent: true,
|
||||
},
|
||||
{
|
||||
name: "Deleting with a request in progress",
|
||||
veleroPhase: velerov1.BackupPhaseDeleting,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
existing: []client.Object{deleteRequest(failedVeleroBackup, velerov1.DeleteBackupRequestPhaseInProgress)},
|
||||
wantRequests: map[string]velerov1.DeleteBackupRequestPhase{failedVeleroBackup: velerov1.DeleteBackupRequestPhaseInProgress},
|
||||
wantRequeue: failedJobSweepRequeue,
|
||||
},
|
||||
{
|
||||
name: "Deleting left by an errored request",
|
||||
veleroPhase: velerov1.BackupPhaseDeleting,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
existing: []client.Object{deleteRequest(failedVeleroBackup, velerov1.DeleteBackupRequestPhaseProcessed, "error deleting backup from object storage")},
|
||||
wantRequests: map[string]velerov1.DeleteBackupRequestPhase{failedVeleroBackup: ""},
|
||||
wantRequeue: 2 * failedJobSweepRequeue,
|
||||
wantEvent: true,
|
||||
},
|
||||
{
|
||||
name: "Completed is never deleted",
|
||||
veleroPhase: velerov1.BackupPhaseCompleted,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
},
|
||||
{
|
||||
name: "Deleting is skipped",
|
||||
veleroPhase: velerov1.BackupPhaseDeleting,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
},
|
||||
{
|
||||
name: "failed backup referenced by a Backup the cache has not seen",
|
||||
veleroPhase: velerov1.BackupPhaseFailed,
|
||||
@@ -308,13 +335,13 @@ func TestReconcile_FailedJobVeleroBackupSweep(t *testing.T) {
|
||||
name: "running backup",
|
||||
veleroPhase: velerov1.BackupPhaseInProgress,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
wantRequeue: ctrl.Result{RequeueAfter: failedJobRunningBackupRequeue},
|
||||
wantRequeue: failedJobRunningBackupRequeue,
|
||||
},
|
||||
{
|
||||
name: "new backup",
|
||||
veleroPhase: "",
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
wantRequeue: ctrl.Result{RequeueAfter: failedJobRunningBackupRequeue},
|
||||
wantRequeue: failedJobRunningBackupRequeue,
|
||||
},
|
||||
{
|
||||
name: "succeeded job",
|
||||
@@ -330,26 +357,100 @@ func TestReconcile_FailedJobVeleroBackupSweep(t *testing.T) {
|
||||
r.APIReader = newVeleroCleanupClient(t, nil, tc.uncached...)
|
||||
}
|
||||
|
||||
if res := reconcileCleanupJob(t, r); res != tc.wantRequeue {
|
||||
t.Errorf("Result = %+v, want %+v", res, tc.wantRequeue)
|
||||
if res := reconcileCleanupJob(t, r); res.RequeueAfter != tc.wantRequeue {
|
||||
t.Errorf("RequeueAfter = %v, want %v", res.RequeueAfter, tc.wantRequeue)
|
||||
}
|
||||
|
||||
dbrs := listDeleteBackupRequests(t, c)
|
||||
if len(dbrs) != len(tc.wantNames) {
|
||||
t.Fatalf("expected requests %v, got %+v", tc.wantNames, dbrs)
|
||||
if len(dbrs) != len(tc.wantRequests) {
|
||||
t.Fatalf("expected requests %v, got %+v", tc.wantRequests, dbrs)
|
||||
}
|
||||
for i, want := range tc.wantNames {
|
||||
if dbrs[i].Name != want || dbrs[i].Spec.BackupName != failedVeleroBackup {
|
||||
t.Errorf("request %d = %s for %s, want %s for %s", i, dbrs[i].Name, dbrs[i].Spec.BackupName, want, failedVeleroBackup)
|
||||
for _, dbr := range dbrs {
|
||||
wantPhase, ok := tc.wantRequests[dbr.Name]
|
||||
if !ok || dbr.Spec.BackupName != failedVeleroBackup {
|
||||
t.Errorf("unexpected request %s for %s", dbr.Name, dbr.Spec.BackupName)
|
||||
continue
|
||||
}
|
||||
if dbrs[i].Status.Phase != tc.wantPhase {
|
||||
t.Errorf("request %s phase = %q, want %q", dbrs[i].Name, dbrs[i].Status.Phase, tc.wantPhase)
|
||||
if dbr.Status.Phase != wantPhase {
|
||||
t.Errorf("request %s phase = %q, want %q", dbr.Name, dbr.Status.Phase, wantPhase)
|
||||
}
|
||||
}
|
||||
|
||||
events := r.Recorder.(*record.FakeRecorder).Events
|
||||
if got := len(events); (got > 0) != tc.wantEvent {
|
||||
t.Errorf("got %d events, want an event: %v", got, tc.wantEvent)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 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 TestReconcile_FailedJobVeleroDeletionBacksOff(t *testing.T) {
|
||||
r, c := newVeleroCleanupReconciler(t,
|
||||
veleroCleanupJob(backupsv1alpha1.BackupJobPhaseFailed),
|
||||
ownedVeleroBackup(failedVeleroBackup, velerov1.BackupPhaseFailed))
|
||||
events := r.Recorder.(*record.FakeRecorder).Events
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if res := reconcileCleanupJob(t, r); res.RequeueAfter != want {
|
||||
t.Errorf("retry %d: RequeueAfter = %v, want %v", i+1, res.RequeueAfter, want)
|
||||
}
|
||||
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)
|
||||
}
|
||||
if n := len(listDeleteBackupRequests(t, c)); n != 1 {
|
||||
t.Fatalf("retry %d: expected one request, got %d", i+1, 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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcile_FailedJobWithoutVeleroCRDs(t *testing.T) {
|
||||
funcs := &interceptor.Funcs{
|
||||
List: func(ctx context.Context, c client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
|
||||
if _, ok := list.(*velerov1.BackupList); ok {
|
||||
return &meta.NoKindMatchError{GroupKind: schema.GroupKind{Group: "velero.io", Kind: "Backup"}}
|
||||
}
|
||||
return c.List(ctx, list, opts...)
|
||||
},
|
||||
}
|
||||
c := newVeleroCleanupClient(t, funcs, veleroCleanupJob(backupsv1alpha1.BackupJobPhaseFailed))
|
||||
r := &BackupJobReconciler{Client: c, Recorder: record.NewFakeRecorder(16)}
|
||||
if res := reconcileCleanupJob(t, r); res.RequeueAfter != 0 {
|
||||
t.Errorf("RequeueAfter = %v, want none", res.RequeueAfter)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestVeleroBackupDeletion_ReplacesByUID(t *testing.T) {
|
||||
stale := deleteRequest(failedVeleroBackup, velerov1.DeleteBackupRequestPhaseProcessed, "backup storage location unavailable")
|
||||
var preconditionUID types.UID
|
||||
@@ -364,9 +465,9 @@ func TestRequestVeleroBackupDeletion_ReplacesByUID(t *testing.T) {
|
||||
},
|
||||
}
|
||||
c := newVeleroCleanupClient(t, funcs, stale)
|
||||
created, err := requestVeleroBackupDeletion(context.Background(), c, veleroNamespace, failedVeleroBackup)
|
||||
if err != nil || !created {
|
||||
t.Fatalf("requestVeleroBackupDeletion = (%v, %v), want (true, nil)", created, err)
|
||||
req, err := requestVeleroBackupDeletion(context.Background(), c, veleroNamespace, failedVeleroBackup)
|
||||
if err != nil || !req.created || req.replaced == nil {
|
||||
t.Fatalf("requestVeleroBackupDeletion = (%+v, %v), want a replaced and a created request", req, err)
|
||||
}
|
||||
if preconditionUID != stale.UID {
|
||||
t.Errorf("Delete precondition UID = %q, want %q", preconditionUID, stale.UID)
|
||||
@@ -390,9 +491,9 @@ 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"))
|
||||
created, err := requestVeleroBackupDeletion(context.Background(), c, veleroNamespace, failedVeleroBackup)
|
||||
if created || !apierrors.IsForbidden(err) {
|
||||
t.Fatalf("requestVeleroBackupDeletion = (%v, %v), want a Forbidden error", created, err)
|
||||
req, err := requestVeleroBackupDeletion(context.Background(), c, veleroNamespace, failedVeleroBackup)
|
||||
if req.created || !apierrors.IsForbidden(err) {
|
||||
t.Fatalf("requestVeleroBackupDeletion = (%+v, %v), want a Forbidden error", req, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user