-
Notifications
You must be signed in to change notification settings - Fork 374
/
ess.go
699 lines (631 loc) · 20.3 KB
/
ess.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
// 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 ess
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
api "github.com/aliyun/alibaba-cloud-sdk-go/services/ess"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/cmp/impl/labels"
"github.com/erda-project/erda/modules/cmp/impl/mns"
"github.com/erda-project/erda/modules/cmp/impl/nodes"
"github.com/erda-project/erda/pkg/crypto/encrypt"
"github.com/erda-project/erda/pkg/dlock"
)
const (
EssGroupNameSuff = "-dice-ess"
EssScaleSimpleRuleSuff = "-dice-auto-rule"
EssScaleSchedulerRuleSuff = "-dice-scheduler-rule"
EssScaleSchedulerTaskSuff = "-dice-scheduler-task"
TimeLayout = "2006-01-02T15:04Z"
DetectInterval = 30
MaxLimit = 80
MinLimit = 70
)
type Ess struct {
bdl *bundle.Bundle
mns *mns.Mns
nodes *nodes.Nodes
labels *labels.Labels
Config *Config
}
type Config struct {
Region string
AccessKeyID string
AccessSecret string
client *api.Client
EssGroupID string
ScaleConfID string
EssScaleRule string
ScheduledTaskId string
ScalingRuleId string
IsExist bool
ScalePipeLineID uint64
}
func New(bdl *bundle.Bundle, mns *mns.Mns, nodes *nodes.Nodes, labels *labels.Labels) *Ess {
return &Ess{bdl: bdl, mns: mns, nodes: nodes, labels: labels}
}
// Init Init auto scale
func (e *Ess) Init(req apistructs.BasicCloudConf, m *mns.Mns, n *nodes.Nodes) (*Ess, error) {
accessKey := encrypt.AesDecrypt(req.AccessKeyId, apistructs.TerraformEcyKey)
secretKey := encrypt.AesDecrypt(req.AccessKeySecret, apistructs.TerraformEcyKey)
client, err := api.NewClientWithAccessKey(req.Region, accessKey, secretKey)
if err != nil {
return nil, errors.Wrap(err, "failed to new ess client with accessKey")
}
config := &Config{
Region: req.Region,
AccessKeyID: accessKey,
AccessSecret: secretKey,
client: client,
}
instance := &Ess{
Config: config,
mns: m,
nodes: n,
}
return instance, nil
}
// CreateAutoFlow Create ess with auto mode
func (e *Ess) CreateAutoFlow(clusterName string, vSwitchID string, ecsPassword string, sgID string) error {
essScaleSimpleRuleName := clusterName + EssScaleSimpleRuleSuff
err := e.CreateFlow(clusterName, vSwitchID, ecsPassword, sgID)
if err != nil {
return err
}
err = e.DeleteScaleRule(essScaleSimpleRuleName)
if err != nil {
return err
}
// Create ess simple scale rule
err = e.CreateScaleRule(essScaleSimpleRuleName, 2)
return err
}
// CreateSchedulerFlow Create ess with schedule mode
func (e *Ess) CreateSchedulerFlow(req apistructs.SchedulerScaleReq) error {
essScaleSchedulerRuleName := req.ClusterName + EssScaleSchedulerRuleSuff
essScaleSchedulerTask := req.ClusterName + EssScaleSchedulerTaskSuff
err := e.CreateFlow(req.ClusterName, req.VSwitchID, req.EcsPassword, req.SgID)
if err != nil {
return err
}
err = e.DeleteScaleRule(essScaleSchedulerRuleName)
if err != nil {
return err
}
// Create ess autoscale rules with simple model.
err = e.CreateScaleRule(essScaleSchedulerRuleName, req.Num)
if err != nil {
return err
}
if req.IsEdit {
err = e.UpdateScheduledTasks(req.ScheduledTaskId, req.RecurrenceType, req.RecurrenceValue, req.LaunchTime)
if err != nil {
return err
}
} else {
err = e.DeleteScheduledTasks(essScaleSchedulerTask)
if err != nil {
return err
}
// Create ess autoscale rules with schedule model.
err = e.CreateScheduledTask(essScaleSchedulerTask, req.RecurrenceType, req.RecurrenceValue, req.LaunchTime)
if err != nil {
return err
}
}
// Create autoscale rules with schedule model.
pTime1, err := time.Parse(TimeLayout, req.LaunchTime)
o, _ := time.ParseDuration(strconv.Itoa(req.ScaleDuration) + "h")
t := pTime1.Add(o)
id, err := e.nodes.DeleteEssNodesCron(apistructs.DeleteNodesCronRequest{
DeleteNodesRequest: apistructs.DeleteNodesRequest{
RmNodesRequest: apistructs.RmNodesRequest{
ClusterName: req.ClusterName,
OrgID: uint64(req.OrgID),
Password: req.EcsPassword,
Force: false,
},
AccessKey: req.AccessKeyId,
SecretKey: req.AccessKeySecret,
Region: req.Region,
ScalingGroupId: e.Config.EssGroupID,
},
LaunchTime: t.Format(TimeLayout),
RecurrenceType: req.RecurrenceType,
RecurrenceValue: req.RecurrenceValue,
}, apistructs.AutoScaleUserID, strconv.Itoa(req.OrgID))
if err != nil {
e.DeleteScheduledTasks(essScaleSchedulerTask)
return err
}
e.Config.ScalePipeLineID = *id
return nil
}
// CreateFlow Create elastic scale group, elastic scale configuration, elastic scale rule in order when start autoscale
func (e *Ess) CreateFlow(clusterName string, vSwitchID string, ecsPassword string, sgID string) error {
var err error
essGroupName := clusterName + EssGroupNameSuff
// First, check elastic scale group whether exists
flag, err := e.EnsureScaleGroupExist(essGroupName)
if err != nil {
return err
}
if flag {
return nil
}
// Create elastic scale group
err = e.CreateScaleGroup(essGroupName, vSwitchID)
if err != nil {
return err
}
// Create elastic scale configuration
err = e.CreateScaleConf(ecsPassword, sgID)
if err != nil {
return err
}
// Create mns queue
var r apistructs.MnsReq
r.Region = e.Config.Region
r.AccessKeyId = e.Config.AccessKeyID
r.AccessKeySecret = e.Config.AccessSecret
r.ClusterName = clusterName
err = e.mns.InitConfig(r)
if err != nil {
return err
}
err = e.mns.CreateQueue()
if err != nil {
return err
}
mnsQueueName := strings.Join([]string{
"acs",
"ess",
e.Config.Region,
e.mns.Config.AccountID,
"queue/" + e.mns.Config.QueueName,
}, ":")
// Related ess elastic scale group to mns queue
err = e.CreateNotificationConfiguration(mnsQueueName)
if err != nil {
return err
}
// Enable elastic scale group
err = e.EnableScalingGroup()
return err
}
// EnsureScaleGroupExist Ensure elastic scale group whether exists
func (e *Ess) EnsureScaleGroupExist(essGroupName string) (bool, error) {
request := api.CreateDescribeScalingGroupsRequest()
request.Scheme = "https"
request.ScalingGroupName1 = essGroupName
response, err := e.Config.client.DescribeScalingGroups(request)
if err != nil {
logrus.Errorf("failed to get ess group: %v", err)
return false, err
}
if response.TotalCount != 1 {
return false, nil
}
e.Config.EssGroupID = response.ScalingGroups.ScalingGroup[0].ScalingGroupId
e.Config.IsExist = true
return true, nil
}
// EnsureScaleRuleExist Ensure elastic scale rule whether exists
func (e *Ess) EnsureScaleRuleExist(name string) (bool, error) {
request := api.CreateDescribeScalingRulesRequest()
request.Scheme = "https"
request.ScalingGroupId = e.Config.EssGroupID
request.ScalingRuleName1 = name
response, err := e.Config.client.DescribeScalingRules(request)
if err != nil {
logrus.Errorf("failed to get ess group: %v", err)
return false, err
}
if response.TotalCount != 1 {
return false, nil
}
//ScalingRuleId
e.Config.EssScaleRule = response.ScalingRules.ScalingRule[0].ScalingRuleAri
e.Config.ScalingRuleId = response.ScalingRules.ScalingRule[0].ScalingRuleId
return true, nil
}
// DeleteScaleRule Delete elastic scale rule
func (e *Ess) DeleteScaleRule(name string) error {
f, err := e.EnsureScaleRuleExist(name)
if err != nil {
return err
}
if !f {
return nil
}
request := api.CreateDeleteScalingRuleRequest()
request.Scheme = "https"
request.ScalingRuleId = e.Config.ScalingRuleId
_, err = e.Config.client.DeleteScalingRule(request)
if err != nil {
logrus.Errorf("failed to delete scale rule: %v", err)
return err
}
return nil
}
// EnsureScheduledTasks Ensure schedule task whether exists
func (e *Ess) EnsureScheduledTasks(name string) (bool, error) {
request := api.CreateDescribeScheduledTasksRequest()
request.Scheme = "https"
request.ScheduledTaskName1 = name
response, err := e.Config.client.DescribeScheduledTasks(request)
if err != nil {
logrus.Errorf("failed to get ess scheduler task: %v", err)
return false, err
}
if response.TotalCount != 1 {
return false, nil
}
//ScalingRuleId
e.Config.ScheduledTaskId = response.ScheduledTasks.ScheduledTask[0].ScheduledTaskId
return true, nil
}
//DeleteScaleRule Delete scale rule
func (e *Ess) DeleteScheduledTasks(name string) error {
f, err := e.EnsureScheduledTasks(name)
if err != nil {
return err
}
if !f {
return nil
}
request := api.CreateDeleteScheduledTaskRequest()
request.Scheme = "https"
request.ScheduledTaskId = e.Config.ScheduledTaskId
_, err = e.Config.client.DeleteScheduledTask(request)
if err != nil {
logrus.Errorf("failed to delete scheduler task: %v", err)
return err
}
return nil
}
//CreateScaleGroup Create scale group
func (e *Ess) CreateScaleGroup(essGroupName string, vSwitchID string) error {
request := api.CreateCreateScalingGroupRequest()
request.Scheme = "https"
request.MinSize = "0"
request.MaxSize = requests.NewInteger(1000)
request.ScalingGroupName = essGroupName
request.VSwitchId = vSwitchID
response, err := e.Config.client.CreateScalingGroup(request)
if err != nil {
logrus.Errorf("failed to create ess group: %v", err)
return err
}
e.Config.EssGroupID = response.ScalingGroupId
return nil
}
// CreateScaleConf
func (e *Ess) CreateScaleConf(ecsPassword string, sgID string) error {
request := api.CreateCreateScalingConfigurationRequest()
request.Scheme = "https"
request.ScalingGroupId = e.Config.EssGroupID
request.ImageName = "centos_7_7_x64_20G_alibase_20200220.vhd"
request.InstanceTypes = &[]string{
"ecs.sn2ne.2xlarge",
"ecs.sn2.xlarge",
}
//request.InstanceTypes = &[]string{
// "ecs.sn2ne.large",
// "ecs.sn1ne.large",
// "ecs.sn2ne.2xlarge",
//}
request.Cpu = requests.NewInteger(8)
request.Memory = requests.NewInteger(32)
request.SystemDiskCategory = "cloud_ssd"
request.SystemDiskSize = requests.NewInteger(40)
request.SecurityGroupId = sgID
request.DataDisk = &[]api.CreateScalingConfigurationDataDisk{
{
Size: "200",
Category: "cloud_ssd",
},
}
password := encrypt.AesDecrypt(ecsPassword, apistructs.TerraformEcyKey)
request.Password = password
resp, err := e.Config.client.CreateScalingConfiguration(request)
if err != nil {
logrus.Errorf("failed to create ess scale config: %v", err)
return err
}
e.Config.ScaleConfID = resp.ScalingConfigurationId
return nil
}
// CreateScaleRule Create simple scale rule
func (e *Ess) CreateScaleRule(essScaleSimpleRuleName string, num int) error {
request := api.CreateCreateScalingRuleRequest()
request.Scheme = "https"
request.ScalingGroupId = e.Config.EssGroupID
request.ScalingRuleName = essScaleSimpleRuleName
request.AdjustmentType = "QuantityChangeInCapacity"
request.AdjustmentValue = requests.NewInteger(num)
request.ScalingRuleType = "SimpleScalingRule"
resp, err := e.Config.client.CreateScalingRule(request)
if err != nil {
logrus.Errorf("failed to create ess scale rule: %v", err)
return err
}
e.Config.EssScaleRule = resp.ScalingRuleAri
return nil
}
// UpdateScheduledTasks Update schedule task
func (e *Ess) UpdateScheduledTasks(id string, recurrenceType string, recurrenceValue string, launchTime string) error {
d, _ := time.ParseDuration(strconv.Itoa(300*24) + "h")
pTime, err := time.Parse(TimeLayout, launchTime)
d1 := pTime.Add(d)
request := api.CreateModifyScheduledTaskRequest()
request.Scheme = "https"
request.ScheduledTaskId = id
request.ScheduledAction = e.Config.EssScaleRule
request.RecurrenceType = recurrenceType
request.RecurrenceValue = recurrenceValue
request.RecurrenceEndTime = d1.Format(TimeLayout)
request.TaskEnabled = requests.NewBoolean(true)
_, err = e.Config.client.ModifyScheduledTask(request)
if err != nil {
logrus.Errorf("failed to modify ess scheduler task: %v", err)
return err
}
e.Config.ScheduledTaskId = id
return nil
}
// CreateScheduledTask Create schedule task
func (e *Ess) CreateScheduledTask(name string, recurrenceType string, recurrenceValue string, launchTime string) error {
d, _ := time.ParseDuration(strconv.Itoa(300*24) + "h")
pTime, err := time.Parse(TimeLayout, launchTime)
d1 := pTime.Add(d)
request := api.CreateCreateScheduledTaskRequest()
request.Scheme = "https"
request.ScheduledTaskName = name
request.ScheduledAction = e.Config.EssScaleRule
request.LaunchTime = launchTime
request.RecurrenceEndTime = d1.Format(TimeLayout)
request.RecurrenceType = recurrenceType
request.RecurrenceValue = recurrenceValue
request.TaskEnabled = requests.NewBoolean(true)
response, err := e.Config.client.CreateScheduledTask(request)
if err != nil {
logrus.Errorf("failed to create ess scheduler task: %v", err)
return err
}
e.Config.ScheduledTaskId = response.ScheduledTaskId
return nil
}
// ExecScaleRule Execute scale rule
func (e *Ess) ExecScaleRule(scaleRuleName string) error {
request := api.CreateExecuteScalingRuleRequest()
request.Scheme = "https"
// request.ScalingRuleAri = "ari:acs:ess:cn-hangzhou:1356642369236709:scalingrule/asr-bp10owakduy0vvzwwe03"
request.ScalingRuleAri = scaleRuleName
_, err := e.Config.client.ExecuteScalingRule(request)
if err != nil {
logrus.Errorf("failed to execute ess scale rule: %v", err)
return err
}
return nil
}
//CreateNotificationConfiguration Related configuration
func (e *Ess) CreateNotificationConfiguration(queueName string) error {
request := api.CreateCreateNotificationConfigurationRequest()
request.Scheme = "https"
request.ScalingGroupId = e.Config.EssGroupID
request.NotificationArn = queueName
request.NotificationType = &[]string{
"AUTOSCALING:SCALE_OUT_SUCCESS",
"AUTOSCALING:SCALE_IN_SUCCESS",
"AUTOSCALING:SCALE_OUT_ERROR",
"AUTOSCALING:SCALE_IN_ERROR",
"AUTOSCALING:SCALE_REJECT",
"AUTOSCALING:SCALE_OUT_START",
"AUTOSCALING:SCALE_IN_START",
"AUTOSCALING:SCHEDULE_TASK_EXPIRING",
}
_, err := e.Config.client.CreateNotificationConfiguration(request)
if err != nil {
logrus.Errorf("failed to create ess scale notification: %v", err)
return err
}
return nil
}
// EnableScalingGroup Enable scale group
func (e *Ess) EnableScalingGroup() error {
request := api.CreateEnableScalingGroupRequest()
request.Scheme = "https"
request.ScalingGroupId = e.Config.EssGroupID
request.ActiveScalingConfigurationId = e.Config.ScaleConfID
_, err := e.Config.client.EnableScalingGroup(request)
if err != nil {
logrus.Errorf("failed enable scale group: %v", err)
return err
}
return nil
}
func (e *Ess) AutoScale() {
// Listen elastic scale
go func() {
for {
ctx, cancel := context.WithCancel(context.Background())
lock, err := dlock.New("/autoscale/auto", func() { cancel() })
if err := lock.Lock(context.Background()); err != nil {
logrus.Errorf("failed to lock: %v", err)
continue
}
if err != nil {
logrus.Errorf("failed to get dlock: %v", err)
// try again
continue
}
e.DetectResource(ctx)
if err := lock.Unlock(); err != nil {
logrus.Errorf("failed to unlock: %v", err)
}
}
}()
}
// DetectResource Detect cluster resource
func (e *Ess) DetectResource(ctx context.Context) {
ticker := time.NewTicker(time.Minute * DetectInterval)
logrus.Errorf("begin to execute autoscale...")
for range ticker.C {
logrus.Errorf("begin to execute autoscale in loop...")
clusters, err := e.bdl.ListClusters("")
if err != nil {
logrus.Errorf("failed get to get cluster list")
continue
}
var totalCPU float64
var totalMem int64
var requestCPU float64
var requestMem int64
var offLineNode string
var lockedNode string
for _, cluster := range clusters {
if cluster.OpsConfig != nil && cluster.OpsConfig.ScaleMode == "auto" {
err := e.validateOpsConfig(cluster.OpsConfig)
if err != nil {
logrus.Errorf("cluster: %s, error: %v", cluster.Name, err)
continue
}
resourceInfoData, err := e.bdl.ResourceInfo(cluster.Name, false)
if err != nil {
logrus.Errorf("failed get to get cluster resource info data: %v\n", err)
continue
}
totalCPU = 0
totalMem = 0
requestCPU = 0
requestMem = 0
for k, v := range resourceInfoData.Nodes {
// Check labels (dice/stateless-service and bigdata-) in production environment
if (isExistInArray("dice/stateless-service", v.Labels) && isExistInArray("dice/workspace-prod", v.Labels)) || isExistInArray("dice/bigdata-job", v.Labels) {
totalCPU += v.CPUAllocatable
totalMem += v.MemAllocatable
requestCPU += v.CPUReqsUsage
requestMem += v.MemReqsUsage
}
// Label host which need offline
if isExistInArray("dice/autoscale", v.Labels) {
if isExistInArray("dice/locked", v.Labels) {
lockedNode = k
}
offLineNode = k
}
}
cpuUsage := requestCPU / totalCPU * 100
memUsage := float64(requestMem) / float64(totalMem) * 100
// Expansion when resource used greater than 80%
if cpuUsage > MaxLimit || memUsage > MaxLimit {
accountInfoReq := apistructs.BasicCloudConf{
Region: cluster.OpsConfig.Region,
AccessKeyId: cluster.OpsConfig.AccessKey,
AccessKeySecret: cluster.OpsConfig.SecretKey,
}
as, err := e.Init(accountInfoReq, e.mns, e.nodes)
if err != nil {
logrus.Errorf("failed to init ess sdk: %v", err)
continue
}
logrus.Errorf("ess sdk hava ready for autostale")
err = as.ExecScaleRule(cluster.OpsConfig.EssScaleRule)
if err != nil {
logrus.Errorf("failed to execute scale rule: %v", err)
}
}
// Reduce capacity when resource used less than 70%
if cpuUsage < MinLimit && memUsage < MinLimit {
if lockedNode != "" {
id, err := e.mns.GetInstancesIDByPrivateIp(apistructs.EcsInfoReq{
BasicCloudConf: apistructs.BasicCloudConf{
AccessKeyId: cluster.OpsConfig.AccessKey,
AccessKeySecret: cluster.OpsConfig.SecretKey,
Region: cluster.OpsConfig.Region,
},
PrivateIPs: []string{lockedNode},
})
if err != nil {
logrus.Errorf("failed get id by private ip: %v", err)
continue
}
logrus.Errorf("execute offline action from ess on id: %v\n", id)
req := apistructs.DeleteNodesRequest{
RmNodesRequest: apistructs.RmNodesRequest{
ClusterName: cluster.Name,
OrgID: uint64(cluster.OrgID),
Hosts: []string{lockedNode},
Password: cluster.OpsConfig.EcsPassword,
Force: false,
},
AccessKey: cluster.OpsConfig.AccessKey,
SecretKey: cluster.OpsConfig.SecretKey,
Region: cluster.OpsConfig.Region,
ScalingGroupId: cluster.OpsConfig.EssGroupID,
InstanceIDs: id,
}
_, err = e.nodes.DeleteEssNodes(req, apistructs.AutoScaleUserID, strconv.Itoa(cluster.OrgID))
if err != nil {
logrus.Errorf("failed delete node from ess: %v", err)
}
}
if offLineNode != "" {
_, err = e.labels.UpdateLabels(apistructs.UpdateLabelsRequest{
ClusterName: cluster.Name,
OrgID: uint64(cluster.OrgID),
Hosts: []string{offLineNode},
Labels: []string{"locked"},
}, apistructs.AutoScaleUserID)
if err != nil {
logrus.Errorf("failed to label the offline node %s: %v", offLineNode, err)
}
}
}
}
}
}
<-ctx.Done()
}
func (e *Ess) validateOpsConfig(opsConf *apistructs.OpsConfig) error {
if opsConf == nil {
err := fmt.Errorf("empty ops config")
logrus.Error(err.Error())
return err
}
if e.isEmpty(opsConf.AccessKey) || e.isEmpty(opsConf.SecretKey) || e.isEmpty(opsConf.Region) {
err := fmt.Errorf("invalid ops config")
return err
}
return nil
}
func (e Ess) isEmpty(str string) bool {
return strings.Replace(str, " ", "", -1) == ""
}
func isExistInArray(value string, array []string) bool {
for _, v := range array {
if v == value {
return true
}
}
return false
}