-
Notifications
You must be signed in to change notification settings - Fork 48
/
ClusterHandler.go
1944 lines (1742 loc) · 70.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
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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package resources
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/platform-services-go-sdk/globaltaggingv1"
"github.com/IBM/platform-services-go-sdk/resourcemanagerv2"
"github.com/IBM/vpc-go-sdk/vpcv1"
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/ibmcloud-vpc/utils/kubernetesserviceapiv1"
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/go-openapi/strfmt"
"github.com/hashicorp/go-version"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
"sync"
"time"
)
const (
// Resource Names
DefaultResourceGroup = "Default"
AutoscalerAddon = "cluster-autoscaler"
ConfigMapNamespace = "kube-system"
AutoscalerConfigMap = "iks-ca-configmap"
AutoscalerConfigMapOptionProperty = "workerPoolsConfig.json"
// Error Codes
RetrieveUnableErr = "Not visible in IBMCloud-VPC"
GetKubeConfigErr = "Get Kube Config Error"
GetAutoScalerConfigErr = "Get Autoscaler Config Map Error"
// Retry Counts
EnableAutoScalerRetry = 120 // minutes
DisableAutoScalerRetry = 120 // minutes
InitSecurityGroupRetry = 120 // minutes
RestoreDefaultSGRetry = 120 // minutes
UpgradeMasterRetry = 120 // minutes
// Status tags
AutoScalerStatus = "CB-SPIDER-PMKS-AUTOSCALER-STATUS:"
SecurityGroupStatus = "CB-SPIDER-PMKS-SECURITYGROUP-STATUS:"
MasterUpgradeStatus = "CB-SPIDER-PMKS-MASTERUPGRADE-STATUS:"
// State Codes
WAITING = "WAITING"
DEPLOYING = "DEPLOYING"
UPGRADE_DEPLOYING = "UPGRADE-DEPLOYING"
ACTIVE = "ACTIVE"
UNINSTALLING = "UNINSTALLING"
FAILED = "FAILED"
INITIALIZING = "INITIALIZING"
INITIALIZED = "INITIALIZED"
UPGRADING = "UPGRADING"
)
var autoSaclerStates []string
var securityGroupStates []string
var masterUpgradeStates []string
func init() {
autoSaclerStates = []string{WAITING, DEPLOYING, UPGRADE_DEPLOYING, ACTIVE, UNINSTALLING, FAILED}
for i, state := range autoSaclerStates {
autoSaclerStates[i] = fmt.Sprintf("%s%s", AutoScalerStatus, state)
}
securityGroupStates = []string{INITIALIZING, INITIALIZED, FAILED}
for i, state := range securityGroupStates {
securityGroupStates[i] = fmt.Sprintf("%s%s", SecurityGroupStatus, state)
}
masterUpgradeStates = []string{WAITING, UPGRADING}
for i, state := range masterUpgradeStates {
masterUpgradeStates[i] = fmt.Sprintf("%s%s", MasterUpgradeStatus, state)
}
}
type IbmClusterHandler struct {
CredentialInfo idrv.CredentialInfo
Region idrv.RegionInfo
Ctx context.Context
VpcService *vpcv1.VpcV1
ClusterService *kubernetesserviceapiv1.KubernetesServiceApiV1
TaggingService *globaltaggingv1.GlobalTaggingV1
}
var defaultResourceGroupId string
func (ic *IbmClusterHandler) CreateCluster(clusterReqInfo irs.ClusterInfo) (irs.ClusterInfo, error) {
hiscallInfo := GetCallLogScheme(ic.Region, call.CLUSTER, clusterReqInfo.IId.NameId, "CreateCluster()")
start := call.Start()
// validation
validationErr := ic.validateAtCreateCluster(clusterReqInfo)
if validationErr != nil {
cblogger.Error(validationErr)
LoggingError(hiscallInfo, validationErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Create Cluster. err = %s", validationErr))
}
// get resource group id
resourceGroupId, getResourceGroupErr := ic.getDefaultResourceGroupId()
if getResourceGroupErr != nil {
cblogger.Error(getResourceGroupErr)
LoggingError(hiscallInfo, getResourceGroupErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Create Cluster. err = %s", getResourceGroupErr))
}
// get vpc info
vpcHandler := IbmVPCHandler{
CredentialInfo: ic.CredentialInfo,
Region: ic.Region,
VpcService: ic.VpcService,
Ctx: ic.Ctx,
}
vpcInfo, getVpcInfoErr := vpcHandler.GetVPC(clusterReqInfo.Network.VpcIID)
if getVpcInfoErr != nil {
cblogger.Error(getVpcInfoErr)
LoggingError(hiscallInfo, getVpcInfoErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Create Cluster. err = %s", getVpcInfoErr))
}
// get subnet info
subnetInfo, getSubnetInfoErr := ic.validateAndGetSubnetInfo(clusterReqInfo.Network)
if getSubnetInfoErr != nil {
cblogger.Error(getSubnetInfoErr)
LoggingError(hiscallInfo, getSubnetInfoErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Create Cluster. err = %s", getSubnetInfoErr))
}
// check exists
_, _, getClusterErr := ic.ClusterService.VpcGetClusterWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetClusterOptions{
Cluster: core.StringPtr(clusterReqInfo.IId.NameId),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
ShowResources: core.StringPtr("false"),
})
if getClusterErr != nil && getClusterErr.Error() == "Not Found" {
// get first worker pool for cluster creation
workerPool := ic.getWorkerPoolFromNodeGroupInfo(clusterReqInfo.NodeGroupList[0], vpcInfo.IId.SystemId, subnetInfo.IId.SystemId)
// create cluster if not exists
_, _, createClusterErr := ic.ClusterService.VpcCreateClusterWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcCreateClusterOptions{
DisablePublicServiceEndpoint: core.BoolPtr(false),
KubeVersion: core.StringPtr(clusterReqInfo.Version),
Name: core.StringPtr(clusterReqInfo.IId.NameId),
Provider: core.StringPtr("vpc-gen2"),
WorkerPool: &workerPool,
XAuthResourceGroup: core.StringPtr(resourceGroupId),
})
if createClusterErr != nil {
cblogger.Error(createClusterErr)
LoggingError(hiscallInfo, createClusterErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Create Cluster. err = %s", createClusterErr))
}
rawVpcInfo, _, getRawVpcErr := ic.VpcService.GetVPCWithContext(ic.Ctx, &vpcv1.GetVPCOptions{
ID: core.StringPtr(vpcInfo.IId.SystemId),
})
if getRawVpcErr != nil {
cblogger.Error(getRawVpcErr)
LoggingError(hiscallInfo, getRawVpcErr)
ic.DeleteCluster(clusterReqInfo.IId)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Create Cluster. err = %s", getRawVpcErr))
}
sgHander := IbmSecurityHandler{
CredentialInfo: ic.CredentialInfo,
Region: ic.Region,
Ctx: ic.Ctx,
VpcService: ic.VpcService,
}
// restore VPC default security group
// VPC default security group lost rules for unknown reasons while creating a cluster with API
// It makes communication failure between worker and master nodes in the cluster and worker status reaches failure
go func() {
cnt := 0
for cnt < RestoreDefaultSGRetry {
isSkip := false
brokenVpcDefaultSg, getBrokenVpcDefaultSgErr := sgHander.GetSecurity(irs.IID{SystemId: *rawVpcInfo.DefaultSecurityGroup.ID})
if getBrokenVpcDefaultSgErr != nil {
isSkip = true
} else {
rawClusters, _, getClustersErr := ic.ClusterService.VpcGetClusterWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetClusterOptions{
Cluster: core.StringPtr(clusterReqInfo.IId.NameId),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
ShowResources: core.StringPtr("true"),
})
if getClustersErr != nil {
isSkip = true
}
rawCluster := (*rawClusters)[0]
if rawCluster.State != "deploying" {
isSkip = true
}
}
if isSkip {
cnt++
time.Sleep(time.Minute)
} else {
mandatoriyRuleList := []irs.SecurityRuleInfo{{
Direction: "inbound",
IPProtocol: "tcp",
FromPort: "22",
ToPort: "22",
}, {
Direction: "inbound",
IPProtocol: "icmp",
FromPort: "-1",
ToPort: "-1",
}, {
Direction: "outbound",
IPProtocol: "all",
FromPort: "-1",
ToPort: "-1",
CIDR: "0.0.0.0/0",
}}
_, addRuleErr := sgHander.AddRules(brokenVpcDefaultSg.IId, &mandatoriyRuleList)
if addRuleErr != nil {
cblogger.Error(addRuleErr)
LoggingError(hiscallInfo, addRuleErr)
ic.DeleteCluster(clusterReqInfo.IId)
}
break
}
}
}()
} else if getClusterErr != nil {
cblogger.Error(getClusterErr)
LoggingError(hiscallInfo, getClusterErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Create Cluster. err = %s", getClusterErr))
}
// get created cluster info
rawClusters, _, getClustersErr := ic.ClusterService.VpcGetClusterWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetClusterOptions{
Cluster: core.StringPtr(clusterReqInfo.IId.NameId),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
ShowResources: core.StringPtr("true"),
})
if getClustersErr != nil {
cblogger.Error(getClustersErr)
LoggingError(hiscallInfo, getClustersErr)
ic.DeleteCluster(clusterReqInfo.IId)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Get Cluster. err = %s", getClustersErr))
}
rawCluster := (*rawClusters)[0]
// Enable cluster-autoscaler addon and apply autoscaler option
autoScalerErr := ic.installAutoScalerAddon(clusterReqInfo, rawCluster.Id, rawCluster.Crn, resourceGroupId, false)
if autoScalerErr != nil {
cblogger.Error(autoScalerErr)
LoggingError(hiscallInfo, autoScalerErr)
ic.DeleteCluster(clusterReqInfo.IId)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Get Cluster. err = %s", autoScalerErr))
}
// Add remaining worker pools
ic.createRemainingWorkerPools(clusterReqInfo, vpcInfo, subnetInfo, rawCluster, resourceGroupId)
// Set Security Group
ic.initSecurityGroup(clusterReqInfo, rawCluster.Id, rawCluster.Crn)
clusterInfo, getClusterErr := ic.GetCluster(irs.IID{SystemId: rawCluster.Id})
if getClusterErr != nil {
cblogger.Error(getClusterErr)
LoggingError(hiscallInfo, getClusterErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Create Cluster. err = %s", getClusterErr))
}
LoggingInfo(hiscallInfo, start)
return clusterInfo, nil
}
func (ic *IbmClusterHandler) ListCluster() ([]*irs.ClusterInfo, error) {
hiscallInfo := GetCallLogScheme(ic.Region, call.CLUSTER, "", "ListCluster()")
start := call.Start()
resourceGroupId, getResourceGroupIdErr := ic.getDefaultResourceGroupId()
if getResourceGroupIdErr != nil {
cblogger.Error(getResourceGroupIdErr)
LoggingError(hiscallInfo, getResourceGroupIdErr)
return []*irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to List Cluster. err = %s", getResourceGroupIdErr))
}
clusterList, _, getClusterListErr := ic.ClusterService.VpcGetClustersWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetClustersOptions{
XAuthResourceGroup: core.StringPtr(resourceGroupId),
Provider: core.StringPtr("vpc-gen2"),
})
if getClusterListErr != nil {
cblogger.Error(getClusterListErr)
LoggingError(hiscallInfo, getClusterListErr)
return []*irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to List Cluster. err = %s", getClusterListErr))
}
var wait sync.WaitGroup
wait.Add(len(clusterList))
var ret []*irs.ClusterInfo
for _, cluster := range clusterList {
go func() {
defer wait.Done()
irsCluster, getIrsClusterErr := ic.GetCluster(irs.IID{SystemId: *cluster.ID})
if getIrsClusterErr == nil {
ret = append(ret, &irsCluster)
}
}()
}
wait.Wait()
LoggingInfo(hiscallInfo, start)
return ret, nil
}
func (ic *IbmClusterHandler) GetCluster(clusterIID irs.IID) (irs.ClusterInfo, error) {
hiscallInfo := GetCallLogScheme(ic.Region, call.CLUSTER, clusterIID.NameId, "GetCluster()")
start := call.Start()
if clusterIID.NameId == "" && clusterIID.SystemId == "" {
return irs.ClusterInfo{}, errors.New("Failed to Get Cluster. err = invalid IID")
}
resourceGroupId, getResourceGroupIdErr := ic.getDefaultResourceGroupId()
if getResourceGroupIdErr != nil {
cblogger.Error(getResourceGroupIdErr)
LoggingError(hiscallInfo, getResourceGroupIdErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Get Cluster. err = %s", getResourceGroupIdErr))
}
var cluster string
if clusterIID.SystemId != "" {
cluster = clusterIID.SystemId
} else {
cluster = clusterIID.NameId
}
rawClusters, _, getClustersErr := ic.ClusterService.VpcGetClusterWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetClusterOptions{
Cluster: core.StringPtr(cluster),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
ShowResources: core.StringPtr("true"),
})
if getClustersErr != nil {
cblogger.Error(getClustersErr)
LoggingError(hiscallInfo, getClustersErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Get Cluster. err = %s", getClustersErr))
}
for _, rawCluster := range *rawClusters {
if rawCluster.Id == clusterIID.SystemId || rawCluster.Name == clusterIID.NameId {
ret, getClusterInfoErr := ic.setClusterInfo(rawCluster)
if getClusterInfoErr != nil {
cblogger.Error(getClusterInfoErr)
LoggingError(hiscallInfo, getClusterInfoErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Get Cluster. err = %s", getClusterInfoErr))
}
LoggingInfo(hiscallInfo, start)
return ret, nil
}
}
LoggingInfo(hiscallInfo, start)
return irs.ClusterInfo{}, nil
}
func (ic *IbmClusterHandler) DeleteCluster(clusterIID irs.IID) (bool, error) {
hiscallInfo := GetCallLogScheme(ic.Region, call.CLUSTER, clusterIID.NameId, "DeleteCluster()")
start := call.Start()
if clusterIID.NameId == "" && clusterIID.SystemId == "" {
return false, errors.New("Failed to Delete Cluster. err = invalid IID")
}
// get resource group id
resourceGroupId, getResourceGroupErr := ic.getDefaultResourceGroupId()
if getResourceGroupErr != nil {
cblogger.Error(getResourceGroupErr)
LoggingError(hiscallInfo, getResourceGroupErr)
return false, errors.New(fmt.Sprintf("Failed to Delete Cluster. err = %s", getResourceGroupErr))
}
// check exists
fullClusterIID, getClusterIIDErr := ic.getClusterIID(clusterIID)
if getClusterIIDErr != nil {
cblogger.Error(getClusterIIDErr)
LoggingError(hiscallInfo, getClusterIIDErr)
return false, errors.New(fmt.Sprintf("Failed to Delete Cluster. err = %s", getClusterIIDErr))
}
rawCluster, _, getClusterErr := ic.ClusterService.VpcGetClusterWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetClusterOptions{
Cluster: core.StringPtr(fullClusterIID.SystemId),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
ShowResources: core.StringPtr("false"),
})
if getClusterErr != nil {
cblogger.Error(getClusterErr)
LoggingError(hiscallInfo, getClusterErr)
return false, errors.New(fmt.Sprintf("Failed to Delete Cluster. err = %s", getClusterErr))
}
// delete cluster
_, deleteClusterErr := ic.ClusterService.RemoveClusterWithContext(ic.Ctx, &kubernetesserviceapiv1.RemoveClusterOptions{
IdOrName: core.StringPtr((*rawCluster)[0].Id),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
DeleteResources: core.StringPtr("true"),
})
if deleteClusterErr != nil {
cblogger.Error(deleteClusterErr)
LoggingError(hiscallInfo, deleteClusterErr)
return false, errors.New(fmt.Sprintf("Failed to Delete Cluster. err = %s", deleteClusterErr))
}
LoggingInfo(hiscallInfo, start)
return true, nil
}
func (ic *IbmClusterHandler) AddNodeGroup(clusterIID irs.IID, nodeGroupReqInfo irs.NodeGroupInfo) (irs.NodeGroupInfo, error) {
hiscallInfo := GetCallLogScheme(ic.Region, call.CLUSTER, clusterIID.NameId, "AddNodeGroup()")
start := call.Start()
// validation
validateErr := ic.validateAtAddNodeGroup(clusterIID, nodeGroupReqInfo)
if validateErr != nil {
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Add Node Group. err = %s", validateErr))
}
// get resource group id
resourceGroupId, getResourceGroupErr := ic.getDefaultResourceGroupId()
if getResourceGroupErr != nil {
cblogger.Error(getResourceGroupErr)
LoggingError(hiscallInfo, getResourceGroupErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Add Node Group. err = %s", getResourceGroupErr))
}
irsCluster, getIrsClusterErr := ic.GetCluster(clusterIID)
if getIrsClusterErr != nil {
cblogger.Error(getIrsClusterErr)
LoggingError(hiscallInfo, getIrsClusterErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Add Node Group. err = %s", getIrsClusterErr))
}
if irsCluster.Status == irs.ClusterCreating || irsCluster.Status == irs.ClusterDeleting {
clusterStatusErr := errors.New(fmt.Sprintf("Cannot Add Node Group at %s status", irsCluster.Status))
cblogger.Error(clusterStatusErr)
LoggingError(hiscallInfo, clusterStatusErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Add Node Group. err = %s", clusterStatusErr))
}
// Get Network.Subnet Info
rawVpeList, _, getRawVpeListErr := ic.VpcService.ListEndpointGateways(&vpcv1.ListEndpointGatewaysOptions{
ResourceGroupID: core.StringPtr(resourceGroupId),
})
if getRawVpeListErr != nil {
cblogger.Error(getRawVpeListErr)
LoggingError(hiscallInfo, getRawVpeListErr)
return irs.NodeGroupInfo{}, getRawVpeListErr
}
var target vpcv1.EndpointGateway
for _, rawVpe := range rawVpeList.EndpointGateways {
if *rawVpe.Name == fmt.Sprintf("iks-%s", irsCluster.IId.SystemId) {
target = rawVpe
}
}
var subnetIID irs.IID
for _, ip := range target.Ips {
if strings.Contains(*ip.Name, irsCluster.IId.SystemId) {
subnetId := strings.Split(*ip.Href, "/")[5]
rawSubnet, _, getRawSubnetErr := ic.VpcService.GetSubnet(&vpcv1.GetSubnetOptions{
ID: core.StringPtr(subnetId),
})
if getRawSubnetErr != nil {
cblogger.Error(getRawSubnetErr)
LoggingError(hiscallInfo, getRawSubnetErr)
return irs.NodeGroupInfo{}, getRawSubnetErr
}
subnetIID.NameId = *rawSubnet.Name
subnetIID.SystemId = *rawSubnet.ID
}
}
addNodeGroupResponse, _, addNodeGroupErr := ic.ClusterService.VpcCreateWorkerPoolWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcCreateWorkerPoolOptions{
Cluster: core.StringPtr(irsCluster.IId.SystemId),
Flavor: core.StringPtr(nodeGroupReqInfo.VMSpecName),
Isolation: core.StringPtr("public"),
Name: core.StringPtr(nodeGroupReqInfo.IId.NameId),
VpcID: core.StringPtr(irsCluster.Network.VpcIID.SystemId),
WorkerCount: core.Int64Ptr(int64(nodeGroupReqInfo.DesiredNodeSize)),
Zones: []kubernetesserviceapiv1.Zone{{
ID: core.StringPtr(ic.Region.Zone),
SubnetID: core.StringPtr(subnetIID.SystemId),
}},
Authorization: core.StringPtr(ic.CredentialInfo.AuthToken),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
})
if addNodeGroupErr != nil {
cblogger.Error(addNodeGroupErr)
LoggingError(hiscallInfo, addNodeGroupErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Add Node Group. err = %s", addNodeGroupErr))
}
newNodeGroup, _, getNewNodeGroupErr := ic.ClusterService.VpcGetWorkerPoolWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetWorkerPoolOptions{
Cluster: core.StringPtr(irsCluster.IId.SystemId),
Workerpool: addNodeGroupResponse.WorkerPoolID,
XRegion: core.StringPtr(ic.Region.Region),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
})
if getNewNodeGroupErr != nil {
cblogger.Error(getNewNodeGroupErr)
LoggingError(hiscallInfo, getNewNodeGroupErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Add Node Group. err = %s", getNewNodeGroupErr))
}
// Apply Node Group autosacler options
irsCluster.NodeGroupList = append(irsCluster.NodeGroupList, nodeGroupReqInfo)
applyAutoScalerOptionErr := ic.applyAutoScalerOptions(irsCluster, irsCluster.IId.SystemId, resourceGroupId)
if applyAutoScalerOptionErr != nil {
cblogger.Error(applyAutoScalerOptionErr)
LoggingError(hiscallInfo, applyAutoScalerOptionErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Add Node Group. err = %s", applyAutoScalerOptionErr))
}
// Get Workers in pool
getWorkersResult, _, getWorkersErr := ic.ClusterService.VpcGetWorkersWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetWorkersOptions{
Cluster: core.StringPtr(irsCluster.IId.SystemId),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
ShowDeleted: core.StringPtr("false"),
Pool: newNodeGroup.ID,
})
if getWorkersErr != nil {
cblogger.Error(getWorkersErr)
LoggingError(hiscallInfo, getWorkersErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Add Node Group. err = %s", getWorkersErr))
}
var nodesIID []irs.IID
for _, worker := range getWorkersResult {
nodesIID = append(nodesIID, irs.IID{
NameId: RetrieveUnableErr,
SystemId: *worker.ID,
})
}
LoggingInfo(hiscallInfo, start)
return irs.NodeGroupInfo{
IId: irs.IID{
NameId: *newNodeGroup.PoolName,
SystemId: *newNodeGroup.ID,
},
ImageIID: irs.IID{
NameId: RetrieveUnableErr,
SystemId: RetrieveUnableErr,
},
VMSpecName: *newNodeGroup.Flavor,
RootDiskType: RetrieveUnableErr,
RootDiskSize: RetrieveUnableErr,
KeyPairIID: irs.IID{
NameId: RetrieveUnableErr,
SystemId: RetrieveUnableErr,
},
OnAutoScaling: false,
DesiredNodeSize: -1,
MinNodeSize: -1,
MaxNodeSize: -1,
Status: ic.getNodeGroupStatusFromString(*newNodeGroup.Lifecycle.DesiredState),
Nodes: nodesIID,
KeyValueList: nil,
}, nil
}
func (ic *IbmClusterHandler) SetNodeGroupAutoScaling(clusterIID irs.IID, nodeGroupIID irs.IID, on bool) (bool, error) {
hiscallInfo := GetCallLogScheme(ic.Region, call.CLUSTER, clusterIID.NameId, "SetNodeGroupAutoScaling()")
start := call.Start()
// validation
if clusterIID.SystemId == "" && clusterIID.NameId == "" {
return false, errors.New("Failed to Set Node Group Auto Scaling. err = Invalid Cluster IID")
}
if nodeGroupIID.SystemId == "" && nodeGroupIID.NameId == "" {
return false, errors.New("Failed to Set Node Group Auto Scaling. err = Invalid Node Group IID")
}
// get resource group id
resourceGroupId, getResourceGroupErr := ic.getDefaultResourceGroupId()
if getResourceGroupErr != nil {
cblogger.Error(getResourceGroupErr)
LoggingError(hiscallInfo, getResourceGroupErr)
return false, errors.New(fmt.Sprintf("Failed to Set Node Group Auto Scaling. err = %s", getResourceGroupErr))
}
irsCluster, getIrsClusterErr := ic.GetCluster(clusterIID)
if getIrsClusterErr != nil {
cblogger.Error(getIrsClusterErr)
LoggingError(hiscallInfo, getIrsClusterErr)
return false, errors.New(fmt.Sprintf("Failed to Set Node Group Auto Scaling. err = %s", getIrsClusterErr))
}
if irsCluster.Status == irs.ClusterCreating || irsCluster.Status == irs.ClusterDeleting || irsCluster.Status == irs.ClusterUpdating {
clusterStatusErr := errors.New(fmt.Sprintf("Cannot Set Node Group AutoScaling at %s status", irsCluster.Status))
cblogger.Error(clusterStatusErr)
LoggingError(hiscallInfo, clusterStatusErr)
return false, errors.New(fmt.Sprintf("Failed to Set Node Group AutoScaling. err = %s", clusterStatusErr))
}
nodeGroups, _, getNodeGroupsErr := ic.ClusterService.VpcGetWorkerPoolsWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetWorkerPoolsOptions{
Cluster: core.StringPtr(irsCluster.IId.SystemId),
XRegion: core.StringPtr(ic.Region.Region),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
})
if getNodeGroupsErr != nil {
cblogger.Error(getNodeGroupsErr)
LoggingError(hiscallInfo, getNodeGroupsErr)
return false, errors.New(fmt.Sprintf("Failed to Set Node Group Auto Scaling. err = %s", getNodeGroupsErr))
}
var targetNodeGroup *kubernetesserviceapiv1.GetWorkerPoolsDetailResponse
for _, nodeGroup := range *nodeGroups {
if nodeGroup.Id == nodeGroupIID.SystemId || nodeGroup.PoolName == nodeGroupIID.NameId {
targetNodeGroup = &nodeGroup
break
}
}
if targetNodeGroup == nil {
nodeGroupNotExistErr := errors.New(fmt.Sprintf("Failed to Set Node Group Auto Scaling. err = cannot find node group: %s", nodeGroupIID))
cblogger.Error(nodeGroupNotExistErr)
LoggingError(hiscallInfo, nodeGroupNotExistErr)
return false, nodeGroupNotExistErr
}
nodeGroupName := targetNodeGroup.PoolName
kubeConfigStr, getKubeConfigErr := ic.getKubeConfig(irsCluster.IId.SystemId, resourceGroupId)
if getKubeConfigErr != nil {
cblogger.Error(getKubeConfigErr)
LoggingError(hiscallInfo, getKubeConfigErr)
return false, errors.New(fmt.Sprintf("Failed to Set Node Group Auto Scaling. err = %s", getKubeConfigErr))
}
var newNodeGroupInfo []irs.NodeGroupInfo
configMap, getConfigMapErr := ic.getAutoScalerConfigMap(kubeConfigStr)
if getConfigMapErr != nil {
cblogger.Error(getConfigMapErr)
LoggingError(hiscallInfo, getConfigMapErr)
return false, errors.New(fmt.Sprintf("Failed to Set Node Group Auto Scaling. err = %s", getConfigMapErr))
}
if configMap == nil {
configMapNotExistErr := errors.New("Failed to Set Node Group Auto Scaling. err = Cannot find Auto Scaler Config Map, Please try after autoscaler addon is deployed")
cblogger.Error(configMapNotExistErr)
LoggingError(hiscallInfo, configMapNotExistErr)
return false, configMapNotExistErr
} else {
jsonProperty, exists := configMap.Data[AutoscalerConfigMapOptionProperty]
if !exists {
propertyNotExistErr := errors.New("Failed to Set Node Group Auto Scaling. err = Cannot find Auto Scaler Config Map, Please try after autoscaler addon is deployed")
cblogger.Error(propertyNotExistErr)
LoggingError(hiscallInfo, propertyNotExistErr)
return false, propertyNotExistErr
}
var workerPoolAutoscalerConfigs []kubernetesserviceapiv1.WorkerPoolAutoscalerConfig
unmarshalErr := json.Unmarshal([]byte(jsonProperty), &workerPoolAutoscalerConfigs)
if unmarshalErr != nil {
cblogger.Error(unmarshalErr)
LoggingError(hiscallInfo, unmarshalErr)
return false, errors.New(fmt.Sprintf("Failed to Set Node Group Auto Scaling. err = %s", unmarshalErr))
}
isIncluded := false
for i, config := range workerPoolAutoscalerConfigs {
if config.Name == nodeGroupName {
isIncluded = true
workerPoolAutoscalerConfigs[i].Enabled = on
}
newNodeGroupInfo = append(newNodeGroupInfo, irs.NodeGroupInfo{
IId: irs.IID{NameId: workerPoolAutoscalerConfigs[i].Name},
OnAutoScaling: workerPoolAutoscalerConfigs[i].Enabled,
MinNodeSize: workerPoolAutoscalerConfigs[i].MinSize,
MaxNodeSize: workerPoolAutoscalerConfigs[i].MaxSize,
})
}
if !isIncluded {
autoScalingSettingNotExistsErr := errors.New("Failed to Set Node Group Auto Scaling. err = Cannot find Node Group Auto Scaling Setting in Auto Scaler Config Map, Please try change Node Group scaling")
cblogger.Error(autoScalingSettingNotExistsErr)
LoggingError(hiscallInfo, autoScalingSettingNotExistsErr)
return false, autoScalingSettingNotExistsErr
}
}
updateConfigMapErr := ic.updateAutoScalerConfigMap(kubeConfigStr, newNodeGroupInfo)
if updateConfigMapErr != nil {
cblogger.Error(updateConfigMapErr)
LoggingError(hiscallInfo, updateConfigMapErr)
return false, errors.New(fmt.Sprintf("Failed to Set Node Group Auto Scaling. err = %s", updateConfigMapErr))
}
LoggingInfo(hiscallInfo, start)
return true, nil
}
func (ic *IbmClusterHandler) ChangeNodeGroupScaling(clusterIID irs.IID, nodeGroupIID irs.IID, DesiredNodeSize int, MinNodeSize int, MaxNodeSize int) (irs.NodeGroupInfo, error) {
hiscallInfo := GetCallLogScheme(ic.Region, call.CLUSTER, clusterIID.NameId, "ChangeNodeGroupScaling()")
start := call.Start()
// validation
validateErr := ic.validateAtChangeNodeGroupScaling(clusterIID, nodeGroupIID, MinNodeSize, MaxNodeSize)
if validateErr != nil {
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Change Node Group Scaling. err = %s", validateErr))
}
// get resource group id
resourceGroupId, getResourceGroupErr := ic.getDefaultResourceGroupId()
if getResourceGroupErr != nil {
cblogger.Error(getResourceGroupErr)
LoggingError(hiscallInfo, getResourceGroupErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Change Node Group Scaling. err = %s", getResourceGroupErr))
}
irsCluster, getIrsClusterErr := ic.GetCluster(clusterIID)
if getIrsClusterErr != nil {
cblogger.Error(getIrsClusterErr)
LoggingError(hiscallInfo, getIrsClusterErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Change Node Group Scaling. err = %s", getIrsClusterErr))
}
if irsCluster.Status == irs.ClusterCreating || irsCluster.Status == irs.ClusterDeleting || irsCluster.Status == irs.ClusterUpdating {
clusterStatusErr := errors.New(fmt.Sprintf("Cannot Change Node Group Scaling at %s status", irsCluster.Status))
cblogger.Error(clusterStatusErr)
LoggingError(hiscallInfo, clusterStatusErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Change Node Group Scaling. err = %s", clusterStatusErr))
}
nodeGroups, _, getNodeGroupsErr := ic.ClusterService.VpcGetWorkerPoolsWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetWorkerPoolsOptions{
Cluster: core.StringPtr(irsCluster.IId.SystemId),
XRegion: core.StringPtr(ic.Region.Region),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
})
if getNodeGroupsErr != nil {
cblogger.Error(getNodeGroupsErr)
LoggingError(hiscallInfo, getNodeGroupsErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Change Node Group Scaling. err = %s", getNodeGroupsErr))
}
var targetNodeGroup *kubernetesserviceapiv1.GetWorkerPoolsDetailResponse
for _, nodeGroup := range *nodeGroups {
if nodeGroup.Id == nodeGroupIID.SystemId || nodeGroup.PoolName == nodeGroupIID.NameId {
targetNodeGroup = &nodeGroup
break
}
}
if targetNodeGroup == nil {
nodeGroupNotExistErr := errors.New(fmt.Sprintf("Failed to Change Node Group Scaling. err = cannot find node group: %s", nodeGroupIID))
cblogger.Error(nodeGroupNotExistErr)
LoggingError(hiscallInfo, nodeGroupNotExistErr)
return irs.NodeGroupInfo{}, nodeGroupNotExistErr
}
nodeGroupName := targetNodeGroup.PoolName
kubeConfigStr, getKubeConfigErr := ic.getKubeConfig(irsCluster.IId.SystemId, resourceGroupId)
if getKubeConfigErr != nil {
cblogger.Error(getKubeConfigErr)
LoggingError(hiscallInfo, getKubeConfigErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Change Node Group Scaling. err = %s", getKubeConfigErr))
}
var newNodeGroupInfo []irs.NodeGroupInfo
var changedNodeGroupIndex int
configMap, getConfigMapErr := ic.getAutoScalerConfigMap(kubeConfigStr)
if getConfigMapErr != nil {
cblogger.Error(getConfigMapErr)
LoggingError(hiscallInfo, getConfigMapErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Change Node Group Scaling. err = %s", getConfigMapErr))
}
if configMap == nil {
configMapNotExistErr := errors.New("Failed to Change Node Group Scaling. err = Cannot find Auto Scaler Config Map, Please try after autoscaler addon is deployed")
cblogger.Error(configMapNotExistErr)
LoggingError(hiscallInfo, configMapNotExistErr)
return irs.NodeGroupInfo{}, configMapNotExistErr
} else {
jsonProperty, exists := configMap.Data[AutoscalerConfigMapOptionProperty]
if !exists {
propertyNotExistErr := errors.New("Failed to Change Node Group Scaling. err = Cannot find Auto Scaler Config Map, Please try after autoscaler addon is deployed")
cblogger.Error(propertyNotExistErr)
LoggingError(hiscallInfo, propertyNotExistErr)
return irs.NodeGroupInfo{}, propertyNotExistErr
}
var workerPoolAutoscalerConfigs []kubernetesserviceapiv1.WorkerPoolAutoscalerConfig
unmarshalErr := json.Unmarshal([]byte(jsonProperty), &workerPoolAutoscalerConfigs)
if unmarshalErr != nil {
cblogger.Error(unmarshalErr)
LoggingError(hiscallInfo, unmarshalErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Change Node Group Scaling. err = %s", unmarshalErr))
}
isIncluded := false
for i, config := range workerPoolAutoscalerConfigs {
if config.Name == nodeGroupName {
isIncluded = true
changedNodeGroupIndex = i
workerPoolAutoscalerConfigs[i].MinSize = MinNodeSize
workerPoolAutoscalerConfigs[i].MaxSize = MaxNodeSize
}
newNodeGroupInfo = append(newNodeGroupInfo, irs.NodeGroupInfo{
IId: irs.IID{NameId: workerPoolAutoscalerConfigs[i].Name},
OnAutoScaling: workerPoolAutoscalerConfigs[i].Enabled,
MinNodeSize: workerPoolAutoscalerConfigs[i].MinSize,
MaxNodeSize: workerPoolAutoscalerConfigs[i].MaxSize,
})
}
if !isIncluded {
newNodeGroupInfo = append(newNodeGroupInfo, irs.NodeGroupInfo{
IId: irs.IID{NameId: nodeGroupName},
OnAutoScaling: false,
MinNodeSize: MinNodeSize,
MaxNodeSize: MaxNodeSize,
})
changedNodeGroupIndex = len(newNodeGroupInfo) - 1
}
}
updateConfigMapErr := ic.updateAutoScalerConfigMap(kubeConfigStr, newNodeGroupInfo)
if updateConfigMapErr != nil {
cblogger.Error(updateConfigMapErr)
LoggingError(hiscallInfo, updateConfigMapErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Change Node Group Scaling. err = %s", updateConfigMapErr))
}
// Get Workers in pool
getWorkersResult, _, getWorkersErr := ic.ClusterService.VpcGetWorkersWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetWorkersOptions{
Cluster: core.StringPtr(irsCluster.IId.SystemId),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
ShowDeleted: core.StringPtr("false"),
Pool: core.StringPtr(targetNodeGroup.Id),
})
if getWorkersErr != nil {
cblogger.Error(getWorkersErr)
LoggingError(hiscallInfo, getWorkersErr)
return irs.NodeGroupInfo{}, errors.New(fmt.Sprintf("Failed to Add Node Group. err = %s", getWorkersErr))
}
var nodesIID []irs.IID
for _, worker := range getWorkersResult {
nodesIID = append(nodesIID, irs.IID{
NameId: RetrieveUnableErr,
SystemId: *worker.ID,
})
}
LoggingInfo(hiscallInfo, start)
return irs.NodeGroupInfo{
IId: irs.IID{
NameId: targetNodeGroup.PoolName,
SystemId: targetNodeGroup.Id,
},
ImageIID: irs.IID{
NameId: RetrieveUnableErr,
SystemId: RetrieveUnableErr,
},
VMSpecName: targetNodeGroup.Flavor,
RootDiskType: RetrieveUnableErr,
RootDiskSize: RetrieveUnableErr,
KeyPairIID: irs.IID{
NameId: RetrieveUnableErr,
SystemId: RetrieveUnableErr,
},
OnAutoScaling: newNodeGroupInfo[changedNodeGroupIndex].OnAutoScaling,
DesiredNodeSize: -1,
MinNodeSize: newNodeGroupInfo[changedNodeGroupIndex].MinNodeSize,
MaxNodeSize: newNodeGroupInfo[changedNodeGroupIndex].MaxNodeSize,
Status: ic.getNodeGroupStatusFromString(targetNodeGroup.Lifecycle.DesiredState),
Nodes: nodesIID,
KeyValueList: nil,
}, nil
}
func (ic *IbmClusterHandler) RemoveNodeGroup(clusterIID irs.IID, nodeGroupIID irs.IID) (bool, error) {
hiscallInfo := GetCallLogScheme(ic.Region, call.CLUSTER, clusterIID.NameId, "RemoveNodeGroup()")
start := call.Start()
// validation
if clusterIID.SystemId == "" && clusterIID.NameId == "" {
return false, errors.New("Failed to Set Node Group Auto Scaling. err = Invalid Cluster IID")
}
if nodeGroupIID.SystemId == "" && nodeGroupIID.NameId == "" {
return false, errors.New("Failed to Set Node Group Auto Scaling. err = Invalid Node Group IID")
}
// get resource group id
resourceGroupId, getResourceGroupErr := ic.getDefaultResourceGroupId()
if getResourceGroupErr != nil {
cblogger.Error(getResourceGroupErr)
LoggingError(hiscallInfo, getResourceGroupErr)
return false, errors.New(fmt.Sprintf("Failed to Remove Node Group. err = %s", getResourceGroupErr))
}
irsCluster, getIrsClusterErr := ic.GetCluster(clusterIID)
if getIrsClusterErr != nil {
cblogger.Error(getIrsClusterErr)
LoggingError(hiscallInfo, getIrsClusterErr)
return false, errors.New(fmt.Sprintf("Failed to Remove Node Group. err = %s", getIrsClusterErr))
}
if irsCluster.Status == irs.ClusterCreating || irsCluster.Status == irs.ClusterDeleting {
clusterStatusErr := errors.New(fmt.Sprintf("Cannot Remove Node Group at %s status", irsCluster.Status))
cblogger.Error(clusterStatusErr)
LoggingError(hiscallInfo, clusterStatusErr)
return false, errors.New(fmt.Sprintf("Failed to Remove Node Group. err = %s", clusterStatusErr))
}
nodeGroups, _, getNodeGroupsErr := ic.ClusterService.VpcGetWorkerPoolsWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetWorkerPoolsOptions{
Cluster: core.StringPtr(irsCluster.IId.SystemId),
XRegion: core.StringPtr(ic.Region.Region),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
})
if getNodeGroupsErr != nil {
cblogger.Error(getNodeGroupsErr)
LoggingError(hiscallInfo, getNodeGroupsErr)
return false, errors.New(fmt.Sprintf("Failed to Remove Node Group. err = %s", getNodeGroupsErr))
}
var targetNodeGroup *kubernetesserviceapiv1.GetWorkerPoolsDetailResponse
for _, nodeGroup := range *nodeGroups {
if nodeGroup.Id == nodeGroupIID.SystemId || nodeGroup.PoolName == nodeGroupIID.NameId {
targetNodeGroup = &nodeGroup
break
}
}
_, removeErr := ic.ClusterService.RemoveWorkerPoolWithContext(ic.Ctx, &kubernetesserviceapiv1.RemoveWorkerPoolOptions{
IdOrName: core.StringPtr(irsCluster.IId.SystemId),
PoolidOrName: core.StringPtr(targetNodeGroup.Id),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
})
if removeErr != nil {
cblogger.Error(removeErr)
LoggingError(hiscallInfo, removeErr)
return false, errors.New(fmt.Sprintf("Failed to Remove Node Group. err = %s", removeErr))
}
LoggingInfo(hiscallInfo, start)
return true, nil
}
func (ic *IbmClusterHandler) UpgradeCluster(clusterIID irs.IID, newVersion string) (irs.ClusterInfo, error) {
hiscallInfo := GetCallLogScheme(ic.Region, call.CLUSTER, clusterIID.NameId, "UpgradeCluster()")
start := call.Start()
// validation
if clusterIID.SystemId == "" && clusterIID.NameId == "" {
return irs.ClusterInfo{}, errors.New("Failed to Set Node Group Auto Scaling. err = Invalid Cluster IID")
}
if newVersion == "" {
return irs.ClusterInfo{}, errors.New("Failed to Set Node Group Auto Scaling. err = New Version is required")
}
// get resource group id
resourceGroupId, getResourceGroupErr := ic.getDefaultResourceGroupId()
if getResourceGroupErr != nil {
cblogger.Error(getResourceGroupErr)
LoggingError(hiscallInfo, getResourceGroupErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Upgrade Cluster. err = %s", getResourceGroupErr))
}
fullClusterIID, getClusterIIDErr := ic.getClusterIID(clusterIID)
if getClusterIIDErr != nil {
cblogger.Error(getClusterIIDErr)
LoggingError(hiscallInfo, getClusterIIDErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Upgrade Cluster. err = %s", getClusterIIDErr))
}
prevIrsClsuter, getIrsClusterErr := ic.GetCluster(fullClusterIID)
if getIrsClusterErr != nil {
cblogger.Error(getIrsClusterErr)
LoggingError(hiscallInfo, getIrsClusterErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Upgrade Cluster. err = %s", getIrsClusterErr))
}
if prevIrsClsuter.Status != irs.ClusterActive {
clusterStatusErr := errors.New(fmt.Sprintf("Failed to Upgrade Cluster. err = Cannot upgrade cluster in %s status", prevIrsClsuter.Status))
cblogger.Error(clusterStatusErr)
LoggingError(hiscallInfo, clusterStatusErr)
return irs.ClusterInfo{}, clusterStatusErr
}
rawCluster, _, getClusterErr := ic.ClusterService.VpcGetClusterWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetClusterOptions{
Cluster: core.StringPtr(fullClusterIID.SystemId),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
ShowResources: core.StringPtr("true"),
})
if getClusterErr != nil {
cblogger.Error(getClusterErr)
LoggingError(hiscallInfo, getClusterErr)
return irs.ClusterInfo{}, errors.New(fmt.Sprintf("Failed to Upgrade Cluster. err = %s", getClusterErr))