-
Notifications
You must be signed in to change notification settings - Fork 376
/
redis.go
311 lines (291 loc) · 9.2 KB
/
redis.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package legacy
import (
"bytes"
"fmt"
"github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/scheduler/executor/plugins/k8s/addon"
"github.com/erda-project/erda/modules/scheduler/executor/plugins/k8s/k8sapi"
"github.com/erda-project/erda/pkg/http/httpclient"
"github.com/erda-project/erda/pkg/schedule/schedulepolicy/constraintbuilders"
"github.com/erda-project/erda/pkg/strutil"
)
type RedisOperator struct {
k8s addon.K8SUtil
deployment addon.DeploymentUtil
statefulset addon.StatefulsetUtil
ns addon.NamespaceUtil
service addon.ServiceUtil
overcommit addon.OvercommitUtil
client *httpclient.HTTPClient
}
func New(k8sutil addon.K8SUtil,
deploy addon.DeploymentUtil,
sts addon.StatefulsetUtil,
service addon.ServiceUtil,
ns addon.NamespaceUtil,
overcommit addon.OvercommitUtil,
client *httpclient.HTTPClient) *RedisOperator {
return &RedisOperator{
k8s: k8sutil,
deployment: deploy,
statefulset: sts,
service: service,
ns: ns,
overcommit: overcommit,
client: client,
}
}
func (ro *RedisOperator) IsSupported() bool {
resp, err := ro.client.Get(ro.k8s.GetK8SAddr()).
Path("/apis/storage.spotahome.com/v1alpha2").
Do().
DiscardBody()
if err != nil {
logrus.Errorf("failed to query /apis/storage.spotahome.com/v1alpha2, host: %v, err: %v",
ro.k8s.GetK8SAddr(), err)
return false
}
if !resp.IsOK() {
return false
}
return true
}
// Validate
func (ro *RedisOperator) Validate(sg *apistructs.ServiceGroup) error {
operator, ok := sg.Labels["USE_OPERATOR"]
if !ok {
return fmt.Errorf("[BUG] sg need USE_OPERATOR label")
}
if strutil.ToLower(operator) != svcNameRedis {
return fmt.Errorf("[BUG] value of label USE_OPERATOR should be 'redis'")
}
if len(sg.Services) != 2 {
return fmt.Errorf("illegal services num: %d", len(sg.Services))
}
if sg.Services[0].Name != svcNameRedis && sg.Services[0].Name != svcNameSentinel {
return fmt.Errorf("illegal service: %+v, should be one of [redis, sentinel]", sg.Services[0])
}
if sg.Services[1].Name != svcNameRedis && sg.Services[1].Name != svcNameSentinel {
return fmt.Errorf("illegal service: %+v, should be one of [redis, sentinel]", sg.Services[1])
}
var redis apistructs.Service
if sg.Services[0].Name == svcNameRedis {
redis = sg.Services[0]
}
// if sg.Services[0].Name == svcNameSentinel {
// sentinel = sg.Services[0]
// }
if sg.Services[1].Name == svcNameRedis {
redis = sg.Services[1]
}
// if sg.Services[1].Name == svcNameSentinel {
// sentinel = sg.Services[1]
// }
if _, ok := redis.Env["requirepass"]; !ok {
return fmt.Errorf("redis service not provide 'requirepass' env")
}
return nil
}
func (ro *RedisOperator) Convert(sg *apistructs.ServiceGroup) interface{} {
svc0 := sg.Services[0]
svc1 := sg.Services[1]
var redis RedisSettings
var sentinel SentinelSettings
switch svc0.Name {
case svcNameRedis:
redis = ro.convertRedis(svc0)
case svcNameSentinel:
sentinel = convertSentinel(svc0)
}
switch svc1.Name {
case svcNameRedis:
redis = ro.convertRedis(svc1)
case svcNameSentinel:
sentinel = convertSentinel(svc1)
}
scheinfo := sg.ScheduleInfo2
scheinfo.Stateful = true
affinity := constraintbuilders.K8S(&scheinfo, nil, nil, nil).Affinity.NodeAffinity
return RedisFailover{
TypeMeta: metav1.TypeMeta{
APIVersion: "storage.spotahome.com/v1alpha2",
Kind: "RedisFailover",
},
ObjectMeta: metav1.ObjectMeta{
Name: sg.ID,
Namespace: genK8SNamespace(sg.Type, sg.ID),
},
Spec: RedisFailoverSpec{
Redis: redis,
Sentinel: sentinel,
NodeAffinity: affinity,
},
}
}
func (ro *RedisOperator) Create(k8syml interface{}) error {
redis, ok := k8syml.(RedisFailover)
if !ok {
return fmt.Errorf("[BUG] this k8syml should be RedisFailover")
}
if err := ro.ns.Exists(redis.Namespace); err != nil {
if err := ro.ns.Create(redis.Namespace, nil); err != nil {
return err
}
}
var b bytes.Buffer
resp, err := ro.client.Post(ro.k8s.GetK8SAddr()).
Path(fmt.Sprintf("/apis/storage.spotahome.com/v1alpha2/namespaces/%s/redisfailovers", redis.Namespace)).
JSONBody(redis).
Do().
Body(&b)
if err != nil {
return fmt.Errorf("failed to create redisfailover, %s/%s, err: %v", redis.Namespace, redis.Name, err)
}
if !resp.IsOK() {
return fmt.Errorf("failed to create redisfailover, %s/%s, statuscode: %v, body: %v",
redis.Namespace, redis.Name, resp.StatusCode(), b.String())
}
return nil
}
func (ro *RedisOperator) Inspect(sg *apistructs.ServiceGroup) (*apistructs.ServiceGroup, error) {
deploylist, err := ro.deployment.List(genK8SNamespace(sg.Type, sg.ID), nil)
if err != nil {
return nil, err
}
stslist, err := ro.statefulset.List(genK8SNamespace(sg.Type, sg.ID))
if err != nil {
return nil, err
}
svclist, err := ro.service.List(genK8SNamespace(sg.Type, sg.ID), nil)
if err != nil {
return nil, err
}
var redis, sentinel *apistructs.Service
if sg.Services[0].Name == svcNameRedis {
redis = &(sg.Services[0])
}
if sg.Services[1].Name == svcNameRedis {
redis = &(sg.Services[1])
}
if sg.Services[0].Name == svcNameSentinel {
sentinel = &(sg.Services[0])
}
if sg.Services[1].Name == svcNameSentinel {
sentinel = &(sg.Services[1])
}
for _, deploy := range deploylist.Items {
for _, cond := range deploy.Status.Conditions {
if cond.Type == appsv1.DeploymentAvailable {
if cond.Status == corev1.ConditionTrue {
sentinel.Status = apistructs.StatusHealthy
} else {
sentinel.Status = apistructs.StatusUnHealthy
}
}
}
}
for _, sts := range stslist.Items {
if sts.Spec.Replicas == nil {
redis.Status = apistructs.StatusUnknown
} else if *sts.Spec.Replicas == sts.Status.ReadyReplicas {
redis.Status = apistructs.StatusHealthy
} else {
redis.Status = apistructs.StatusUnHealthy
}
}
for _, svc := range svclist.Items {
sentinel.Vip = strutil.Join([]string{svc.Name, svc.Namespace, "svc.cluster.local"}, ".")
}
if redis.Status == apistructs.StatusHealthy && sentinel.Status == apistructs.StatusHealthy {
sg.Status = apistructs.StatusHealthy
} else {
sg.Status = apistructs.StatusUnHealthy
}
return sg, nil
}
func (ro *RedisOperator) Remove(sg *apistructs.ServiceGroup) error {
k8snamespace := genK8SNamespace(sg.Type, sg.ID)
var b bytes.Buffer
resp, err := ro.client.Delete(ro.k8s.GetK8SAddr()).
Path(fmt.Sprintf("/apis/storage.spotahome.com/v1alpha2/namespaces/%s/redisfailovers/%s", k8snamespace, sg.ID)).
JSONBody(k8sapi.DeleteOptions).
Do().
Body(&b)
if err != nil {
return fmt.Errorf("failed to delele redisfailover: %s/%s, err: %v", sg.Type, sg.ID, err)
}
if !resp.IsOK() {
if resp.IsNotfound() {
return nil
}
return fmt.Errorf("failed to delete redisfailover: %s/%s, statuscode: %v, body: %v",
sg.Type, sg.ID, resp.StatusCode(), b.String())
}
if err := ro.ns.Delete(k8snamespace); err != nil {
logrus.Errorf("failed to delete namespace: %s: %v", k8snamespace, err)
return nil
}
return nil
}
func (ro *RedisOperator) Update(k8syml interface{}) error {
// TODO:
return fmt.Errorf("redisoperator not impl Update yet")
}
func (ro *RedisOperator) convertRedis(svc apistructs.Service) RedisSettings {
settings := RedisSettings{}
settings.Version = "3.2.12"
settings.Envs = svc.Env
settings.Replicas = int32(svc.Scale)
settings.Resources = RedisFailoverResources{
Requests: CPUAndMem{
CPU: fmt.Sprintf("%dm", int(1000*ro.overcommit.CPUOvercommit(svc.Resources.Cpu))),
Memory: fmt.Sprintf("%dMi", ro.overcommit.MemoryOvercommit(int(svc.Resources.Mem))),
},
Limits: CPUAndMem{
CPU: fmt.Sprintf("%dm", int(1000*svc.Resources.Cpu)),
Memory: fmt.Sprintf("%dMi", int(svc.Resources.Mem)),
},
}
settings.Image = svc.Image
return settings
}
func convertSentinel(svc apistructs.Service) SentinelSettings {
settings := SentinelSettings{}
settings.Envs = svc.Env
settings.Replicas = int32(svc.Scale)
settings.Resources = RedisFailoverResources{
Requests: CPUAndMem{ // sentinel Not over-provisioned, because it should already occupy very little resources
CPU: fmt.Sprintf("%dm", int(1000*svc.Resources.Cpu)),
Memory: fmt.Sprintf("%dMi", int(svc.Resources.Mem)),
},
Limits: CPUAndMem{
CPU: fmt.Sprintf("%dm", int(1000*svc.Resources.Cpu)),
Memory: fmt.Sprintf("%dMi", int(svc.Resources.Mem)),
},
}
settings.CustomConfig = []string{
fmt.Sprintf("auth-pass %s", svc.Env["requirepass"]),
"down-after-milliseconds 12000",
"failover-timeout 12000",
}
return settings
}
func genK8SNamespace(namespace, name string) string {
return strutil.Concat(namespace, "--", name)
}