-
Notifications
You must be signed in to change notification settings - Fork 48
/
ClusterHandler.go
967 lines (846 loc) · 35.6 KB
/
ClusterHandler.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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
// Tencent Driver of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// This is Tencent Driver.
//
// by CB-Spider Team, 2022.09.
package resources
import (
"encoding/json"
"fmt"
"regexp"
"runtime/debug"
"strconv"
"strings"
"time"
call "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/call-log"
"github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/tencent/utils/tencent"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
"github.com/jeremywohl/flatten"
tke "github.com/tencentcloud/tencentcloud-sdk-go-intl-en/tencentcloud/tke/v20180525"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
)
// calllogger
// 공통로거 만들기 이전까지 사용
// var once sync.Once
// var calllogger *logrus.Logger
// func init() {
// once.Do(func() {
// calllogger = call.GetLogger("HISCALL")
// })
// }
const (
defaultContainerRuntime = "containerd"
)
type TencentClusterHandler struct {
RegionInfo idrv.RegionInfo
CredentialInfo idrv.CredentialInfo
}
func (clusterHandler *TencentClusterHandler) CreateCluster(clusterReqInfo irs.ClusterInfo) (irs.ClusterInfo, error) {
cblogger.Info("Tencent Cloud Driver: called CreateCluster()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo, call.CLUSTER, "CreateCluster()", "CreateCluster()")
start := call.Start()
//
// Validation
//
err := validateAtCreateCluster(clusterReqInfo)
if err != nil {
err = fmt.Errorf("Failed to Create Cluster : %v", err)
cblogger.Error(err)
callLogInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(callLogInfo))
return irs.ClusterInfo{}, err
}
// 클러스터 생성 요청 변환
request, err := getCreateClusterRequest(clusterHandler, clusterReqInfo)
if err != nil {
err := fmt.Errorf("Failed to Get Create Cluster Request : %v", err)
cblogger.Error(err)
return irs.ClusterInfo{}, err
}
res, err := tencent.CreateCluster(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, request)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Create Cluster : %v", err)
cblogger.Error(err)
callLogInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(callLogInfo))
return irs.ClusterInfo{}, err
}
calllogger.Info(call.String(callLogInfo))
// NodeGroup 생성 정보가 있는경우 생성을 시도한다.
// 현재는 생성 시도를 안한다. 생성하기로 결정되면 아래 주석을 풀어서 사용한다.
// 이유:
// - Cluster 생성이 완료되어야 NodeGroup 생성이 가능하다.
// - Cluster 생성이 완료되려면 최소 10분 이상 걸린다.
// - 성공할때까지 대기한 후에 생성을 시도해야 한다.
// for _, node_group := range clusterReqInfo.NodeGroupList {
// res, err := clusterHandler.AddNodeGroup(clusterReqInfo.IId, node_group)
// if err != nil {
// cblogger.Error(err)
// return irs.ClusterInfo{}, err
// }
// }
cluster_info, err := getClusterInfo(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, *res.Response.ClusterId)
if err != nil {
err := fmt.Errorf("Failed to Get ClusterInfo : %v", err)
cblogger.Error(err)
return irs.ClusterInfo{}, err
}
return *cluster_info, nil
}
func (clusterHandler *TencentClusterHandler) ListCluster() ([]*irs.ClusterInfo, error) {
cblogger.Info("Tencent Cloud Driver: called ListCluster()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo, call.CLUSTER, "ListCluster()", "ListCluster()")
start := call.Start()
res, err := tencent.GetClusters(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Get Clusters : %v", err)
cblogger.Error(err)
callLogInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(callLogInfo))
return nil, err
}
calllogger.Info(call.String(callLogInfo))
cluster_info_list := make([]*irs.ClusterInfo, *res.Response.TotalCount)
for i, cluster := range res.Response.Clusters {
cluster_info_list[i], err = getClusterInfo(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, *cluster.ClusterId)
if err != nil {
err := fmt.Errorf("Failed to Get ClusterInfo : %v", err)
cblogger.Error(err)
return nil, err
}
}
return cluster_info_list, nil
}
func (clusterHandler *TencentClusterHandler) GetCluster(clusterIID irs.IID) (irs.ClusterInfo, error) {
cblogger.Info("Tencent Cloud Driver: called GetCluster()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo, call.CLUSTER, clusterIID.NameId, "GetCluster()")
start := call.Start()
cluster_info, err := getClusterInfo(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, clusterIID.SystemId)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Get ClusterInfo : %v", err)
cblogger.Error(err)
callLogInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(callLogInfo))
return irs.ClusterInfo{}, err
}
calllogger.Info(call.String(callLogInfo))
return *cluster_info, nil
}
func (clusterHandler *TencentClusterHandler) DeleteCluster(clusterIID irs.IID) (bool, error) {
cblogger.Info("Tencent Cloud Driver: called DeleteCluster()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo, call.CLUSTER, clusterIID.NameId, "DeleteCluster()")
start := call.Start()
res, err := tencent.DeleteCluster(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, clusterIID.SystemId)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Delete Cluster : %v", err)
cblogger.Error(err)
callLogInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(callLogInfo))
return false, err
}
cblogger.Info("DeleteCluster(): ", res)
calllogger.Info(call.String(callLogInfo))
return true, nil
}
func (clusterHandler *TencentClusterHandler) AddNodeGroup(clusterIID irs.IID, nodeGroupReqInfo irs.NodeGroupInfo) (irs.NodeGroupInfo, error) {
cblogger.Info("Tencent Cloud Driver: called AddNodeGroup()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo, call.CLUSTER, clusterIID.NameId, "AddNodeGroup()")
start := call.Start()
//
// Validation
//
err := validateAtAddNodeGroup(clusterIID, nodeGroupReqInfo)
if err != nil {
err := fmt.Errorf("Failed to Add Node Group : %v", err)
cblogger.Error(err)
callLogInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(callLogInfo))
return irs.NodeGroupInfo{}, err
}
// 노드 그룹 생성 요청 변환
// get cluster info. to get security_group_id
request, err := getNodeGroupRequest(clusterHandler, clusterIID.SystemId, nodeGroupReqInfo)
if err != nil {
err := fmt.Errorf("Failed to Get Node Group Request : %v", err)
cblogger.Error(err)
return irs.NodeGroupInfo{}, err
}
response, err := tencent.CreateNodeGroup(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, request)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Add Node Group : %v", err)
cblogger.Error(err)
callLogInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(callLogInfo))
return irs.NodeGroupInfo{}, err
}
calllogger.Info(call.String(callLogInfo))
node_group_info, err := getNodeGroupInfo(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, clusterIID.SystemId, *response.Response.NodePoolId)
if err != nil {
err := fmt.Errorf("Failed to Get Node Group Info : %v", err)
cblogger.Error(err)
return irs.NodeGroupInfo{}, err
}
return *node_group_info, nil
}
// func (clusterHandler *TencentClusterHandler) ListNodeGroup(clusterIID irs.IID) ([]*irs.NodeGroupInfo, error) {
// cblogger.Info("Tencent Cloud Driver: called ListNodeGroup()")
// callLogInfo := getCallLogScheme(clusterHandler.RegionInfo.Region, call.CLUSTER, clusterIID.NameId, "ListNodeGroup()")
// start := call.Start()
// node_group_info_list := []*irs.NodeGroupInfo{}
// res, err := tencent.ListNodeGroup(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, clusterIID.SystemId)
// callLogInfo.ElapsedTime = call.Elapsed(start)
// if err != nil {
// err := fmt.Errorf("Failed to List Node Group : %v", err)
// cblogger.Error(err)
// callLogInfo.ErrorMSG = err.Error()
// calllogger.Error(call.String(callLogInfo))
// return node_group_info_list, err
// }
// calllogger.Info(call.String(callLogInfo))
// for _, node_group := range res.Response.NodePoolSet {
// node_group_info, err := getNodeGroupInfo(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, clusterIID.SystemId, *node_group.NodePoolId)
// if err != nil {
// err := fmt.Errorf("Failed to Get Node Group Info: %v", err)
// cblogger.Error(err)
// return nil, err
// }
// node_group_info_list = append(node_group_info_list, node_group_info)
// }
// return node_group_info_list, nil
// }
// func (clusterHandler *TencentClusterHandler) GetNodeGroup(clusterIID irs.IID, nodeGroupIID irs.IID) (irs.NodeGroupInfo, error) {
// cblogger.Info("Tencent Cloud Driver: called GetNodeGroup()")
// callLogInfo := getCallLogScheme(clusterHandler.RegionInfo.Region, call.CLUSTER, clusterIID.NameId, "GetNodeGroup()")
// start := call.Start()
// temp, err := getNodeGroupInfo(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, clusterIID.SystemId, nodeGroupIID.SystemId)
// callLogInfo.ElapsedTime = call.Elapsed(start)
// if err != nil {
// err := fmt.Errorf("Failed to Get Node Group Info: %v", err)
// cblogger.Error(err)
// callLogInfo.ErrorMSG = err.Error()
// calllogger.Error(call.String(callLogInfo))
// return irs.NodeGroupInfo{}, err
// }
// calllogger.Info(call.String(callLogInfo))
// return *temp, nil
// }
func (clusterHandler *TencentClusterHandler) SetNodeGroupAutoScaling(clusterIID irs.IID, nodeGroupIID irs.IID, on bool) (bool, error) {
cblogger.Info("Tencent Cloud Driver: called SetNodeGroupAutoScaling()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo, call.CLUSTER, clusterIID.NameId, "SetNodeGroupAutoScaling()")
start := call.Start()
temp, err := tencent.SetNodeGroupAutoScaling(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, clusterIID.SystemId, nodeGroupIID.SystemId, on)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Set Node Group AutoScaling: %v", err)
cblogger.Error(err)
callLogInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(callLogInfo))
return false, err
}
cblogger.Debug(temp.ToJsonString())
calllogger.Info(call.String(callLogInfo))
return true, nil
}
func (clusterHandler *TencentClusterHandler) ChangeNodeGroupScaling(clusterIID irs.IID, nodeGroupIID irs.IID, desiredNodeSize int, minNodeSize int, maxNodeSize int) (irs.NodeGroupInfo, error) {
cblogger.Info("Tencent Cloud Driver: called ChangeNodeGroupScaling()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo, call.CLUSTER, clusterIID.NameId, "ChangeNodeGroupScaling()")
start := call.Start()
//
// Validation
//
err := validateAtChangeNodeGroupScaling(clusterIID, nodeGroupIID, minNodeSize, maxNodeSize)
if err != nil {
err := fmt.Errorf("Failed to Change Node Group Scaling: %v", err)
cblogger.Error(err)
callLogInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(callLogInfo))
return irs.NodeGroupInfo{}, err
}
nodegroup, err := tencent.GetNodeGroup(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, clusterIID.SystemId, nodeGroupIID.SystemId)
if err != nil {
err := fmt.Errorf("Failed to Get Node Group: %v", err)
cblogger.Error(err)
return irs.NodeGroupInfo{}, err
}
temp, err := tencent.ChangeNodeGroupScaling(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, *nodegroup.Response.NodePool.AutoscalingGroupId, uint64(desiredNodeSize), uint64(minNodeSize), uint64(maxNodeSize))
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Change Node Group Scaling: %v", err)
cblogger.Error(err)
callLogInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(callLogInfo))
return irs.NodeGroupInfo{}, err
}
cblogger.Debug(temp.ToJsonString())
calllogger.Info(call.String(callLogInfo))
node_group_info, err := getNodeGroupInfo(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, clusterIID.SystemId, nodeGroupIID.SystemId)
if err != nil {
err := fmt.Errorf("Failed to Get NodeGroupInfo: %v", err)
cblogger.Error(err)
return irs.NodeGroupInfo{}, err
}
return *node_group_info, nil
}
func (clusterHandler *TencentClusterHandler) RemoveNodeGroup(clusterIID irs.IID, nodeGroupIID irs.IID) (bool, error) {
cblogger.Info("Tencent Cloud Driver: called RemoveNodeGroup()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo, call.CLUSTER, clusterIID.NameId, "RemoveNodeGroup()")
start := call.Start()
res, err := tencent.DeleteNodeGroup(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, clusterIID.SystemId, nodeGroupIID.SystemId)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Delete NodeGroup: %v", err)
cblogger.Error(err)
callLogInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(callLogInfo))
return false, err
}
cblogger.Debug(res.ToJsonString())
calllogger.Info(call.String(callLogInfo))
return true, nil
}
func (clusterHandler *TencentClusterHandler) UpgradeCluster(clusterIID irs.IID, newVersion string) (irs.ClusterInfo, error) {
cblogger.Info("Tencent Cloud Driver: called UpgradeCluster()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo, call.CLUSTER, clusterIID.NameId, "UpgradeCluster()")
start := call.Start()
res, err := tencent.UpgradeCluster(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, clusterIID.SystemId, newVersion)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Upgrade Cluster: %v", err)
cblogger.Error(err)
callLogInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(callLogInfo))
return irs.ClusterInfo{}, err
}
cblogger.Debug(res.ToJsonString())
calllogger.Info(call.String(callLogInfo))
clusterInfo, err := getClusterInfo(clusterHandler.CredentialInfo.ClientId, clusterHandler.CredentialInfo.ClientSecret, clusterHandler.RegionInfo.Region, clusterIID.SystemId)
if err != nil {
err := fmt.Errorf("Failed to Get ClusterInfo: %v", err)
cblogger.Error(err)
return irs.ClusterInfo{}, err
}
return *clusterInfo, nil
}
func getClusterInfo(access_key string, access_secret string, region_id string, cluster_id string) (clusterInfo *irs.ClusterInfo, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Failed to Process getClusterInfo() : %v\n\n%v", r, string(debug.Stack()))
cblogger.Error(err)
}
}()
res, err := tencent.GetCluster(access_key, access_secret, region_id, cluster_id)
if err != nil {
err := fmt.Errorf("Failed to Get Cluster: %v", err)
cblogger.Error(err)
return nil, err
}
if *res.Response.TotalCount == 0 {
err := fmt.Errorf("Failed to Get Cluster: cluster_id: %s", cluster_id)
cblogger.Error(err)
return nil, err
}
// https://intl.cloud.tencent.com/document/api/457/32022#ClusterStatus
// Cluster status (Running, Creating, Idling or Abnormal)
health_status := *res.Response.Clusters[0].ClusterStatus
cluster_status := irs.ClusterActive
if strings.EqualFold(health_status, "Creating") {
cluster_status = irs.ClusterCreating
} else if strings.EqualFold(health_status, "Upgrading") {
cluster_status = irs.ClusterUpdating
} else if strings.EqualFold(health_status, "Deleting") {
cluster_status = irs.ClusterDeleting
} else if strings.EqualFold(health_status, "Running") {
cluster_status = irs.ClusterActive
} else {
cluster_status = irs.ClusterInactive
}
// else if strings.EqualFold(health_status, "") { // tencent has no "delete" state
// cluster_status = irs.ClusterDeleting
//}
created_at := *res.Response.Clusters[0].CreatedTime // "2022-09-09T13:10:06Z",
datetime, err := time.Parse(time.RFC3339, created_at)
if err != nil {
err := fmt.Errorf("Failed to Parse Create Time : %v", err)
cblogger.Error(err)
panic(err)
}
// description에서 security group 이름 추출
security_group_id := ""
re := regexp.MustCompile(`\S*#CB-SPIDER:PMKS:SECURITYGROUP:ID:\S*`)
found := re.FindString(*res.Response.Clusters[0].ClusterDescription)
if found != "" {
split := strings.Split(found, "#CB-SPIDER:PMKS:SECURITYGROUP:ID:")
security_group_id = split[1]
}
subnet_id := ""
re = regexp.MustCompile(`\S*#CB-SPIDER:PMKS:SUBNET:ID:\S*`)
found = re.FindString(*res.Response.Clusters[0].ClusterDescription)
if found != "" {
split := strings.Split(found, "#CB-SPIDER:PMKS:SUBNET:ID:")
subnet_id = split[1]
}
accessInfo, err := getClusterAccessInfo(access_key, access_secret, region_id, cluster_id, security_group_id)
if err != nil {
cblogger.Error(err)
return nil, err
}
clusterInfo = &irs.ClusterInfo{
IId: irs.IID{
NameId: *res.Response.Clusters[0].ClusterName,
SystemId: *res.Response.Clusters[0].ClusterId,
},
Version: *res.Response.Clusters[0].ClusterVersion,
Network: irs.NetworkInfo{
VpcIID: irs.IID{
NameId: "",
SystemId: *res.Response.Clusters[0].ClusterNetworkSettings.VpcId,
},
SecurityGroupIIDs: []irs.IID{{NameId: "", SystemId: security_group_id}},
SubnetIIDs: []irs.IID{{NameId: "", SystemId: subnet_id}},
},
Status: cluster_status,
CreatedTime: datetime,
AccessInfo: accessInfo,
// KeyValueList: []irs.KeyValue{}, // flatten data 입력하기
}
// k,v 추출 & 추가
// KeyValueList: []irs.KeyValue{}, // flatten data 입력하기
temp, err := json.Marshal(*res.Response.Clusters[0])
if err != nil {
err := fmt.Errorf("Failed to Marshal Cluster Info : %v", err)
cblogger.Error(err)
panic(err)
}
var json_obj map[string]interface{}
json.Unmarshal([]byte(temp), &json_obj)
flat, err := flatten.Flatten(json_obj, "", flatten.DotStyle)
if err != nil {
err := fmt.Errorf("Failed to Flatten Cluster Info : %v", err)
cblogger.Error(err)
return nil, err
}
for k, v := range flat {
temp := fmt.Sprintf("%v", v)
clusterInfo.KeyValueList = append(clusterInfo.KeyValueList, irs.KeyValue{Key: k, Value: temp})
}
// NodeGroups
res2, err := tencent.ListNodeGroup(access_key, access_secret, region_id, cluster_id)
if err != nil {
err := fmt.Errorf("Failed to List Node Group : %v", err)
cblogger.Error(err)
return nil, err
}
for _, nodepool := range res2.Response.NodePoolSet {
node_group_info, err := getNodeGroupInfo(access_key, access_secret, region_id, cluster_id, *nodepool.NodePoolId)
if err != nil {
err := fmt.Errorf("Failed to Get Node Group Info : %v", err)
cblogger.Error(err)
return nil, err
}
clusterInfo.NodeGroupList = append(clusterInfo.NodeGroupList, *node_group_info)
}
return clusterInfo, err
}
func getClusterAccessInfo(access_key string, access_secret string, region_id string, cluster_id string, security_group_id string) (accessInfo irs.AccessInfo, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Failed to Process getClusterAccessInfo() : %v\n\n%v", r, string(debug.Stack()))
cblogger.Error(err)
}
}()
accessInfo = irs.AccessInfo{
Endpoint: "Endpoint is not ready yet!",
Kubeconfig: "Kubeconfig is not ready yet!",
}
// (1) Endpoint
res, err := tencent.GetClusterEndpoint(access_key, access_secret, region_id, cluster_id)
if err != nil {
if strings.Contains(err.Error(), "CLUSTER_IN_ABNORMAL_STAT") || strings.Contains(err.Error(), "CLUSTER_STATE_ERROR") {
cblogger.Error(cluster_id + err.Error())
accessInfo.Endpoint = "Cluster is not ready yet!"
} else {
err := fmt.Errorf("Failed to Get Cluster Endpoint: %v", err)
cblogger.Error(err)
return irs.AccessInfo{}, err
}
}
if res == nil || res.Response == nil {
return accessInfo, nil
}
if *res.Response.ClusterExternalEndpoint == "" {
_, err := tencent.CreateClusterEndpoint(access_key, access_secret, region_id, cluster_id, security_group_id)
if err != nil {
if strings.Contains(err.Error(), "CLUSTER_IN_ABNORMAL_STAT") || strings.Contains(err.Error(), "CLUSTER_STATE_ERROR") {
cblogger.Error(cluster_id + err.Error())
accessInfo.Endpoint = "First, add a nodegroup."
} else if strings.Contains(err.Error(), "same type task in execution") {
cblogger.Error(cluster_id + err.Error())
accessInfo.Endpoint = "Preparing...."
} else {
err := fmt.Errorf("Failed to Create Cluster Endpoint: %v", err)
cblogger.Error(err)
return irs.AccessInfo{}, err
}
}
} else {
accessInfo.Endpoint = *res.Response.ClusterExternalEndpoint
}
// (2) Kubeconfig
resKubeconfig, err := tencent.GetClusterKubeconfig(access_key, access_secret, region_id, cluster_id)
if err != nil {
if strings.Contains(err.Error(), "CLUSTER_IN_ABNORMAL_STAT") || strings.Contains(err.Error(), "CLUSTER_STATE_ERROR") {
cblogger.Error(cluster_id + err.Error())
accessInfo.Kubeconfig = "Cluster is not ready yet!"
} else {
err := fmt.Errorf("Failed to Get Cluster Kubeconfig: %v", err)
cblogger.Error(err)
return irs.AccessInfo{}, err
}
}
if resKubeconfig == nil || resKubeconfig.Response == nil {
return accessInfo, nil
}
if *resKubeconfig.Response.Kubeconfig == "" {
accessInfo.Kubeconfig = "Preparing...."
} else {
accessInfo.Kubeconfig = changeDomainNameToIP(*resKubeconfig.Response.Kubeconfig, accessInfo.Endpoint)
}
return accessInfo, nil
}
func changeDomainNameToIP(kubeConfig string, endpoint string) string {
TargetStr := " server: https://"
if kubeConfig == "" || !strings.Contains(kubeConfig, TargetStr) {
return kubeConfig
}
if endpoint == "" || !strings.Contains(endpoint, ":") {
return kubeConfig
}
// get IP from 1.2.3.4:443
splits := strings.Split(endpoint, ":")
ip := splits[0]
// replace 'domain name' with 'ip'
// ex) server: https://cls-amu0j0tf.ccs.tencent-cloud.com
// => server: https://1.2.3.4
lines := strings.Split(kubeConfig, "\n")
for i, line := range lines {
if strings.Contains(line, TargetStr) {
lines[i] = TargetStr + ip
}
}
return strings.Join(lines, "\n")
}
func getNodeGroupInfo(access_key, access_secret, region_id, cluster_id, node_group_id string) (nodeGroupInfo *irs.NodeGroupInfo, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Failed to Process getNodeGroupInfo() : %v\n\n%v", r, string(debug.Stack()))
cblogger.Error(err)
}
}()
res, err := tencent.GetNodeGroup(access_key, access_secret, region_id, cluster_id, node_group_id)
if err != nil {
err := fmt.Errorf("Failed to Get Node Group : %v", err)
cblogger.Error(err)
return nil, err
}
launch_config, err := tencent.GetLaunchConfiguration(access_key, access_secret, region_id, *res.Response.NodePool.LaunchConfigurationId)
if err != nil {
err := fmt.Errorf("Failed to Get Launch Configuration : %v", err)
cblogger.Error(err)
return nil, err
}
auto_scaling_group, err := tencent.GetAutoScalingGroup(access_key, access_secret, region_id, *res.Response.NodePool.AutoscalingGroupId)
if err != nil {
err := fmt.Errorf("Failed to Get Auto Scaling Group : %v", err)
cblogger.Error(err)
return nil, err
}
// nodepool LifeState
// The lifecycle state of the current node pool.
// Valid values: creating, normal, updating, deleting, and deleted.
health_status := *res.Response.NodePool.LifeState
status := irs.NodeGroupActive
if strings.EqualFold(health_status, "normal") {
status = irs.NodeGroupActive
} else if strings.EqualFold(health_status, "creating") {
status = irs.NodeGroupUpdating
} else if strings.EqualFold(health_status, "removing") {
status = irs.NodeGroupUpdating // removing is a kind of updating?
} else if strings.EqualFold(health_status, "deleting") {
status = irs.NodeGroupDeleting
} else if strings.EqualFold(health_status, "updating") {
status = irs.NodeGroupUpdating
}
auto_scale_enalbed := false
if strings.EqualFold(*res.Response.NodePool.AutoscalingGroupStatus, "ENABLED") {
auto_scale_enalbed = true
}
if len(launch_config.Response.LaunchConfigurationSet) > 0 && len(auto_scaling_group.Response.AutoScalingGroupSet) > 0 {
nodeGroupInfo = &irs.NodeGroupInfo{
IId: irs.IID{
NameId: *res.Response.NodePool.Name,
SystemId: *res.Response.NodePool.NodePoolId,
},
ImageIID: irs.IID{
NameId: "",
SystemId: *launch_config.Response.LaunchConfigurationSet[0].ImageId,
},
VMSpecName: *launch_config.Response.LaunchConfigurationSet[0].InstanceType,
RootDiskType: *launch_config.Response.LaunchConfigurationSet[0].SystemDisk.DiskType,
RootDiskSize: fmt.Sprintf("%d", *launch_config.Response.LaunchConfigurationSet[0].SystemDisk.DiskSize),
KeyPairIID: irs.IID{NameId: "", SystemId: *launch_config.Response.LaunchConfigurationSet[0].LoginSettings.KeyIds[0]},
Status: status,
OnAutoScaling: auto_scale_enalbed,
MinNodeSize: int(*auto_scaling_group.Response.AutoScalingGroupSet[0].MinSize),
MaxNodeSize: int(*auto_scaling_group.Response.AutoScalingGroupSet[0].MaxSize),
DesiredNodeSize: int(*auto_scaling_group.Response.AutoScalingGroupSet[0].DesiredCapacity),
Nodes: []irs.IID{}, // to be implemented
KeyValueList: []irs.KeyValue{},
}
}
nodes, err := tencent.DescribeClusterInstances(access_key, access_secret, region_id, cluster_id)
if err != nil {
err := fmt.Errorf("Failed to Get Nodes : %v", err)
cblogger.Error(err)
return nil, err
}
for _, node := range nodes.Response.InstanceSet {
if node_group_id == *node.NodePoolId {
if *node.InstanceId != "" {
nodeGroupInfo.Nodes = append(nodeGroupInfo.Nodes, irs.IID{NameId: "", SystemId: *node.InstanceId})
}
}
}
// add key value list
temp, err := json.Marshal(*res.Response.NodePool)
if err != nil {
err := fmt.Errorf("Failed to Marshal NodeGroup Info : %v", err)
cblogger.Error(err)
panic(err)
}
var json_obj map[string]interface{}
json.Unmarshal([]byte(temp), &json_obj)
flat, err := flatten.Flatten(json_obj, "", flatten.DotStyle)
if err != nil {
err := fmt.Errorf("Failed to Flatten NodeGroup Info : %v", err)
cblogger.Error(err)
return nil, err
}
for k, v := range flat {
temp := fmt.Sprintf("%v", v)
nodeGroupInfo.KeyValueList = append(nodeGroupInfo.KeyValueList, irs.KeyValue{Key: k, Value: temp})
}
return nodeGroupInfo, err
}
func getCreateClusterRequest(clusterHandler *TencentClusterHandler, clusterInfo irs.ClusterInfo) (request *tke.CreateClusterRequest, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Failed to Process getCreateClusterRequest() : %v\n\n%v", r, string(debug.Stack()))
cblogger.Error(err)
}
}()
// 172.X.0.0.16: X Range:16, 17, ... , 31
m_cidr := make(map[string]bool)
for i := 16; i < 32; i++ {
m_cidr[fmt.Sprintf("172.%v.0.0/16", i)] = true
}
clusters, err := clusterHandler.ListCluster()
if err != nil {
err := fmt.Errorf("Failed to List Cluster : %v", err)
cblogger.Error(err)
return nil, err
}
for _, cluster := range clusters {
for _, v := range cluster.KeyValueList {
if v.Key == "ClusterNetworkSettings.ClusterCIDR" {
delete(m_cidr, v.Value)
}
}
}
cidr_list := []string{}
for k := range m_cidr {
cidr_list = append(cidr_list, k)
}
request = tke.NewCreateClusterRequest()
request.ClusterCIDRSettings = &tke.ClusterCIDRSettings{
ClusterCIDR: common.StringPtr(cidr_list[0]), // 172.X.0.0.16: X Range:16, 17, ... , 31
}
// security_group_name을 저장하는 방법이 없음.
// description에 securityp_group_name을 저장해서 사용함.
// 향후, 추가 정보가 필요하면, description에 json 문서를 저장하는 방식으로 사용할 수도 있음.
//
// 정보검색은
// 사용자가 필요에 따라서 다른 description내용을 추가할 수 도 있으니,
// "#CB-SPIDER:PMKS:SECURITYGROUP:ID"을 포함하는 Line을 찾아서 처리
// >> regex로 구현
// ------------------------------------------------------------
// subnet_id 저장이 안됨
// description 정보에 저장해서 사용
// SubnetId: common.StringPtr(clusterInfo.Network.SubnetIIDs[0].SystemId),
// " #CB-SPIDER:PMKS:SUBNET:ID:"
desc_str := `#CB-SPIDER:PMKS:SECURITYGROUP:ID:%s #CB-SPIDER:PMKS:SUBNET:ID:%s`
desc_str = fmt.Sprintf(desc_str, clusterInfo.Network.SecurityGroupIIDs[0].SystemId, clusterInfo.Network.SubnetIIDs[0].SystemId)
request.ClusterBasicSettings = &tke.ClusterBasicSettings{
ClusterName: common.StringPtr(clusterInfo.IId.NameId),
VpcId: common.StringPtr(clusterInfo.Network.VpcIID.SystemId),
ClusterVersion: common.StringPtr(clusterInfo.Version), // option, version: 1.22.5
ClusterDescription: common.StringPtr(desc_str), // option, #CB-SPIDER:PMKS:SECURITYGROUP:sg-c00t00ih
}
request.ClusterType = common.StringPtr("MANAGED_CLUSTER") //default value
request.ClusterAdvancedSettings = &tke.ClusterAdvancedSettings{
ContainerRuntime: common.StringPtr(defaultContainerRuntime),
}
return request, err
}
func getNodeGroupRequest(clusterHandler *TencentClusterHandler, cluster_id string, nodeGroupReqInfo irs.NodeGroupInfo) (request *tke.CreateClusterNodePoolRequest, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Failed to Process getNodeGroupRequest() : %v\n\n%v", r, string(debug.Stack()))
cblogger.Error(err)
}
}()
cluster, res := clusterHandler.GetCluster(irs.IID{SystemId: cluster_id})
if res != nil {
err := fmt.Errorf("Failed to Get Cluster : %v", err)
cblogger.Error(err)
return nil, res
}
vpc_id := cluster.Network.VpcIID.SystemId
subnet_id := cluster.Network.SubnetIIDs[0].SystemId
security_group_id := cluster.Network.SecurityGroupIIDs[0].SystemId
disk_size, _ := strconv.ParseInt(nodeGroupReqInfo.RootDiskSize, 10, 64)
strSystemDisk := ""
switch {
case nodeGroupReqInfo.RootDiskType == "" && disk_size == 0:
strSystemDisk = ""
case nodeGroupReqInfo.RootDiskType == "" && disk_size != 0:
strSystemDisk = `"SystemDisk": { "DiskSize": %d },`
strSystemDisk = fmt.Sprintf(strSystemDisk, disk_size)
case nodeGroupReqInfo.RootDiskType != "" && disk_size == 0:
strSystemDisk = `"SystemDisk": { "DiskType" : "%s" },`
strSystemDisk = fmt.Sprintf(strSystemDisk, nodeGroupReqInfo.RootDiskType)
default:
strSystemDisk = `"SystemDisk": { "DiskType" : "%s", "DiskSize": %d },`
strSystemDisk = fmt.Sprintf(strSystemDisk, nodeGroupReqInfo.RootDiskType, disk_size)
}
// '{"LaunchConfigurationName":"name","InstanceType":"S3.MEDIUM2","ImageId":"img-pi0ii46r"}'
// "SystemDisk": { "DiskType" : "CLOUD_BSSD", "DiskSize": 50 },
// ImageId를 설정하면 에러 발생, 설정안됨.
launch_config_json_str := `{
"InstanceType": "%s",
"SecurityGroupIds": ["%s"],
"LoginSettings": { "KeyIds" : ["%s"] },
"InstanceChargeType": "POSTPAID_BY_HOUR",
%s
"InternetAccessible": {
"InternetChargeType":"TRAFFIC_POSTPAID_BY_HOUR",
"InternetMaxBandwidthOut": 1,
"PublicIpAssigned": true
}
}`
launch_config_json_str = fmt.Sprintf(launch_config_json_str, nodeGroupReqInfo.VMSpecName, security_group_id, nodeGroupReqInfo.KeyPairIID.SystemId, strSystemDisk)
auto_scaling_group_json_str := `{
"MinSize": %d,
"MaxSize": %d,
"DesiredCapacity": %d,
"VpcId": "%s",
"SubnetIds": ["%s"]
}`
auto_scaling_group_json_str = fmt.Sprintf(auto_scaling_group_json_str, nodeGroupReqInfo.MinNodeSize, nodeGroupReqInfo.MaxNodeSize, nodeGroupReqInfo.DesiredNodeSize, vpc_id, subnet_id)
request = tke.NewCreateClusterNodePoolRequest()
request.Name = common.StringPtr(nodeGroupReqInfo.IId.NameId)
request.ClusterId = common.StringPtr(cluster_id)
request.LaunchConfigurePara = common.StringPtr(launch_config_json_str)
request.AutoScalingGroupPara = common.StringPtr(auto_scaling_group_json_str)
request.EnableAutoscale = common.BoolPtr(nodeGroupReqInfo.OnAutoScaling)
request.InstanceAdvancedSettings = &tke.InstanceAdvancedSettings{
// DataDisks: []*tke.DataDisk{
// {
// DiskType: common.StringPtr(nodeGroupReqInfo.RootDiskType), //ex. "CLOUD_PREMIUM"
// DiskSize: common.Int64Ptr(disk_size), //ex. 50
// },
// },
}
if nodeGroupReqInfo.ImageIID.SystemId != "" {
// 등록 가능한 이미지 이름 목록: https://www.tencentcloud.com/document/product/457/46750
request.NodePoolOs = common.StringPtr(nodeGroupReqInfo.ImageIID.SystemId) // ex: "tlinux3.1x86_64"
}
// request.ContainerRuntime = common.StringPtr("docker")
// request.RuntimeVersion = common.StringPtr("19.3")
print(request.ToJsonString())
return request, err
}
// func getCallLogScheme(region string, resourceType call.RES_TYPE, resourceName string, apiName string) call.CLOUDLOGSCHEMA {
// cblogger.Info(fmt.Sprintf("Call %s %s", call.TENCENT, apiName))
// return call.CLOUDLOGSCHEMA{
// CloudOS: call.TENCENT,
// RegionZone: region,
// ResourceType: resourceType,
// ResourceName: resourceName,
// CloudOSAPI: apiName,
// }
// }
func validateAtCreateCluster(clusterInfo irs.ClusterInfo) error {
if clusterInfo.IId.NameId == "" {
return fmt.Errorf("Cluster name is required")
}
if clusterInfo.Network.VpcIID.SystemId == "" && clusterInfo.Network.VpcIID.NameId == "" {
return fmt.Errorf("Cannot identify VPC(IID=%s)", clusterInfo.Network.VpcIID)
}
if len(clusterInfo.Network.SubnetIIDs) < 1 {
return fmt.Errorf("At least one Subnet must be specified")
}
if len(clusterInfo.Network.SecurityGroupIIDs) < 1 {
return fmt.Errorf("At least one Subnet must be specified")
}
// CAUTION: Currently CB-Spider's Tencent PMKS Drivers does not support to create a cluster with nodegroups
if len(clusterInfo.NodeGroupList) > 0 {
return fmt.Errorf("Node Group cannot be specified")
}
return nil
}
func validateAtAddNodeGroup(clusterIID irs.IID, nodeGroupInfo irs.NodeGroupInfo) error {
if clusterIID.SystemId == "" && clusterIID.NameId == "" {
return fmt.Errorf("Invalid Cluster IID")
}
if nodeGroupInfo.IId.NameId == "" {
return fmt.Errorf("Node Group name is required")
}
if nodeGroupInfo.MaxNodeSize < 1 {
return fmt.Errorf("MaxNodeSize cannot be smaller than 1")
}
if nodeGroupInfo.MinNodeSize < 1 {
return fmt.Errorf("MaxNodeSize cannot be smaller than 1")
}
if nodeGroupInfo.DesiredNodeSize < 1 {
return fmt.Errorf("DesiredNodeSize cannot be smaller than 1")
}
if nodeGroupInfo.VMSpecName == "" {
return fmt.Errorf("VM Spec Name is required")
}
return nil
}
func validateAtChangeNodeGroupScaling(clusterIID irs.IID, nodeGroupIID irs.IID, minNodeSize int, maxNodeSize int) error {
if clusterIID.SystemId == "" && clusterIID.NameId == "" {
return fmt.Errorf("Invalid Cluster IID")
}
if nodeGroupIID.SystemId == "" && nodeGroupIID.NameId == "" {
return fmt.Errorf("Invalid Node Group IID")
}
if minNodeSize < 1 {
return fmt.Errorf("MaxNodeSize cannot be smaller than 1")
}
if maxNodeSize < 1 {
return fmt.Errorf("MaxNodeSize cannot be smaller than 1")
}
return nil
}