Skip to content

Commit 0c2f131

Browse files
committed
feat(backups): back up Valkey with the Redis strategy
Valkey runs on the same spotahome RedisFailover operator as Redis and speaks the same replication protocol and RDB format, so it needs no strategy kind of its own. The only difference the backup Job sees is the chart's resource-name prefix: valkey-<app> instead of redis-<app> for the sentinel Service and the auth Secret. The Redis driver now accepts applicationRef kind Valkey as well as Redis and hands the template .Release.Prefix, derived from the app's kind, which cozy-default-redis uses for every name it builds. A kind with no known prefix fails the render instead of producing an empty one. cozy-default routes apps.cozystack.io/Valkey to cozy-default-redis. A restore must target an app of the same kind as the backup's source: replaying a Redis dump into Valkey, or the reverse, is not something the strategy can promise across engine releases. The shipped template reads the prefix as `.Release.Prefix | default "redis-"`: during an upgrade the previous controller, which has no Prefix in its render context and serves only Redis, renders the new CR, and a bare reference would print "<no value>" into the names. The redis:8 image already loads RDB v11, which both shipped Valkey versions (7.2, 8.1) write. Signed-off-by: Andrey Kolkov <[email protected]> Assisted-By: LLM
1 parent 3a46360 commit 0c2f131

12 files changed

Lines changed: 263 additions & 57 deletions

File tree

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ const (
2929
// +kubebuilder:subresource:status
3030
// +kubebuilder:resource:scope=Cluster
3131

32-
// Redis defines a backup strategy for a spotahome RedisFailover app. The
33-
// spotahome operator ships no backup CRD, so the strategy runs a one-shot Job
34-
// that dumps an RDB snapshot to object storage (and replays it on restore) -
35-
// the same PodTemplateSpec model as the Job/Altinity strategies rather than an
36-
// operator-CR delegation.
32+
// Redis defines a backup strategy for a spotahome RedisFailover app, Redis or
33+
// Valkey. The spotahome operator ships no backup CRD, so the strategy runs a
34+
// one-shot Job that dumps an RDB snapshot to object storage (and replays it on
35+
// restore) - the same PodTemplateSpec model as the Job/Altinity strategies
36+
// rather than an operator-CR delegation.
3737
type Redis struct {
3838
metav1.TypeMeta `json:",inline"`
3939
metav1.ObjectMeta `json:"metadata,omitempty"`
@@ -61,6 +61,10 @@ type RedisSpec struct {
6161
// `.Application.spec.replicas`).
6262
// - `.Release.Name` and `.Release.Namespace` — the application's name
6363
// and namespace.
64+
// - `.Release.Prefix` — the prefix the application's chart puts before
65+
// the name of every resource it creates: `redis-` for a Redis
66+
// application, `valkey-` for a Valkey one. Both run on the same
67+
// RedisFailover operator, so one strategy serves either kind.
6468
// - `.Mode` — `"backup"`, `"restore"`, or `"cleanup"` (delete the object
6569
// of a removed Backup), identifying the run.
6670
// - `.ObjectKey` — the object-storage key for this run: unique per backup,

‎docs/operations/backup-classes.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ Tenants reference `cozy-default` from `BackupJob`, `Plan`, and `RestoreJob` reso
1818
| `apps.cozystack.io/Etcd` | etcd-operator snapshot | `strategy.backups.cozystack.io/Etcd` `cozy-default-etcd` |
1919
| `apps.cozystack.io/RabbitMQ` | RabbitMQ definitions (management API) | `strategy.backups.cozystack.io/Rabbitmq` `cozy-default-rabbitmq` |
2020
| `apps.cozystack.io/Redis` | RDB dump Job (sentinel-discovered master) | `strategy.backups.cozystack.io/Redis` `cozy-default-redis` |
21+
| `apps.cozystack.io/Valkey` | RDB dump Job (sentinel-discovered master) | `strategy.backups.cozystack.io/Redis` `cozy-default-redis` |
2122
| `apps.cozystack.io/VMInstance` | Velero + kubevirt-velero-plugin | `strategy.backups.cozystack.io/Velero` `cozy-default-velero-vminstance` |
2223
| `apps.cozystack.io/VMDisk` | Velero | `strategy.backups.cozystack.io/Velero` `cozy-default-velero-vmdisk` |
2324

25+
Valkey runs on the same RedisFailover operator as Redis, so `cozy-default-redis` serves both: the driver hands the template `.Release.Prefix` (`redis-` or `valkey-`, after the app's Kind), from which it builds the Sentinel Service and the auth and CA Secret names. A Valkey backup restores only into a Valkey app, a Redis backup only into a Redis app. The Valkey chart has no TLS option, so a Valkey dump always connects in plaintext.
26+
2427
### Shipped but NOT bound (admin opt-in required)
2528

2629
| Application Kind | Driver | Strategy CR |

‎examples/backups/redis/redis-strategy.yaml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ spec:
2828
# presence — mirrors the shipped strategy.
2929
- name: redis-ca
3030
secret:
31-
secretName: "redis-{{ .Release.Name }}.ca-cert"
31+
secretName: '{{ .Release.Prefix | default "redis-" }}{{ .Release.Name }}.ca-cert'
3232
optional: true
3333
- name: s3-ca
3434
secret:
@@ -51,7 +51,7 @@ spec:
5151
- name: REDIS_PASSWORD
5252
valueFrom:
5353
secretKeyRef:
54-
name: "redis-{{ .Release.Name }}-auth"
54+
name: '{{ .Release.Prefix | default "redis-" }}{{ .Release.Name }}-auth'
5555
key: password
5656
optional: true
5757
# S3 coordinates come from this demo's Bucket: the access keys are
@@ -137,7 +137,7 @@ spec:
137137
RTLS="--tls --cacert /etc/redis-ca/ca.crt"
138138
fi
139139
140-
SENTINEL="rfs-redis-{{ .Release.Name }}"
140+
SENTINEL="rfs-{{ .Release.Prefix | default "redis-" }}{{ .Release.Name }}"
141141
addr=$(redis-cli ${RTLS} -h "${SENTINEL}" -p 26379 sentinel get-master-addr-by-name mymaster)
142142
MHOST=$(echo "${addr}" | sed -n '1p')
143143
MPORT=$(echo "${addr}" | sed -n '2p')

‎hack/redis-backup-strategy.bats‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,32 @@ load test_helper
2525

2626
CHART=packages/system/backupstrategy-controller
2727

28-
# Render the shipped strategy's in-Pod script into $1, substitute the three
29-
# controller-render placeholders (Mode, Release.Name, ObjectKey) with the given
30-
# values ($2 mode, $3 app, $4 object key), and rebind every absolute path it
31-
# touches into $5 so it runs unprivileged. An empty render is an early failure,
32-
# and no real /etc or /tmp path may survive the rebinding — these tests EXECUTE
33-
# the script.
28+
# Render the shipped strategy's in-Pod script into $1, substitute the
29+
# controller-render placeholders (Mode, Release.Name, ObjectKey, Release.Prefix)
30+
# with the given values ($2 mode, $3 app, $4 object key, $6 chart prefix,
31+
# redis- by default), and rebind every absolute path it touches into $5 so it
32+
# runs unprivileged. An empty render is an early failure, as is a placeholder
33+
# left unsubstituted, and no real /etc or /tmp path may survive the rebinding —
34+
# these tests EXECUTE the script.
3435
render_script() {
35-
out=$1; mode=$2; app=$3; key=$4; sandbox=$5
36+
out=$1; mode=$2; app=$3; key=$4; sandbox=$5; prefix=${6:-redis-}
3637
mkdir -p "$sandbox/redis-ca" "$sandbox/s3-ca" "$sandbox/restore"
3738
helm template t "$CHART" -n cozy-backup-controller \
3839
--set backupStorage.bucketNameOverride=test-bucket 2>/dev/null \
3940
| yq eval 'select(.kind == "Redis") | .spec.template.spec.containers[0].args[0]' - \
4041
| sed -e "s#{{ .Mode }}#${mode}#g" \
42+
-e "s#{{ .Release.Prefix | default \"redis-\" }}#${prefix}#g" \
4143
-e "s#{{ .Release.Name }}#${app}#g" \
4244
-e "s#{{ .ObjectKey }}#${key}#g" \
4345
-e "s#/etc/redis-ca#${sandbox}/redis-ca#g" \
4446
-e "s#/etc/s3-ca#${sandbox}/s3-ca#g" \
4547
-e "s#/tmp/restore#${sandbox}/restore#g" \
4648
-e "s#/tmp/dump.rdb#${sandbox}/dump.rdb#g" > "$out"
4749
[ -s "$out" ] || return 1
50+
if grep -q '{{' "$out"; then
51+
echo "rendered script still holds a controller placeholder" >&2
52+
return 1
53+
fi
4854
# The exact absolute paths the rebinding above replaces; a survivor means the
4955
# script grew a new one and the test would touch the real filesystem. Matched
5056
# literally (not a bare /tmp/, which the sandbox itself lives under in CI).
@@ -131,6 +137,17 @@ s3_env() {
131137
if grep -qi 'FLUSHALL' "$tmp/rc.log"; then echo "FAIL: backup must not flush the source"; false; fi
132138
}
133139

140+
@test "a Valkey backup resolves rfs-valkey-<app>, the valkey chart's sentinel" {
141+
tmp=$(mktemp -d)
142+
make_stubs "$tmp/bin" "$tmp/rc.log" "$tmp/curl.log"
143+
render_script "$tmp/s.sh" backup myapp "tenant-x/myapp/bkp-42.rdb" "$tmp/sb" valkey-
144+
s3_env
145+
PATH="$tmp/bin:$PATH" bash "$tmp/s.sh"
146+
grep -q -- '-h rfs-valkey-myapp -p 26379' "$tmp/rc.log"
147+
grep -q -- '-X PUT' "$tmp/curl.log"
148+
if grep -q 'rfs-redis-' "$tmp/rc.log"; then echo "FAIL: a Valkey backup must not query the redis sentinel"; false; fi
149+
}
150+
134151
@test "an unrecognised mode exits non-zero without touching the master" {
135152
tmp=$(mktemp -d)
136153
make_stubs "$tmp/bin" "$tmp/rc.log" "$tmp/curl.log"

‎internal/backupcontroller/backup_controller_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,3 +602,29 @@ func TestBackupCleanup_Redis_NoObjectKeyIsNoOp(t *testing.T) {
602602
t.Errorf("expected no cleanup Job for a keyless Backup, got %d", len(jobs.Items))
603603
}
604604
}
605+
606+
// TestBackupCleanup_Redis_UnknownKindRendersNothing pins that a Backup routed
607+
// to the Redis strategy for a kind with no chart prefix is released without a
608+
// Job, rather than rendered with an empty prefix.
609+
func TestBackupCleanup_Redis_UnknownKindRendersNothing(t *testing.T) {
610+
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "tenant-test"}}
611+
backup := redisCleanupBackup("redis-odd", "tenant-test/cache/redis-odd.rdb")
612+
backup.Spec.ApplicationRef.Kind = "Postgres"
613+
strategy := newRedisCleanupStrategy("cozy-default-redis")
614+
c, s := newRabbitmqBackupReconcilerClient(ns, backup, strategy)
615+
r := &BackupReconciler{Client: c, Scheme: s, Recorder: record.NewFakeRecorder(10)}
616+
res, err := r.cleanupOnDelete(context.Background(), backup)
617+
if err != nil {
618+
t.Fatalf("cleanupOnDelete returned %v", err)
619+
}
620+
if res.RequeueAfter != 0 {
621+
t.Error("expected the Backup released, not requeued")
622+
}
623+
jobs := &batchv1.JobList{}
624+
if err := c.List(context.Background(), jobs, client.InNamespace("tenant-test")); err != nil {
625+
t.Fatalf("list jobs: %v", err)
626+
}
627+
if len(jobs.Items) != 0 {
628+
t.Errorf("expected no cleanup Job for a kind with no prefix, got %d", len(jobs.Items))
629+
}
630+
}

‎internal/backupcontroller/redisstrategy_controller.go‎

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ import (
2929
// Constants
3030
// ---------------------------------------------------------------------------
3131

32+
// redisAppPrefixes maps each application Kind this driver serves to the
33+
// resource-name prefix its chart puts in front of the app name (RedisFailover,
34+
// sentinel Service, auth and CA Secrets). Valkey runs on the same RedisFailover
35+
// operator, so the one strategy dumps and restores both; the template reads the
36+
// prefix as .Release.Prefix.
37+
var redisAppPrefixes = map[string]string{
38+
"Redis": "redis-",
39+
"Valkey": "valkey-",
40+
}
41+
3242
const (
3343
redisAppKind = "Redis"
3444

@@ -72,15 +82,15 @@ const (
7282
redisDefaultBackupDeadline = 20 * time.Minute
7383
)
7484

75-
// validateRedisApplicationRef rejects ApplicationRefs that are not
76-
// apps.cozystack.io/Redis. Without this gate the dispatcher would route
77-
// non-Redis refs to the Redis driver and the templated redis-dump invocation
78-
// would fail with confusing runtime errors. Mirrors
85+
// validateRedisApplicationRef rejects ApplicationRefs that are not one of the
86+
// apps.cozystack.io kinds in redisAppPrefixes. Without this gate the dispatcher
87+
// would route other refs to the Redis driver and the templated redis-cli
88+
// invocation would fail with confusing runtime errors. Mirrors
7989
// validateAltinityApplicationRef; empty APIGroup is accepted and treated as
8090
// the default (apps.cozystack.io).
8191
func validateRedisApplicationRef(ref corev1.TypedLocalObjectReference) error {
82-
if ref.Kind != redisAppKind {
83-
return fmt.Errorf("redis strategy supports applicationRef.kind=%q, got %q", redisAppKind, ref.Kind)
92+
if _, ok := redisAppPrefixes[ref.Kind]; !ok {
93+
return fmt.Errorf("redis strategy supports applicationRef.kind Redis or Valkey, got %q", ref.Kind)
8494
}
8595
apiGroup := ""
8696
if ref.APIGroup != nil {
@@ -187,15 +197,20 @@ func redisRestoreObjectKey(b *backupsv1alpha1.Backup) string {
187197
func renderRedisTemplate(
188198
tmpl corev1.PodTemplateSpec,
189199
app map[string]any,
190-
releaseName, releaseNamespace, mode, objectKey string,
200+
appKind, releaseName, releaseNamespace, mode, objectKey string,
191201
parameters map[string]string,
192202
backup *backupsv1alpha1.Backup,
193203
) (*corev1.PodTemplateSpec, error) {
204+
prefix, ok := redisAppPrefixes[appKind]
205+
if !ok {
206+
return nil, fmt.Errorf("no resource-name prefix for application kind %q", appKind)
207+
}
194208
ctxMap := map[string]any{
195209
"Application": app,
196210
"Release": map[string]string{
197211
"Name": releaseName,
198212
"Namespace": releaseNamespace,
213+
"Prefix": prefix,
199214
},
200215
"Mode": mode,
201216
"ObjectKey": objectKey,
@@ -279,7 +294,7 @@ func (r *BackupJobReconciler) reconcileRedis(ctx context.Context, j *backupsv1al
279294
app, err := r.getApplicationUnstructured(ctx, j.Namespace, j.Spec.ApplicationRef)
280295
if err != nil {
281296
if apierrors.IsNotFound(err) || apimeta.IsNoMatchError(err) {
282-
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("Redis application not found or kind not registered: %s/%s", j.Namespace, j.Spec.ApplicationRef.Name))
297+
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("%s application not found or kind not registered: %s/%s", j.Spec.ApplicationRef.Kind, j.Namespace, j.Spec.ApplicationRef.Name))
283298
}
284299
return ctrl.Result{}, err
285300
}
@@ -300,14 +315,14 @@ func (r *BackupJobReconciler) reconcileRedis(ctx context.Context, j *backupsv1al
300315
if proceed, reason, message := redisAppReady(app); !proceed {
301316
if redisBackupDeadlineExceeded(j.Status.StartedAt) {
302317
return r.markBackupJobFailed(ctx, j, fmt.Sprintf(
303-
"Redis application %s/%s not ready within %s (%s: %s)",
304-
j.Namespace, j.Spec.ApplicationRef.Name, redisDefaultBackupDeadline, reason, message))
318+
"%s application %s/%s not ready within %s (%s: %s)",
319+
j.Spec.ApplicationRef.Kind, j.Namespace, j.Spec.ApplicationRef.Name, redisDefaultBackupDeadline, reason, message))
305320
}
306321
apimeta.SetStatusCondition(&j.Status.Conditions, metav1.Condition{
307322
Type: "Ready",
308323
Status: metav1.ConditionFalse,
309324
Reason: "ApplicationNotReady",
310-
Message: fmt.Sprintf("Redis application %s/%s is not ready (%s: %s)", j.Namespace, j.Spec.ApplicationRef.Name, reason, message),
325+
Message: fmt.Sprintf("%s application %s/%s is not ready (%s: %s)", j.Spec.ApplicationRef.Kind, j.Namespace, j.Spec.ApplicationRef.Name, reason, message),
311326
})
312327
if updateErr := r.Status().Update(ctx, j); updateErr != nil {
313328
return ctrl.Result{}, updateErr
@@ -322,6 +337,7 @@ func (r *BackupJobReconciler) reconcileRedis(ctx context.Context, j *backupsv1al
322337
rendered, err := renderRedisTemplate(
323338
strategy.Spec.Template,
324339
app,
340+
j.Spec.ApplicationRef.Kind,
325341
j.Spec.ApplicationRef.Name,
326342
j.Namespace,
327343
redisModeBackup,
@@ -549,6 +565,12 @@ func (r *RestoreJobReconciler) reconcileRedisRestore(ctx context.Context, restor
549565
if err := validateRedisApplicationRef(targetRef); err != nil {
550566
return r.markRestoreJobFailed(ctx, restoreJob, err.Error())
551567
}
568+
// Redis and Valkey diverge in commands and RDB versions across releases, so
569+
// a cross-engine replay is not something the strategy can promise.
570+
if targetAppKind != backup.Spec.ApplicationRef.Kind {
571+
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf(
572+
"cannot restore a %s backup into a %s application", backup.Spec.ApplicationRef.Kind, targetAppKind))
573+
}
552574

553575
if restoreJob.Status.StartedAt == nil {
554576
fresh := &backupsv1alpha1.RestoreJob{}
@@ -582,8 +604,8 @@ func (r *RestoreJobReconciler) reconcileRedisRestore(ctx context.Context, restor
582604
if err != nil {
583605
if apierrors.IsNotFound(err) || apimeta.IsNoMatchError(err) {
584606
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf(
585-
"target Redis application not found or kind not registered: %s/%s (deploy it before requesting a restore)",
586-
targetNamespace, targetAppName))
607+
"target %s application not found or kind not registered: %s/%s (deploy it before requesting a restore)",
608+
targetAppKind, targetNamespace, targetAppName))
587609
}
588610
return ctrl.Result{}, err
589611
}
@@ -601,14 +623,14 @@ func (r *RestoreJobReconciler) reconcileRedisRestore(ctx context.Context, restor
601623
if proceed, reason, message := redisAppReady(app); !proceed {
602624
if redisBackupDeadlineExceeded(restoreJob.Status.StartedAt) {
603625
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf(
604-
"target Redis application %s/%s not ready within %s (%s: %s)",
605-
targetNamespace, targetAppName, redisDefaultBackupDeadline, reason, message))
626+
"target %s application %s/%s not ready within %s (%s: %s)",
627+
targetAppKind, targetNamespace, targetAppName, redisDefaultBackupDeadline, reason, message))
606628
}
607629
apimeta.SetStatusCondition(&restoreJob.Status.Conditions, metav1.Condition{
608630
Type: "Ready",
609631
Status: metav1.ConditionFalse,
610632
Reason: "ApplicationNotReady",
611-
Message: fmt.Sprintf("target Redis application %s/%s is not ready (%s: %s)", targetNamespace, targetAppName, reason, message),
633+
Message: fmt.Sprintf("target %s application %s/%s is not ready (%s: %s)", targetAppKind, targetNamespace, targetAppName, reason, message),
612634
})
613635
if updateErr := r.Status().Update(ctx, restoreJob); updateErr != nil {
614636
return ctrl.Result{}, updateErr
@@ -622,6 +644,7 @@ func (r *RestoreJobReconciler) reconcileRedisRestore(ctx context.Context, restor
622644
rendered, err := renderRedisTemplate(
623645
strategy.Spec.Template,
624646
app,
647+
targetAppKind,
625648
targetAppName,
626649
targetNamespace,
627650
redisModeRestore,
@@ -759,6 +782,7 @@ func (r *BackupReconciler) cleanupRedisBackup(ctx context.Context, backup *backu
759782
rendered, rerr := renderRedisTemplate(
760783
strategy.Spec.Template,
761784
nil,
785+
backup.Spec.ApplicationRef.Kind,
762786
backup.Spec.ApplicationRef.Name,
763787
backup.Namespace,
764788
redisModeCleanup,

0 commit comments

Comments
 (0)