@@ -307,6 +307,14 @@ func (r *BackupJobReconciler) ensureMariaDBBackup(ctx context.Context, j *backup
307307// findMariaDBBackupForJob returns the k8s.mariadb.com/Backup labelled with
308308// the BackupJob's OwningJob{Name,Namespace}, if any. Returns (nil, nil)
309309// when no match is found.
310+ //
311+ // In steady state at most one Backup CR carries a given OwningJob label
312+ // pair (ensureMariaDBBackup is idempotent by this label). The list path
313+ // can in theory observe two if two controller replicas raced Create
314+ // between the first list returning empty and the cache catching up. The
315+ // driver picks list.Items[0] deterministically; the V(1) breadcrumb on
316+ // duplicates exists so an operator triaging "wrong Backup got reused"
317+ // has a log line to grep for instead of staring at silent reuse.
310318func (r * BackupJobReconciler ) findMariaDBBackupForJob (ctx context.Context , j * backupsv1alpha1.BackupJob ) (* mariadbtypes.Backup , error ) {
311319 list := & mariadbtypes.BackupList {}
312320 if err := r .List (ctx , list ,
@@ -321,6 +329,14 @@ func (r *BackupJobReconciler) findMariaDBBackupForJob(ctx context.Context, j *ba
321329 if len (list .Items ) == 0 {
322330 return nil , nil
323331 }
332+ if len (list .Items ) > 1 {
333+ names := make ([]string , 0 , len (list .Items ))
334+ for i := range list .Items {
335+ names = append (names , list .Items [i ].Name )
336+ }
337+ getLogger (ctx ).Debug ("multiple k8s.mariadb.com/Backup CRs match BackupJob OwningJob labels; reusing first" ,
338+ "backupjob" , j .Name , "namespace" , j .Namespace , "matches" , names , "picked" , names [0 ])
339+ }
324340 return & list .Items [0 ], nil
325341}
326342
@@ -355,6 +371,17 @@ func (r *BackupJobReconciler) createMariaDBBackupArtifact(
355371 return nil , fmt .Errorf ("encode source snapshot for Backup.status.underlyingResources: %w" , err )
356372 }
357373
374+ status := backupsv1alpha1.BackupStatus {
375+ Phase : backupsv1alpha1 .BackupPhaseReady ,
376+ UnderlyingResources : underlyingResources ,
377+ }
378+ // Status.Artifact carries a "this backup has an artefact at URI X"
379+ // signal. PVC/Volume storage has no stable URI (the location is the
380+ // operator's PVC ID), so an Artifact{URI: ""} is a worse signal than
381+ // no Artifact at all - leave it nil instead.
382+ if uri := driverMD [mariadbStorageURIKey ]; uri != "" {
383+ status .Artifact = & backupsv1alpha1.BackupArtifact {URI : uri }
384+ }
358385 backup := & backupsv1alpha1.Backup {
359386 ObjectMeta : metav1.ObjectMeta {
360387 Name : j .Name ,
@@ -366,13 +393,7 @@ func (r *BackupJobReconciler) createMariaDBBackupArtifact(
366393 TakenAt : takenAt ,
367394 DriverMetadata : driverMD ,
368395 },
369- Status : backupsv1alpha1.BackupStatus {
370- Phase : backupsv1alpha1 .BackupPhaseReady ,
371- Artifact : & backupsv1alpha1.BackupArtifact {
372- URI : driverMD [mariadbStorageURIKey ],
373- },
374- UnderlyingResources : underlyingResources ,
375- },
396+ Status : status ,
376397 }
377398 if j .Spec .PlanRef != nil {
378399 backup .Spec .PlanRef = j .Spec .PlanRef
@@ -417,6 +438,20 @@ func (r *RestoreJobReconciler) reconcileMariaDBRestore(ctx context.Context, rest
417438 return r .markRestoreJobFailed (ctx , restoreJob , err .Error ())
418439 }
419440
441+ // Validate the resolved target shape before any apiserver call. This is
442+ // cheap (in-memory state only) and lets a malformed TargetApplicationRef
443+ // fail fast instead of after parseMariaDBRestoreOptions + the srcBackup
444+ // Get round-trip.
445+ target := r .resolveMariaDBRestoreTarget (restoreJob , backup )
446+ if target .Kind != mariadbAppKind {
447+ return r .markRestoreJobFailed (ctx , restoreJob , fmt .Sprintf (
448+ "target applicationRef.kind=%q is not supported by the MariaDB driver" , target .Kind ))
449+ }
450+ if target .APIGroup != "" && target .APIGroup != mariadbapp .GroupName {
451+ return r .markRestoreJobFailed (ctx , restoreJob , fmt .Sprintf (
452+ "target applicationRef.apiGroup=%q is not supported by the MariaDB driver" , target .APIGroup ))
453+ }
454+
420455 if restoreJob .Status .StartedAt == nil {
421456 fresh := & backupsv1alpha1.RestoreJob {}
422457 if err := r .Get (ctx , types.NamespacedName {Namespace : restoreJob .Namespace , Name : restoreJob .Name }, fresh ); err != nil {
@@ -438,21 +473,15 @@ func (r *RestoreJobReconciler) reconcileMariaDBRestore(ctx context.Context, rest
438473 return ctrl.Result {RequeueAfter : mariadbPollInterval }, nil
439474 }
440475
441- target := r .resolveMariaDBRestoreTarget (restoreJob , backup )
442- if target .Kind != mariadbAppKind {
443- return r .markRestoreJobFailed (ctx , restoreJob , fmt .Sprintf (
444- "target applicationRef.kind=%q is not supported by the MariaDB driver" , target .Kind ))
445- }
446- if target .APIGroup != "" && target .APIGroup != mariadbapp .GroupName {
447- return r .markRestoreJobFailed (ctx , restoreJob , fmt .Sprintf (
448- "target applicationRef.apiGroup=%q is not supported by the MariaDB driver" , target .APIGroup ))
449- }
450-
476+ // parseMariaDBRestoreOptions is intentionally permissive (event + log,
477+ // proceed with defaults). This diverges from the structural failures
478+ // below (missing driverMetadata, MariaDB CR genuinely absent) because a
479+ // bad spec.options is best-effort tenant-supplied override, not a
480+ // load-bearing piece of the restore contract - the defaults are still
481+ // the right behaviour, so surfacing a transient breadcrumb beats
482+ // failing the restore on a typo.
451483 options , err := parseMariaDBRestoreOptions (restoreJob .Spec .Options )
452484 if err != nil {
453- // Permissive on parse errors: surface as event + log so a tenant
454- // who wonders why their override didn't apply has a breadcrumb,
455- // but proceed with defaults.
456485 logger .Info ("malformed restoreJob.spec.options; falling back to defaults" , "error" , err )
457486 r .Recorder .Eventf (restoreJob , corev1 .EventTypeWarning , "MalformedOptions" ,
458487 "spec.options is not valid JSON; falling back to defaults: %v" , err )
@@ -480,17 +509,33 @@ func (r *RestoreJobReconciler) reconcileMariaDBRestore(ctx context.Context, rest
480509 }
481510
482511 // The operator Restore CR replays the dump against an existing live
483- // MariaDB; for to-copy this requires the target MariaDB to exist and
484- // be Ready. Surface the precondition explicitly so a tenant sees a
485- // clear "deploy the target first" message instead of a cryptic
486- // operator-side failure.
512+ // MariaDB; for to-copy this requires the target MariaDB to exist. A
513+ // tenant who fires a RestoreJob seconds before their target
514+ // HelmRelease materialises the MariaDB CR shouldn't have to recreate
515+ // the RestoreJob - mirror the backup path's transient handling and let
516+ // the operator-side StartedAt + effectiveRestoreDeadline guard against
517+ // a target that never shows up.
487518 targetMDBName := mariadbNameForApp (target .AppName )
488519 if err := r .Get (ctx , types.NamespacedName {Namespace : target .Namespace , Name : targetMDBName },
489520 & mariadbtypes.MariaDB {}); err != nil {
490521 if apierrors .IsNotFound (err ) {
491- return r .markRestoreJobFailed (ctx , restoreJob , fmt .Sprintf (
492- "target k8s.mariadb.com/MariaDB %s/%s not found (deploy the target MariaDB application before requesting the restore)" ,
493- target .Namespace , targetMDBName ))
522+ deadline := options .effectiveRestoreDeadline ()
523+ if restoreJob .Status .StartedAt != nil && time .Since (restoreJob .Status .StartedAt .Time ) > deadline {
524+ return r .markRestoreJobFailed (ctx , restoreJob , fmt .Sprintf (
525+ "target k8s.mariadb.com/MariaDB %s/%s not found within %s (deploy the target MariaDB application before requesting the restore; override via spec.options.restoreTimeoutSeconds)" ,
526+ target .Namespace , targetMDBName , deadline ))
527+ }
528+ apimeta .SetStatusCondition (& restoreJob .Status .Conditions , metav1.Condition {
529+ Type : "Ready" ,
530+ Status : metav1 .ConditionFalse ,
531+ Reason : "TargetMariaDBNotReady" ,
532+ Message : fmt .Sprintf ("waiting for target k8s.mariadb.com/MariaDB %s/%s to exist" ,
533+ target .Namespace , targetMDBName ),
534+ })
535+ if err := r .Status ().Update (ctx , restoreJob ); err != nil {
536+ return ctrl.Result {}, err
537+ }
538+ return ctrl.Result {RequeueAfter : mariadbPollInterval }, nil
494539 }
495540 return ctrl.Result {}, err
496541 }
0 commit comments