-
Notifications
You must be signed in to change notification settings - Fork 47
/
ClusterHandler.go
756 lines (665 loc) · 28.1 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
// Proof of Concepts 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 a Cloud Driver Example for PoC Test.
//
// by ETRI, 2022.08.
package resources
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"runtime/debug"
nhnsdk "github.com/cloud-barista/nhncloud-sdk-go"
"github.com/jeremywohl/flatten"
call "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/call-log"
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"
)
type NhnCloudClusterHandler struct {
RegionInfo idrv.RegionInfo
VMClient *nhnsdk.ServiceClient
ClusterClient *nhnsdk.ServiceClient
}
func (clusterHandler *NhnCloudClusterHandler) CreateCluster(clusterReqInfo irs.ClusterInfo) (irs.ClusterInfo, error) {
cblogger.Info("NHN Cloud Driver: called CreateCluster()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo.Region, call.CLUSTER, clusterReqInfo.IId.NameId, "CreateCluster()")
start := call.Start()
// 클러스터 생성 요청을 JSON 요청으로 변환
payload, err := getClusterInfoRequestJSONString(clusterReqInfo, clusterHandler)
if err != nil {
err := fmt.Errorf("Failed to Get ClusterInfo JSON String : %v", err)
cblogger.Error(err)
return irs.ClusterInfo{}, err
}
response_json_str, err := CreateCluster(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, payload)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Create Cluster : %v", err)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return irs.ClusterInfo{}, err
}
cblogger.Info(call.String(callLogInfo))
// 클러스터를 생성하고 나서 바로 GetClusterInfo를 하면
// 클러스터 정보를 가져올 때도 있지만 못가져 오는 경우도 있다.
// 생성요청이 진행되는 중에 조회를 시도해서 그런것으로 추정된다.
// 실패를 방지하기 위해 10초간 대기한 후에 조회를 시도한다.
time.Sleep(time.Second * 10)
// 클러스터 생성이 성공하면 getClusterInfo로 조회해서 반환한다.
// 만약 getClusterInfo가 실패하면, 요청정보에 cluster_id를 포함해서 반환하는 것으로 처리한다.
var response_json_obj map[string]interface{}
json.Unmarshal([]byte(response_json_str), &response_json_obj)
clusterReqInfo.IId.SystemId = response_json_obj["uuid"].(string)
// // NodeGroup 생성 정보가 있는경우 생성을 시도한다.
// // 문제는 Cluster 생성이 완료되어야 NodeGroup 생성이 가능하다.
// // Cluster 생성이 완료되려면 최소 10분 이상 걸린다.
// // 성공할때까지 대기한 후에 생성을 시도해야 하는가?
// for {
// time.Sleep(10 * time.Second)
// clusterInfo, err := getClusterInfo(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterReqInfo.IId.SystemId)
// if err != nil {
// err := fmt.Errorf("Failed to Get ClusterInfo : %v", err)
// cblogger.Error(err)
// return irs.ClusterInfo{}, err
// }
// cblogger.Info(clusterInfo.Status)
// if clusterInfo.Status == irs.ClusterActive {
// break
// }
// }
// for _, node_group := range clusterReqInfo.NodeGroupList {
// node_group_info, err := clusterHandler.AddNodeGroup(cluster_info.IId, node_group)
// if err != nil {
// cblogger.Error(err)
// return irs.ClusterInfo{}, err
// }
// cluster_info.NodeGroupList = append(cluster_info.NodeGroupList, node_group_info)
// }
cluster_info, err := getClusterInfo(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterReqInfo.IId.SystemId)
if err != nil {
err := fmt.Errorf("Failed to Get ClusterInfo : %v", err)
cblogger.Error(err)
return irs.ClusterInfo{}, err
}
if cluster_info == nil {
err = fmt.Errorf("Succeeded in Creating Cluster but Failed to Get ClusterInfo")
cblogger.Error(err)
return clusterReqInfo, err
}
return *cluster_info, nil
}
func (clusterHandler *NhnCloudClusterHandler) ListCluster() ([]*irs.ClusterInfo, error) {
cblogger.Info("NHN Cloud Driver: called ListCluster()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo.Region, call.CLUSTER, "ListCluster()", "ListCluster()") // HisCall logging
start := call.Start()
clusters_json_str, err := GetClusters(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to List Cluster : %v", err)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return nil, err
}
cblogger.Info(call.String(callLogInfo))
var clusters_json_obj map[string]interface{}
json.Unmarshal([]byte(clusters_json_str), &clusters_json_obj)
clusters := clusters_json_obj["clusters"].([]interface{})
cluster_info_list := make([]*irs.ClusterInfo, len(clusters))
for i, cluster := range clusters {
uuid := cluster.(map[string]interface{})["uuid"].(string)
cluster_info_list[i], err = getClusterInfo(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, uuid)
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 *NhnCloudClusterHandler) GetCluster(clusterIID irs.IID) (irs.ClusterInfo, error) {
cblogger.Info("NHN Cloud Driver: called GetCluster()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo.Region, call.CLUSTER, clusterIID.NameId, "GetCluster()")
start := call.Start()
cluster_info, err := getClusterInfo(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Get ClusterInfo : %v", err)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return irs.ClusterInfo{}, err
}
cblogger.Info(call.String(callLogInfo))
return *cluster_info, nil
}
func (clusterHandler *NhnCloudClusterHandler) DeleteCluster(clusterIID irs.IID) (bool, error) {
cblogger.Info("NHN Cloud Driver: called DeleteCluster()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo.Region, call.CLUSTER, clusterIID.NameId, "DeleteCluster()")
start := call.Start()
res, err := DeleteCluster(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Delete Cluster : %v", err)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return false, err
}
if res != "" {
// 삭제 처리를 성공하면 ""를 리턴한다.
// 삭제 처리를 실패하면 에러 메시지를 리턴한다.
err = fmt.Errorf("Failed to Delete Cluster : %s", res)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return false, err
}
cblogger.Info(call.String(callLogInfo))
return true, nil
}
func (clusterHandler *NhnCloudClusterHandler) AddNodeGroup(clusterIID irs.IID, nodeGroupReqInfo irs.NodeGroupInfo) (irs.NodeGroupInfo, error) {
cblogger.Info("NHN Cloud Driver: called AddNodeGroup()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo.Region, call.CLUSTER, clusterIID.NameId, "AddNodeGroup()")
start := call.Start()
// 노드 그룹 생성 요청을 JSON 요청으로 변환
payload, err := getNodeGroupRequestJSONString(nodeGroupReqInfo, clusterHandler)
if err != nil {
err := fmt.Errorf("Failed to Get NodeGroup Request JSON String : %v", err)
cblogger.Error(err)
return irs.NodeGroupInfo{}, err
}
result_json_str, err := CreateNodeGroup(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId, payload)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Create NodeGroup : %v", err)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return irs.NodeGroupInfo{}, err
}
cblogger.Info(call.String(callLogInfo))
var result_json_obj map[string]interface{}
json.Unmarshal([]byte(result_json_str), &result_json_obj)
if result_json_obj["errors"] != nil {
err := fmt.Errorf("Failed to Create NodeGroup : %s", result_json_str)
cblogger.Error(err)
return irs.NodeGroupInfo{}, err
}
uuid := result_json_obj["uuid"].(string)
node_group_info, err := getNodeGroupInfo(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId, uuid)
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 *NhnCloudClusterHandler) ListNodeGroup(clusterIID irs.IID) ([]*irs.NodeGroupInfo, error) {
cblogger.Info("NHN Cloud Driver: called ListNodeGroup()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo.Region, call.CLUSTER, clusterIID.NameId, "ListNodeGroup()")
start := call.Start()
node_group_info_list := []*irs.NodeGroupInfo{}
node_groups_json_str, err := GetNodeGroups(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to List NodeGroup : %v", err)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return node_group_info_list, err
}
cblogger.Info(call.String(callLogInfo))
var node_groups_json_obj map[string]interface{}
json.Unmarshal([]byte(node_groups_json_str), &node_groups_json_obj)
node_groups := node_groups_json_obj["nodegroups"].([]interface{})
for _, node_group := range node_groups {
node_group_info, err := getNodeGroupInfo(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId, node_group.(map[string]interface{})["uuid"].(string))
if err != nil {
err := fmt.Errorf("Failed to Get NodeGroupInfo : %v", err)
cblogger.Error(err)
return node_group_info_list, err
}
node_group_info_list = append(node_group_info_list, node_group_info)
}
return node_group_info_list, nil
}
func (clusterHandler *NhnCloudClusterHandler) GetNodeGroup(clusterIID irs.IID, nodeGroupIID irs.IID) (irs.NodeGroupInfo, error) {
cblogger.Info("NHN Cloud Driver: called GetNodeGroup()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo.Region, call.CLUSTER, clusterIID.NameId, "GetNodeGroup()")
start := call.Start()
temp, err := getNodeGroupInfo(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId, nodeGroupIID.SystemId)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Get NodeGroup : %v", err)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return irs.NodeGroupInfo{}, err
}
cblogger.Info(call.String(callLogInfo))
return *temp, nil
}
func (clusterHandler *NhnCloudClusterHandler) SetNodeGroupAutoScaling(clusterIID irs.IID, nodeGroupIID irs.IID, on bool) (bool, error) {
cblogger.Info("NHN Cloud Driver: called SetNodeGroupAutoScaling()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo.Region, call.CLUSTER, clusterIID.NameId, "GetNodeGroup()")
start := call.Start()
temp := `{"ca_enable": %t}`
payload := fmt.Sprintf(temp, on)
_, err := ChangeNodeGroupAutoScaler(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId, nodeGroupIID.SystemId, payload)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Set NodeGroup AutoScaling : %v", err)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return false, err
}
cblogger.Info(call.String(callLogInfo))
return true, nil
}
func (clusterHandler *NhnCloudClusterHandler) ChangeNodeGroupScaling(clusterIID irs.IID, nodeGroupIID irs.IID, desiredNodeSize int, minNodeSize int, maxNodeSize int) (irs.NodeGroupInfo, error) {
cblogger.Info("NHN Cloud Driver: called ChangeNodeGroupScaling()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo.Region, call.CLUSTER, clusterIID.NameId, "GetNodeGroup()")
start := call.Start()
temp := `{
"ca_enable": true,
"ca_max_node_count": %d,
"ca_min_node_count": %d
}`
payload := fmt.Sprintf(temp, maxNodeSize, minNodeSize)
//desiredNodeSize is not supported in NHN Cloud
_, err := ChangeNodeGroupAutoScaler(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId, nodeGroupIID.SystemId, payload)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Change NodeGroup Scaling : %v", err)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return irs.NodeGroupInfo{}, err
}
cblogger.Info(call.String(callLogInfo))
node_group_info, err := getNodeGroupInfo(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, 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 *NhnCloudClusterHandler) RemoveNodeGroup(clusterIID irs.IID, nodeGroupIID irs.IID) (bool, error) {
cblogger.Info("NHN Cloud Driver: called RemoveNodeGroup()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo.Region, call.CLUSTER, clusterIID.NameId, "RemoveNodeGroup()")
start := call.Start()
res, err := DeleteNodeGroup(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId, nodeGroupIID.SystemId)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Remove NodeGroup : %v", err)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return false, err
}
if res != "" {
// 삭제 처리를 성공하면 ""를 리턴한다.
// 삭제 처리를 실패하면 에러 메시지를 리턴한다.
err := fmt.Errorf("Failed to Remove NodeGroup : %s", res)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return false, err
}
cblogger.Info(call.String(callLogInfo))
return true, nil
}
// 업그레이드 순서
// default-master 노드 그룹을 업그레이드한다.
// default-master 업그레이드가 완료되면, worker 노드들을 업그레이드 한다.
// default-master 업그레이드가 완료되기 전에는 worker 노드를 업그레이드 할 수 없다.
// default-master 업그레이드가 완료된 후에 (10분? 정도 소요됨) worker 노드를 업그레이드해야 한다.
func (clusterHandler *NhnCloudClusterHandler) UpgradeCluster(clusterIID irs.IID, newVersion string) (irs.ClusterInfo, error) {
cblogger.Info("NHN Cloud Driver: called UpgradeCluster()")
callLogInfo := GetCallLogScheme(clusterHandler.RegionInfo.Region, call.CLUSTER, clusterIID.NameId, "UpgradeCluster()")
start := call.Start()
temp := `{"version": "%s"}`
payload := fmt.Sprintf(temp, newVersion)
res, err := UpgradeCluster(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId, "default-master", payload)
callLogInfo.ElapsedTime = call.Elapsed(start)
if err != nil {
err := fmt.Errorf("Failed to Upgrade Cluster : %v", err)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return irs.ClusterInfo{}, err
}
if strings.Contains(res, "errors") {
// {"errors": [{"request_id": "", "code": "", "status": 406, "title": "", "detail": "", "links": []}]}
err := fmt.Errorf("Failed to Upgrade Cluster : %s", res)
cblogger.Error(err)
cblogger.Error(call.String(callLogInfo))
return irs.ClusterInfo{}, err
}
cblogger.Info(call.String(callLogInfo))
// // default-master 업그레이드 완료 확인 코드
// // 업그레이드가 완료되면 worker 노드 업그레이드 진행
// for {
// time.Sleep(10 * time.Second)
// clusterInfo, err := getClusterInfo(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId)
// if err != nil {
// err := fmt.Errorf("Failed to Get ClusterInfo : %v", err)
// cblogger.Error(err)
// return irs.ClusterInfo{}, err
// }
// cblogger.Info(clusterInfo.Status)
// if clusterInfo.Status == irs.ClusterActive {
// break
// }
// }
// node_groups_json_str, err := GetNodeGroups(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId)
// if err != nil {
// err := fmt.Errorf("Failed to Get NodeGroups : %v", err)
// cblogger.Error(err)
// return irs.ClusterInfo{}, err
// }
// var node_groups_json_obj map[string]interface{}
// json.Unmarshal([]byte(node_groups_json_str), &node_groups_json_obj)
// node_groups := node_groups_json_obj["nodegroups"].([]interface{})
// for _, node_group := range node_groups {
// start := call.Start()
// res, err := UpgradeCluster(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, clusterIID.SystemId, node_group.(map[string]interface{})["uuid"].(string), payload)
// callLogInfo.ElapsedTime = call.Elapsed(start)
// cblogger.Info(call.String(callLogInfo))
// if err != nil {
// err := fmt.Errorf("Failed to Upgrade Cluster : %v", err)
// cblogger.Error(err)
// return irs.ClusterInfo{}, err
// }
// if strings.Contains(res, "errors") {
// // {"errors": [{"request_id": "", "code": "", "status": 406, "title": "", "detail": "", "links": []}]}
// err := fmt.Errorf("Failed to Upgrade Cluster : %s", res)
// cblogger.Error(err)
// return irs.ClusterInfo{}, err
// }
// }
clusterInfo, err := getClusterInfo(clusterHandler.ClusterClient.Endpoint, clusterHandler.ClusterClient.TokenID, 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(host string, token 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)
}
}()
cluster_json_str, err := GetCluster(host, token, cluster_id)
if err != nil {
err := fmt.Errorf("Failed to Get Cluster : %v", err)
cblogger.Error(err)
return nil, err
}
var cluster_json_obj map[string]interface{}
json.Unmarshal([]byte(cluster_json_str), &cluster_json_obj)
health_status := cluster_json_obj["status"].(string)
cluster_status := irs.ClusterActive
if health_status == "CREATE_IN_PROGRESS" {
cluster_status = irs.ClusterCreating
} else if health_status == "UPDATE_IN_PROGRESS" {
cluster_status = irs.ClusterUpdating
} else if health_status == "UPDATE_FAILED" {
cluster_status = irs.ClusterInactive
} else if health_status == "DELETE_IN_PROGRESS" {
cluster_status = irs.ClusterDeleting
} else if health_status == "CREATE_COMPLETE" {
cluster_status = irs.ClusterActive
}
created_at := cluster_json_obj["created_at"].(string)
datetime, err := time.Parse(time.RFC3339, created_at)
//RFC3339 = "2006-01-02T15:04:05Z07:00"
//datetime, err := time.Parse("2006-01-02T15:04:05+07:00", created_at)
if err != nil {
err := fmt.Errorf("Failed to Parse Created Time : %v", err)
cblogger.Error(err)
panic(err)
}
version := ""
if cluster_json_obj["coe_version"] != nil {
version = cluster_json_obj["coe_version"].(string)
}
clusterInfo = &irs.ClusterInfo{
IId: irs.IID{
NameId: cluster_json_obj["name"].(string),
SystemId: cluster_json_obj["uuid"].(string),
},
Version: version,
Network: irs.NetworkInfo{
VpcIID: irs.IID{
NameId: "",
SystemId: cluster_json_obj["fixed_network"].(string),
},
SubnetIIDs: []irs.IID{
{
NameId: "",
SystemId: cluster_json_obj["fixed_subnet"].(string),
},
},
SecurityGroupIIDs: []irs.IID{
{
NameId: "",
SystemId: "",
},
},
},
Addons: irs.AddonsInfo{
KeyValueList: []irs.KeyValue{},
},
Status: cluster_status,
CreatedTime: datetime,
KeyValueList: []irs.KeyValue{},
}
// k,v 추출
// k,v 변환 규칙 작성 [k,v]:[ClusterInfo.k, ClusterInfo.v]
// 변환 규칙에 따라 k,v 변환
// flat, err := flatten.FlattenString(cluster_json_str, "", flatten.DotStyle)
flat, err := flatten.Flatten(cluster_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
node_groups_json_str, err := GetNodeGroups(host, token, cluster_id)
if err != nil {
err := fmt.Errorf("Failed to Get NodeGroups : %v", err)
cblogger.Error(err)
return nil, err
}
if node_groups_json_str == "" {
err := fmt.Errorf("Failed to Get Node Groups")
cblogger.Error(err)
return nil, err
}
var node_groups_json_obj map[string]interface{}
json.Unmarshal([]byte(node_groups_json_str), &node_groups_json_obj)
node_groups := node_groups_json_obj["nodegroups"].([]interface{})
for _, node_group := range node_groups {
node_group_id := node_group.(map[string]interface{})["uuid"].(string)
node_group_info, err := getNodeGroupInfo(host, token, cluster_id, node_group_id)
if err != nil {
err := fmt.Errorf("Failed to Get NodeGroupInfo : %v", err)
cblogger.Error(err)
return nil, err
}
clusterInfo.NodeGroupList = append(clusterInfo.NodeGroupList, *node_group_info)
}
return clusterInfo, err
}
func getNodeGroupInfo(host string, token string, cluster_id string, node_group_id string) (nodeGroupInfo *irs.NodeGroupInfo, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Failed to Process NodeGroupInfo : %v\n\n%v", r, string(debug.Stack()))
cblogger.Error(err)
}
}()
node_group_json_str, err := GetNodeGroup(host, token, cluster_id, node_group_id)
if err != nil {
err := fmt.Errorf("Failed to Get NodeGroup : %v", err)
cblogger.Error(err)
return nil, err
}
if node_group_json_str == "" {
err := fmt.Errorf("Failed to Get NodeGroup")
cblogger.Error(err)
return nil, err
}
var node_group_json_obj map[string]interface{}
json.Unmarshal([]byte(node_group_json_str), &node_group_json_obj)
health_status := node_group_json_obj["status"].(string)
status := irs.NodeGroupActive
if strings.EqualFold(health_status, "UPDATE_COMPLETE") {
status = irs.NodeGroupActive
} else if strings.EqualFold(health_status, "CREATE_IN_PROGRESS") {
status = irs.NodeGroupUpdating
} else if strings.EqualFold(health_status, "UPDATE_IN_PROGRESS") {
status = irs.NodeGroupUpdating // removing is a kind of updating?
} else if strings.EqualFold(health_status, "DELETE_IN_PROGRESS") {
status = irs.NodeGroupDeleting
} else if strings.EqualFold(health_status, "UPDATE_IN_PROGRESS") {
status = irs.NodeGroupUpdating
} else if strings.EqualFold(health_status, "CREATE_COMPLETE") {
status = irs.NodeGroupActive
}
auto_scaling, _ := strconv.ParseBool(node_group_json_obj["labels"].(map[string]interface{})["ca_enable"].(string))
ca_min_node_count, _ := strconv.ParseInt(node_group_json_obj["labels"].(map[string]interface{})["ca_min_node_count"].(string), 10, 32)
ca_max_node_count, _ := strconv.ParseInt(node_group_json_obj["labels"].(map[string]interface{})["ca_max_node_count"].(string), 10, 32)
node_count := int(node_group_json_obj["node_count"].(float64))
nodeGroupInfo = &irs.NodeGroupInfo{
IId: irs.IID{
NameId: node_group_json_obj["name"].(string),
SystemId: node_group_json_obj["uuid"].(string),
},
ImageIID: irs.IID{
NameId: "",
SystemId: node_group_json_obj["image_id"].(string),
},
VMSpecName: node_group_json_obj["flavor_id"].(string),
RootDiskType: node_group_json_obj["labels"].(map[string]interface{})["boot_volume_size"].(string),
RootDiskSize: node_group_json_obj["labels"].(map[string]interface{})["boot_volume_type"].(string),
KeyPairIID: irs.IID{},
OnAutoScaling: auto_scaling,
MinNodeSize: int(ca_min_node_count),
MaxNodeSize: int(ca_max_node_count),
DesiredNodeSize: int(node_count),
Nodes: []irs.IID{},
KeyValueList: []irs.KeyValue{},
Status: status,
}
// k,v 추출
// k,v 변환 규칙 작성 [k,v]:[ClusterInfo.k, ClusterInfo.v]
// 변환 규칙에 따라 k,v 변환
// flat, err := flatten.FlattenString(cluster_json_str, "", flatten.DotStyle)
flat, err := flatten.Flatten(node_group_json_obj, "", flatten.DotStyle)
if err != nil {
err := fmt.Errorf("Failed to Flatten NodeGroup")
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 getClusterInfoRequestJSONString(clusterInfo irs.ClusterInfo, clusterHandler *NhnCloudClusterHandler) (info string, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Failed to Process getClusterInfoRequestJSONString() : %v\n\n%v", r, string(debug.Stack()))
cblogger.Error(err)
}
}()
fixed_network := clusterInfo.Network.VpcIID.SystemId
fixed_subnet := clusterInfo.Network.SubnetIIDs[0].SystemId
flavor_id := clusterInfo.NodeGroupList[0].VMSpecName
keypair := clusterInfo.NodeGroupList[0].KeyPairIID.NameId
availability_zone := clusterHandler.RegionInfo.Zone
boot_volume_size := clusterInfo.NodeGroupList[0].RootDiskSize
boot_volume_type := clusterInfo.NodeGroupList[0].RootDiskType
ca_enable := "false"
if clusterInfo.NodeGroupList[0].OnAutoScaling {
ca_enable = "true"
}
ca_max_node_count := strconv.Itoa(clusterInfo.NodeGroupList[0].MaxNodeSize)
ca_min_node_count := strconv.Itoa(clusterInfo.NodeGroupList[0].MinNodeSize)
kube_tag := clusterInfo.Version
node_image := clusterInfo.NodeGroupList[0].ImageIID.SystemId
name := clusterInfo.IId.NameId
node_count := strconv.Itoa(clusterInfo.NodeGroupList[0].DesiredNodeSize)
for _, v := range clusterInfo.NodeGroupList[0].KeyValueList {
switch v.Key {
case "availability_zone":
availability_zone = v.Value
}
}
temp := `{
"cluster_template_id": "iaas_console",
"create_timeout": 60,
"fixed_network": "%s",
"fixed_subnet": "%s",
"flavor_id": "%s",
"keypair": "%s",
"labels": {
"availability_zone": "%s",
"boot_volume_size": "%s",
"boot_volume_type": "%s",
"ca_enable": "%s",
"ca_max_node_count": "%s",
"ca_min_node_count": "%s",
"cert_manager_api": "True",
"clusterautoscale": "nodegroupfeature",
"kube_tag": "%s",
"master_lb_floating_ip_enabled": "False",
"node_image": "%s",
"user_script_v2": ""
},
"name": "%s",
"node_count": %s
}`
info = fmt.Sprintf(temp, fixed_network, fixed_subnet, flavor_id, keypair, availability_zone, boot_volume_size, boot_volume_type, ca_enable, ca_max_node_count, ca_min_node_count, kube_tag, node_image, name, node_count)
return info, err
}
func getNodeGroupRequestJSONString(nodeGroupReqInfo irs.NodeGroupInfo, clusterHandler *NhnCloudClusterHandler) (payload string, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Failed to Process getNodeGroupRequestJSONString() : %v\n\n%v", r, string(debug.Stack()))
cblogger.Error(err)
}
}()
name := nodeGroupReqInfo.IId.NameId
node_count := nodeGroupReqInfo.DesiredNodeSize
flavor_id := nodeGroupReqInfo.VMSpecName
image_id := nodeGroupReqInfo.ImageIID.SystemId
boot_volume_size := nodeGroupReqInfo.RootDiskSize
boot_volume_type := nodeGroupReqInfo.RootDiskType
ca_enable := strconv.FormatBool(nodeGroupReqInfo.OnAutoScaling)
ca_max_node_count := strconv.Itoa(nodeGroupReqInfo.MaxNodeSize)
ca_min_node_count := strconv.Itoa(nodeGroupReqInfo.MinNodeSize)
availability_zone := clusterHandler.RegionInfo.Zone
temp := `{
"name": "%s",
"node_count": %d,
"flavor_id": "%s",
"image_id": "%s",
"labels": {
"availability_zone": "%s",
"boot_volume_size": "%s",
"boot_volume_type": "%s",
"ca_enable": "%s",
"ca_max_node_count": "%s",
"ca_min_node_count": "%s"
}
}`
payload = fmt.Sprintf(temp, name, node_count, flavor_id, image_id, availability_zone, boot_volume_size, boot_volume_type, ca_enable, ca_max_node_count, ca_min_node_count)
return payload, err
}