Skip to content

Commit 12c7dfc

Browse files
authored
fix: tke-node-pool support launch config attributes modify (#744)
1 parent 019d0ad commit 12c7dfc

File tree

3 files changed

+112
-23
lines changed

3 files changed

+112
-23
lines changed

tencentcloud/resource_tc_kubernetes_node_pool.go

Lines changed: 89 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ package tencentcloud
9898
import (
9999
"context"
100100
"fmt"
101+
"log"
101102
"strings"
102103

103104
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
@@ -150,24 +151,20 @@ func composedKubernetesAsScalingConfigPara() map[string]*schema.Schema {
150151
"system_disk_type": {
151152
Type: schema.TypeString,
152153
Optional: true,
153-
ForceNew: true,
154154
Default: SYSTEM_DISK_TYPE_CLOUD_PREMIUM,
155155
ValidateFunc: validateAllowedStringValue(SYSTEM_DISK_ALLOW_TYPE),
156156
Description: "Type of a CVM disk. Valid value: `CLOUD_PREMIUM` and `CLOUD_SSD`. Default is `CLOUD_PREMIUM`.",
157157
},
158158
"system_disk_size": {
159159
Type: schema.TypeInt,
160160
Optional: true,
161-
ForceNew: true,
162161
Default: 50,
163162
ValidateFunc: validateIntegerInRange(50, 500),
164163
Description: "Volume of system disk in GB. Default is `50`.",
165164
},
166165
"data_disk": {
167166
Type: schema.TypeList,
168167
Optional: true,
169-
ForceNew: true,
170-
MaxItems: 11,
171168
Description: "Configurations of data disk.",
172169
Elem: &schema.Resource{
173170
Schema: map[string]*schema.Schema{
@@ -198,7 +195,6 @@ func composedKubernetesAsScalingConfigPara() map[string]*schema.Schema {
198195
"internet_charge_type": {
199196
Type: schema.TypeString,
200197
Optional: true,
201-
ForceNew: true,
202198
Default: INTERNET_CHARGE_TYPE_TRAFFIC_POSTPAID_BY_HOUR,
203199
ValidateFunc: validateAllowedStringValue(INTERNET_CHARGE_ALLOW_TYPE),
204200
Description: "Charge types for network traffic. Valid value: `BANDWIDTH_PREPAID`, `TRAFFIC_POSTPAID_BY_HOUR`, `TRAFFIC_POSTPAID_BY_HOUR` and `BANDWIDTH_PACKAGE`.",
@@ -217,7 +213,6 @@ func composedKubernetesAsScalingConfigPara() map[string]*schema.Schema {
217213
"public_ip_assigned": {
218214
Type: schema.TypeBool,
219215
Optional: true,
220-
ForceNew: true,
221216
Description: "Specify whether to assign an Internet IP address.",
222217
},
223218
"password": {
@@ -240,7 +235,6 @@ func composedKubernetesAsScalingConfigPara() map[string]*schema.Schema {
240235
"security_group_ids": {
241236
Type: schema.TypeList,
242237
Optional: true,
243-
ForceNew: true,
244238
Elem: &schema.Schema{Type: schema.TypeString},
245239
Description: "Security groups to which a CVM instance belongs.",
246240
},
@@ -350,7 +344,6 @@ func ResourceTencentCloudKubernetesNodePool() *schema.Resource {
350344
"auto_scaling_config": {
351345
Type: schema.TypeList,
352346
Required: true,
353-
ForceNew: true,
354347
MaxItems: 1,
355348
Elem: &schema.Resource{
356349
Schema: composedKubernetesAsScalingConfigPara(),
@@ -519,7 +512,7 @@ func composeParameterToAsScalingGroupParaSerial(d *schema.ResourceData) (string,
519512
return result, errRet
520513
}
521514

522-
//this function is similar to kubernetesAsScalingConfigParaSerial, but less parameter
515+
//This function is used to specify tke as group launch config, similar to kubernetesAsScalingConfigParaSerial, but less parameter
523516
func composedKubernetesAsScalingConfigParaSerial(dMap map[string]interface{}, meta interface{}) (string, error) {
524517
var (
525518
result string
@@ -635,6 +628,71 @@ func composedKubernetesAsScalingConfigParaSerial(dMap map[string]interface{}, me
635628
return result, errRet
636629
}
637630

631+
func composeAsLaunchConfigModifyRequest(d *schema.ResourceData, launchConfigId string) *as.ModifyLaunchConfigurationAttributesRequest {
632+
launchConfigRaw := d.Get("auto_scaling_config").([]interface{})
633+
dMap := launchConfigRaw[0].(map[string]interface{})
634+
request := as.NewModifyLaunchConfigurationAttributesRequest()
635+
request.LaunchConfigurationId = &launchConfigId
636+
637+
request.SystemDisk = &as.SystemDisk{}
638+
if v, ok := dMap["system_disk_type"]; ok {
639+
request.SystemDisk.DiskType = helper.String(v.(string))
640+
}
641+
642+
if v, ok := dMap["system_disk_size"]; ok {
643+
request.SystemDisk.DiskSize = helper.IntUint64(v.(int))
644+
}
645+
646+
if v, ok := dMap["data_disk"]; ok {
647+
dataDisks := v.([]interface{})
648+
request.DataDisks = make([]*as.DataDisk, 0, len(dataDisks))
649+
for _, d := range dataDisks {
650+
value := d.(map[string]interface{})
651+
diskType := value["disk_type"].(string)
652+
diskSize := uint64(value["disk_size"].(int))
653+
snapshotId := value["snapshot_id"].(string)
654+
dataDisk := as.DataDisk{
655+
DiskType: &diskType,
656+
DiskSize: &diskSize,
657+
}
658+
if snapshotId != "" {
659+
dataDisk.SnapshotId = &snapshotId
660+
}
661+
request.DataDisks = append(request.DataDisks, &dataDisk)
662+
}
663+
} else {
664+
request.DataDisks = []*as.DataDisk{}
665+
}
666+
667+
request.InternetAccessible = &as.InternetAccessible{}
668+
if v, ok := dMap["internet_charge_type"]; ok {
669+
request.InternetAccessible.InternetChargeType = helper.String(v.(string))
670+
}
671+
if v, ok := dMap["bandwidth_package_id"]; ok {
672+
if v.(string) != "" {
673+
request.InternetAccessible.BandwidthPackageId = helper.String(v.(string))
674+
}
675+
}
676+
if v, ok := dMap["internet_max_bandwidth_out"]; ok {
677+
request.InternetAccessible.InternetMaxBandwidthOut = helper.IntUint64(v.(int))
678+
}
679+
if v, ok := dMap["public_ip_assigned"]; ok {
680+
publicIpAssigned := v.(bool)
681+
request.InternetAccessible.PublicIpAssigned = &publicIpAssigned
682+
}
683+
684+
if v, ok := dMap["security_group_ids"]; ok {
685+
securityGroups := v.([]interface{})
686+
request.SecurityGroupIds = make([]*string, 0, len(securityGroups))
687+
for i := range securityGroups {
688+
securityGroup := securityGroups[i].(string)
689+
request.SecurityGroupIds = append(request.SecurityGroupIds, &securityGroup)
690+
}
691+
}
692+
693+
return request
694+
}
695+
638696
func resourceKubernetesNodePoolRead(d *schema.ResourceData, meta interface{}) error {
639697
defer logElapsed("resource.tencentcloud_kubernetes_node_pool.read")()
640698

@@ -955,8 +1013,9 @@ func resourceKubernetesNodePoolUpdate(d *schema.ResourceData, meta interface{})
9551013
var (
9561014
logId = getLogId(contextNil)
9571015
ctx = context.WithValue(context.TODO(), logIdKey, logId)
958-
service = TkeService{client: meta.(*TencentCloudClient).apiV3Conn}
959-
asService = AsService{client: meta.(*TencentCloudClient).apiV3Conn}
1016+
client = meta.(*TencentCloudClient).apiV3Conn
1017+
service = TkeService{client: client}
1018+
asService = AsService{client: client}
9601019
items = strings.Split(d.Id(), FILED_SP)
9611020
)
9621021
if len(items) != 2 {
@@ -967,6 +1026,24 @@ func resourceKubernetesNodePoolUpdate(d *schema.ResourceData, meta interface{})
9671026

9681027
d.Partial(true)
9691028

1029+
// LaunchConfig
1030+
if d.HasChange("auto_scaling_config") {
1031+
nodePool, _, err := service.DescribeNodePool(ctx, clusterId, nodePoolId)
1032+
if err != nil {
1033+
return err
1034+
}
1035+
launchConfigId := *nodePool.LaunchConfigurationId
1036+
request := composeAsLaunchConfigModifyRequest(d, launchConfigId)
1037+
_, err = client.UseAsClient().ModifyLaunchConfigurationAttributes(request)
1038+
if err != nil {
1039+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
1040+
logId, request.GetAction(), request.ToJsonString(), err.Error())
1041+
return err
1042+
}
1043+
d.SetPartial("auto_scaling_config")
1044+
}
1045+
1046+
// ModifyClusterNodePool
9701047
if d.HasChange("min_size") || d.HasChange("max_size") || d.HasChange("name") || d.HasChange("labels") || d.HasChange("taints") || d.HasChange("enable_auto_scale") || d.HasChange("node_os_type") || d.HasChange("node_os") {
9711048
maxSize := int64(d.Get("max_size").(int))
9721049
minSize := int64(d.Get("min_size").(int))
@@ -996,6 +1073,7 @@ func resourceKubernetesNodePoolUpdate(d *schema.ResourceData, meta interface{})
9961073
d.SetPartial("taints")
9971074
}
9981075

1076+
// ModifyScalingGroup
9991077
if d.HasChange("scaling_group_name") ||
10001078
d.HasChange("zones") ||
10011079
d.HasChange("scaling_group_project_id") ||

tencentcloud/resource_tc_kubernetes_node_pool_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func TestAccTencentCloudTkeNodePoolResource(t *testing.T) {
2626
resource.TestCheckResourceAttrSet(testTkeClusterNodePoolResourceKey, "cluster_id"),
2727
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "node_config.#", "1"),
2828
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "auto_scaling_config.#", "1"),
29+
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "auto_scaling_config.0.system_disk_size", "50"),
30+
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "auto_scaling_config.0.data_disk.#", "1"),
31+
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "auto_scaling_config.0.internet_max_bandwidth_out", "10"),
2932
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "taints.#", "1"),
3033
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "labels.test1", "test1"),
3134
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "labels.test2", "test2"),
@@ -47,6 +50,9 @@ func TestAccTencentCloudTkeNodePoolResource(t *testing.T) {
4750
resource.TestCheckResourceAttrSet(testTkeClusterNodePoolResourceKey, "cluster_id"),
4851
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "node_config.#", "1"),
4952
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "auto_scaling_config.#", "1"),
53+
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "auto_scaling_config.0.system_disk_size", "100"),
54+
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "auto_scaling_config.0.data_disk.#", "2"),
55+
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "auto_scaling_config.0.internet_max_bandwidth_out", "20"),
5056
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "max_size", "5"),
5157
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "min_size", "2"),
5258
resource.TestCheckResourceAttr(testTkeClusterNodePoolResourceKey, "labels.test3", "test3"),
@@ -148,6 +154,8 @@ data "tencentcloud_vpc_subnets" "vpc" {
148154
availability_zone = var.availability_zone
149155
}
150156
157+
data "tencentcloud_security_groups" "sg" {}
158+
151159
variable "default_instance_type" {
152160
default = "S1.SMALL1"
153161
}
@@ -205,7 +213,7 @@ resource "tencentcloud_kubernetes_node_pool" "np_test" {
205213
instance_type = var.default_instance_type
206214
system_disk_type = "CLOUD_PREMIUM"
207215
system_disk_size = "50"
208-
security_group_ids = ["sg-24vswocp"]
216+
security_group_ids = [data.tencentcloud_security_groups.group.security_groups[0].security_group_id]
209217
210218
data_disk {
211219
disk_type = "CLOUD_PREMIUM"
@@ -238,7 +246,6 @@ resource "tencentcloud_kubernetes_node_pool" "np_test" {
238246
]
239247
}
240248
}
241-
242249
`
243250

244251
const testAccTkeNodePoolClusterUpdate string = testAccTkeNodePoolClusterBasic + `
@@ -261,16 +268,20 @@ resource "tencentcloud_kubernetes_node_pool" "np_test" {
261268
auto_scaling_config {
262269
instance_type = var.default_instance_type
263270
system_disk_type = "CLOUD_PREMIUM"
264-
system_disk_size = "50"
265-
security_group_ids = ["sg-24vswocp"]
271+
system_disk_size = "100"
272+
security_group_ids = [data.tencentcloud_security_groups.group.security_groups[0].security_group_id]
266273
267274
data_disk {
268275
disk_type = "CLOUD_PREMIUM"
269276
disk_size = 50
270277
}
278+
data_disk {
279+
disk_type = "CLOUD_PREMIUM"
280+
disk_size = 100
281+
}
271282
272283
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
273-
internet_max_bandwidth_out = 10
284+
internet_max_bandwidth_out = 20
274285
public_ip_assigned = true
275286
password = "test123#"
276287
enhanced_security_service = false

website/docs/r/kubernetes_node_pool.html.markdown

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ resource "tencentcloud_kubernetes_node_pool" "mynodepool" {
106106

107107
The following arguments are supported:
108108

109-
* `auto_scaling_config` - (Required, ForceNew) Auto scaling config parameters.
109+
* `auto_scaling_config` - (Required) Auto scaling config parameters.
110110
* `cluster_id` - (Required, ForceNew) ID of the cluster.
111111
* `max_size` - (Required) Maximum number of node.
112112
* `min_size` - (Required) Minimum number of node.
@@ -135,17 +135,17 @@ The `auto_scaling_config` object supports the following:
135135
* `instance_type` - (Required, ForceNew) Specified types of CVM instance.
136136
* `backup_instance_types` - (Optional) Backup CVM instance types if specified instance type sold out or mismatch.
137137
* `bandwidth_package_id` - (Optional) bandwidth package id. if user is standard user, then the bandwidth_package_id is needed, or default has bandwidth_package_id.
138-
* `data_disk` - (Optional, ForceNew) Configurations of data disk.
138+
* `data_disk` - (Optional) Configurations of data disk.
139139
* `enhanced_monitor_service` - (Optional, ForceNew) To specify whether to enable cloud monitor service. Default is TRUE.
140140
* `enhanced_security_service` - (Optional, ForceNew) To specify whether to enable cloud security service. Default is TRUE.
141-
* `internet_charge_type` - (Optional, ForceNew) Charge types for network traffic. Valid value: `BANDWIDTH_PREPAID`, `TRAFFIC_POSTPAID_BY_HOUR`, `TRAFFIC_POSTPAID_BY_HOUR` and `BANDWIDTH_PACKAGE`.
141+
* `internet_charge_type` - (Optional) Charge types for network traffic. Valid value: `BANDWIDTH_PREPAID`, `TRAFFIC_POSTPAID_BY_HOUR`, `TRAFFIC_POSTPAID_BY_HOUR` and `BANDWIDTH_PACKAGE`.
142142
* `internet_max_bandwidth_out` - (Optional) Max bandwidth of Internet access in Mbps. Default is `0`.
143143
* `key_ids` - (Optional, ForceNew) ID list of keys.
144144
* `password` - (Optional, ForceNew) Password to access.
145-
* `public_ip_assigned` - (Optional, ForceNew) Specify whether to assign an Internet IP address.
146-
* `security_group_ids` - (Optional, ForceNew) Security groups to which a CVM instance belongs.
147-
* `system_disk_size` - (Optional, ForceNew) Volume of system disk in GB. Default is `50`.
148-
* `system_disk_type` - (Optional, ForceNew) Type of a CVM disk. Valid value: `CLOUD_PREMIUM` and `CLOUD_SSD`. Default is `CLOUD_PREMIUM`.
145+
* `public_ip_assigned` - (Optional) Specify whether to assign an Internet IP address.
146+
* `security_group_ids` - (Optional) Security groups to which a CVM instance belongs.
147+
* `system_disk_size` - (Optional) Volume of system disk in GB. Default is `50`.
148+
* `system_disk_type` - (Optional) Type of a CVM disk. Valid value: `CLOUD_PREMIUM` and `CLOUD_SSD`. Default is `CLOUD_PREMIUM`.
149149

150150
The `data_disk` object supports the following:
151151

0 commit comments

Comments
 (0)