-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.go
284 lines (249 loc) · 9.31 KB
/
engine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
Copyright 2023 The RedisOperator Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sentinel
import (
"context"
"fmt"
"github.com/alauda/redis-operator/pkg/actor"
"github.com/alauda/redis-operator/pkg/config"
"github.com/alauda/redis-operator/pkg/kubernetes"
"github.com/alauda/redis-operator/pkg/kubernetes/builder/clusterbuilder"
"github.com/alauda/redis-operator/pkg/kubernetes/builder/sentinelbuilder"
"github.com/alauda/redis-operator/pkg/types"
"github.com/alauda/redis-operator/pkg/types/redis"
"github.com/alauda/redis-operator/pkg/types/user"
"github.com/alauda/redis-operator/pkg/util"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
)
type RuleEngine struct {
client kubernetes.ClientSet
eventRecorder record.EventRecorder
logger logr.Logger
}
func NewRuleEngine(client kubernetes.ClientSet, eventRecorder record.EventRecorder, logger logr.Logger) (*RuleEngine, error) {
if client == nil {
return nil, fmt.Errorf("require client set")
}
if eventRecorder == nil {
return nil, fmt.Errorf("require EventRecorder")
}
ctrl := RuleEngine{
client: client,
eventRecorder: eventRecorder,
logger: logger,
}
return &ctrl, nil
}
func (g *RuleEngine) Inspect(ctx context.Context, val types.RedisInstance) *actor.ActorResult {
if g == nil {
return nil
}
sentinel := val.(types.RedisFailoverInstance)
logger := g.logger.WithName("Inspect").WithValues("namespace", sentinel.GetNamespace(), "name", sentinel.GetName())
if sentinel == nil {
logger.Info("failover is nil")
return nil
}
cr := sentinel.Definition()
if (cr.Spec.Redis.PodAnnotations != nil) && cr.Spec.Redis.PodAnnotations[config.PAUSE_ANNOTATION_KEY] != "" {
return actor.NewResult(CommandEnsureResource)
}
logger.V(3).Info("Inspecting sentinel")
check_password := func() *actor.ActorResult {
logger.V(3).Info("check_password")
return g.CheckRedisPasswordChange(ctx, sentinel)
}
if ret := check_password(); ret != nil {
logger.V(3).Info("check_password", "result", ret)
return ret
}
if len(sentinel.Nodes()) == 0 || len(sentinel.SentinelNodes()) == 0 {
logger.Info("sentinel has no nodes,ensure resource")
return actor.NewResult(CommandEnsureResource)
}
//first: check labels
check_2 := func() *actor.ActorResult {
logger.V(3).Info("check_labels")
return g.CheckRedisLabels(ctx, sentinel)
}
if ret := check_2(); ret != nil {
logger.V(3).Info("check_labels", "result", ret)
return ret
}
// check only one master
check_0 := func() *actor.ActorResult {
logger.V(3).Info("check_only_one_master")
return g.CheckMaster(ctx, sentinel)
}
if ret := check_0(); ret != nil {
logger.V(3).Info("check_only_one_master", "result", ret)
return ret
}
// check sentinel
check_1 := func() *actor.ActorResult {
logger.V(3).Info("check_sentinel")
return g.CheckSentinel(ctx, sentinel)
}
if ret := check_1(); ret != nil {
logger.V(3).Info("check_sentinel", "result", ret)
return ret
}
check_configmap := func() *actor.ActorResult {
logger.V(3).Info("check_configmap")
return g.CheckRedisConfig(ctx, sentinel)
}
if ret := check_configmap(); ret != nil {
logger.V(3).Info("check_configmap", "result", ret)
return ret
}
check_finally := func() *actor.ActorResult {
logger.V(3).Info("check_finnaly")
return g.FinallyCheck(ctx, sentinel)
}
if ret := check_finally(); ret != nil {
logger.V(3).Info("check_finnaly", "result", ret)
return ret
}
logger.Info("sentinel inspect is healthy", "sentinel", sentinel.GetName(), "namespace", sentinel.GetNamespace())
return nil
}
func (g *RuleEngine) CheckRedisLabels(ctx context.Context, st types.RedisInstance) *actor.ActorResult {
if len(st.Masters()) != 1 {
return nil
}
for _, v := range st.Nodes() {
labels := v.Definition().GetLabels()
if len(labels) > 0 {
switch v.Role() {
case redis.RedisRoleMaster:
if labels[sentinelbuilder.RedisRoleLabel] != sentinelbuilder.RedisRoleMaster {
g.logger.Info("master labels not match", "node", v.GetName(), "labels", labels)
return actor.NewResult(CommandPatchLabels)
}
case redis.RedisRoleSlave:
if labels[sentinelbuilder.RedisRoleLabel] != sentinelbuilder.RedisRoleSlave {
g.logger.Info("slave labels not match", "node", v.GetName(), "labels", labels)
return actor.NewResult(CommandPatchLabels)
}
}
}
}
return nil
}
// 检测是否只有一个master
func (g *RuleEngine) CheckMaster(ctx context.Context, st types.RedisFailoverInstance) *actor.ActorResult {
if len(st.Nodes()) == 0 {
return actor.NewResult(CommandEnsureResource)
}
if len(st.Masters()) == 0 {
return actor.NewResult(CommandInitMaster)
}
// 如果脑裂根据sentienl 选最优
if len(st.Masters()) > 1 {
g.logger.Info("master more than one", "sentinel", st.GetName())
return actor.NewResultWithError(CommandHealMaster, fmt.Errorf("sentinel %s has more than one master", st.GetName()))
}
// 节点掉了,根据sentinel选最优
for _, n := range st.Nodes() {
if n.Info().MasterLinkStatus == "down" && n.Role() == redis.RedisRoleSlave {
g.logger.Info("master link down", "sentinel", st.GetName(), "node", n.GetName())
return actor.NewResult(CommandHealMaster)
}
}
return nil
}
func (g *RuleEngine) CheckSentinel(ctx context.Context, st types.RedisFailoverInstance) *actor.ActorResult {
rf := st.Definition()
if len(st.SentinelNodes()) == 0 {
return actor.NewResult(CommandEnsureResource)
}
for _, v := range st.SentinelNodes() {
if v.Info().SentinelMaster0.Status != "ok" {
g.logger.Info("sentinel status not ok", "sentinel", v.GetName(), "status", v.Info().SentinelMaster0.Status)
return actor.NewResult(CommandSentinelHeal)
}
if v.Info().SentinelMaster0.Replicas != len(st.Nodes())-1 {
g.logger.Info("sentinel slaves not match", "sentinel", v.GetName(), "slaves", v.Info().SentinelMaster0.Replicas, "nodes", rf.Spec.Redis.Replicas)
return actor.NewResult(CommandHealMaster)
}
}
return nil
}
func (g *RuleEngine) CheckRedisPasswordChange(ctx context.Context, st types.RedisFailoverInstance) *actor.ActorResult {
if st.Version().IsACLSupported() && !st.IsACLUserExists() {
return actor.NewResult(CommandUpdateAccount)
}
for _, v := range st.Users() {
if v.Name == user.DefaultUserName {
if v.Password == nil && st.Definition().Spec.Auth.SecretPath != "" {
return actor.NewResult(CommandUpdateAccount)
}
if v.Password != nil && v.Password.SecretName != st.Definition().Spec.Auth.SecretPath {
return actor.NewResult(CommandUpdateAccount)
}
}
if v.Name == user.DefaultOperatorUserName {
for _, vv := range st.Nodes() {
container := util.GetContainerByName(&vv.Definition().Spec, sentinelbuilder.ServerContainerName)
if container != nil && container.Env != nil {
for _, vc := range container.Env {
if vc.Name == sentinelbuilder.OperatorSecretName && vc.ValueFrom != nil {
if vc.ValueFrom.SecretKeyRef.Name != st.Definition().Spec.Auth.SecretPath {
return actor.NewResult(CommandUpdateAccount)
}
}
}
}
}
if len(v.Rules) != 0 && len(v.Rules[0].Channels) == 0 && st.Version().IsACL2Supported() {
return actor.NewResult(CommandUpdateAccount)
}
}
}
return nil
}
func (g *RuleEngine) CheckRedisConfig(ctx context.Context, st types.RedisFailoverInstance) *actor.ActorResult {
newCm := sentinelbuilder.NewRedisConfigMap(st, st.Selector())
oldCm, err := g.client.GetConfigMap(ctx, newCm.GetNamespace(), newCm.GetName())
if errors.IsNotFound(err) || oldCm.Data[clusterbuilder.RedisConfKey] == "" {
return actor.NewResultWithError(CommandEnsureResource, fmt.Errorf("configmap %s not found", newCm.GetName()))
} else if err != nil {
return actor.NewResultWithError(CommandRequeue, err)
}
newConf, _ := clusterbuilder.LoadRedisConfig(newCm.Data[clusterbuilder.RedisConfKey])
oldConf, _ := clusterbuilder.LoadRedisConfig(oldCm.Data[clusterbuilder.RedisConfKey])
added, changed, deleted := oldConf.Diff(newConf)
if len(added)+len(changed)+len(deleted) != 0 {
return actor.NewResult(CommandUpdateConfig)
}
return nil
}
// 最后比对数量是否相同
func (g *RuleEngine) FinallyCheck(ctx context.Context, st types.RedisFailoverInstance) *actor.ActorResult {
rf := st.Definition()
for _, n := range st.Nodes() {
if n.Role() == redis.RedisRoleMaster && n.Info().ConnectedReplicas != int64(st.Definition().Spec.Redis.Replicas-1) {
g.logger.Info("master slaves not match", "sentinel", st.GetName(), "slaves", n.Info().ConnectedReplicas, "replicas", rf.Spec.Redis.Replicas)
return actor.NewResult(CommandHealMaster)
}
}
for _, v := range st.SentinelNodes() {
if v.Info().SentinelMaster0.Sentinels != int(st.Definition().Spec.Sentinel.Replicas) {
g.logger.Info("sentinel sentinels not match", "sentinel", v.GetName(), "sentinels", v.Info().SentinelMaster0.Sentinels, "replicas", rf.Spec.Sentinel.Replicas)
return actor.NewResult(CommandSentinelHeal)
}
}
return nil
}