Skip to content

Commit a74c88c

Browse files
authored
fix: reset sentinel config on reconcile (#1533)
Signed-off-by: yangw <[email protected]>
1 parent 7149c7b commit a74c88c

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

internal/controller/common/redis/heal.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import (
2222

2323
type Healer interface {
2424
SentinelMonitor(ctx context.Context, rs *rsvb2.RedisSentinel, master string) error
25+
// SentinelSet set the config for specific master
26+
// reference: https://redis.io/docs/latest/operate/oss_and_stack/management/sentinel/#reconfiguring-sentinel-at-runtime
27+
SentinelSet(ctx context.Context, rs *rsvb2.RedisSentinel, master string) error
2528
SentinelReset(ctx context.Context, rs *rsvb2.RedisSentinel) error
2629

2730
// UpdatePodRoleLabel connect to all redis pods and update pod role label `redis-role` to `master` or `slave` according to their role.
@@ -84,6 +87,35 @@ func (h *healer) UpdateRedisRoleLabel(ctx context.Context, ns string, labels map
8487
return nil
8588
}
8689

90+
func (h *healer) SentinelSet(ctx context.Context, rs *rsvb2.RedisSentinel, master string) error {
91+
pods, err := h.getSentinelPods(ctx, rs)
92+
if err != nil {
93+
return err
94+
}
95+
sentinelPass, err := NewChecker(h.k8s).GetPassword(ctx, rs.Namespace, rs.Spec.KubernetesConfig.ExistingPasswordSecret)
96+
if err != nil {
97+
return err
98+
}
99+
for _, pod := range pods.Items {
100+
connInfo := createConnectionInfo(ctx, pod, sentinelPass, rs.Spec.TLS, h.k8s, rs.Namespace, "26379")
101+
102+
for k, v := range map[string]string{
103+
"down-after-milliseconds": rs.Spec.RedisSentinelConfig.DownAfterMilliseconds,
104+
"parallel-syncs": rs.Spec.RedisSentinelConfig.ParallelSyncs,
105+
"failover-timeout": rs.Spec.RedisSentinelConfig.FailoverTimeout,
106+
} {
107+
if v == "" {
108+
continue
109+
}
110+
err = h.redis.Connect(connInfo).SentinelSet(ctx, rs.Spec.RedisSentinelConfig.MasterGroupName, k, v)
111+
if err != nil {
112+
return err
113+
}
114+
}
115+
}
116+
return nil
117+
}
118+
87119
// SentinelReset range all sentinel execute `sentinel reset *`
88120
func (h *healer) SentinelReset(ctx context.Context, rs *rsvb2.RedisSentinel) error {
89121
pods, err := h.getSentinelPods(ctx, rs)

internal/controller/redissentinel/redissentinel_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ func (r *RedisSentinelReconciler) reconcileSentinel(ctx context.Context, instanc
115115
if err := k8sutils.CreateRedisSentinel(ctx, r.K8sClient, instance, r.K8sClient, r.Client); err != nil {
116116
return intctrlutil.RequeueE(ctx, err, "")
117117
}
118-
119118
if instance.Spec.RedisSentinelConfig == nil {
120119
return intctrlutil.Reconciled()
121120
}
@@ -138,14 +137,15 @@ func (r *RedisSentinelReconciler) reconcileSentinel(ctx context.Context, instanc
138137
monitorAddr = master.Status.PodIP
139138
}
140139
}
141-
142140
if err := r.Healer.SentinelMonitor(ctx, instance, monitorAddr); err != nil {
143141
return intctrlutil.RequeueE(ctx, err, "")
144142
}
143+
if err := r.Healer.SentinelSet(ctx, instance, monitorAddr); err != nil {
144+
return intctrlutil.RequeueE(ctx, err, "")
145+
}
145146
if err := r.Healer.SentinelReset(ctx, instance); err != nil {
146147
return intctrlutil.RequeueE(ctx, err, "")
147148
}
148-
149149
return intctrlutil.Reconciled()
150150
}
151151

internal/service/redis/client.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type Service interface {
5454
IsMaster(ctx context.Context) (bool, error)
5555
GetAttachedReplicaCount(ctx context.Context) (int, error)
5656
SentinelMonitor(ctx context.Context, master *ConnectionInfo, masterGroupName, quorum string) error
57+
SentinelSet(ctx context.Context, masterGroupName, key, value string) error
5758
SentinelReset(ctx context.Context, masterGroupName string) error
5859
GetClusterInfo(ctx context.Context) (*ClusterStatus, error)
5960
}
@@ -81,6 +82,24 @@ func (s *service) createClient() *rediscli.Client {
8182
return rediscli.NewClient(opts)
8283
}
8384

85+
func (c *service) SentinelSet(ctx context.Context, masterGroupName, key, value string) error {
86+
client := c.createClient()
87+
if client == nil {
88+
return nil
89+
}
90+
defer client.Close()
91+
92+
cmd := rediscli.NewStringCmd(ctx, "SENTINEL", "SET", masterGroupName, key, value)
93+
err := client.Process(ctx, cmd)
94+
if err != nil {
95+
return err
96+
}
97+
if err = cmd.Err(); err != nil {
98+
return err
99+
}
100+
return nil
101+
}
102+
84103
func (c *service) SentinelReset(ctx context.Context, masterGroupName string) error {
85104
client := c.createClient()
86105
if client == nil {

0 commit comments

Comments
 (0)