Skip to content

Commit 7a94a9e

Browse files
committed
review fixes
Signed-off-by: Andrey Kolkov <[email protected]>
1 parent cbaf8ed commit 7a94a9e

8 files changed

Lines changed: 402 additions & 35 deletions

File tree

‎api/backups/strategy/v1alpha1/mariadb_types.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ const (
3333
// mariadb-operator (k8s.mariadb.com). The strategy carries a templated
3434
// logical-Backup configuration; the driver materialises k8s.mariadb.com
3535
// Backup objects per BackupJob and surfaces them as Cozystack Backup
36-
// artifacts. Restores are driven by patching MariaDB.spec.bootstrapFrom
37-
// on the target application's MariaDB CR.
36+
// artifacts. Restores create a fresh k8s.mariadb.com/Restore CR with
37+
// mariaDbRef pointing at the target MariaDB - the operator replays the
38+
// logical dump into the live database; the driver does not touch
39+
// MariaDB.spec.bootstrapFrom.
3840
type MariaDB struct {
3941
metav1.TypeMeta `json:",inline"`
4042
metav1.ObjectMeta `json:"metadata,omitempty"`

‎internal/backupcontroller/backupjob_controller.go‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,25 @@ func (r *BackupJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
8888
logger.V(1).Info("BackupJob resolved StrategyRef.Kind not supported, skipping",
8989
"backupjob", j.Name,
9090
"kind", strategyRef.Kind,
91-
"supported", []string{strategyv1alpha1.JobStrategyKind, strategyv1alpha1.VeleroStrategyKind, strategyv1alpha1.CNPGStrategyKind, strategyv1alpha1.MariaDBStrategyKind})
91+
"supported", supportedBackupStrategyKinds())
9292
return ctrl.Result{}, nil
9393
}
9494
}
9595

96+
// supportedBackupStrategyKinds returns every strategy.Kind the dispatch
97+
// switch above handles. Centralised so the unsupported-strategy diagnostic
98+
// can't drift out of sync with the real dispatch table - the unit test
99+
// TestSupportedBackupStrategyKindsMatchesDispatch locks in this invariant.
100+
func supportedBackupStrategyKinds() []string {
101+
return []string{
102+
strategyv1alpha1.JobStrategyKind,
103+
strategyv1alpha1.VeleroStrategyKind,
104+
strategyv1alpha1.CNPGStrategyKind,
105+
strategyv1alpha1.AltinityStrategyKind,
106+
strategyv1alpha1.MariaDBStrategyKind,
107+
}
108+
}
109+
96110
// SetupWithManager registers our controller with the Manager and sets up watches.
97111
func (r *BackupJobReconciler) SetupWithManager(mgr ctrl.Manager) error {
98112
// index BackupJob by backupClassName for efficient lookups when BackupClass changes

‎internal/backupcontroller/backupjob_controller_test.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package backupcontroller
33

44
import (
55
"context"
6+
"sort"
67
"testing"
78

89
"k8s.io/apimachinery/pkg/api/meta"
@@ -12,9 +13,40 @@ import (
1213
"sigs.k8s.io/controller-runtime/pkg/client"
1314
clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
1415

16+
strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1"
1517
backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1"
1618
)
1719

20+
// TestSupportedBackupStrategyKindsMatchesDispatch pins every Kind that the
21+
// BackupJob reconciler's dispatch switch handles. Before the fix the
22+
// unsupported-strategy log payload was hand-maintained inline next to the
23+
// switch and silently dropped AltinityStrategyKind when MariaDB was added,
24+
// which misled operators staring at the message and trying to figure out
25+
// which strategies the controller actually supports. The single
26+
// supportedBackupStrategyKinds() source of truth keeps log and dispatch in
27+
// lockstep; this test fails the moment a new strategy is added to either
28+
// half but not the other.
29+
func TestSupportedBackupStrategyKindsMatchesDispatch(t *testing.T) {
30+
got := append([]string(nil), supportedBackupStrategyKinds()...)
31+
want := []string{
32+
strategyv1alpha1.JobStrategyKind,
33+
strategyv1alpha1.VeleroStrategyKind,
34+
strategyv1alpha1.CNPGStrategyKind,
35+
strategyv1alpha1.AltinityStrategyKind,
36+
strategyv1alpha1.MariaDBStrategyKind,
37+
}
38+
sort.Strings(got)
39+
sort.Strings(want)
40+
if len(got) != len(want) {
41+
t.Fatalf("supportedBackupStrategyKinds: got %d kinds %v, want %d %v", len(got), got, len(want), want)
42+
}
43+
for i := range want {
44+
if got[i] != want[i] {
45+
t.Errorf("supportedBackupStrategyKinds[%d]: got %q, want %q", i, got[i], want[i])
46+
}
47+
}
48+
}
49+
1850
// TestMarkBackupJobFailed_DoesNotAppendDuplicateReady locks in the fix:
1951
// markBackupJobFailed used to `append` to Status.Conditions, which violates
2052
// the +listType=map +listMapKey=type contract on the field. Two terminal

‎internal/backupcontroller/mariadbstrategy_controller.go‎

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
310318
func (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

Comments
 (0)