mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
fix(backups): delete the Velero backup of a failed BackupJob
When a Velero strategy run ended Failed or PartiallyFailed, no Backup was
created, and the Backup finalizer was the only code that asked Velero to
delete backup data. The failed backup.velero.io object, labelled for the
BackupJob but not owned by it, stayed in the bucket with its partial
resources and logs until Velero's TTL (720h by default) expired it, and
deleting the BackupJob did not remove it.
The strategy now requests the deletion through a DeleteBackupRequest before
it marks the job Failed, and the failure message is still read from the
Velero backup first. A direct delete is not enough, because the BSL sync
recreates the object from the object store. A reconcile of a Failed
BackupJob, which used to return at once, now requests deletion of every
Velero backup still labelled for the job. That retries a request that could
not be created and removes backups that earlier versions leaked, after an
upgrade. A Velero backup that a Backup references (the job failed after its
Backup was created) is left alone, and one still running is polled until
Velero accepts a request for it. The sweep matches on the job labels, so it
also covers the Velero backup of a failed Kubernetes strategy run.
The request is named after the Velero backup, and the Backup finalizer uses
the same helper, so repeated reconciles converge on one request. Nothing is
created while another request for the backup is pending. A request that
Velero processed with errors is replaced, because Velero keeps it for a day
and its name would block the retry until then. A job with more than one
Velero backup now fails with a message that names them, not with an empty
one.
Fake-client unit tests cover Failed and PartiallyFailed runs, a repeated
reconcile, leftovers of already Failed jobs (no request, a pending request,
a request processed with errors, a Backup-referenced backup, a running
backup) and the unchanged Succeeded path.
Closes: #5355
Assisted-by: LLM
Signed-off-by: Andrey Kolkov <androndo@gmail.com>
(cherry picked from commit 5fbf7e9a38)
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
This commit is contained in:
1 parent
4fec6e0c47
commit
eeab99ae1a
5 files changed
+445
-26
No files matched your search
@@ -234,28 +234,16 @@ func (r *BackupReconciler) cleanupVeleroBackup(ctx context.Context, backup *back
|
||||
return fmt.Errorf("failed to get Velero backup %s/%s: %w", veleroBackupNamespace, veleroBackupName, err)
|
||||
}
|
||||
|
||||
// Create a DeleteBackupRequest so Velero removes backup data from storage.
|
||||
// Without this, BSL sync will recreate the backup.velero.io resource.
|
||||
dbr := &velerov1.DeleteBackupRequest{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
GenerateName: veleroBackupName + "-",
|
||||
Namespace: veleroBackupNamespace,
|
||||
},
|
||||
Spec: velerov1.DeleteBackupRequestSpec{
|
||||
BackupName: veleroBackupName,
|
||||
},
|
||||
created, err := requestVeleroBackupDeletion(ctx, r.Client, veleroBackupNamespace, veleroBackupName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.Create(ctx, dbr); err != nil {
|
||||
if apierrors.IsAlreadyExists(err) {
|
||||
logger.V(1).Info("DeleteBackupRequest already exists", "backup", veleroBackupName)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("failed to create DeleteBackupRequest for %s/%s: %w", veleroBackupNamespace, veleroBackupName, err)
|
||||
if created {
|
||||
logger.Info("created DeleteBackupRequest for Velero backup",
|
||||
"name", veleroBackupName, "namespace", veleroBackupNamespace)
|
||||
} else {
|
||||
logger.V(1).Info("deletion of Velero backup already requested", "backup", veleroBackupName)
|
||||
}
|
||||
|
||||
logger.Info("created DeleteBackupRequest for Velero backup",
|
||||
"name", veleroBackupName, "namespace", veleroBackupNamespace,
|
||||
"deleteRequest", dbr.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,11 @@ func (r *BackupJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
|
||||
// would otherwise materialise cozy-backups-creds in tenant namespaces
|
||||
// long after the BackupJob is done and pin needless work on the
|
||||
// apiserver.
|
||||
if j.Status.Phase == backupsv1alpha1.BackupJobPhaseSucceeded || j.Status.Phase == backupsv1alpha1.BackupJobPhaseFailed {
|
||||
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)
|
||||
}
|
||||
if j.Status.Phase == backupsv1alpha1.BackupJobPhaseSucceeded {
|
||||
logger.V(1).Info("BackupJob already terminal, skipping", "phase", j.Status.Phase)
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
package backupcontroller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1"
|
||||
|
||||
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
)
|
||||
|
||||
// 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.
|
||||
//
|
||||
// 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 replaced: Velero keeps it for a day, and until then its name would block
|
||||
// the retry.
|
||||
func requestVeleroBackupDeletion(ctx context.Context, c client.Client, namespace, name string) (bool, error) {
|
||||
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)
|
||||
}
|
||||
var failed *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.Name == name {
|
||||
failed = dbr
|
||||
}
|
||||
}
|
||||
if failed != 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)
|
||||
}
|
||||
}
|
||||
|
||||
dbr := &velerov1.DeleteBackupRequest{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
},
|
||||
Spec: velerov1.DeleteBackupRequestSpec{
|
||||
BackupName: name,
|
||||
},
|
||||
}
|
||||
if err := c.Create(ctx, dbr); err != nil {
|
||||
if apierrors.IsAlreadyExists(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, fmt.Errorf("failed to create DeleteBackupRequest for %s/%s: %w", namespace, name, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// deleteFailedJobVeleroBackups requests deletion of the Velero backups a Failed
|
||||
// BackupJob left behind. A failed run creates no Backup, and the Backup
|
||||
// finalizer is the only other path that deletes Velero data, so without this
|
||||
// the backup and its objects stay in the bucket until Velero's TTL expires
|
||||
// them. It runs on every reconcile of a Failed job, which also clears backups
|
||||
// left by controller versions that did not request the deletion.
|
||||
func (r *BackupJobReconciler) deleteFailedJobVeleroBackups(ctx context.Context, j *backupsv1alpha1.BackupJob) (ctrl.Result, error) {
|
||||
logger := getLogger(ctx)
|
||||
|
||||
veleroBackups := &velerov1.BackupList{}
|
||||
if err := r.List(ctx, veleroBackups, client.InNamespace(veleroNamespace), client.MatchingLabels{
|
||||
backupsv1alpha1.OwningJobNamespaceLabel: j.Namespace,
|
||||
backupsv1alpha1.OwningJobNameLabel: j.Name,
|
||||
}); err != nil {
|
||||
if meta.IsNoMatchError(err) {
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
return ctrl.Result{}, fmt.Errorf("failed to list Velero Backups of BackupJob %s/%s: %w", j.Namespace, j.Name, err)
|
||||
}
|
||||
if len(veleroBackups.Items) == 0 {
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
// A job can fail after its Backup was created: the status update that
|
||||
// records the Backup is lost, and the retried create hits AlreadyExists.
|
||||
// That Backup owns the Velero backup and deletes it itself.
|
||||
referenced, err := r.veleroBackupsReferencedByBackups(ctx, j.Namespace)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
var running bool
|
||||
for i := range veleroBackups.Items {
|
||||
vb := &veleroBackups.Items[i]
|
||||
if referenced[client.ObjectKeyFromObject(vb)] {
|
||||
continue
|
||||
}
|
||||
switch vb.Status.Phase {
|
||||
case velerov1.BackupPhaseCompleted, velerov1.BackupPhasePartiallyFailed,
|
||||
velerov1.BackupPhaseFailed, velerov1.BackupPhaseFailedValidation:
|
||||
case velerov1.BackupPhaseDeleting:
|
||||
continue
|
||||
default:
|
||||
// Velero rejects a request for a backup that is still running.
|
||||
running = true
|
||||
continue
|
||||
}
|
||||
created, err := requestVeleroBackupDeletion(ctx, r.Client, vb.Namespace, vb.Name)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
if created {
|
||||
logger.Info("requested deletion of the Velero Backup of a failed BackupJob",
|
||||
"backupjob", j.Name, "namespace", j.Namespace, "veleroBackup", vb.Name)
|
||||
}
|
||||
}
|
||||
if running {
|
||||
return ctrl.Result{RequeueAfter: defaultActiveJobPollingInterval}, nil
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
func (r *BackupJobReconciler) veleroBackupsReferencedByBackups(ctx context.Context, namespace string) (map[client.ObjectKey]bool, error) {
|
||||
backups := &backupsv1alpha1.BackupList{}
|
||||
if err := r.List(ctx, backups, client.InNamespace(namespace)); err != nil {
|
||||
return nil, fmt.Errorf("failed to list Backups in %s: %w", namespace, err)
|
||||
}
|
||||
referenced := map[client.ObjectKey]bool{}
|
||||
for _, b := range backups.Items {
|
||||
name := b.Spec.DriverMetadata[veleroBackupNameMetadataKey]
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
ns := b.Spec.DriverMetadata[veleroBackupNamespaceMetadataKey]
|
||||
if ns == "" {
|
||||
ns = veleroNamespace
|
||||
}
|
||||
referenced[client.ObjectKey{Namespace: ns, Name: name}] = true
|
||||
}
|
||||
return referenced, nil
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
package backupcontroller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/tools/record"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
|
||||
strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1"
|
||||
backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1"
|
||||
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
failedJobNamespace = "tenant-acme"
|
||||
failedJobName = "vm-backup"
|
||||
)
|
||||
|
||||
func newVeleroCleanupReconciler(t *testing.T, objs ...client.Object) (*BackupJobReconciler, client.Client) {
|
||||
t.Helper()
|
||||
c := fake.NewClientBuilder().
|
||||
WithScheme(newReconcilerScheme(t)).
|
||||
WithObjects(objs...).
|
||||
WithStatusSubresource(&backupsv1alpha1.BackupJob{}).
|
||||
Build()
|
||||
return &BackupJobReconciler{Client: c, Recorder: record.NewFakeRecorder(16)}, c
|
||||
}
|
||||
|
||||
func veleroCleanupJob(phase backupsv1alpha1.BackupJobPhase) *backupsv1alpha1.BackupJob {
|
||||
started := metav1.Now()
|
||||
return &backupsv1alpha1.BackupJob{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: failedJobNamespace, Name: failedJobName},
|
||||
Status: backupsv1alpha1.BackupJobStatus{
|
||||
Phase: phase,
|
||||
StartedAt: &started,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func ownedVeleroBackup(name string, phase velerov1.BackupPhase) *velerov1.Backup {
|
||||
return &velerov1.Backup{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: veleroNamespace,
|
||||
Name: name,
|
||||
Labels: map[string]string{
|
||||
backupsv1alpha1.OwningJobNameLabel: failedJobName,
|
||||
backupsv1alpha1.OwningJobNamespaceLabel: failedJobNamespace,
|
||||
},
|
||||
},
|
||||
Status: velerov1.BackupStatus{Phase: phase, FailureReason: "object store unreachable"},
|
||||
}
|
||||
}
|
||||
|
||||
func veleroCleanupResolved() *ResolvedBackupConfig {
|
||||
apiGroup := strategyv1alpha1.GroupVersion.Group
|
||||
return &ResolvedBackupConfig{StrategyRef: corev1.TypedLocalObjectReference{
|
||||
APIGroup: &apiGroup, Kind: strategyv1alpha1.VeleroStrategyKind, Name: "velero",
|
||||
}}
|
||||
}
|
||||
|
||||
func veleroCleanupStrategy() *strategyv1alpha1.Velero {
|
||||
return &strategyv1alpha1.Velero{ObjectMeta: metav1.ObjectMeta{Name: "velero"}}
|
||||
}
|
||||
|
||||
func listDeleteBackupRequests(t *testing.T, c client.Client) []velerov1.DeleteBackupRequest {
|
||||
t.Helper()
|
||||
l := &velerov1.DeleteBackupRequestList{}
|
||||
if err := c.List(context.Background(), l, client.InNamespace(veleroNamespace)); err != nil {
|
||||
t.Fatalf("list DeleteBackupRequests: %v", err)
|
||||
}
|
||||
return l.Items
|
||||
}
|
||||
|
||||
func reconcileFailedJob(t *testing.T, r *BackupJobReconciler) ctrl.Result {
|
||||
t.Helper()
|
||||
res, err := r.Reconcile(context.Background(), ctrl.Request{
|
||||
NamespacedName: types.NamespacedName{Namespace: failedJobNamespace, Name: failedJobName},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Reconcile: %v", err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func getCleanupJob(t *testing.T, c client.Client) *backupsv1alpha1.BackupJob {
|
||||
t.Helper()
|
||||
j := &backupsv1alpha1.BackupJob{}
|
||||
if err := c.Get(context.Background(), types.NamespacedName{Namespace: failedJobNamespace, Name: failedJobName}, j); err != nil {
|
||||
t.Fatalf("get BackupJob: %v", err)
|
||||
}
|
||||
return j
|
||||
}
|
||||
|
||||
// A Velero backup that ends Failed or PartiallyFailed produces no Backup, so
|
||||
// the strategy must request its deletion itself, keep the failure message, and
|
||||
// converge on a single request across reconciles.
|
||||
func TestReconcileVelero_FailedBackupRequestsDeletion(t *testing.T) {
|
||||
for _, phase := range []velerov1.BackupPhase{velerov1.BackupPhaseFailed, velerov1.BackupPhasePartiallyFailed} {
|
||||
t.Run(string(phase), func(t *testing.T) {
|
||||
vb := ownedVeleroBackup("tenant-acme.vm-backup-x7k2p", phase)
|
||||
j := veleroCleanupJob(backupsv1alpha1.BackupJobPhaseRunning)
|
||||
r, c := newVeleroCleanupReconciler(t, j, vb, veleroCleanupStrategy())
|
||||
|
||||
if _, err := r.reconcileVelero(context.Background(), j, veleroCleanupResolved()); err != nil {
|
||||
t.Fatalf("reconcileVelero: %v", err)
|
||||
}
|
||||
|
||||
got := getCleanupJob(t, c)
|
||||
if got.Status.Phase != backupsv1alpha1.BackupJobPhaseFailed {
|
||||
t.Fatalf("expected BackupJob Failed, got %q", got.Status.Phase)
|
||||
}
|
||||
wantMsg := "Velero Backup failed with phase " + string(phase) + ": object store unreachable"
|
||||
if got.Status.Message != wantMsg {
|
||||
t.Errorf("message = %q, want %q", got.Status.Message, wantMsg)
|
||||
}
|
||||
|
||||
dbrs := listDeleteBackupRequests(t, c)
|
||||
if len(dbrs) != 1 || dbrs[0].Spec.BackupName != vb.Name {
|
||||
t.Fatalf("expected one DeleteBackupRequest for %s, got %+v", vb.Name, dbrs)
|
||||
}
|
||||
|
||||
reconcileFailedJob(t, r)
|
||||
if n := len(listDeleteBackupRequests(t, c)); n != 1 {
|
||||
t.Fatalf("a second reconcile must not add a request, got %d", n)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcileVelero_CompletedBackupRequestsNoDeletion(t *testing.T) {
|
||||
vb := ownedVeleroBackup("tenant-acme.vm-backup-x7k2p", velerov1.BackupPhaseCompleted)
|
||||
j := veleroCleanupJob(backupsv1alpha1.BackupJobPhaseRunning)
|
||||
r, c := newVeleroCleanupReconciler(t, j, vb, veleroCleanupStrategy())
|
||||
|
||||
if _, err := r.reconcileVelero(context.Background(), j, veleroCleanupResolved()); err != nil {
|
||||
t.Fatalf("reconcileVelero: %v", err)
|
||||
}
|
||||
if got := getCleanupJob(t, c); got.Status.Phase != backupsv1alpha1.BackupJobPhaseSucceeded {
|
||||
t.Fatalf("expected BackupJob Succeeded, got %q (%s)", got.Status.Phase, got.Status.Message)
|
||||
}
|
||||
reconcileFailedJob(t, r)
|
||||
if dbrs := listDeleteBackupRequests(t, c); len(dbrs) != 0 {
|
||||
t.Fatalf("a succeeded run must not request deletion, got %+v", dbrs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcileVelero_SeveralVeleroBackupsFailWithMessage(t *testing.T) {
|
||||
j := veleroCleanupJob(backupsv1alpha1.BackupJobPhaseRunning)
|
||||
r, c := newVeleroCleanupReconciler(t, j, veleroCleanupStrategy(),
|
||||
ownedVeleroBackup("vb-b", velerov1.BackupPhaseFailed),
|
||||
ownedVeleroBackup("vb-a", velerov1.BackupPhaseCompleted))
|
||||
|
||||
if _, err := r.reconcileVelero(context.Background(), j, veleroCleanupResolved()); err != nil {
|
||||
t.Fatalf("reconcileVelero: %v", err)
|
||||
}
|
||||
got := getCleanupJob(t, c)
|
||||
if got.Status.Phase != backupsv1alpha1.BackupJobPhaseFailed || got.Status.CompletedAt == nil {
|
||||
t.Fatalf("expected BackupJob Failed with completedAt, got %+v", got.Status)
|
||||
}
|
||||
if want := "more than one Velero Backup references this BackupJob: vb-a, vb-b"; got.Status.Message != want {
|
||||
t.Errorf("message = %q, want %q", got.Status.Message, want)
|
||||
}
|
||||
|
||||
reconcileFailedJob(t, r)
|
||||
if n := len(listDeleteBackupRequests(t, c)); n != 2 {
|
||||
t.Fatalf("expected a request for each Velero backup, got %d", n)
|
||||
}
|
||||
}
|
||||
|
||||
// A job that failed under a controller version without the cleanup still
|
||||
// holds its Velero backup; reconciling it must request the deletion.
|
||||
func TestReconcile_FailedJobLeftoverVeleroBackup(t *testing.T) {
|
||||
vbName := "tenant-acme.vm-backup-x7k2p"
|
||||
cases := []struct {
|
||||
name string
|
||||
veleroPhase velerov1.BackupPhase
|
||||
jobPhase backupsv1alpha1.BackupJobPhase
|
||||
existing []client.Object
|
||||
wantNames []string
|
||||
wantRequeue bool
|
||||
}{
|
||||
{
|
||||
name: "no request yet",
|
||||
veleroPhase: velerov1.BackupPhasePartiallyFailed,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
wantNames: []string{vbName},
|
||||
},
|
||||
{
|
||||
name: "pending request from elsewhere",
|
||||
veleroPhase: velerov1.BackupPhaseFailed,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
existing: []client.Object{&velerov1.DeleteBackupRequest{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: veleroNamespace, Name: vbName + "-abcde"},
|
||||
Spec: velerov1.DeleteBackupRequestSpec{BackupName: vbName},
|
||||
Status: velerov1.DeleteBackupRequestStatus{Phase: velerov1.DeleteBackupRequestPhaseInProgress},
|
||||
}},
|
||||
wantNames: []string{vbName + "-abcde"},
|
||||
},
|
||||
{
|
||||
name: "own request processed with errors is replaced",
|
||||
veleroPhase: velerov1.BackupPhaseFailed,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
existing: []client.Object{&velerov1.DeleteBackupRequest{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: veleroNamespace, Name: vbName},
|
||||
Spec: velerov1.DeleteBackupRequestSpec{BackupName: vbName},
|
||||
Status: velerov1.DeleteBackupRequestStatus{
|
||||
Phase: velerov1.DeleteBackupRequestPhaseProcessed,
|
||||
Errors: []string{"backup is still in progress"},
|
||||
},
|
||||
}},
|
||||
wantNames: []string{vbName},
|
||||
},
|
||||
{
|
||||
name: "completed backup owned by a Backup",
|
||||
veleroPhase: velerov1.BackupPhaseCompleted,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
existing: []client.Object{&backupsv1alpha1.Backup{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: failedJobNamespace, Name: failedJobName},
|
||||
Spec: backupsv1alpha1.BackupSpec{DriverMetadata: map[string]string{
|
||||
veleroBackupNameMetadataKey: vbName,
|
||||
veleroBackupNamespaceMetadataKey: veleroNamespace,
|
||||
}},
|
||||
}},
|
||||
},
|
||||
{
|
||||
name: "backup still running",
|
||||
veleroPhase: velerov1.BackupPhaseInProgress,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseFailed,
|
||||
wantRequeue: true,
|
||||
},
|
||||
{
|
||||
name: "succeeded job",
|
||||
veleroPhase: velerov1.BackupPhaseCompleted,
|
||||
jobPhase: backupsv1alpha1.BackupJobPhaseSucceeded,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
objs := append([]client.Object{veleroCleanupJob(tc.jobPhase), ownedVeleroBackup(vbName, tc.veleroPhase)}, tc.existing...)
|
||||
r, c := newVeleroCleanupReconciler(t, objs...)
|
||||
|
||||
res := reconcileFailedJob(t, r)
|
||||
if (res.RequeueAfter > 0) != tc.wantRequeue {
|
||||
t.Errorf("RequeueAfter = %v, want requeue %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)
|
||||
}
|
||||
for i, want := range tc.wantNames {
|
||||
if dbrs[i].Name != want || dbrs[i].Spec.BackupName != vbName {
|
||||
t.Errorf("request %d = %s for %s, want %s for %s", i, dbrs[i].Name, dbrs[i].Spec.BackupName, want, vbName)
|
||||
}
|
||||
if dbrs[i].Status.Phase == velerov1.DeleteBackupRequestPhaseProcessed {
|
||||
t.Errorf("request %s is the processed one, expected a fresh request", dbrs[i].Name)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -307,12 +307,12 @@ func (r *BackupJobReconciler) reconcileVelero(ctx context.Context, j *backupsv1a
|
||||
}
|
||||
|
||||
if len(veleroBackupList.Items) > 1 {
|
||||
logger.Error(fmt.Errorf("too many Velero backups for BackupJob"), "found more than one Velero Backup referencing a single BackupJob as owner")
|
||||
j.Status.Phase = backupsv1alpha1.BackupJobPhaseFailed
|
||||
if err := r.Status().Update(ctx, j); err != nil {
|
||||
logger.Error(err, "failed to update BackupJob status")
|
||||
names := make([]string, 0, len(veleroBackupList.Items))
|
||||
for _, vb := range veleroBackupList.Items {
|
||||
names = append(names, vb.Name)
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
sort.Strings(names)
|
||||
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("more than one Velero Backup references this BackupJob: %s", strings.Join(names, ", ")))
|
||||
}
|
||||
|
||||
veleroBackup := veleroBackupList.Items[0].DeepCopy()
|
||||
@@ -361,6 +361,12 @@ func (r *BackupJobReconciler) reconcileVelero(ctx context.Context, j *backupsv1a
|
||||
// Step 5: On failure
|
||||
if phase == "Failed" || phase == "PartiallyFailed" {
|
||||
message := formatVeleroBackupFailureMessageForBackupJob(ctx, r.Client, veleroBackup)
|
||||
// No Backup is created for a failed run, so nothing else deletes this
|
||||
// Velero backup. A failed request is retried by
|
||||
// deleteFailedJobVeleroBackups once the job is Failed.
|
||||
if _, err := requestVeleroBackupDeletion(ctx, r.Client, veleroBackup.Namespace, veleroBackup.Name); err != nil {
|
||||
logger.Error(err, "failed to request deletion of the failed Velero Backup", "name", veleroBackup.Name)
|
||||
}
|
||||
return r.markBackupJobFailed(ctx, j, message)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user