Files
cozystack/internal/backupcontroller/mariadbstrategy_controller.go
T
Aleksei Sviridkin 526f08e17d fix(mariadb): restore the dump a Backup was taken as (#5227)
## What this PR does

A RestoreJob that names a MariaDB `Backup` restored the newest dump of
the application, not the dump of that `Backup`. The driver created the
operator `Restore` with only `backupRef`, and mariadb-operator then
restores the dump nearest to the moment the restore starts, from the
prefix every run of the app shares. Restoring an older `Backup` brought
back the latest data, and when its own dump was already gone it silently
restored another one.

The `Backup` now records the dump its operator `Backup` wrote, the file
name in `k8s.mariadb.com/dump-file` and the timestamp from that name in
`k8s.mariadb.com/dump-time`. They come from the ownership-proven match
the size probe already makes, and nothing is recorded when ownership is
not proven. A restore first checks with a bounded S3 listing of the
operator `Backup`'s own storage that the dump still exists, then sets
`targetRecoveryTime` on the operator `Restore` to the dump's own
timestamp, which mariadb-operator matches exactly. If the dump is gone
the RestoreJob fails and names it. If the check cannot be made it waits
with `Ready=False/DumpCheckPending` until the restore deadline and then
fails with the cause.

`Backup`s taken before this stay restorable. The restore makes the same
ownership-proven match against their operator `Backup` and pins the dump
when it is provable, otherwise it restores the newest dump as before and
a `BestEffortDump` warning event on the RestoreJob says so. One case is
not covered: the `Backup`'s own dump is gone, a neighbour's dump from
within the minute of slack is still there and its operator `Backup` was
deleted, so that dump is taken as the match. Nothing in the platform
deletes operator `Backup`s, it needs someone to delete one by hand. A
`Backup` made of an operator `Backup` whose Job failed (`Complete` with
reason `JobFailed`) has no dump, so its RestoreJob now fails instead of
restoring another run's dump.

Stacked on https://github.com/cozystack/cozystack/pull/5223 and targets
its branch until that merges, only the last three commits belong here.

Checked on a test cluster with mariadb-operator 25.10.2. An operator
`Restore` of an older `Backup` with `targetRecoveryTime` set to its dump
timestamp restored exactly that dump (`obtained target backup` names it
and a row inserted after it is absent from the target), the same
`Restore` without the target restored the newest one. Against the real
bucket and operator `Backup`s the recording probe attributed to each
`Backup` the dump its backup pod logged, the existence check passed for
those dumps and refused a pruned one, legacy `Backup`s with a provable
dump got pinned while back-to-back runs fell back with the warning, and
the two failed runs on that cluster were refused. The controller itself
was not run on the cluster, only its code driven by a test against the
real bucket.

Fixes https://github.com/cozystack/cozystack/issues/5196

### Screenshots

Not a UI change.

### Downstream repositories

The diff is confined to the backup controller's Go code. It changes no
CRD or API type (`driverMetadata` is an opaque map), no chart, values,
package layout or `hack/` script, so no entry of the trigger map
applies.

- [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(mariadb): a RestoreJob now restores the dump of the Backup it names instead of the newest dump of the application. It fails with a clear message when that dump no longer exists in storage, or when the Backup was made of a failed backup run. Backups taken before this release are restored by the one dump that can be matched to them, or else, as before, by the newest dump, with a BestEffortDump warning event on the RestoreJob.
```



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

## Summary by CodeRabbit

* **New Features**
* MariaDB backups now record the selected dump’s filename and timestamp,
allowing restores to target the corresponding recovery time.
* For older backups without dump metadata, restores use a uniquely
matched dump when possible or fall back to the latest dump with a
warning.
* Restore processing verifies recorded dumps, retries temporary storage
checks, and reports failures for invalid or missing recorded dumps.
* **Bug Fixes**
* Deleting a MariaDB backup now removes its associated dump and operator
backup when ownership is verified and no restore is active. Cleanup
avoids deleting artifacts when it cannot safely identify them.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-13 20:46:45 +03:00

1051 lines
45 KiB
Go

// SPDX-License-Identifier: Apache-2.0
package backupcontroller
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1"
backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1"
"github.com/cozystack/cozystack/internal/backupcontroller/mariadbapp"
"github.com/cozystack/cozystack/internal/backupcontroller/mariadbtypes"
"github.com/cozystack/cozystack/internal/template"
)
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const (
// mariadb-operator's MariaDB ApplicationDefinition (release.prefix). The
// cozystack mariadb-rd renders the HelmRelease with releaseName =
// "mariadb-" + appName (see packages/system/mariadb-rd/cozyrds/mariadb.yaml,
// release.prefix). The chart's templates/mariadb.yaml then materialises
// the operator-side k8s.mariadb.com/MariaDB CR with metadata.name set to
// .Release.Name, so the driver must look up by the prefixed name.
// Mirrors the CNPG driver's postgresAppPrefix / cnpgClusterNameForApp.
mariadbAppKind = "MariaDB"
mariadbAppPrefix = "mariadb-"
// Driver-metadata keys persisted on Cozystack Backup artifacts. Restore
// path reads these to drive the operator Restore CR.
mariadbBackupNameKey = "k8s.mariadb.com/backup-name"
mariadbBackupNamespaceKey = "k8s.mariadb.com/backup-namespace"
mariadbStorageURIKey = "k8s.mariadb.com/storage-uri"
// The dump the operator Backup wrote: its file name under the storage
// prefix, and the RFC3339 timestamp in that name. Every run of an
// application writes under the same prefix, so only this name tells its
// dump from the others, and a restore asks for exactly that dump.
// Recorded only when findMariaDBDump proved the dump belongs to the
// operator Backup.
mariadbDumpFileKey = "k8s.mariadb.com/dump-file"
mariadbDumpTimeKey = "k8s.mariadb.com/dump-time"
// Polling cadence for the operator Backup/Restore lifecycle.
mariadbPollInterval = 5 * time.Second
// Wall-clock cap on a BackupJob waiting for the operator Backup to
// flip Complete. CNPG uses the same number for the same reason: a
// permanently-stuck Backup must not pin the BackupJob in Running and
// wedge the Plan-controller queue.
mariadbDefaultBackupDeadline = 30 * time.Minute
// Default deadline on a RestoreJob waiting for the operator Restore
// to terminate. Tenants override via spec.options.restoreTimeoutSeconds.
mariadbDefaultRestoreDeadline = 30 * time.Minute
// Retry cadence while the dump a restore asks for cannot be checked.
// Longer than mariadbPollInterval because each attempt can hold the
// RestoreJob controller's single worker for
// mariadbRestoreDumpCheckTimeout.
mariadbDumpCheckRetryInterval = 30 * time.Second
// mariadbBackupSnapshotKind is the Kind stamped onto the snapshot
// persisted in Backup.status.underlyingResources. The same shape carries
// the storage descriptor and the rendered strategy parameters used at
// backup time, so restore-time re-rendering produces deterministic
// values.
mariadbBackupSnapshotKind = "MariaDBBackupSnapshot"
)
// mariadbBackupSnapshotAPIVersion is the apiVersion stamped onto the snapshot.
// Borrows the Cozystack backups group so the field is self-typed within the
// existing API surface.
var mariadbBackupSnapshotAPIVersion = backupsv1alpha1.GroupVersion.String()
// mariadbRestoreDumpCheckTimeout bounds the object-store round trips a
// restore makes before it creates the operator Restore. The RestoreJob
// controller has a single worker, so an unreachable endpoint stalls every
// other RestoreJob for this long. A variable so tests can shorten it.
var mariadbRestoreDumpCheckTimeout = 10 * time.Second
// mariadbNameForApp returns the k8s.mariadb.com/MariaDB CR name for a
// cozystack MariaDB application instance. The mapping mirrors the mariadb
// ApplicationDefinition (release.prefix=mariadb-).
func mariadbNameForApp(appName string) string {
return mariadbAppPrefix + appName
}
// validateMariaDBApplicationRef rejects ApplicationRefs that name a
// Kind/APIGroup the MariaDB driver does not own. The driver assumes
// apps.cozystack.io/MariaDB; without this gate a ref like
// other.example.com/MariaDB would be accepted by the Kind check alone and
// then reconciled against the wrong CRD via the typed client.
func validateMariaDBApplicationRef(ref corev1.TypedLocalObjectReference) error {
if ref.Kind != mariadbAppKind {
return fmt.Errorf("MariaDB strategy supports applicationRef.kind=%q, got %q", mariadbAppKind, ref.Kind)
}
apiGroup := ""
if ref.APIGroup != nil {
apiGroup = *ref.APIGroup
}
if apiGroup != "" && apiGroup != mariadbapp.GroupName {
return fmt.Errorf("MariaDB strategy supports applicationRef.apiGroup=%q, got %q", mariadbapp.GroupName, apiGroup)
}
return nil
}
// ---------------------------------------------------------------------------
// BackupJob path
// ---------------------------------------------------------------------------
func (r *BackupJobReconciler) reconcileMariaDB(ctx context.Context, j *backupsv1alpha1.BackupJob, resolved *ResolvedBackupConfig) (ctrl.Result, error) {
logger := getLogger(ctx)
logger.Debug("reconciling MariaDB strategy", "backupjob", j.Name, "phase", j.Status.Phase)
if j.Status.Phase == backupsv1alpha1.BackupJobPhaseSucceeded ||
j.Status.Phase == backupsv1alpha1.BackupJobPhaseFailed {
return ctrl.Result{}, nil
}
if err := validateMariaDBApplicationRef(j.Spec.ApplicationRef); err != nil {
return r.markBackupJobFailed(ctx, j, err.Error())
}
if j.Status.StartedAt == nil {
// Refetch the latest persisted state before writing StartedAt: a
// stale informer cache that returns StartedAt==nil after we already
// persisted it would otherwise let the deadline gate slide forward
// on every poll. Same idempotency pattern as the CNPG driver.
fresh := &backupsv1alpha1.BackupJob{}
if err := r.Get(ctx, types.NamespacedName{Namespace: j.Namespace, Name: j.Name}, fresh); err != nil {
return ctrl.Result{}, err
}
if fresh.Status.StartedAt != nil {
j.Status.StartedAt = fresh.Status.StartedAt
} else {
base := fresh.DeepCopy()
now := metav1.Now()
fresh.Status.StartedAt = &now
if err := r.Status().Patch(ctx, fresh, client.MergeFrom(base)); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{RequeueAfter: mariadbPollInterval}, nil
}
}
strategy := &strategyv1alpha1.MariaDB{}
if err := r.Get(ctx, client.ObjectKey{Name: resolved.StrategyRef.Name}, strategy); err != nil {
if apierrors.IsNotFound(err) {
return r.requeueStrategyNotReady(ctx, j, resolved.StrategyRef.Name)
}
return ctrl.Result{}, err
}
app, err := r.getMariaDBApp(ctx, j.Namespace, j.Spec.ApplicationRef.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("MariaDB application not found: %s/%s", j.Namespace, j.Spec.ApplicationRef.Name))
}
return ctrl.Result{}, err
}
rendered, err := renderMariaDBTemplate(strategy.Spec.Template, app, resolved.Parameters)
if err != nil {
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("failed to template MariaDB strategy: %v", err))
}
// Operator-side MariaDB CR carries the prefixed release name (see
// mariadbNameForApp); verify it exists before we ask the operator to
// back it up. NotFound is normally transient (the chart may still be
// rendering on a fresh app), but bounded by mariadbDefaultBackupDeadline
// — otherwise a BackupJob whose applicationRef.name typo never resolves
// would pin in Running forever. StartedAt was persisted at line 132,
// so the deadline clock already started ticking on the first reconcile.
mdbName := mariadbNameForApp(j.Spec.ApplicationRef.Name)
if err := r.Get(ctx, types.NamespacedName{Namespace: j.Namespace, Name: mdbName},
&mariadbtypes.MariaDB{}); err != nil {
if apierrors.IsNotFound(err) {
if mariadbBackupDeadlineExceeded(j.Status.StartedAt) {
return r.markBackupJobFailed(ctx, j, fmt.Sprintf(
"k8s.mariadb.com/MariaDB %s/%s never reached existence within %s",
j.Namespace, mdbName, mariadbDefaultBackupDeadline))
}
apimeta.SetStatusCondition(&j.Status.Conditions, metav1.Condition{
Type: "Ready",
Status: metav1.ConditionFalse,
Reason: "MariaDBNotReady",
Message: fmt.Sprintf("waiting for k8s.mariadb.com/MariaDB %s/%s to exist", j.Namespace, mdbName),
})
if err := r.Status().Update(ctx, j); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{RequeueAfter: mariadbPollInterval}, nil
}
return ctrl.Result{}, err
}
mdbBackup, err := r.ensureMariaDBBackup(ctx, j, rendered)
if err != nil {
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("failed to ensure k8s.mariadb.com/Backup: %v", err))
}
if j.Status.Phase != backupsv1alpha1.BackupJobPhaseRunning {
j.Status.Phase = backupsv1alpha1.BackupJobPhaseRunning
if err := r.Status().Update(ctx, j); err != nil {
return ctrl.Result{}, err
}
}
cond := apimeta.FindStatusCondition(mdbBackup.Status.Conditions, mariadbtypes.ConditionTypeComplete)
switch {
case cond != nil && cond.Reason == mariadbtypes.ConditionReasonJobFailed:
return r.markBackupJobFailed(ctx, j, mariadbJobFailedMessage(ctx, r.Client, "Backup", mdbBackup.Namespace, mdbBackup.Name, cond))
case cond != nil && cond.Status == metav1.ConditionTrue && cond.Reason == mariadbtypes.ConditionReasonJobComplete:
// Backup completed successfully. Materialise the Cozystack artifact
// and finalise the BackupJob. Idempotent: if the artifact already
// exists (a previous reconcile created it and then raced on the
// status update), reuse the existing object.
if j.Status.BackupRef != nil {
return ctrl.Result{}, nil
}
artifact, err := r.createMariaDBBackupArtifact(ctx, j, resolved, mdbBackup, rendered)
if err != nil {
return r.failBackupArtifact(ctx, j, err)
}
now := metav1.Now()
j.Status.BackupRef = &corev1.LocalObjectReference{Name: artifact.Name}
j.Status.CompletedAt = &now
j.Status.Phase = backupsv1alpha1.BackupJobPhaseSucceeded
apimeta.SetStatusCondition(&j.Status.Conditions, metav1.Condition{
Type: "Ready",
Status: metav1.ConditionTrue,
Reason: "BackupCompleted",
Message: "k8s.mariadb.com Backup completed",
})
if err := r.Status().Update(ctx, j); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
default:
// Still running (Complete=False/JobRunning, Complete=False/JobNotComplete,
// JobSuspended, a reason this driver does not know, or no Complete
// condition yet). Apply a wall-clock deadline so a permanently-stuck
// Backup eventually fails the BackupJob instead of pinning it Running
// forever.
if mariadbBackupDeadlineExceeded(j.Status.StartedAt) {
return r.markBackupJobFailed(ctx, j, fmt.Sprintf(
"k8s.mariadb.com Backup did not complete within %s (%s)", mariadbDefaultBackupDeadline, mariadbCompleteDetail(cond)))
}
return ctrl.Result{RequeueAfter: mariadbPollInterval}, nil
}
}
// mariadbJobFailedMessage names the operator object whose Job failed and
// carries that Job's own Failed condition, because the operator's condition
// message is only "Failed". The operator names the Job after the object.
func mariadbJobFailedMessage(ctx context.Context, c client.Reader, kind, namespace, name string, cond *metav1.Condition) string {
message := fmt.Sprintf("k8s.mariadb.com/%s %s/%s failed (Complete=%s/%s)", kind, namespace, name, cond.Status, cond.Reason)
job := &batchv1.Job{}
if err := c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, job); err != nil {
if apierrors.IsNotFound(err) {
return fmt.Sprintf("%s; Job %s/%s not found", message, namespace, name)
}
return fmt.Sprintf("%s; reading Job %s/%s: %v", message, namespace, name, err)
}
for _, jc := range job.Status.Conditions {
if jc.Type == batchv1.JobFailed && jc.Status == corev1.ConditionTrue {
return fmt.Sprintf("%s; Job %s/%s: %s: %s", message, namespace, name, jc.Reason, jc.Message)
}
}
return fmt.Sprintf("%s; Job %s/%s reports no Failed condition", message, namespace, name)
}
// mariadbCompleteDetail describes the last Complete condition for a deadline
// failure. The message matters for reason Failed, which the operator sets
// with its own cause ("Error creating Job", "Backup not complete") rather
// than from a Job.
func mariadbCompleteDetail(cond *metav1.Condition) string {
if cond == nil {
return "no Complete condition observed"
}
if cond.Message == "" {
return fmt.Sprintf("Complete=%s/%s", cond.Status, cond.Reason)
}
return fmt.Sprintf("Complete=%s/%s: %s", cond.Status, cond.Reason, cond.Message)
}
// mariadbBackupDeadlineExceeded reports whether enough wall-clock time has
// elapsed since the BackupJob started that we should give up on a stuck
// operator Backup. Returns false when StartedAt is nil so the very first
// reconcile (which sets StartedAt) does not trip the gate.
func mariadbBackupDeadlineExceeded(startedAt *metav1.Time) bool {
if startedAt == nil {
return false
}
return time.Since(startedAt.Time) > mariadbDefaultBackupDeadline
}
// ensureMariaDBBackup creates a one-shot k8s.mariadb.com/Backup CR labelled
// with the BackupJob, or returns the existing one if a previous reconcile
// already created it. Idempotency relies on the OwningJob labels.
func (r *BackupJobReconciler) ensureMariaDBBackup(ctx context.Context, j *backupsv1alpha1.BackupJob, rendered *strategyv1alpha1.MariaDBTemplate) (*mariadbtypes.Backup, error) {
existing, err := r.findMariaDBBackupForJob(ctx, j)
if err != nil {
return nil, err
}
if existing != nil {
return existing, nil
}
storage, err := buildMariaDBBackupStorage(rendered.Storage)
if err != nil {
return nil, err
}
obj := &mariadbtypes.Backup{
ObjectMeta: metav1.ObjectMeta{
Namespace: j.Namespace,
GenerateName: fmt.Sprintf("%s-", j.Name),
Labels: map[string]string{
backupsv1alpha1.OwningJobNameLabel: j.Name,
backupsv1alpha1.OwningJobNamespaceLabel: j.Namespace,
},
},
Spec: mariadbtypes.BackupSpec{
MariaDBRef: mariadbtypes.MariaDBObjectRef{Name: mariadbNameForApp(j.Spec.ApplicationRef.Name)},
Storage: storage,
Databases: append([]string(nil), rendered.Databases...),
Compression: rendered.Compression,
LogLevel: rendered.LogLevel,
MaxRetention: rendered.MaxRetention.DeepCopy(),
// Keep the source's grant table out of the dump so a restore into a
// copy leaves the target's chart-managed users and root intact (see
// BackupSpec.IgnoreGlobalPriv).
IgnoreGlobalPriv: ptr.To(true),
},
}
if err := r.Create(ctx, obj); err != nil {
return nil, err
}
return obj, nil
}
// findMariaDBBackupForJob returns the k8s.mariadb.com/Backup labelled with
// the BackupJob's OwningJob{Name,Namespace}, if any. Returns (nil, nil)
// when no match is found.
//
// In steady state at most one Backup CR carries a given OwningJob label
// pair (ensureMariaDBBackup is idempotent by this label). The list path
// can in theory observe two if two controller replicas raced Create
// between the first list returning empty and the cache catching up. The
// driver picks list.Items[0] deterministically; the V(1) breadcrumb on
// duplicates exists so an operator triaging "wrong Backup got reused"
// has a log line to grep for instead of staring at silent reuse.
func (r *BackupJobReconciler) findMariaDBBackupForJob(ctx context.Context, j *backupsv1alpha1.BackupJob) (*mariadbtypes.Backup, error) {
list := &mariadbtypes.BackupList{}
if err := r.List(ctx, list,
client.InNamespace(j.Namespace),
client.MatchingLabels{
backupsv1alpha1.OwningJobNameLabel: j.Name,
backupsv1alpha1.OwningJobNamespaceLabel: j.Namespace,
},
); err != nil {
return nil, err
}
if len(list.Items) == 0 {
return nil, nil
}
if len(list.Items) > 1 {
names := make([]string, 0, len(list.Items))
for i := range list.Items {
names = append(names, list.Items[i].Name)
}
getLogger(ctx).Debug("multiple k8s.mariadb.com/Backup CRs match BackupJob OwningJob labels; reusing first",
"backupjob", j.Name, "namespace", j.Namespace, "matches", names, "picked", names[0])
}
return &list.Items[0], nil
}
// createMariaDBBackupArtifact materialises a Cozystack Backup resource
// carrying the metadata callers need to drive a future restore. The
// rendered storage descriptor plus the BackupClass parameters are persisted
// in status.underlyingResources so a future fallback path (see
// marshalMariaDBBackupSnapshot) can re-render them when the operator-side
// Backup CR has been reaped.
func (r *BackupJobReconciler) createMariaDBBackupArtifact(
ctx context.Context,
j *backupsv1alpha1.BackupJob,
resolved *ResolvedBackupConfig,
mdbBackup *mariadbtypes.Backup,
rendered *strategyv1alpha1.MariaDBTemplate,
) (*backupsv1alpha1.Backup, error) {
existing, probe := r.existingBackup(ctx, j.Namespace, j.Name)
if existing != nil {
return existing, nil
}
takenAt := metav1.Now()
if cond := apimeta.FindStatusCondition(mdbBackup.Status.Conditions, mariadbtypes.ConditionTypeComplete); cond != nil && !cond.LastTransitionTime.IsZero() {
takenAt = cond.LastTransitionTime
}
driverMD := map[string]string{
mariadbBackupNameKey: mdbBackup.Name,
mariadbBackupNamespaceKey: mdbBackup.Namespace,
}
if uri := mariadbBackupURI(rendered, mdbBackup); uri != "" {
driverMD[mariadbStorageURIKey] = uri
}
underlyingResources, err := marshalMariaDBBackupSnapshot(rendered, resolved.Parameters)
if err != nil {
return nil, fmt.Errorf("encode source snapshot for Backup.status.underlyingResources: %w", err)
}
status := backupsv1alpha1.BackupStatus{
Phase: backupsv1alpha1.BackupPhaseReady,
UnderlyingResources: underlyingResources,
}
// Status.Artifact carries a "this backup has an artefact at URI X"
// signal. PVC/Volume storage has no stable URI (the location is the
// operator's PVC ID), so an Artifact{URI: ""} is a worse signal than
// no Artifact at all - leave it nil instead.
if uri := driverMD[mariadbStorageURIKey]; uri != "" {
status.Artifact = &backupsv1alpha1.BackupArtifact{URI: uri}
if probe {
if dump, ok := r.attributedMariaDBDump(ctx, mdbBackup, takenAt.Time); ok {
status.Artifact.SizeBytes = dump.Size
driverMD[mariadbDumpFileKey] = dump.Name
driverMD[mariadbDumpTimeKey] = dump.Timestamp.UTC().Format(time.RFC3339)
}
}
}
backup := &backupsv1alpha1.Backup{
ObjectMeta: metav1.ObjectMeta{
Name: j.Name,
Namespace: j.Namespace,
},
Spec: backupsv1alpha1.BackupSpec{
ApplicationRef: j.Spec.ApplicationRef,
StrategyRef: resolved.StrategyRef,
TakenAt: takenAt,
DriverMetadata: driverMD,
},
Status: status,
}
if j.Spec.PlanRef != nil {
backup.Spec.PlanRef = j.Spec.PlanRef
}
if err := r.Create(ctx, backup); err != nil {
if !apierrors.IsAlreadyExists(err) {
return nil, err
}
existing := &backupsv1alpha1.Backup{}
if getErr := r.apiReader().Get(ctx, types.NamespacedName{Namespace: backup.Namespace, Name: backup.Name}, existing); getErr != nil {
return nil, fmt.Errorf("%w: %v", errBackupNotReadBack, getErr)
}
return existing, nil
}
return backup, nil
}
// mariadbBackupURI synthesises a human-readable URI for the Cozystack
// Backup artifact. Empty when the storage shape doesn't resolve to a stable
// URI (PVC/Volume, where the location is the operator's PVC ID).
func mariadbBackupURI(rendered *strategyv1alpha1.MariaDBTemplate, mdbBackup *mariadbtypes.Backup) string {
if rendered.Storage.S3 == nil {
return ""
}
s := rendered.Storage.S3
prefix := s.Prefix
if prefix != "" && prefix[len(prefix)-1] != '/' {
prefix += "/"
}
return fmt.Sprintf("s3://%s/%s%s", s.Bucket, prefix, mdbBackup.Name)
}
// ---------------------------------------------------------------------------
// RestoreJob path
// ---------------------------------------------------------------------------
func (r *RestoreJobReconciler) reconcileMariaDBRestore(ctx context.Context, restoreJob *backupsv1alpha1.RestoreJob, backup *backupsv1alpha1.Backup) (ctrl.Result, error) {
logger := getLogger(ctx)
logger.Debug("reconciling MariaDB restore", "restorejob", restoreJob.Name, "backup", backup.Name)
if err := validateMariaDBApplicationRef(backup.Spec.ApplicationRef); err != nil {
return r.markRestoreJobFailed(ctx, restoreJob, err.Error())
}
// Validate the resolved target shape before any apiserver call. This is
// cheap (in-memory state only) and lets a malformed TargetApplicationRef
// fail fast instead of after parseMariaDBRestoreOptions + the srcBackup
// Get round-trip.
target := r.resolveMariaDBRestoreTarget(restoreJob, backup)
if target.Kind != mariadbAppKind {
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf(
"target applicationRef.kind=%q is not supported by the MariaDB driver", target.Kind))
}
if target.APIGroup != "" && target.APIGroup != mariadbapp.GroupName {
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf(
"target applicationRef.apiGroup=%q is not supported by the MariaDB driver", target.APIGroup))
}
if restoreJob.Status.StartedAt == nil {
// Refetch the latest persisted state before writing StartedAt; same
// stale-cache idempotency pattern the BackupJob path uses.
fresh := &backupsv1alpha1.RestoreJob{}
if err := r.Get(ctx, types.NamespacedName{Namespace: restoreJob.Namespace, Name: restoreJob.Name}, fresh); err != nil {
return ctrl.Result{}, err
}
if fresh.Status.StartedAt != nil {
// A previous reconcile (or another replica) already persisted
// StartedAt - adopt it locally and continue inline so we don't
// waste a reconcile cycle bouncing through the cache. Mirrors
// the BackupJob path above.
restoreJob.Status.StartedAt = fresh.Status.StartedAt
if fresh.Status.Phase != "" {
restoreJob.Status.Phase = fresh.Status.Phase
}
} else {
base := fresh.DeepCopy()
now := metav1.Now()
fresh.Status.StartedAt = &now
fresh.Status.Phase = backupsv1alpha1.RestoreJobPhaseRunning
if err := r.Status().Patch(ctx, fresh, client.MergeFrom(base)); err != nil {
return ctrl.Result{}, err
}
restoreJob.Status.StartedAt = fresh.Status.StartedAt
restoreJob.Status.Phase = fresh.Status.Phase
// Requeue so the patched status round-trips via the informer
// cache before subsequent reconciles read it back.
return ctrl.Result{RequeueAfter: mariadbPollInterval}, nil
}
}
// parseMariaDBRestoreOptions is intentionally permissive (event + log,
// proceed with defaults). This diverges from the structural failures
// below (missing driverMetadata, MariaDB CR genuinely absent) because a
// bad spec.options is best-effort tenant-supplied override, not a
// load-bearing piece of the restore contract - the defaults are still
// the right behaviour, so surfacing a transient breadcrumb beats
// failing the restore on a typo.
options, err := parseMariaDBRestoreOptions(restoreJob.Spec.Options)
if err != nil {
logger.Info("malformed restoreJob.spec.options; falling back to defaults", "error", err)
r.Recorder.Eventf(restoreJob, corev1.EventTypeWarning, "MalformedOptions",
"spec.options is not valid JSON; falling back to defaults: %v", err)
}
sourceBackupName := backup.Spec.DriverMetadata[mariadbBackupNameKey]
if sourceBackupName == "" {
return r.markRestoreJobFailed(ctx, restoreJob,
"Backup driverMetadata is missing k8s.mariadb.com/backup-name (re-take the backup with a controller version that persists the operator Backup name)")
}
// Verify the operator Backup CR still exists. The operator's Restore CR
// only resolves backupRef in the same namespace as the Restore itself,
// so cross-namespace restores aren't supported here. NotFound surfaces
// as a terminal failure with a clear migration path (re-run the
// BackupJob to materialise a fresh artifact).
srcBackup := &mariadbtypes.Backup{}
if err := r.Get(ctx, types.NamespacedName{Namespace: backup.Namespace, Name: sourceBackupName}, srcBackup); err != nil {
if apierrors.IsNotFound(err) {
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf(
"k8s.mariadb.com/Backup %s/%s not found; the operator-side artifact has been reaped",
backup.Namespace, sourceBackupName))
}
return ctrl.Result{}, err
}
// The operator Restore CR replays the dump against an existing live
// MariaDB; for to-copy this requires the target MariaDB to exist. A
// tenant who fires a RestoreJob seconds before their target
// HelmRelease materialises the MariaDB CR shouldn't have to recreate
// the RestoreJob - mirror the backup path's transient handling and let
// the operator-side StartedAt + effectiveRestoreDeadline guard against
// a target that never shows up.
targetMDBName := mariadbNameForApp(target.AppName)
if err := r.Get(ctx, types.NamespacedName{Namespace: target.Namespace, Name: targetMDBName},
&mariadbtypes.MariaDB{}); err != nil {
if apierrors.IsNotFound(err) {
deadline := options.effectiveRestoreDeadline()
if restoreJob.Status.StartedAt != nil && time.Since(restoreJob.Status.StartedAt.Time) > deadline {
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf(
"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)",
target.Namespace, targetMDBName, deadline))
}
apimeta.SetStatusCondition(&restoreJob.Status.Conditions, metav1.Condition{
Type: "Ready",
Status: metav1.ConditionFalse,
Reason: "TargetMariaDBNotReady",
Message: fmt.Sprintf("waiting for target k8s.mariadb.com/MariaDB %s/%s to exist",
target.Namespace, targetMDBName),
})
if err := r.Status().Update(ctx, restoreJob); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{RequeueAfter: mariadbPollInterval}, nil
}
return ctrl.Result{}, err
}
mdbRestore, err := r.findMariaDBRestoreForJob(ctx, restoreJob)
if err != nil {
// Retried: failing the RestoreJob here could fail it under an
// operator Restore that is running.
return ctrl.Result{}, fmt.Errorf("find k8s.mariadb.com/Restore of RestoreJob %s/%s: %w", restoreJob.Namespace, restoreJob.Name, err)
}
// The dump is chosen once, as the operator Restore is created; one that
// already exists has made its choice.
if mdbRestore == nil {
targetTime, err := r.mariadbRestoreTargetTime(ctx, restoreJob, backup, srcBackup)
var refused mariadbRestoreRefused
if errors.As(err, &refused) {
return r.markRestoreJobFailed(ctx, restoreJob, refused.Error())
}
if err != nil {
return r.awaitMariaDBDumpCheck(ctx, restoreJob, options, err)
}
if mdbRestore, err = r.createMariaDBRestore(ctx, restoreJob, sourceBackupName, targetMDBName, targetTime); err != nil {
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf("failed to ensure k8s.mariadb.com/Restore: %v", err))
}
}
cond := apimeta.FindStatusCondition(mdbRestore.Status.Conditions, mariadbtypes.ConditionTypeComplete)
switch {
case cond != nil && cond.Reason == mariadbtypes.ConditionReasonJobFailed:
return r.markRestoreJobFailed(ctx, restoreJob, mariadbJobFailedMessage(ctx, r.Client, "Restore", mdbRestore.Namespace, mdbRestore.Name, cond))
case cond != nil && cond.Status == metav1.ConditionTrue && cond.Reason == mariadbtypes.ConditionReasonJobComplete:
now := metav1.Now()
restoreJob.Status.CompletedAt = &now
restoreJob.Status.Phase = backupsv1alpha1.RestoreJobPhaseSucceeded
apimeta.SetStatusCondition(&restoreJob.Status.Conditions, metav1.Condition{
Type: "Ready",
Status: metav1.ConditionTrue,
Reason: "RestoreCompleted",
Message: fmt.Sprintf("k8s.mariadb.com/Restore %s/%s completed", mdbRestore.Namespace, mdbRestore.Name),
})
if err := r.Status().Update(ctx, restoreJob); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
default:
deadline := options.effectiveRestoreDeadline()
if restoreJob.Status.StartedAt != nil && time.Since(restoreJob.Status.StartedAt.Time) > deadline {
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf(
"k8s.mariadb.com/Restore did not complete within %s (%s; override via spec.options.restoreTimeoutSeconds)",
deadline, mariadbCompleteDetail(cond)))
}
return ctrl.Result{RequeueAfter: mariadbPollInterval}, nil
}
}
// mariadbRestoreRefused is why the dump a RestoreJob asks for cannot be
// restored. It fails the RestoreJob, where any other error from choosing
// the dump is retried.
type mariadbRestoreRefused string
func (e mariadbRestoreRefused) Error() string { return string(e) }
// mariadbRestoreTargetTime returns the targetRecoveryTime of the operator
// Restore for backup: the timestamp in the name of the dump backup
// records. mariadb-operator restores the dump whose timestamp equals the
// target and, when there is none, the nearest one, so the dump is first
// checked to still exist. A Backup that records no dump is resolved by
// legacyMariaDBRestoreTargetTime.
func (r *RestoreJobReconciler) mariadbRestoreTargetTime(ctx context.Context, rj *backupsv1alpha1.RestoreJob, backup *backupsv1alpha1.Backup, src *mariadbtypes.Backup) (string, error) {
// Deleting a Backup deletes its dump, and the operator restores the
// nearest other dump once its own is gone, so a restore must not start
// from a Backup being deleted. The copy in hand can predate the delete;
// the cleanup holds the Backup only for restores that exist when it
// lists them.
live := &backupsv1alpha1.Backup{}
if err := r.apiReader().Get(ctx, client.ObjectKeyFromObject(backup), live); err != nil {
if apierrors.IsNotFound(err) {
return "", mariadbRestoreRefused(fmt.Sprintf("Backup %s/%s is gone, and its dump may be gone with it", backup.Namespace, backup.Name))
}
return "", fmt.Errorf("read Backup %s/%s: %w", backup.Namespace, backup.Name, err)
}
if !live.DeletionTimestamp.IsZero() {
return "", mariadbRestoreRefused(fmt.Sprintf("Backup %s/%s is being deleted, and its dump with it", backup.Namespace, backup.Name))
}
// A failed Job is Complete=True too, with reason JobFailed, and a
// BackupJob that took Complete=True for success made a Backup of it.
if cond := apimeta.FindStatusCondition(src.Status.Conditions, mariadbtypes.ConditionTypeComplete); cond != nil && cond.Reason == mariadbtypes.ConditionReasonJobFailed {
return "", mariadbRestoreRefused(fmt.Sprintf(
"the backup run of k8s.mariadb.com/Backup %s/%s failed (Complete=%s/%s) and left no dump to restore; restoring would bring back another run's dump",
src.Namespace, src.Name, cond.Status, cond.Reason))
}
ctx, cancel := context.WithTimeout(ctx, mariadbRestoreDumpCheckTimeout)
defer cancel()
name := backup.Spec.DriverMetadata[mariadbDumpFileKey]
if name == "" {
return r.legacyMariaDBRestoreTargetTime(ctx, rj, backup, src), nil
}
ts, ok := parseMariaDBLogicalBackupFile(name)
if !ok {
return "", mariadbRestoreRefused(fmt.Sprintf("Backup driverMetadata %s=%q is not the name of a mariadb-operator dump", mariadbDumpFileKey, name))
}
exists, err := mariadbDumpExists(ctx, r.objectStore(), src, name)
if err != nil {
return "", fmt.Errorf("cannot check that dump %s of k8s.mariadb.com/Backup %s/%s still exists in storage: %w", name, src.Namespace, src.Name, err)
}
if !exists {
s3 := src.Spec.Storage.S3
return "", mariadbRestoreRefused(fmt.Sprintf(
"dump %s of k8s.mariadb.com/Backup %s/%s no longer exists in storage (s3://%s/%s); restoring now would bring back a different dump",
name, src.Namespace, src.Name, s3.Bucket, mariadbOperatorS3Prefix(s3.Prefix)))
}
return ts.UTC().Format(time.RFC3339), nil
}
// legacyMariaDBRestoreTargetTime chooses the dump of a Backup taken before
// Backups recorded theirs: the one findMariaDBDump proves belongs to src,
// with the completion time the size probe was given, which takenAt keeps.
// When none is proven, for any reason, it returns empty, and the operator
// restores the newest dump under the prefix as such a restore always did:
// refusing would leave these Backups unrestorable, and the controller may
// never have been able to reach their storage. A Warning event says the
// dump is a best effort.
func (r *RestoreJobReconciler) legacyMariaDBRestoreTargetTime(ctx context.Context, rj *backupsv1alpha1.RestoreJob, backup *backupsv1alpha1.Backup, src *mariadbtypes.Backup) string {
dump, err := findMariaDBDump(ctx, r.objectStore(), src, backup.Spec.TakenAt.Time)
if err != nil {
getLogger(ctx).Info("no dump proven for a MariaDB Backup that records none; restoring the newest", "restorejob", rj.Name, "backup", backup.Name, "error", err)
r.Recorder.Eventf(rj, corev1.EventTypeWarning, "BestEffortDump",
"Backup %s records no dump and none in storage could be proven to belong to k8s.mariadb.com/Backup %s/%s (%v); mariadb-operator restores the newest dump under its prefix as a best effort, which may be another backup run's",
backup.Name, src.Namespace, src.Name, err)
return ""
}
return dump.Timestamp.UTC().Format(time.RFC3339)
}
// awaitMariaDBDumpCheck keeps the RestoreJob waiting, with Ready=False
// saying why, while the dump it asks for cannot be checked, and fails it
// with the cause once the restore deadline has passed.
func (r *RestoreJobReconciler) awaitMariaDBDumpCheck(ctx context.Context, rj *backupsv1alpha1.RestoreJob, options MariaDBRestoreOptions, cause error) (ctrl.Result, error) {
deadline := options.effectiveRestoreDeadline()
if rj.Status.StartedAt != nil && time.Since(rj.Status.StartedAt.Time) > deadline {
return r.markRestoreJobFailed(ctx, rj, fmt.Sprintf(
"%v; gave up after %s (override via spec.options.restoreTimeoutSeconds)", cause, deadline))
}
getLogger(ctx).Info("cannot check the MariaDB dump to restore; retrying", "restorejob", rj.Name, "error", cause)
apimeta.SetStatusCondition(&rj.Status.Conditions, metav1.Condition{
Type: "Ready",
Status: metav1.ConditionFalse,
Reason: "DumpCheckPending",
Message: cause.Error(),
})
if err := r.Status().Update(ctx, rj); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{RequeueAfter: mariadbDumpCheckRetryInterval}, nil
}
// findMariaDBRestoreForJob returns the k8s.mariadb.com/Restore labelled
// with the RestoreJob, if a previous reconcile already created one. It is
// read uncached: one the cache has not caught up with would be created
// again, or, once its Backup is being deleted, refused while it runs.
func (r *RestoreJobReconciler) findMariaDBRestoreForJob(ctx context.Context, rj *backupsv1alpha1.RestoreJob) (*mariadbtypes.Restore, error) {
list := &mariadbtypes.RestoreList{}
if err := r.apiReader().List(ctx, list,
client.InNamespace(rj.Namespace),
client.MatchingLabels{
backupsv1alpha1.OwningJobNameLabel: rj.Name,
backupsv1alpha1.OwningJobNamespaceLabel: rj.Namespace,
},
); err != nil {
return nil, err
}
if len(list.Items) == 0 {
return nil, nil
}
return &list.Items[0], nil
}
// createMariaDBRestore creates the k8s.mariadb.com/Restore of the
// RestoreJob, labelled with it. targetRecoveryTime is RFC3339, or empty.
func (r *RestoreJobReconciler) createMariaDBRestore(ctx context.Context, rj *backupsv1alpha1.RestoreJob, sourceBackupName, targetMariaDBName, targetRecoveryTime string) (*mariadbtypes.Restore, error) {
obj := &mariadbtypes.Restore{
ObjectMeta: metav1.ObjectMeta{
Namespace: rj.Namespace,
GenerateName: fmt.Sprintf("%s-", rj.Name),
Labels: map[string]string{
backupsv1alpha1.OwningJobNameLabel: rj.Name,
backupsv1alpha1.OwningJobNamespaceLabel: rj.Namespace,
},
},
Spec: mariadbtypes.RestoreSpec{
MariaDBRef: mariadbtypes.MariaDBObjectRef{Name: targetMariaDBName},
BackupRef: &mariadbtypes.BackupReference{
Kind: mariadbtypes.BackupKind,
Name: sourceBackupName,
},
TargetRecoveryTime: targetRecoveryTime,
},
}
if err := r.Create(ctx, obj); err != nil {
return nil, err
}
return obj, nil
}
// mariadbRestoreTarget captures the resolved target for a MariaDB restore.
// The reconcile path treats in-place and to-copy identically: both create a
// k8s.mariadb.com/Restore CR pointing at the named target MariaDB. Callers
// infer the mode from AppName != backup.Spec.ApplicationRef.Name.
type mariadbRestoreTarget struct {
Namespace string
AppName string
Kind string
APIGroup string
}
func (r *RestoreJobReconciler) resolveMariaDBRestoreTarget(restoreJob *backupsv1alpha1.RestoreJob, backup *backupsv1alpha1.Backup) mariadbRestoreTarget {
t := mariadbRestoreTarget{
Namespace: backup.Namespace,
AppName: backup.Spec.ApplicationRef.Name,
Kind: backup.Spec.ApplicationRef.Kind,
}
if backup.Spec.ApplicationRef.APIGroup != nil {
t.APIGroup = *backup.Spec.ApplicationRef.APIGroup
}
if restoreJob.Spec.TargetApplicationRef != nil {
if restoreJob.Spec.TargetApplicationRef.Name != "" {
t.AppName = restoreJob.Spec.TargetApplicationRef.Name
}
if restoreJob.Spec.TargetApplicationRef.Kind != "" {
t.Kind = restoreJob.Spec.TargetApplicationRef.Kind
}
if restoreJob.Spec.TargetApplicationRef.APIGroup != nil {
t.APIGroup = *restoreJob.Spec.TargetApplicationRef.APIGroup
}
}
return t
}
// ---------------------------------------------------------------------------
// Snapshot persisted on Cozystack Backup.status.underlyingResources
// ---------------------------------------------------------------------------
// mariadbBackupSnapshot is the MariaDB-specific payload persisted in
// Backup.status.underlyingResources at backup time. Carries the rendered
// storage descriptor and BackupClass parameters. The current restore path
// resolves the operator-side k8s.mariadb.com/Backup CR directly and fails
// terminally when it has been reaped; this snapshot is reserved for a
// future fallback path that would re-materialise the operator Backup from
// the persisted state. Keep the shape stable so an older snapshot stays
// machine-readable when that fallback lands.
//
// SECRET-HANDLING CONTRACT (this is plaintext-readable to anyone with read
// access to backups.cozystack.io/Backups):
//
// - Storage carries only Secret REFERENCES (Name + Key) - the
// MariaDBS3Template / MariaDBSecretKeySelector schema has no field for
// a raw credential value, so the snapshot can never contain an access
// key or secret access key. The mariadb-operator dereferences these
// names at runtime against the apiserver's Secret cache.
// - Parameters lands here verbatim from BackupClassStrategy.parameters.
// Callers MUST NOT put credentials in Parameters (it is documented on
// MariaDBSpec.Template that those values are not credential-bearing);
// a tenant who templates `{{ .Parameters.secretAccessKey }}` into a
// string field would persist that value here plaintext. The schema's
// credential-by-reference shape is the enforcement; this comment is
// the contract.
type mariadbBackupSnapshot struct {
Kind string `json:"kind"`
APIVersion string `json:"apiVersion"`
Storage *strategyv1alpha1.MariaDBStorageTemplate `json:"storage,omitempty"`
Databases []string `json:"databases,omitempty"`
Parameters map[string]string `json:"parameters,omitempty"`
}
// marshalMariaDBBackupSnapshot encodes the snapshot for persistence on the
// Cozystack Backup. See mariadbBackupSnapshot godoc for the secret-handling
// contract: Storage carries only Secret references (Name + Key), never
// raw credential values; Parameters is persisted verbatim and is plaintext.
func marshalMariaDBBackupSnapshot(rendered *strategyv1alpha1.MariaDBTemplate, parameters map[string]string) (*runtime.RawExtension, error) {
storage := rendered.Storage.DeepCopy()
snap := mariadbBackupSnapshot{
Kind: mariadbBackupSnapshotKind,
APIVersion: mariadbBackupSnapshotAPIVersion,
Storage: storage,
Databases: append([]string(nil), rendered.Databases...),
Parameters: parameters,
}
raw, err := json.Marshal(snap)
if err != nil {
return nil, err
}
return &runtime.RawExtension{Raw: raw}, nil
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
// renderMariaDBTemplate templates the strategy template against a context
// containing the live application object and the BackupClass parameters.
// Reuses the same templating helper as the CNPG / Velero strategies.
func renderMariaDBTemplate(t strategyv1alpha1.MariaDBTemplate, app *mariadbapp.MariaDB, parameters map[string]string) (*strategyv1alpha1.MariaDBTemplate, error) {
appAsMap, err := toJSONMapMariaDB(app)
if err != nil {
return nil, fmt.Errorf("encode application for templating: %w", err)
}
templateContext := map[string]any{
"Application": appAsMap,
"Parameters": parameters,
}
return template.Template(&t, templateContext)
}
// toJSONMapMariaDB converts a typed object to a generic map via JSON tags
// so user-authored go-templates address fields by their JSON names (e.g.
// .Application.metadata.name). Mirrors the helper in the CNPG controller;
// scoped here to avoid cross-strategy import.
func toJSONMapMariaDB(obj any) (map[string]any, error) {
raw, err := json.Marshal(obj)
if err != nil {
return nil, err
}
out := map[string]any{}
if err := json.Unmarshal(raw, &out); err != nil {
return nil, err
}
return out, nil
}
// getMariaDBApp fetches the apps.cozystack.io MariaDB instance via the
// shared typed client. The MariaDB scheme is registered in main.go so the
// controller-runtime cache serves it directly.
func (r *BackupJobReconciler) getMariaDBApp(ctx context.Context, namespace, name string) (*mariadbapp.MariaDB, error) {
app := &mariadbapp.MariaDB{}
if err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, app); err != nil {
return nil, err
}
return app, nil
}
// buildMariaDBBackupStorage translates the typed strategy template into the
// k8s.mariadb.com/v1alpha1 Backup.spec.storage shape. Exactly one of S3, PVC
// or Volume must be set; the CRD's XValidation rule enforces this at
// admission, but a defence-in-depth check here keeps the driver's behaviour
// well-defined for any pre-existing strategy CR that pre-dates the
// admission rule (otherwise the switch below would silently pick whichever
// branch comes first in source order).
func buildMariaDBBackupStorage(in strategyv1alpha1.MariaDBStorageTemplate) (mariadbtypes.BackupStorage, error) {
set := 0
if in.S3 != nil {
set++
}
if in.PersistentVolumeClaim != nil {
set++
}
if in.Volume != nil {
set++
}
if set != 1 {
return mariadbtypes.BackupStorage{}, fmt.Errorf(
"strategy.spec.template.storage requires exactly one of s3, persistentVolumeClaim or volume; got %d", set)
}
switch {
case in.S3 != nil:
out := &mariadbtypes.S3Storage{
Bucket: in.S3.Bucket,
Endpoint: in.S3.Endpoint,
Prefix: in.S3.Prefix,
Region: in.S3.Region,
AccessKeyIdSecretKeyRef: mariadbtypes.SecretKeySelector{
Name: in.S3.AccessKeyIDSecretKeyRef.Name,
Key: in.S3.AccessKeyIDSecretKeyRef.Key,
},
SecretAccessKeySecretKeyRef: mariadbtypes.SecretKeySelector{
Name: in.S3.SecretAccessKeySecretKeyRef.Name,
Key: in.S3.SecretAccessKeySecretKeyRef.Key,
},
}
if in.S3.SessionTokenSecretKeyRef != nil {
out.SessionTokenSecretKeyRef = &mariadbtypes.SecretKeySelector{
Name: in.S3.SessionTokenSecretKeyRef.Name,
Key: in.S3.SessionTokenSecretKeyRef.Key,
}
}
if in.S3.TLS != nil {
tls := &mariadbtypes.S3TLS{Enabled: in.S3.TLS.Enabled}
if in.S3.TLS.CASecretKeyRef != nil {
tls.CASecretKeyRef = &mariadbtypes.SecretKeySelector{
Name: in.S3.TLS.CASecretKeyRef.Name,
Key: in.S3.TLS.CASecretKeyRef.Key,
}
}
out.TLS = tls
}
return mariadbtypes.BackupStorage{S3: out}, nil
case in.PersistentVolumeClaim != nil:
return mariadbtypes.BackupStorage{PersistentVolumeClaim: in.PersistentVolumeClaim.DeepCopy()}, nil
case in.Volume != nil:
return mariadbtypes.BackupStorage{Volume: in.Volume.DeepCopy()}, nil
default:
return mariadbtypes.BackupStorage{}, fmt.Errorf("strategy.spec.template.storage requires exactly one of s3, persistentVolumeClaim or volume")
}
}
// MariaDBRestoreOptions is the typed shape of RestoreJob.Spec.Options for
// the MariaDB driver. Mirrors the CNPG strategy's RestoreOptions pattern
// so the boundary parses lazily and keeps behaviour permissive.
type MariaDBRestoreOptions struct {
// RestoreTimeoutSeconds caps the time the driver waits for the
// k8s.mariadb.com/Restore to terminate before it marks the RestoreJob
// Failed. Zero or unset falls back to mariadbDefaultRestoreDeadline.
// +optional
RestoreTimeoutSeconds int64 `json:"restoreTimeoutSeconds,omitempty"`
}
func parseMariaDBRestoreOptions(opts *runtime.RawExtension) (MariaDBRestoreOptions, error) {
var out MariaDBRestoreOptions
if opts == nil || len(opts.Raw) == 0 {
return out, nil
}
if err := json.Unmarshal(opts.Raw, &out); err != nil {
return MariaDBRestoreOptions{}, fmt.Errorf("decode restoreJob.spec.options: %w", err)
}
return out, nil
}
func (o MariaDBRestoreOptions) effectiveRestoreDeadline() time.Duration {
if o.RestoreTimeoutSeconds > 0 {
return time.Duration(o.RestoreTimeoutSeconds) * time.Second
}
return mariadbDefaultRestoreDeadline
}