Skip to content

Commit c5f30e6

Browse files
authored
feat(operator): Enable snapshot on master only (#367)
Signed-off-by: Kathleen <[email protected]>
1 parent ca0b8fa commit c5f30e6

File tree

8 files changed

+35
-1
lines changed

8 files changed

+35
-1
lines changed

api/v1alpha1/dragonfly_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ type Snapshot struct {
218218
// +kubebuilder:validation:Optional
219219
Cron string `json:"cron,omitempty"`
220220

221+
// (Optional) Enable snapshot on master only
222+
// +optional
223+
// +kubebuilder:validation:Optional
224+
EnableOnMasterOnly bool `json:"enableOnMasterOnly,omitempty"`
225+
221226
// (Optional) Dragonfly PVC spec
222227
// +optional
223228
// +kubebuilder:validation:Optional

charts/dragonfly-operator/templates/crds.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,6 +3147,9 @@ spec:
31473147
cron:
31483148
description: (Optional) Dragonfly snapshot schedule
31493149
type: string
3150+
enableOnMasterOnly:
3151+
description: (Optional) Enable snapshot on master only
3152+
type: boolean
31503153
dir:
31513154
description: |-
31523155
(Optional) The path to the snapshot directory

config/crd/bases/dragonflydb.io_dragonflies.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6388,6 +6388,9 @@ spec:
63886388
This can also be an S3 URI with the prefix `s3://` when
63896389
using S3 as the snapshot backend
63906390
type: string
6391+
enableOnMasterOnly:
6392+
description: (Optional) Enable snapshot on master only
6393+
type: boolean
63916394
persistentVolumeClaimSpec:
63926395
description: (Optional) Dragonfly PVC spec
63936396
properties:

config/samples/pvc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ spec:
1919
memory: 750Mi
2020
snapshot:
2121
cron: "*/5 * * * *"
22+
enableOnMasterOnly: false
2223
persistentVolumeClaimSpec:
2324
accessModes:
2425
- ReadWriteOnce

internal/controller/dragonfly_instance.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,13 @@ func (dfi *DragonflyInstance) replicaOf(ctx context.Context, pod *corev1.Pod, ma
390390
return fmt.Errorf("response of `SLAVE OF` on replica is not OK: %s", resp)
391391
}
392392

393+
if dfi.df.Spec.Snapshot != nil && dfi.df.Spec.Snapshot.EnableOnMasterOnly {
394+
dfi.log.Info("clearing snapshot cron schedule on replica", "pod", pod.Name)
395+
if _, err := redisClient.ConfigSet(ctx, "snapshot_cron", "").Result(); err != nil {
396+
return fmt.Errorf("failed to clear snapshot_cron on replica %s: %w", pod.Name, err)
397+
}
398+
}
399+
393400
dfi.log.Info("marking pod role as replica", "pod", pod.Name)
394401
patchFrom := client.MergeFrom(pod.DeepCopy())
395402
pod.Labels[resources.RoleLabelKey] = resources.Replica
@@ -418,6 +425,14 @@ func (dfi *DragonflyInstance) replicaOfNoOne(ctx context.Context, pod *corev1.Po
418425
return fmt.Errorf("response of `SLAVE OF NO ONE` on master is not OK: %s", resp)
419426
}
420427

428+
if dfi.df.Spec.Snapshot != nil && dfi.df.Spec.Snapshot.EnableOnMasterOnly {
429+
dfi.log.Info("setting snapshot cron schedule on master", "pod", pod.Name)
430+
cron := dfi.df.Spec.Snapshot.Cron
431+
if _, err := redisClient.ConfigSet(ctx, "snapshot_cron", cron).Result(); err != nil {
432+
return fmt.Errorf("failed to set snapshot_cron on master %s: %w", pod.Name, err)
433+
}
434+
}
435+
421436
dfi.log.Info("marking pod role as master", "pod", pod.Name)
422437
patchFrom := client.MergeFrom(pod.DeepCopy())
423438
pod.Labels[resources.RoleLabelKey] = resources.Master

internal/controller/dragonfly_pod_lifecycle_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
2324
"github.com/dragonflydb/dragonfly-operator/internal/resources"
2425
corev1 "k8s.io/api/core/v1"
2526
"k8s.io/apimachinery/pkg/types"

manifests/crd.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6380,7 +6380,10 @@ spec:
63806380
properties:
63816381
cron:
63826382
description: (Optional) Dragonfly snapshot schedule
6383-
type: string
6383+
type: string
6384+
enableOnMasterOnly:
6385+
description: (Optional) Enable snapshot on master only
6386+
type: boolean
63846387
dir:
63856388
description: |-
63866389
(Optional) The path to the snapshot directory

manifests/dragonfly-operator.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6394,6 +6394,9 @@ spec:
63946394
cron:
63956395
description: (Optional) Dragonfly snapshot schedule
63966396
type: string
6397+
enableOnMasterOnly:
6398+
description: (Optional) Enable snapshot on master only
6399+
type: boolean
63976400
dir:
63986401
description: |-
63996402
(Optional) The path to the snapshot directory

0 commit comments

Comments
 (0)