Skip to content

Add restic-based backup and restore for PVCs - #2

Merged
DeanKamali merged 8 commits into
mainfrom
feat/backup-restic
Apr 20, 2026
Merged

DeanKamali merged 8 commits into
mainfrom
feat/backup-restic

Conversation

@DeanKamali

Copy link
Copy Markdown

Summary

Adds VolSync-style backup and restore for TopoLVM PVCs, pushing to any S3-compatible object store via restic. One restic repository per source PVC, keyed by <namespace>/<pvc-name> under an optional cluster-wide prefix.

Three new CRDs:

  • BackupConfig (cluster-scoped singleton named default) — global S3 endpoint/bucket, credentials + restic password Secret refs, default schedule, retention, PVC selector, keepSnapshotAfterBackup toggle.
  • PVCBackup (namespaced, per PVC) — backup state machine: Idle → Snapshotting → Cloning → Moving → Cleanup → Synced|Failed. In-process cron + manual .spec.trigger for on-demand runs.
  • Restore (namespaced) — provisions a fresh target PVC and runs a restic restore Job against it. The target PVC is intentionally not owner-referenced so deleting the Restore never drops restored data.

Each backup run creates a VolumeSnapshot, a temp PVC cloned from it, and a Job that mounts the temp PVC read-only at /data. All children carry owner references, so GC handles cleanup on CR deletion; the reconciler also deletes them explicitly in the Cleanup phase.

The BackupConfig controller watches PVCs, applies the selector + per-PVC topolvm.io/backup* annotations, creates managed PVCBackup objects, and mirrors both user-authored Secrets into every namespace that has a managed PVC. User-authored PVCBackup objects are never touched (keyed on the managed-by label).

Opt-in via --enable-backup / controller.backup.enabled=true. New flags: --restic-image (default restic/restic:0.17.3), --backup-mover-service-account, --controller-namespace. CRDs, ClusterRole additions (jobs/secrets/pods/log/volumesnapshots/backup CRDs), and Helm values wired.

Test plan

  • Unit tests (14 cases) covering manifest shaping, owner refs, env composition, retention flag propagation, region env gating, annotation-merging, selector logic, spec equality: go test ./internal/backup/... ./internal/controller/... — all green.
  • Helm render check with controller.backup.enabled=true — CRDs, RBAC, and flags all flow through.
  • New backup: e2e job (this PR) runs --focus=backup on Kubernetes 1.34.3 against the dedicated S3 bucket. Covers: write file → trigger backup → wait Synced → delete source PVC → apply Restore → wait Succeeded → verify file contents on restored PVC.
  • Full existing matrix (default, scheduler-manifest, use-legacy, storage-capacity-scoring, rwx) must stay green — this change touches existing ClusterRole and the controller Deployment template, so regression risk is non-zero.

Risk notes

  • The backup controllers only activate behind --enable-backup; the flag defaults off, so an operator-level upgrade with no value change is a no-op.
  • POD_NAMESPACE is now unconditionally set on the controller Pod via downward API (previously only set when legacy/env was configured). This is a benign change — the controller already tolerated it being unset.
  • Fork PRs will skip the backup e2e job (no secret access); the gate github.event.pull_request.head.repo.full_name == github.repository keeps noise out of those runs.

🤖 Generated with Claude Code

DeanKamali and others added 8 commits April 20, 2026 10:40
Introduces three new CRDs and three reconcilers that add VolSync-style
backup/restore for TopoLVM PVCs using restic against any S3-compatible
object store:

- BackupConfig (cluster-scoped singleton) — global S3 endpoint/bucket,
  credentials + restic password Secret refs, default schedule, retention,
  selector, and `keepSnapshotAfterBackup` toggle.
- PVCBackup (namespaced, per PVC) — backup state machine with phases
  Idle → Snapshotting → Cloning → Moving → Cleanup → Synced|Failed, an
  in-process cron, and a manual `.spec.trigger` for on-demand runs.
- Restore (namespaced) — provisions a fresh target PVC and mounts a
  restic mover Job against it. The target PVC is intentionally not
  owner-referenced so deleting the Restore never drops restored data.

Each backup run creates a VolumeSnapshot, a temp PVC cloned from it, and
a Job mounting the temp PVC read-only; restic pushes to
`s3:<endpoint>/<bucket>/[<prefix>/]<namespace>/<pvc-name>`, giving one
repository per source PVC. All children carry owner references, so GC
handles cleanup on CR deletion; the reconciler also deletes them in the
Cleanup phase at end of each run.

The BackupConfig controller watches PVCs, applies selector + per-PVC
`topolvm.io/backup*` annotation overrides, creates managed PVCBackup
objects, and mirrors both user-authored Secrets into every namespace
that has a managed PVC (user-authored PVCBackups are never touched).

Wiring: `--enable-backup`, `--restic-image`, `--backup-mover-service-
account`, `--controller-namespace` flags on topolvm-controller; new
controller.backup.* Helm values block; CRD templates; ClusterRole
additions for jobs/secrets/pods/log/volumesnapshots/backup CRDs.

Testing: 14 pure-Go unit tests cover manifest shaping, owner refs, env
composition, annotation merging, selector logic, and spec equality. A
new e2e spec (`test/e2e/backup_test.go`, opt-in via BACKUP_ENABLED=1)
walks the full PVC → backup → restore → verify loop. A new backup job
in .github/workflows/e2e-k8s.yaml runs that spec with `--focus=backup`
against Kubernetes 1.34.3 on same-repo events only (fork PRs cannot
access the repo secrets).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Dean Kamali <dean.kamali@gmail.com>
helm-docs picks up the three new controller.backup.* rows introduced by
the prior commit.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Dean Kamali <dean.kamali@gmail.com>
- Re-emit the 6 chart CRD templates with the trailing layout `make
  manifests` produces: content, blank line, `{{ end }}`, newline. The
  previous pattern (`{{ end }}` then trailing blank) diverged from the
  Makefile's xargs-printf template and broke `make check-uncommitted`.

- Regenerate config/rbac/role.yaml across every package that carries
  +kubebuilder:rbac markers. Earlier I only ran controller-gen against
  internal/controller/... which dropped the `csidrivers` resource and
  collapsed `logicalvolumes` into the wrong rule group.

No runtime behavior change; CI parity only.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Dean Kamali <dean.kamali@gmail.com>
TopoLVM's default StorageClass uses volumeBindingMode: WaitForFirstConsumer,
meaning a PVC stays Pending until a Pod references it. Both the PVCBackup
and Restore controllers were waiting for their PVCs to reach Bound before
creating the mover/restore Job — a deadlock: the PVC never binds because
no Pod exists, and the controller never creates the Pod because it's
waiting on bind.

The mover and restore Jobs are the *intended* first consumers. Create
them eagerly; the scheduler picks a node when the Pod is scheduled,
which triggers TopoLVM provisioning and binds the PVC.

The e2e test had the same shape mistake: it waited for the source PVC to
bind before creating the writer pod. Replaced with just waiting for the
writer pod to reach Succeeded, which is an implicit (and correct)
bind-and-provision check.

This is why the first backup e2e run failed at `source PVC not bound:
Pending` — the Eventually timed out at 120s because nothing was
consuming the PVC.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Dean Kamali <dean.kamali@gmail.com>
`make test` runs `gofmt -s -l` via the lint target; these three files
were out of the canonical form:
- import group ordering in internal/backup/manifests.go
- const/field alignment in api/v1/restore_types.go
- map literal alignment in internal/controller/backupconfig_controller_test.go

No code behavior change.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Dean Kamali <dean.kamali@gmail.com>
`generate-legacy-api` in the Makefile copies api/v1/* over api/legacy/v1/
verbatim. The prior commit ran gofmt -s on api/v1/restore_types.go but I
forgot to propagate that same formatting to the legacy mirror, so
check-uncommitted saw a drift on CI.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Dean Kamali <dean.kamali@gmail.com>
The lint step in `make test` runs `gofmt -s -l .` across the entire repo.
These two files had const-block alignment that diverged from gofmt's
canonical form after my edits. No behavior change.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Dean Kamali <dean.kamali@gmail.com>
Eight findings surfaced by CI's golangci-lint that local build didn't
catch:

- lll (max 120 chars): five long lines split across run.go,
  pkg/controller/backup_controllers.go, and test/e2e/backup_test.go.
- unparam: removed unused `cfg backup.RuntimeConfig` parameter from
  PVCBackupReconciler.advanceMoving and .advanceCleanup — neither
  touched the config, only the CR status and child objects.
- staticcheck QF1008: simplified pb.Status.LastSyncTime.Time.After(anchor)
  to pb.Status.LastSyncTime.After(anchor) — metav1.Time embeds time.Time
  so the selector is redundant.

No behavior change.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Dean Kamali <dean.kamali@gmail.com>
@DeanKamali
DeanKamali merged commit 0950f9c into main Apr 20, 2026
29 checks passed
@DeanKamali
DeanKamali deleted the feat/backup-restic branch April 20, 2026 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant