Skip to content

Commit 139f63b

Browse files
authored
feat: tke_cluster - support cluster level settings (#942)
1 parent 866cee1 commit 139f63b

File tree

12 files changed

+815
-32
lines changed

12 files changed

+815
-32
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ require (
3030
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.283
3131
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.199
3232
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.291
33-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.376
33+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.378
3434
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.351
3535
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.359
3636
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dayu v1.0.335
@@ -54,7 +54,7 @@ require (
5454
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcaplusdb v1.0.199
5555
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr v1.0.267
5656
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq v1.0.268
57-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.370
57+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.378
5858
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199
5959
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.357
6060
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.370 h1:FlmN
511511
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.370/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
512512
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.376 h1:pXsCDParuAiDUt/SuDBeJ0Fv90kItYMbk37W1cMVHck=
513513
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.376/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
514+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.378 h1:o+GKr3tHDivmoWxBXX5oeJC8EmmNV64g/yw5s0RjBdg=
515+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.378/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
514516
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.199 h1:ajgJogYSIQ5u1PIbiV5nsvr5K0fYpm1/T7Dy+mxEM6U=
515517
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.199/go.mod h1:AqyM/ZZMD7q5mHBqNY9YImbSpEpoEe7E/vrTbUWX+po=
516518
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.351 h1:zqJsH5pxGT57La7NAOOyMQxsuM11pupNBwV1dzXcT24=
@@ -578,6 +580,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.369 h1:bp9vCew
578580
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.369/go.mod h1:HlPCDRZ3zUnEXifuTNbCiEOwKn/2LmA6ziPkCr3VHsk=
579581
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.370 h1:rzExiYuyKD9JhRAyQzoECLz8RRyT0bNgLxmDjPQxTRg=
580582
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.370/go.mod h1:qs+mOMyRpnBtCNHBl70LMH826P1kbYDIjgX2+p5nqMg=
583+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.378 h1:gVtRJRENcLw2LxA06oS0OOOeRADStVrrpIyKjlrmkW4=
584+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.378/go.mod h1:3fugxjsjhR7P3HeVRsUCMIM2P7KnIvqnPhcsImS7zN0=
581585
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199 h1:6Yt74l4pA5QtzhwMNIEUt0spXdSBKH744DDqTHJOCP0=
582586
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199/go.mod h1:Yw6OQ33z3s4k0HVYTNSffB12qOzEJ2Zf1Vj4+5S3sRs=
583587
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.199 h1:UDZ59pvaqjDy2QIsMsv9hxm0BEJLmPIbHF1ms0MqaRk=

tencentcloud/basic_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tencentcloud
33
import (
44
"os"
55
"regexp"
6+
"time"
67
)
78

89
/*
@@ -22,6 +23,15 @@ const (
2223

2324
var persistResource = regexp.MustCompile("^(keep|Default)")
2425

26+
// Check if resource should persist instead of recycled
27+
func isResourcePersist(name string, createdTime *time.Time) bool {
28+
createdWithin30Minutes := false
29+
if createdTime != nil {
30+
createdWithin30Minutes = createdTime.Add(time.Minute * 30).After(time.Now())
31+
}
32+
return persistResource.MatchString(name) || createdWithin30Minutes
33+
}
34+
2535
// vpn
2636
const defaultVpnDataSource = `
2737
data "tencentcloud_vpn_gateways" "foo" {
@@ -75,7 +85,7 @@ const (
7585
tkeExclusiveVpcName = "keep_tke_exclusive_vpc"
7686
tkeExclusiveSubnetId = "subnet-ljyn7h30"
7787
defaultTkeClusterId = "cls-ely08ic4"
78-
defaultTkeClusterName = "preset_tke_cluster"
88+
defaultTkeClusterName = "keep-tke-cluster"
7989
)
8090

8191
/*

tencentcloud/resource_tc_eni_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func testSweepEniInstance(region string) error {
3434
client: client.apiV3Conn,
3535
}
3636

37-
instances, err := vpcService.DescribeEniByFilters(ctx, nil, nil, nil, nil, nil, nil,nil,nil)
37+
instances, err := vpcService.DescribeEniByFilters(ctx, nil, nil, nil, nil, nil, nil, nil, nil)
3838
if err != nil {
3939
return fmt.Errorf("get instance list error: %s", err.Error())
4040
}

tencentcloud/resource_tc_kubernetes_cluster.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,17 @@ func resourceTencentCloudTkeCluster() *schema.Resource {
893893
ForceNew: true,
894894
Optional: true,
895895
Default: false,
896-
Description: "Indicates whether to enable cluster node auto scaler. Default is false.",
896+
Description: "Indicates whether to enable cluster node auto scaling. Default is false.",
897+
},
898+
"cluster_level": {
899+
Type: schema.TypeString,
900+
Optional: true,
901+
Description: "Specify cluster level, valid for managed cluster.",
902+
},
903+
"auto_upgrade_cluster_level": {
904+
Type: schema.TypeBool,
905+
Optional: true,
906+
Description: "Whether the cluster level auto upgraded, valid for managed cluster.",
897907
},
898908
"node_pool_global_config": {
899909
Type: schema.TypeList,
@@ -1781,6 +1791,14 @@ func resourceTencentCloudTkeClusterCreate(d *schema.ResourceData, meta interface
17811791
basic.ClusterDescription = v.(string)
17821792
}
17831793

1794+
if v, ok := d.GetOk("cluster_level"); ok {
1795+
basic.ClusterLevel = v.(string)
1796+
}
1797+
1798+
if v, ok := d.GetOk("auto_upgrade_cluster_level"); ok {
1799+
basic.AutoUpgradeClusterLevel = v.(bool)
1800+
}
1801+
17841802
advanced.Ipvs = d.Get("cluster_ipvs").(bool)
17851803
advanced.AsEnabled = d.Get("cluster_as_enabled").(bool)
17861804
advanced.ContainerRuntime = d.Get("container_runtime").(string)
@@ -2623,16 +2641,15 @@ func resourceTencentCloudTkeClusterUpdate(d *schema.ResourceData, meta interface
26232641
d.SetPartial("managed_cluster_internet_security_policies")
26242642
}
26252643

2626-
if d.HasChange("project_id") || d.HasChange("cluster_name") || d.HasChange("cluster_desc") {
2644+
if d.HasChange("project_id") || d.HasChange("cluster_name") || d.HasChange("cluster_desc") || d.HasChange("cluster_level") || d.HasChange("auto_upgrade_cluster_level") {
26272645
projectId := int64(d.Get("project_id").(int))
26282646
clusterName := d.Get("cluster_name").(string)
26292647
clusterDesc := d.Get("cluster_desc").(string)
2630-
if err := tkeService.ModifyClusterAttribute(ctx, id, projectId, clusterName, clusterDesc); err != nil {
2648+
clusterLevel := d.Get("cluster_level").(string)
2649+
autoUpgradeClusterLevel := d.Get("auto_upgrade_cluster_level").(bool)
2650+
if err := tkeService.ModifyClusterAttribute(ctx, id, projectId, clusterName, clusterDesc, clusterLevel, autoUpgradeClusterLevel); err != nil {
26312651
return err
26322652
}
2633-
d.SetPartial("project_id")
2634-
d.SetPartial("cluster_name")
2635-
d.SetPartial("cluster_desc")
26362653
}
26372654

26382655
//upgrade k8s cluster version

tencentcloud/resource_tc_kubernetes_cluster_test.go

Lines changed: 174 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"testing"
8+
"time"
89

910
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1011
"github.com/hashicorp/terraform-plugin-sdk/terraform"
@@ -13,6 +14,38 @@ import (
1314
var testTkeClusterName = "tencentcloud_kubernetes_cluster"
1415
var testTkeClusterResourceKey = testTkeClusterName + ".managed_cluster"
1516

17+
func init() {
18+
// go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_kubernetes_cluster
19+
resource.AddTestSweepers("tencentcloud_kubernetes_cluster", &resource.Sweeper{
20+
Name: "tencentcloud_kubernetes_cluster",
21+
F: func(r string) error {
22+
logId := getLogId(contextNil)
23+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
24+
cli, _ := sharedClientForRegion(r)
25+
client := cli.(*TencentCloudClient).apiV3Conn
26+
service := TkeService{client: client}
27+
clusters, err := service.DescribeClusters(ctx, "", "")
28+
if err != nil {
29+
return err
30+
}
31+
32+
for _, v := range clusters {
33+
id := v.ClusterId
34+
name := v.ClusterName
35+
createdTime, _ := time.Parse(time.RFC3339, v.CreatedTime)
36+
if isResourcePersist(name, &createdTime) {
37+
continue
38+
}
39+
if err := service.DeleteCluster(ctx, id); err != nil {
40+
return err
41+
}
42+
}
43+
44+
return nil
45+
},
46+
})
47+
}
48+
1649
func TestAccTencentCloudTkeResource(t *testing.T) {
1750
t.Parallel()
1851
resource.Test(t, resource.TestCase{
@@ -53,6 +86,38 @@ func TestAccTencentCloudTkeResource(t *testing.T) {
5386
})
5487
}
5588

89+
func TestAccTencentCloudTkeResourceClusterLevel(t *testing.T) {
90+
t.Parallel()
91+
resource.Test(t, resource.TestCase{
92+
PreCheck: func() { testAccPreCheck(t) },
93+
Providers: testAccProviders,
94+
CheckDestroy: testAccCheckTkeDestroy,
95+
Steps: []resource.TestStep{
96+
{
97+
Config: testAccTkeClusterLevel,
98+
Check: resource.ComposeTestCheckFunc(
99+
testAccCheckTkeExists(testTkeClusterResourceKey),
100+
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "cluster_cidr", "10.31.0.0/16"),
101+
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "cluster_max_pod_num", "32"),
102+
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "cluster_name", "test"),
103+
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "cluster_level", "L5"),
104+
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "auto_upgrade_cluster_level", "true"),
105+
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "worker_instances_list.#", "1"),
106+
resource.TestCheckResourceAttrSet(testTkeClusterResourceKey, "worker_instances_list.0.instance_id"),
107+
),
108+
},
109+
{
110+
Config: testAccTkeClusterLevelUpdate,
111+
Check: resource.ComposeTestCheckFunc(
112+
testAccCheckTkeExists(testTkeClusterResourceKey),
113+
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "cluster_level", "L20"),
114+
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "auto_upgrade_cluster_level", "false"),
115+
),
116+
},
117+
},
118+
})
119+
}
120+
56121
func testAccCheckTkeDestroy(s *terraform.State) error {
57122
logId := getLogId(contextNil)
58123
ctx := context.WithValue(context.TODO(), logIdKey, logId)
@@ -154,7 +219,7 @@ data "tencentcloud_vpc_subnets" "vpc" {
154219
}
155220
156221
resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
157-
vpc_id = "vpc-rkojp4kn"
222+
vpc_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.vpc_id
158223
cluster_cidr = var.cluster_cidr
159224
cluster_max_pod_num = 32
160225
cluster_name = "test"
@@ -173,8 +238,8 @@ resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
173238
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
174239
internet_max_bandwidth_out = 100
175240
public_ip_assigned = true
176-
subnet_id = "subnet-fmcdf57e"
177-
img_id = "img-rkiynh11"
241+
subnet_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.subnet_id
242+
img_id = "`+defaultTkeOSImageId+`"
178243
179244
data_disk {
180245
disk_type = "CLOUD_PREMIUM"
@@ -189,7 +254,6 @@ resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
189254
enhanced_monitor_service = false
190255
user_data = "dGVzdA=="
191256
password = "ZZXXccvv1212"
192-
cam_role_name = "TKE_TEST"
193257
}
194258
195259
cluster_deploy_type = "MANAGED_CLUSTER"
@@ -211,3 +275,109 @@ resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
211275
`, key, value,
212276
)
213277
}
278+
279+
const testAccTkeClusterLevel = `
280+
variable "availability_zone" {
281+
default = "ap-guangzhou-3"
282+
}
283+
284+
variable "cluster_cidr" {
285+
default = "10.31.0.0/16"
286+
}
287+
288+
variable "default_instance_type" {
289+
default = "S1.SMALL1"
290+
}
291+
292+
data "tencentcloud_vpc_subnets" "vpc" {
293+
is_default = true
294+
availability_zone = var.availability_zone
295+
}
296+
297+
resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
298+
vpc_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.vpc_id
299+
cluster_cidr = var.cluster_cidr
300+
cluster_max_pod_num = 32
301+
cluster_name = "test"
302+
cluster_desc = "test cluster desc"
303+
cluster_max_service_num = 32
304+
cluster_version = "1.18.4"
305+
cluster_os = "tlinux2.2(tkernel3)x86_64"
306+
cluster_level = "L5"
307+
auto_upgrade_cluster_level = true
308+
worker_config {
309+
count = 1
310+
availability_zone = var.availability_zone
311+
instance_type = var.default_instance_type
312+
system_disk_type = "CLOUD_SSD"
313+
system_disk_size = 60
314+
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
315+
internet_max_bandwidth_out = 100
316+
public_ip_assigned = true
317+
subnet_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.subnet_id
318+
img_id = "` + defaultTkeOSImageId + `"
319+
320+
enhanced_security_service = false
321+
enhanced_monitor_service = false
322+
user_data = "dGVzdA=="
323+
password = "ZZXXccvv1212"
324+
}
325+
326+
cluster_deploy_type = "MANAGED_CLUSTER"
327+
328+
unschedulable = 0
329+
}
330+
`
331+
332+
const testAccTkeClusterLevelUpdate = `
333+
variable "availability_zone" {
334+
default = "ap-guangzhou-3"
335+
}
336+
337+
variable "cluster_cidr" {
338+
default = "10.31.0.0/16"
339+
}
340+
341+
variable "default_instance_type" {
342+
default = "S1.SMALL1"
343+
}
344+
345+
data "tencentcloud_vpc_subnets" "vpc" {
346+
is_default = true
347+
availability_zone = var.availability_zone
348+
}
349+
350+
resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
351+
vpc_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.vpc_id
352+
cluster_cidr = var.cluster_cidr
353+
cluster_max_pod_num = 32
354+
cluster_name = "test"
355+
cluster_desc = "test cluster desc"
356+
cluster_max_service_num = 32
357+
cluster_version = "1.18.4"
358+
cluster_os = "tlinux2.2(tkernel3)x86_64"
359+
cluster_level = "L20"
360+
auto_upgrade_cluster_level = false
361+
worker_config {
362+
count = 1
363+
availability_zone = var.availability_zone
364+
instance_type = var.default_instance_type
365+
system_disk_type = "CLOUD_SSD"
366+
system_disk_size = 60
367+
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
368+
internet_max_bandwidth_out = 100
369+
public_ip_assigned = true
370+
subnet_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.subnet_id
371+
img_id = "` + defaultTkeOSImageId + `"
372+
373+
enhanced_security_service = false
374+
enhanced_monitor_service = false
375+
user_data = "dGVzdA=="
376+
password = "ZZXXccvv1212"
377+
}
378+
379+
cluster_deploy_type = "MANAGED_CLUSTER"
380+
381+
unschedulable = 0
382+
}
383+
`

0 commit comments

Comments
 (0)