fix(backups): fail Velero jobs when Velero rejects the Backup or Restore (#4589)

## What this PR does

Velero reports a Backup it rejects (missing storage location, bad spec)
as phase `FailedValidation`. The BackupJob path in `reconcileVelero`
only treated `Failed` and `PartiallyFailed` as terminal, so a rejected
Backup left the BackupJob `Running`, requeued every few seconds, with
nothing to time it out.

`FailedValidation` now fails the BackupJob, and the existing failure
message already carries `Status.ValidationErrors`. The RestoreJob path
already handled `FailedValidation` on main, so this PR makes no restore
code change; it only adds a test that pins that behavior.

Validation: `go test ./internal/backupcontroller/...` passes.
`TestReconcileVelero_BackupFailedValidation` fails when the
`FailedValidation` check is removed from `reconcileVelero`.
`TestReconcileVeleroRestore_FailedValidation` passes with or without
this change. I did not run against a live Velero.

Fixes #4446

### Screenshots

Not a UI change.

### Downstream repositories

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

### Release note

```release-note
fix(backups): a Velero-backed BackupJob now fails, with Velero's validation errors in the message, when Velero rejects the Backup with FailedValidation, instead of staying Running.
```

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Backup and restore jobs now fail when Velero reports a validation
failure. Their status includes Velero’s formatted failure message,
making the reason for the failure visible. This ensures validation
failures are reflected as terminal job failures rather than leaving the
jobs in an inaccurate state.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
myasnikovdaniil authored and GitHub committed 2026-12-04 21:16:19 +05:00
commit 85f96620d2
2 files changed
+116 -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,118 @@ 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)
}
}
func TestReconcileVelero_BackupFailedValidation(t *testing.T) {
started := metav1.Now()
backupJob := &backupsv1alpha1.BackupJob{
ObjectMeta: metav1.ObjectMeta{Name: "bj", Namespace: "tenant-src"},
Status: backupsv1alpha1.BackupJobStatus{
Phase: backupsv1alpha1.BackupJobPhaseRunning,
StartedAt: &started,
},
}
veleroBackup := &velerov1.Backup{
ObjectMeta: metav1.ObjectMeta{Name: "vb", Namespace: veleroNamespace, Labels: map[string]string{
backupsv1alpha1.OwningJobNameLabel: backupJob.Name,
backupsv1alpha1.OwningJobNamespaceLabel: backupJob.Namespace,
}},
Status: velerov1.BackupStatus{
Phase: velerov1.BackupPhaseFailedValidation,
ValidationErrors: []string{"storage location not found"},
},
}
strategy := &strategyv1alpha1.Velero{ObjectMeta: metav1.ObjectMeta{Name: "cozy-default-velero"}}
resolved := &ResolvedBackupConfig{
StrategyRef: corev1.TypedLocalObjectReference{Kind: "Velero", Name: "cozy-default-velero"},
}
testScheme := runtime.NewScheme()
_ = scheme.AddToScheme(testScheme)
_ = backupsv1alpha1.AddToScheme(testScheme)
_ = velerov1.AddToScheme(testScheme)
_ = strategyv1alpha1.AddToScheme(testScheme)
reconciler := &BackupJobReconciler{Client: clientfake.NewClientBuilder().
WithScheme(testScheme).
WithObjects(backupJob, veleroBackup, strategy).
WithStatusSubresource(&backupsv1alpha1.BackupJob{}).
Build()}
ctx := context.Background()
if _, err := reconciler.reconcileVelero(ctx, backupJob, resolved); err != nil {
t.Fatalf("reconcileVelero() error = %v", err)
}
got := &backupsv1alpha1.BackupJob{}
if err := reconciler.Get(ctx, client.ObjectKeyFromObject(backupJob), got); err != nil {
t.Fatalf("get BackupJob: %v", err)
}
if got.Status.Phase != backupsv1alpha1.BackupJobPhaseFailed {
t.Fatalf("phase = %q, want Failed", got.Status.Phase)
}
ready := meta.FindStatusCondition(got.Status.Conditions, "Ready")
if ready == nil || !strings.Contains(ready.Message, "storage location not found") {
t.Errorf("Ready condition = %+v, want message containing the validation error", ready)
}
}