-
Notifications
You must be signed in to change notification settings - Fork 375
/
hook.go
498 lines (451 loc) · 19.1 KB
/
hook.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright (c) 2021 Terminus, Inc.
//
// 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 cluster
import (
"context"
"fmt"
"net/url"
"path"
"strconv"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/scheduler/impl/cluster/clusterutil"
"github.com/erda-project/erda/pkg/jsonstore"
"github.com/erda-project/erda/pkg/schedule/schedulepolicy/labelconfig"
"github.com/erda-project/erda/pkg/strutil"
)
var (
// ErrInvalidAction Illegal clusterevent action
ErrInvalidAction = errors.New("invalid cluster event action")
// ErrInvalidClusterType Illegal cluster type
ErrInvalidClusterType = errors.New("invalid cluster type")
// ErrEmptyClusterName cluster name is empty
ErrEmptyClusterName = errors.New("empty cluster name")
// ErrClusterAddrNotSet cluster addr not setting
ErrClusterAddrNotSet = errors.New("cluster addr not set")
)
// Hook receive clusterevent, reference `apistructs.ClusterEvent`
func (c *ClusterImpl) Hook(clusterEvent *apistructs.ClusterEvent) error {
if !strutil.Equal(clusterEvent.Content.Type, clusterTypeDcos, true) {
if strutil.Equal(clusterEvent.Content.Type, clusterTypeK8S, true) {
return c.handleK8SCluster(clusterEvent)
}
if strutil.Equal(clusterEvent.Content.Type, clusterTypeEdas, true) {
return c.handleEdasCluster(clusterEvent)
}
return errors.Wrap(ErrInvalidClusterType, clusterEvent.Content.Type)
}
if clusterEvent.Content.Name == "" {
return errors.Wrap(ErrEmptyClusterName, fmt.Sprintf("%+v", clusterEvent.Content))
}
logrus.Infof("cluster event: %+v, scheduler config: %+v", clusterEvent, *(clusterEvent.Content.SchedConfig))
if clusterEvent.Action == clusterActionCreate {
cluster := generateClusterInfoFromEvent(&clusterEvent.Content)
addr, ok := cluster.Options["ADDR"]
if !ok {
return ErrClusterAddrNotSet
}
srv := cluster
if err := createMarathonExecutor(c.js, srv, addr); err != nil {
return err
}
job := cluster
if err := createMetronomeExecutor(c.js, job, addr); err != nil {
return err
}
logrus.Infof("cluster(%s) %s successfully according to related event", clusterEvent.Action, cluster.ClusterName)
return nil
} else if clusterEvent.Action == clusterActionUpdate {
keys, err := c.findKeysByCluster(clusterEvent.Content.Name)
if err != nil {
return err
}
for _, k := range keys {
cluster := ClusterInfo{}
if err := c.js.Get(context.Background(), k, &cluster); err != nil {
content := fmt.Sprintf("get cluster(%s, key: %s) from etcd error: %v", clusterEvent.Content.Name, k, err)
return errors.New(content)
}
if err := patchClusterConfig(&cluster, &(clusterEvent.Content)); err != nil {
content := fmt.Sprintf("parse cluster(%s, key: %s) url error: %v", clusterEvent.Content.Name, k, err)
return errors.New(content)
}
if err := c.js.Put(context.Background(), k, cluster); err != nil {
content := fmt.Sprintf("patch cluster(%s, key: %s) to etcd error: %v", clusterEvent.Content.Name, k, err)
return errors.New(content)
}
}
return nil
} else if clusterEvent.Action == clusterActionDelete {
if err := c.deleteExecutor(clusterEvent.Content.Name); err != nil {
content := fmt.Sprintf("delete cluster(%s) error: %v", clusterEvent.Content.Name, err)
return errors.New(content)
}
}
return errors.Wrap(ErrInvalidAction, clusterEvent.Action)
}
func (c *ClusterImpl) findKeysByCluster(name string) ([]string, error) {
keys := make([]string, 0)
findKeyBelongedToCluster := func(k string, v interface{}) error {
c, ok := v.(*ClusterInfo)
if !ok {
logrus.Errorf("key(%s) related value(%+v) is not type of ClusterInfo", k, v)
return nil
}
if c.ClusterName == name {
keys = append(keys, k)
}
return nil
}
if err := c.js.ForEach(context.Background(), clusterPrefix, ClusterInfo{}, findKeyBelongedToCluster); err != nil {
content := fmt.Sprintf("cluster(%s) range prefix(%s) from etcd got err: %v", name, clusterPrefix, err)
return keys, errors.New(content)
}
if len(keys) == 0 {
content := fmt.Sprintf("Cannot find executor belonged to cluster(%s)", name)
return keys, errors.New(content)
}
return keys, nil
}
func setDefaultClusterConfig(ci *ClusterInfo) {
t := "true"
if _, ok := ci.Options[labelconfig.ENABLETAG]; !ok {
ci.Options[labelconfig.ENABLETAG] = t
}
if _, ok := ci.Options[labelconfig.ENABLE_ORG]; !ok {
ci.Options[labelconfig.ENABLE_ORG] = t
}
if _, ok := ci.Options[labelconfig.ENABLE_WORKSPACE]; !ok {
ci.Options[labelconfig.ENABLE_WORKSPACE] = t
}
if _, ok := ci.Options[labelconfig.CPU_NUM_QUOTA]; !ok {
// Here "-1" means to limit the maximum cpu according to the real cpu requested by the user,
// That is, if the user applies for a 1 core cpu, the quota corresponds to 1 core
// If set to "0", the cpu quota is unlimited
// For details, please refer to the implementation in the marathon plugin (in the setFineGrainedCPU function)
ci.Options[labelconfig.CPU_NUM_QUOTA] = "-1"
}
}
func create(js jsonstore.JsonStore, key string, c ClusterInfo) error {
setDefaultClusterConfig(&c)
if err := js.Put(context.Background(), key, c); err != nil {
content := fmt.Sprintf("write key(%s) to etcd error: %v", key, err)
return errors.New(content)
}
logrus.Infof("cluster executor(%s) created successfully, content: %+v", c.ExecutorName, c)
return nil
}
func createEdasExector(js jsonstore.JsonStore, key string, c string) error {
if err := js.Put(context.Background(), key, c); err != nil {
content := fmt.Sprintf("write key(%s) to etcd error: %v", key, err)
return errors.New(content)
}
logrus.Infof("cluster executor(%s) created successfully", c)
return nil
}
func createMarathonExecutor(js jsonstore.JsonStore, cluster ClusterInfo, addr string) error {
srv := cluster
srv.Kind = clusterutil.ServiceKindMarathon
srv.ExecutorName = clusterutil.GenerateExecutorByCluster(cluster.ClusterName, clusterutil.ServiceKindMarathon)
srv.Options["ADDR"] = addr + marathonAddrSuffix
// Maximum cpu limit by default (cpu quota)
if _, ok := srv.Options["CPU_NUM_QUOTA"]; !ok {
// Here "-1" means to limit the maximum cpu according to the real cpu requested by the user,
// That is, if the user applies for a 1 core cpu, the quota corresponds to 1 core
// If set to "0", the cpu quota is unlimited
// For details, please refer to the implementation in the marathon plugin (in the setFineGrainedCPU function)
srv.Options["CPU_NUM_QUOTA"] = "-1"
}
return create(js, clusterPrefix+cluster.ClusterName+clusterMarathonSuffix, srv)
}
func createMetronomeExecutor(js jsonstore.JsonStore, cluster ClusterInfo, addr string) error {
job := cluster
job.Kind = clusterutil.JobKindMetronome
job.ExecutorName = clusterutil.GenerateExecutorByCluster(cluster.ClusterName, clusterutil.JobKindMetronome)
job.Options["ADDR"] = addr + metronomeAddrSuffix
return create(js, clusterPrefix+cluster.ClusterName+clusterMetronomeSuffix, job)
}
func generateClusterInfoFromEvent(eventCluster *apistructs.ClusterInfo) ClusterInfo {
configCluster := ClusterInfo{
ClusterName: eventCluster.Name,
ExecutorName: clusterutil.GenerateExecutorByCluster(eventCluster.Name, clusterutil.ServiceKindMarathon),
Kind: clusterutil.ServiceKindMarathon,
Options: make(map[string]string),
}
if eventCluster.SchedConfig == nil {
return configCluster
}
configCluster.Options["ADDR"] = eventCluster.SchedConfig.MasterURL
configCluster.Options["ENABLETAG"] = strconv.FormatBool(eventCluster.SchedConfig.EnableTag)
if eventCluster.SchedConfig.AuthType == "basic" {
configCluster.Options["BASICAUTH"] = eventCluster.SchedConfig.AuthUsername + ":" + eventCluster.SchedConfig.AuthPassword
}
if len(eventCluster.SchedConfig.CACrt) > 0 {
configCluster.Options["CA_CRT"] = eventCluster.SchedConfig.CACrt
configCluster.Options["CLIENT_CRT"] = eventCluster.SchedConfig.ClientCrt
configCluster.Options["CLIENT_KEY"] = eventCluster.SchedConfig.ClientKey
}
return configCluster
}
// TODO: Need to discuss the content that is open to users to modify
// TODO: Support patch refined configuration
func patchClusterConfig(local *ClusterInfo, request *apistructs.ClusterInfo) error {
if request.SchedConfig != nil {
if request.SchedConfig.MasterURL != "" {
u, err := url.Parse(request.SchedConfig.MasterURL)
if err != nil {
return errors.Errorf("cluster addr(%s) is invalid url", request.SchedConfig.MasterURL)
}
if local.Kind == clusterutil.ServiceKindMarathon {
u.Path = path.Join(u.Path, marathonAddrSuffix)
local.Options["ADDR"] = u.String()
} else if local.Kind == clusterutil.JobKindMetronome {
u.Path = path.Join(u.Path, metronomeAddrSuffix)
local.Options["ADDR"] = u.String()
}
}
if request.SchedConfig.AuthUsername != "" && request.SchedConfig.AuthPassword != "" {
local.Options["BASICAUTH"] = request.SchedConfig.AuthUsername + ":" + request.SchedConfig.AuthPassword
}
if request.SchedConfig.CACrt != "" && request.SchedConfig.ClientCrt != "" && request.SchedConfig.ClientKey != "" {
local.Options["CA_CRT"] = request.SchedConfig.CACrt
local.Options["CLIENT_CRT"] = request.SchedConfig.ClientCrt
local.Options["CLIENT_KEY"] = request.SchedConfig.ClientKey
}
}
return nil
}
func (c *ClusterImpl) handleK8SCluster(clusterEvent *apistructs.ClusterEvent) error {
logrus.Infof("cluster hook of k8s, name: %s", clusterEvent.Content.Name)
if clusterEvent.Content.SchedConfig == nil {
return errors.Wrap(ErrInvalidClusterType, clusterEvent.Content.Type)
}
if clusterEvent.Action == clusterActionCreate {
return c.createK8SExecutor(clusterEvent)
} else if clusterEvent.Action == clusterActionUpdate {
return c.updateK8SExecutor(clusterEvent)
} else if clusterEvent.Action == clusterActionDelete {
return c.deleteExecutor(clusterEvent.Content.Name)
}
return nil
}
func (c *ClusterImpl) createK8SExecutor(clusterEvent *apistructs.ClusterEvent) error {
serviceCfg := ClusterInfo{
ClusterName: clusterEvent.Content.Name,
ExecutorName: clusterutil.GenerateExecutorByCluster(clusterEvent.Content.Name, clusterutil.ServiceKindMarathon),
Kind: clusterutil.ServiceKindK8S,
Options: map[string]string{
"cluster": clusterEvent.Content.Name,
"ADDR": clusterEvent.Content.SchedConfig.MasterURL,
"DEV_CPU_SUBSCRIBE_RATIO": clusterEvent.Content.SchedConfig.CPUSubscribeRatio,
"TEST_CPU_SUBSCRIBE_RATIO": clusterEvent.Content.SchedConfig.CPUSubscribeRatio,
"STAGING_CPU_SUBSCRIBE_RATIO": clusterEvent.Content.SchedConfig.CPUSubscribeRatio,
},
}
setDefaultClusterConfig(&serviceCfg)
svcPath := strutil.Concat(clusterPrefix, clusterEvent.Content.Name, clusterK8SSuffix)
if err := create(c.js, svcPath, serviceCfg); err != nil {
logrus.Errorf("failed to create k8s service executor, cluster: %s", clusterEvent.Content.Name)
return err
}
logrus.Infof("succeed to create k8s cluster, name: %s", clusterEvent.Content.Name)
return nil
}
func (c *ClusterImpl) updateK8SExecutor(clusterEvent *apistructs.ClusterEvent) error {
keys, err := c.findKeysByCluster(clusterEvent.Content.Name)
if err != nil {
return err
}
for _, k := range keys {
cluster := ClusterInfo{}
if err := c.js.Get(context.Background(), k, &cluster); err != nil {
content := fmt.Sprintf("get cluster(%s, key: %s) from etcd error: %v", clusterEvent.Content.Name, k, err)
return errors.New(content)
}
if err := patchK8SConfig(&cluster, &(clusterEvent.Content)); err != nil {
content := fmt.Sprintf("parse cluster(%s, key: %s) url error: %v", clusterEvent.Content.Name, k, err)
return errors.New(content)
}
if err := c.js.Put(context.Background(), k, cluster); err != nil {
content := fmt.Sprintf("patch cluster(%s, key: %s) to etcd error: %v", clusterEvent.Content.Name, k, err)
return errors.New(content)
}
}
return nil
}
// todo: Only support to modify the address at the moment
func patchK8SConfig(local *ClusterInfo, request *apistructs.ClusterInfo) error {
if request.SchedConfig != nil {
if request.SchedConfig.MasterURL != "" {
u, err := url.Parse(request.SchedConfig.MasterURL)
if err != nil {
return errors.Errorf("k8s cluster addr is invalid, addr: %s", request.SchedConfig.MasterURL)
}
local.Options["ADDR"] = u.String()
}
c, err := url.Parse(request.SchedConfig.CPUSubscribeRatio)
if err != nil {
return errors.Errorf("k8s cluster addr is invalid, addr: %s", request.SchedConfig.MasterURL)
}
local.Options["DEV_CPU_SUBSCRIBE_RATIO"] = c.String()
local.Options["TEST_CPU_SUBSCRIBE_RATIO"] = c.String()
local.Options["STAGING_CPU_SUBSCRIBE_RATIO"] = c.String()
}
return nil
}
// deleteExecutor delete cluster executor
func (c *ClusterImpl) deleteExecutor(clusterName string) error {
keys, err := c.findKeysByCluster(clusterName)
if err != nil {
return err
}
for _, k := range keys {
if err := c.js.Remove(context.Background(), k, nil); err != nil {
content := fmt.Sprintf("delete cluster(%s, key: %s) from etcd error: %v", clusterName, k, err)
return errors.New(content)
}
}
return nil
}
func (c *ClusterImpl) handleEdasCluster(clusterEvent *apistructs.ClusterEvent) error {
logrus.Infof("cluster hook of edas, name: %s", clusterEvent.Content.Name)
if clusterEvent.Content.SchedConfig == nil {
return errors.Wrap(ErrInvalidClusterType, clusterEvent.Content.Type)
}
if clusterEvent.Action == clusterActionCreate {
return c.createEdasExecutor(clusterEvent)
} else if clusterEvent.Action == clusterActionUpdate {
return c.updateEdasExecutor(clusterEvent)
} else if clusterEvent.Action == clusterActionDelete {
return c.deleteExecutor(clusterEvent.Content.Name)
}
return nil
}
func (c *ClusterImpl) createEdasExecutor(clusterEvent *apistructs.ClusterEvent) error {
serviceExecutorName := clusterutil.GenerateExecutorByCluster(clusterEvent.Content.Name, clusterutil.ServiceKindEdas)
jobExecutorName := clusterutil.GenerateExecutorByCluster(clusterEvent.Content.Name, clusterutil.JobKindK8S)
addonExecutorName := clusterutil.GenerateExecutorByCluster(clusterEvent.Content.Name, clusterutil.ServiceKindK8S)
//write to edas service excutor
svcExecutorPath := strutil.Concat(edasExecutorPrefix, clusterEvent.Content.Name, "/", clusterutil.ServiceKindMarathon)
if err := createEdasExector(c.js, svcExecutorPath, serviceExecutorName); err != nil {
logrus.Errorf("failed to create edas service executor, executor: %s", serviceExecutorName)
return err
}
//write edas service configure
serviceCfg := ClusterInfo{
ClusterName: clusterEvent.Content.Name,
ExecutorName: serviceExecutorName,
Kind: clusterutil.ServiceKindEdas,
Options: map[string]string{
"ADDR": clusterEvent.Content.SchedConfig.EdasConsoleAddr,
"ACCESSKEY": clusterEvent.Content.SchedConfig.AccessKey,
"ACCESSSECRET": clusterEvent.Content.SchedConfig.AccessSecret,
"CLUSTERID": clusterEvent.Content.SchedConfig.ClusterID,
"REGIONID": clusterEvent.Content.SchedConfig.RegionID,
"LOGICALREGIONID": clusterEvent.Content.SchedConfig.LogicalRegionID,
"REGADDR": clusterEvent.Content.SchedConfig.RegAddr,
},
}
svcPath := strutil.Concat(clusterPrefix, clusterEvent.Content.Name, clusterEdasSuffix)
if err := create(c.js, svcPath, serviceCfg); err != nil {
logrus.Errorf("failed to create edas service executor, cluster: %s", clusterEvent.Content.Name)
return err
}
//wirte to edas job excutor
jobExecutorPath := strutil.Concat(edasExecutorPrefix, clusterEvent.Content.Name, "/", clusterutil.JobKindMetronome)
if err := createEdasExector(c.js, jobExecutorPath, jobExecutorName); err != nil {
logrus.Errorf("failed to create edas service executor, executor: %s", jobExecutorName)
return err
}
//write to edas job configure
jobCfg := ClusterInfo{
ClusterName: clusterEvent.Content.Name,
ExecutorName: jobExecutorName,
Kind: clusterutil.JobKindK8S,
Options: map[string]string{
"ADDR": clusterEvent.Content.SchedConfig.K8sAddr,
},
}
setDefaultClusterConfig(&jobCfg)
jobPath := strutil.Concat(clusterPrefix, clusterEvent.Content.Name, clusterK8SJobSuffix)
if err := create(c.js, jobPath, jobCfg); err != nil {
logrus.Errorf("failed to create edas job executor, cluster: %s", clusterEvent.Content.Name)
return err
}
//write to edas addon excutor
addonExecutorPath := strutil.Concat(edasExecutorPrefix, clusterEvent.Content.Name, "/", clusterutil.EdasKindInK8s)
if err := createEdasExector(c.js, addonExecutorPath, addonExecutorName); err != nil {
logrus.Errorf("failed to create edas service executor, executor: %s", addonExecutorName)
return err
}
//write to edas addon configure
addonCfg := ClusterInfo{
ClusterName: clusterEvent.Content.Name,
ExecutorName: addonExecutorName,
Kind: clusterutil.ServiceKindK8S,
Options: map[string]string{
"ADDR": clusterEvent.Content.SchedConfig.K8sAddr,
"IS_EDAS": "true",
},
}
setDefaultClusterConfig(&addonCfg)
addonPath := strutil.Concat(clusterPrefix, clusterEvent.Content.Name, clusterK8SSuffix)
if err := create(c.js, addonPath, addonCfg); err != nil {
logrus.Errorf("failed to create edas addon executor, cluster: %s", clusterEvent.Content.Name)
return err
}
logrus.Infof("succeed to create edas cluster, name: %s", clusterEvent.Content.Name)
return nil
}
func (c *ClusterImpl) updateEdasExecutor(clusterEvent *apistructs.ClusterEvent) error {
keys, err := c.findKeysByCluster(clusterEvent.Content.Name)
if err != nil {
return err
}
for _, k := range keys {
cluster := ClusterInfo{}
if err := c.js.Get(context.Background(), k, &cluster); err != nil {
content := fmt.Sprintf("get cluster(%s, key: %s) from etcd error: %v", clusterEvent.Content.Name, k, err)
return errors.New(content)
}
var pErr error
if cluster.Kind == clusterutil.ServiceKindEdas {
pErr = patchEdasConfig(&cluster, &(clusterEvent.Content))
} else {
pErr = patchK8SConfig(&cluster, &(clusterEvent.Content))
}
if pErr != nil {
content := fmt.Sprintf("parse cluster(%s, key: %s) url error: %v", clusterEvent.Content.Name, k, err)
return errors.New(content)
}
if err := c.js.Put(context.Background(), k, cluster); err != nil {
content := fmt.Sprintf("patch cluster(%s, key: %s) to etcd error: %v", clusterEvent.Content.Name, k, err)
return errors.New(content)
}
}
return nil
}
// update edas executor
func patchEdasConfig(local *ClusterInfo, request *apistructs.ClusterInfo) error {
if request.SchedConfig != nil {
local.Options["ADDR"] = request.SchedConfig.EdasConsoleAddr
local.Options["ACCESSKEY"] = request.SchedConfig.AccessKey
local.Options["ACCESSSECRET"] = request.SchedConfig.AccessSecret
local.Options["CLUSTERID"] = request.SchedConfig.ClusterID
local.Options["REGIONID"] = request.SchedConfig.RegionID
local.Options["LOGICALREGIONID"] = request.SchedConfig.LogicalRegionID
}
return nil
}