fix(backups): fail BackupJob when Velero rejects the Backup

Velero reports a Backup it rejects (missing storage location, bad spec)
as phase FailedValidation. The BackupJob path only treated Failed and
PartiallyFailed as terminal, so a rejected Backup fell into the "still
in progress" branch: the BackupJob was requeued every few seconds and
stayed Running with nothing to time it out.

Treat FailedValidation as a failure for BackupJob, as the RestoreJob
path already does. The failure message already carries
Status.ValidationErrors, so the user sees why Velero refused it.

The RestoreJob side already handled this; the added restore test pins
that behavior and passes without the controller change.

Report: https://github.com/cozystack/cozystack/issues/4446

Assisted-by: LLM
Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
This commit is contained in:
Pujitha Paladugu authored and Myasnikov Daniil committed 2026-12-03 10:30:06 +00:00
1 parent 8e430c011f
commit cc9610cf14
2 files changed
+64 -1

No files matched your search

@@ -376,7 +376,7 @@ func (r *BackupJobReconciler) reconcileVelero(ctx context.Context, j *backupsv1a
}
// Step 5: On failure
if phase == "Failed" || phase == "PartiallyFailed" {
if phase == "Failed" || phase == "PartiallyFailed" || phase == "FailedValidation" {
// The Velero backup is deleted by deleteFailedJobVeleroBackups once
// the Failed phase written here triggers the next reconcile.
message := formatVeleroBackupFailureMessageForBackupJob(ctx, r.Client, veleroBackup)
@@ -2278,3 +2278,66 @@ func TestReconcileVeleroRestore_DescendantTargetPassesTenantCheck(t *testing.T)
t.Fatalf("within-tenant descendant restore was refused: phase=%q, ready=%+v", got.Status.Phase, ready)
}
}
func TestReconcileVeleroRestore_FailedValidation(t *testing.T) {
started := metav1.Now()
backup := &backupsv1alpha1.Backup{
ObjectMeta: metav1.ObjectMeta{Name: "src-sb", Namespace: "tenant-src"},
Spec: backupsv1alpha1.BackupSpec{
ApplicationRef: corev1.TypedLocalObjectReference{
APIGroup: new("apps.cozystack.io"),
Kind: vmInstanceKind,
Name: "src",
},
StrategyRef: corev1.TypedLocalObjectReference{Kind: "Velero", Name: "cozy-default-velero"},
DriverMetadata: map[string]string{veleroBackupNameMetadataKey: "vb"},
},
}
restoreJob := &backupsv1alpha1.RestoreJob{
ObjectMeta: metav1.ObjectMeta{Name: "src-restore", Namespace: "tenant-src"},
Spec: backupsv1alpha1.RestoreJobSpec{BackupRef: corev1.LocalObjectReference{Name: "src-sb"}},
Status: backupsv1alpha1.RestoreJobStatus{
Phase: backupsv1alpha1.RestoreJobPhaseRunning,
StartedAt: &started,
},
}
veleroRestore := &velerov1.Restore{
ObjectMeta: metav1.ObjectMeta{Name: "vr", Namespace: veleroNamespace, Labels: map[string]string{
backupsv1alpha1.OwningJobNameLabel: restoreJob.Name,
backupsv1alpha1.OwningJobNamespaceLabel: restoreJob.Namespace,
}},
Status: velerov1.RestoreStatus{
Phase: velerov1.RestorePhaseFailedValidation,
ValidationErrors: []string{"backup not found"},
},
}
strategy := &strategyv1alpha1.Velero{ObjectMeta: metav1.ObjectMeta{Name: "cozy-default-velero"}}
reconciler := newTestRestoreJobReconcilerWithDynamic(t, nil)
testScheme := runtime.NewScheme()
_ = scheme.AddToScheme(testScheme)
_ = backupsv1alpha1.AddToScheme(testScheme)
_ = velerov1.AddToScheme(testScheme)
_ = strategyv1alpha1.AddToScheme(testScheme)
reconciler.Client = clientfake.NewClientBuilder().
WithScheme(testScheme).
WithObjects(restoreJob, backup, veleroRestore, strategy).
WithStatusSubresource(&backupsv1alpha1.RestoreJob{}).
Build()
ctx := context.Background()
if _, err := reconciler.reconcileVeleroRestore(ctx, restoreJob, backup); err != nil {
t.Fatalf("reconcileVeleroRestore() error = %v", err)
}
got := &backupsv1alpha1.RestoreJob{}
if err := reconciler.Get(ctx, client.ObjectKeyFromObject(restoreJob), got); err != nil {
t.Fatalf("get RestoreJob: %v", err)
}
if got.Status.Phase != backupsv1alpha1.RestoreJobPhaseFailed {
t.Fatalf("phase = %q, want Failed", got.Status.Phase)
}
ready := meta.FindStatusCondition(got.Status.Conditions, "Ready")
if ready == nil || !strings.Contains(ready.Message, "backup not found") {
t.Errorf("Ready condition = %+v, want message containing the validation error", ready)
}
}