Skip to content

Commit 25289b3

Browse files
committed
refactor: in tencentcloud_kubernetes_node_pool
1 parent a78fdfa commit 25289b3

File tree

4 files changed

+156
-67
lines changed

4 files changed

+156
-67
lines changed

.changelog/1545.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
```release-note:enhancement
2-
resource/tencentcloud_kubernetes_node_pool: make enhanced_security_service in tencentcloud_kubernetes_node_pool not forceNew
2+
resource/tencentcloud_kubernetes_node_pool: adjust enhanced_security_service to not forceNew.
33
```

tencentcloud/internal/helper/transform.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ func StrListToStr(strList []*string) string {
235235
return base64.StdEncoding.EncodeToString([]byte(res))
236236
}
237237

238+
func StrListValToStr(strListVal []string) string {
239+
res := ""
240+
for i, v := range strListVal {
241+
res += v
242+
if i < len(strListVal)-1 {
243+
res += ";"
244+
}
245+
}
246+
return base64.StdEncoding.EncodeToString([]byte(res))
247+
}
248+
238249
func StrToStrList(str string) (res []string, err error) {
239250

240251
decodeString, err := base64.StdEncoding.DecodeString(str)

tencentcloud/resource_tc_kubernetes_node_pool.go

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,6 @@ import (
153153
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
154154
as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419"
155155
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
156-
cwp "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp/v20180228"
157-
tat "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tat/v20201028"
158156
tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525"
159157
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
160158
)
@@ -1388,73 +1386,77 @@ func resourceKubernetesNodePoolUpdate(d *schema.ResourceData, meta interface{})
13881386
return err
13891387
}
13901388

1391-
// step 2: change existed cvm security service if necessary
1392-
workersInsIdOfNodePool := make([]string, 0)
1393-
_, workers, err := service.DescribeClusterInstances(ctx, clusterId)
1394-
if err != nil {
1389+
// change existed cvm security service if necessary
1390+
if err := ModifySecurityServiceOfCvmInNodePool(ctx, d, &service, &cvmService, client, clusterId, *nodePool.NodePoolId); err != nil {
13951391
return err
13961392
}
1397-
for _, worker := range workers {
1398-
if worker.NodePoolId != "" && worker.NodePoolId == *nodePool.NodePoolId {
1399-
workersInsIdOfNodePool = append(workersInsIdOfNodePool, worker.InstanceId)
1400-
}
1401-
}
1402-
1403-
if d.HasChange("auto_scaling_config.0.enhanced_security_service") {
1404-
const BatchProcessedInsLimit = 100 // limit 100 items to change each request
1405-
launchConfigRaw := d.Get("auto_scaling_config").([]interface{})
1406-
dMap := launchConfigRaw[0].(map[string]interface{})
1407-
1408-
if v, ok := dMap["enhanced_security_service"]; ok && !v.(bool) {
1409-
// uninstall, cwp/DeleteMachine, need uuid
1410-
// https://cloud.tencent.com/document/product/296/19844
1411-
for i := 0; i < len(workersInsIdOfNodePool); i += BatchProcessedInsLimit {
1412-
var reqInstanceIds []string
1413-
if i+BatchProcessedInsLimit <= len(workersInsIdOfNodePool) {
1414-
reqInstanceIds = workersInsIdOfNodePool[i : i+BatchProcessedInsLimit]
1415-
} else {
1416-
reqInstanceIds = workersInsIdOfNodePool[i:]
1417-
}
1418-
// get uuid
1419-
instanceSet, err := cvmService.DescribeInstanceSetByIds(ctx, helper.StrListToStr(helper.StringsStringsPoint(reqInstanceIds)))
1420-
if err != nil {
1421-
return err
1422-
}
1423-
// call cwp/DeleteMachine
1424-
for _, ins := range instanceSet {
1425-
requestDeleteMachine := cwp.NewDeleteMachineRequest()
1426-
requestDeleteMachine.Uuid = ins.Uuid
1427-
if _, err := client.UseCwpClient().DeleteMachine(requestDeleteMachine); err != nil {
1428-
return err
1429-
}
1430-
}
1431-
}
1432-
} else {
1433-
// default is true, install security agent
1434-
// tat/InvokeCommand, CommandId=cmd-d8jj2skv, instanceId is enough
1435-
// https://cloud.tencent.com/document/product/1340/52678
1436-
for i := 0; i < len(workersInsIdOfNodePool); i += BatchProcessedInsLimit {
1437-
var reqInstanceIds []string
1438-
if i+BatchProcessedInsLimit <= len(workersInsIdOfNodePool) {
1439-
reqInstanceIds = workersInsIdOfNodePool[i : i+BatchProcessedInsLimit]
1440-
} else {
1441-
reqInstanceIds = workersInsIdOfNodePool[i:]
1442-
}
1443-
requestInvokeCommand := tat.NewInvokeCommandRequest()
1444-
requestInvokeCommand.InstanceIds = helper.StringsStringsPoint(reqInstanceIds)
1445-
requestInvokeCommand.CommandId = helper.String(InstallSecurityAgentCommandId)
1446-
requestInvokeCommand.Parameters = helper.String("{}")
1447-
requestInvokeCommand.Timeout = helper.Uint64(60)
1448-
_, err := client.UseTatClient().InvokeCommand(requestInvokeCommand)
1449-
if err != nil {
1450-
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
1451-
logId, request.GetAction(), request.ToJsonString(), err.Error())
1452-
return err
1453-
}
1454-
}
14551393

1456-
}
1457-
}
1394+
//workersInsIdOfNodePool := make([]string, 0)
1395+
//_, workers, err := service.DescribeClusterInstances(ctx, clusterId)
1396+
//if err != nil {
1397+
// return err
1398+
//}
1399+
//for _, worker := range workers {
1400+
// if worker.NodePoolId != "" && worker.NodePoolId == *nodePool.NodePoolId {
1401+
// workersInsIdOfNodePool = append(workersInsIdOfNodePool, worker.InstanceId)
1402+
// }
1403+
//}
1404+
1405+
//if d.HasChange("auto_scaling_config.0.enhanced_security_service") {
1406+
// const BatchProcessedInsLimit = 100 // limit 100 items to change each request
1407+
// launchConfigRaw := d.Get("auto_scaling_config").([]interface{})
1408+
// dMap := launchConfigRaw[0].(map[string]interface{})
1409+
//
1410+
// if v, ok := dMap["enhanced_security_service"]; ok && !v.(bool) {
1411+
// // uninstall, cwp/DeleteMachine, need uuid
1412+
// // https://cloud.tencent.com/document/product/296/19844
1413+
// for i := 0; i < len(workersInsIdOfNodePool); i += BatchProcessedInsLimit {
1414+
// var reqInstanceIds []string
1415+
// if i+BatchProcessedInsLimit <= len(workersInsIdOfNodePool) {
1416+
// reqInstanceIds = workersInsIdOfNodePool[i : i+BatchProcessedInsLimit]
1417+
// } else {
1418+
// reqInstanceIds = workersInsIdOfNodePool[i:]
1419+
// }
1420+
// // get uuid
1421+
// instanceSet, err := cvmService.DescribeInstanceSetByIds(ctx, helper.StrListToStr(helper.StringsStringsPoint(reqInstanceIds)))
1422+
// if err != nil {
1423+
// return err
1424+
// }
1425+
// // call cwp/DeleteMachine
1426+
// for _, ins := range instanceSet {
1427+
// requestDeleteMachine := cwp.NewDeleteMachineRequest()
1428+
// requestDeleteMachine.Uuid = ins.Uuid
1429+
// if _, err := client.UseCwpClient().DeleteMachine(requestDeleteMachine); err != nil {
1430+
// return err
1431+
// }
1432+
// }
1433+
// }
1434+
// } else {
1435+
// // default is true, install security agent
1436+
// // tat/InvokeCommand, CommandId=cmd-d8jj2skv, instanceId is enough
1437+
// // https://cloud.tencent.com/document/product/1340/52678
1438+
// for i := 0; i < len(workersInsIdOfNodePool); i += BatchProcessedInsLimit {
1439+
// var reqInstanceIds []string
1440+
// if i+BatchProcessedInsLimit <= len(workersInsIdOfNodePool) {
1441+
// reqInstanceIds = workersInsIdOfNodePool[i : i+BatchProcessedInsLimit]
1442+
// } else {
1443+
// reqInstanceIds = workersInsIdOfNodePool[i:]
1444+
// }
1445+
// requestInvokeCommand := tat.NewInvokeCommandRequest()
1446+
// requestInvokeCommand.InstanceIds = helper.StringsStringsPoint(reqInstanceIds)
1447+
// requestInvokeCommand.CommandId = helper.String(InstallSecurityAgentCommandId)
1448+
// requestInvokeCommand.Parameters = helper.String("{}")
1449+
// requestInvokeCommand.Timeout = helper.Uint64(60)
1450+
// _, err := client.UseTatClient().InvokeCommand(requestInvokeCommand)
1451+
// if err != nil {
1452+
// log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
1453+
// logId, request.GetAction(), request.ToJsonString(), err.Error())
1454+
// return err
1455+
// }
1456+
// }
1457+
//
1458+
// }
1459+
//}
14581460
d.SetPartial("auto_scaling_config")
14591461
}
14601462

tencentcloud/service_tencentcloud_tke.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"strings"
88

99
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
10+
cwp "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp/v20180228"
11+
tat "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tat/v20201028"
1012

1113
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1214

@@ -2389,6 +2391,7 @@ func (me *TkeService) ModifyClusterVirtualNodePool(ctx context.Context, request
23892391
return
23902392
}
23912393

2394+
<<<<<<< HEAD
23922395
func (me *TkeService) DescribeClusterVirtualNode(ctx context.Context, clusterId string) (virtualNodes []tke.VirtualNode, errRet error) {
23932396
logId := getLogId(ctx)
23942397

@@ -2422,3 +2425,76 @@ func (me *TkeService) DescribeClusterVirtualNode(ctx context.Context, clusterId
24222425
}
24232426
return
24242427
}
2428+
2429+
func ModifySecurityServiceOfCvmInNodePool(ctx context.Context, d *schema.ResourceData, tkeSvc *TkeService, cvmSvc *CvmService, client *connectivity.TencentCloudClient, clusterId string, nodePoolId string) error {
2430+
logId := getLogId(ctx)
2431+
2432+
if d.HasChange("auto_scaling_config.0.enhanced_security_service") {
2433+
workersInsIdOfNodePool := make([]string, 0)
2434+
_, workers, err := tkeSvc.DescribeClusterInstances(ctx, clusterId)
2435+
if err != nil {
2436+
return err
2437+
}
2438+
for _, worker := range workers {
2439+
if worker.NodePoolId != "" && worker.NodePoolId == nodePoolId {
2440+
workersInsIdOfNodePool = append(workersInsIdOfNodePool, worker.InstanceId)
2441+
}
2442+
}
2443+
2444+
const BatchProcessedInsLimit = 100 // limit 100 items to change each request
2445+
launchConfigRaw := d.Get("auto_scaling_config").([]interface{})
2446+
dMap := launchConfigRaw[0].(map[string]interface{})
2447+
2448+
if v, ok := dMap["enhanced_security_service"]; ok && !v.(bool) {
2449+
// uninstall, cwp/DeleteMachine, need uuid
2450+
// https://cloud.tencent.com/document/product/296/19844
2451+
for i := 0; i < len(workersInsIdOfNodePool); i += BatchProcessedInsLimit {
2452+
var reqInstanceIds []string
2453+
if i+BatchProcessedInsLimit <= len(workersInsIdOfNodePool) {
2454+
reqInstanceIds = workersInsIdOfNodePool[i : i+BatchProcessedInsLimit]
2455+
} else {
2456+
reqInstanceIds = workersInsIdOfNodePool[i:]
2457+
}
2458+
// get uuid
2459+
instanceSet, err := cvmSvc.DescribeInstanceSetByIds(ctx, helper.StrListValToStr(reqInstanceIds))
2460+
if err != nil {
2461+
return err
2462+
}
2463+
// call cwp/DeleteMachine
2464+
for _, ins := range instanceSet {
2465+
requestDeleteMachine := cwp.NewDeleteMachineRequest()
2466+
requestDeleteMachine.Uuid = ins.Uuid
2467+
if _, err := client.UseCwpClient().DeleteMachine(requestDeleteMachine); err != nil {
2468+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
2469+
logId, requestDeleteMachine.GetAction(), requestDeleteMachine.ToJsonString(), err.Error())
2470+
return err
2471+
}
2472+
}
2473+
}
2474+
} else {
2475+
// default is true, install security agent
2476+
// tat/InvokeCommand, CommandId=cmd-d8jj2skv, instanceId is enough
2477+
// https://cloud.tencent.com/document/product/1340/52678
2478+
for i := 0; i < len(workersInsIdOfNodePool); i += BatchProcessedInsLimit {
2479+
var reqInstanceIds []string
2480+
if i+BatchProcessedInsLimit <= len(workersInsIdOfNodePool) {
2481+
reqInstanceIds = workersInsIdOfNodePool[i : i+BatchProcessedInsLimit]
2482+
} else {
2483+
reqInstanceIds = workersInsIdOfNodePool[i:]
2484+
}
2485+
requestInvokeCommand := tat.NewInvokeCommandRequest()
2486+
requestInvokeCommand.InstanceIds = helper.StringsStringsPoint(reqInstanceIds)
2487+
requestInvokeCommand.CommandId = helper.String(InstallSecurityAgentCommandId)
2488+
requestInvokeCommand.Parameters = helper.String("{}")
2489+
requestInvokeCommand.Timeout = helper.Uint64(60)
2490+
_, err := client.UseTatClient().InvokeCommand(requestInvokeCommand)
2491+
if err != nil {
2492+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
2493+
logId, requestInvokeCommand.GetAction(), requestInvokeCommand.ToJsonString(), err.Error())
2494+
return err
2495+
}
2496+
}
2497+
}
2498+
}
2499+
return nil
2500+
}

0 commit comments

Comments
 (0)