Skip to content

Commit 42a4889

Browse files
authored
fix: tke support cluster level data source and describe (#947)
* fix: tke support cluster level data source and describe * fix: tke - attachment testcase * fix: tke - cluster level fix * fix: tke - ignore empty cluster_level and auto.. field * fix: tke testcase update add ignore cidr either * fix: ignore tag plan diff * fix: cluster level cidr and deprecate old container cluster * fix: tke - testcase remove err code
1 parent df8e230 commit 42a4889

18 files changed

+382
-134
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.378
57+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.381
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.370 h1:rzExiYu
597597
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.370/go.mod h1:qs+mOMyRpnBtCNHBl70LMH826P1kbYDIjgX2+p5nqMg=
598598
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.378 h1:gVtRJRENcLw2LxA06oS0OOOeRADStVrrpIyKjlrmkW4=
599599
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.378/go.mod h1:3fugxjsjhR7P3HeVRsUCMIM2P7KnIvqnPhcsImS7zN0=
600+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.381 h1:hi3fczplLOukXZZIhPA3ouoPk0bWe3Bx06X6t0zuoCk=
601+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.381/go.mod h1:Y8HK7RDWAdM4QvYsKyoqmn1qL8mEGNaDPJMkZP2TolM=
600602
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199 h1:6Yt74l4pA5QtzhwMNIEUt0spXdSBKH744DDqTHJOCP0=
601603
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199/go.mod h1:Yw6OQ33z3s4k0HVYTNSffB12qOzEJ2Zf1Vj4+5S3sRs=
602604
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.199 h1:UDZ59pvaqjDy2QIsMsv9hxm0BEJLmPIbHF1ms0MqaRk=
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
Provide a datasource to query TKE cluster levels.
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_kubernetes_cluster_levels" "foo" {}
8+
9+
output "level5" {
10+
value = data.tencentcloud_kubernetes_cluster_levels.foo.list.0.alias
11+
}
12+
```
13+
14+
15+
*/
16+
package tencentcloud
17+
18+
import (
19+
"context"
20+
"fmt"
21+
"math/rand"
22+
23+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
24+
)
25+
26+
func datasourceTencentCloudKubernetesClusterLevels() *schema.Resource {
27+
return &schema.Resource{
28+
Read: datasourceTencentCloudKubernetesClusterLevelsRead,
29+
Schema: map[string]*schema.Schema{
30+
"cluster_id": {
31+
Type: schema.TypeString,
32+
Optional: true,
33+
Description: "Specify cluster Id, if set will only query current cluster's available levels.",
34+
},
35+
"result_output_file": {
36+
Type: schema.TypeString,
37+
Optional: true,
38+
Description: "Used for save result.",
39+
},
40+
"list": {
41+
Type: schema.TypeList,
42+
Computed: true,
43+
Description: "List of level information.",
44+
Elem: &schema.Resource{
45+
Schema: map[string]*schema.Schema{
46+
"alias": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
Description: "Alias used for pass to cluster level arguments.",
50+
},
51+
"crd_count": {
52+
Type: schema.TypeInt,
53+
Computed: true,
54+
Description: "Number of CRDs.",
55+
},
56+
"config_map_count": {
57+
Type: schema.TypeInt,
58+
Computed: true,
59+
Description: "Number of ConfigMaps.",
60+
},
61+
"enable": {
62+
Type: schema.TypeBool,
63+
Computed: true,
64+
Description: "Indicates whether the current level enabled.",
65+
},
66+
"name": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: "Level name.",
70+
},
71+
"node_count": {
72+
Type: schema.TypeInt,
73+
Computed: true,
74+
Description: "Number of nodes.",
75+
},
76+
"other_count": {
77+
Type: schema.TypeInt,
78+
Computed: true,
79+
Description: "Number of others.",
80+
},
81+
"pod_count": {
82+
Type: schema.TypeInt,
83+
Computed: true,
84+
Description: "Number of pods.",
85+
},
86+
},
87+
},
88+
},
89+
},
90+
}
91+
}
92+
93+
func datasourceTencentCloudKubernetesClusterLevelsRead(d *schema.ResourceData, meta interface{}) error {
94+
defer logElapsed("datasource.tencentcloud_kubernetes_cluster_levels.read")()
95+
defer inconsistentCheck(d, meta)()
96+
97+
logId := getLogId(contextNil)
98+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
99+
service := TkeService{meta.(*TencentCloudClient).apiV3Conn}
100+
101+
clusterId := d.Get("cluster_id").(string)
102+
result, err := service.DescribeClusterLevelAttribute(ctx, clusterId)
103+
if err != nil {
104+
d.SetId("")
105+
return err
106+
}
107+
d.SetId(fmt.Sprintf("cluster-level-%s__%d", clusterId, rand.Intn(4)))
108+
109+
list := make([]interface{}, 0, len(result))
110+
111+
for i := range result {
112+
item := result[i]
113+
level := map[string]interface{}{
114+
"name": item.Name,
115+
"alias": item.Alias,
116+
"crd_count": item.CRDCount,
117+
"config_map_count": item.ConfigMapCount,
118+
"enable": item.Enable,
119+
"node_count": item.NodeCount,
120+
"other_count": item.OtherCount,
121+
"pod_count": item.PodCount,
122+
}
123+
list = append(list, level)
124+
}
125+
126+
_ = d.Set("list", list)
127+
128+
if output, ok := d.GetOk("result_output_file"); ok {
129+
return writeToFile(output.(string), list)
130+
}
131+
132+
return nil
133+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
8+
)
9+
10+
func TestAccTencentCloudDataSourceKubernetesClusterLevel(t *testing.T) {
11+
t.Parallel()
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testAccProviders,
15+
CheckDestroy: testAccDataSourceKubernetesClusterLevelDestroy,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccDataSourceKubernetesClusterLevelBasic,
19+
Check: resource.ComposeAggregateTestCheckFunc(
20+
resource.TestCheckResourceAttrSet("data.tencentcloud_kubernetes_cluster_levels.foo", "list.#"),
21+
resource.TestCheckResourceAttrSet("data.tencentcloud_kubernetes_cluster_levels.foo", "list.0.name"),
22+
resource.TestCheckResourceAttrSet("data.tencentcloud_kubernetes_cluster_levels.foo", "list.0.alias"),
23+
resource.TestCheckResourceAttrSet("data.tencentcloud_kubernetes_cluster_levels.foo", "list.0.crd_count"),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_kubernetes_cluster_levels.foo", "list.0.config_map_count"),
25+
resource.TestCheckResourceAttrSet("data.tencentcloud_kubernetes_cluster_levels.foo", "list.0.enable"),
26+
resource.TestCheckResourceAttrSet("data.tencentcloud_kubernetes_cluster_levels.foo", "list.0.node_count"),
27+
resource.TestCheckResourceAttrSet("data.tencentcloud_kubernetes_cluster_levels.foo", "list.0.other_count"),
28+
resource.TestCheckResourceAttrSet("data.tencentcloud_kubernetes_cluster_levels.foo", "list.0.pod_count"),
29+
resource.TestCheckResourceAttrSet("data.tencentcloud_kubernetes_cluster_levels.with_cluster", "list.#"),
30+
),
31+
},
32+
},
33+
})
34+
}
35+
36+
func testAccDataSourceKubernetesClusterLevelDestroy(s *terraform.State) error {
37+
return nil
38+
}
39+
40+
const testAccDataSourceKubernetesClusterLevelBasic = `
41+
data "tencentcloud_kubernetes_cluster_levels" "foo" {}
42+
43+
data "tencentcloud_kubernetes_clusters" "cls" {
44+
cluster_name = "` + defaultTkeClusterName + `"
45+
}
46+
47+
data "tencentcloud_kubernetes_cluster_levels" "with_cluster" {
48+
cluster_id = data.tencentcloud_kubernetes_clusters.cls.list.0.cluster_id
49+
}
50+
`

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ Tencent Kubernetes Engine(TKE)
344344
tencentcloud_kubernetes_clusters
345345
tencentcloud_eks_clusters
346346
tencentcloud_eks_cluster_credential
347+
tencentcloud_kubernetes_cluster_levels
347348
tencentcloud_kubernetes_charts
348349
349350
Resource
@@ -772,6 +773,7 @@ func Provider() terraform.ResourceProvider {
772773
"tencentcloud_security_groups": dataSourceTencentCloudSecurityGroups(),
773774
"tencentcloud_kubernetes_clusters": dataSourceTencentCloudKubernetesClusters(),
774775
"tencentcloud_kubernetes_charts": dataSourceTencentCloudKubernetesCharts(),
776+
"tencentcloud_kubernetes_cluster_levels": datasourceTencentCloudKubernetesClusterLevels(),
775777
"tencentcloud_eks_clusters": dataSourceTencentCloudEKSClusters(),
776778
"tencentcloud_eks_cluster_credential": datasourceTencentCloudEksClusterCredential(),
777779
"tencentcloud_container_clusters": dataSourceTencentCloudContainerClusters(),

tencentcloud/resource_tc_container_cluster_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1010
)
1111

12-
func TestAccTencentCloudContainerCluster_basic(t *testing.T) {
12+
// @Deprecated It has been deprecated and replaced by tencentcloud_kubernetes_cluster.
13+
func testAccTencentCloudContainerCluster_basic(t *testing.T) {
1314
t.Parallel()
1415
resource.Test(t, resource.TestCase{
1516
PreCheck: func() { testAccPreCheck(t) },

tencentcloud/resource_tc_kubernetes_addon_attachment.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ func resourceTencentCloudTkeAddonAttachmentRead(d *schema.ResourceData, meta int
227227
return err
228228
}
229229

230+
response, _, err = service.DescribeExtensionAddon(ctx, clusterId, addonName)
231+
232+
if err != nil {
233+
d.SetId("")
234+
return err
235+
}
236+
230237
_ = d.Set("response_body", response)
231238

232239
spec := addonResponseData.Spec

tencentcloud/resource_tc_kubernetes_addon_attachment_test.go

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ func TestAccTencentCloudTkeAddonAttachmentResource(t *testing.T) {
1515
{
1616
Config: testAccTkeAddonAttachment(),
1717
Check: resource.ComposeTestCheckFunc(
18-
resource.TestCheckResourceAttrSet("resource_tc_kubernetes_addon_attachment.cbs", "response_body"),
19-
resource.TestCheckResourceAttr("resource_tc_kubernetes_addon_attachment.cbs", "name", "cbs"),
20-
resource.TestCheckResourceAttr("resource_tc_kubernetes_addon_attachment.cbs", "version", "1.0.0"),
18+
resource.TestCheckResourceAttrSet("tencentcloud_kubernetes_addon_attachment.cos", "response_body"),
19+
resource.TestCheckResourceAttr("tencentcloud_kubernetes_addon_attachment.cos", "name", "cos"),
20+
resource.TestCheckResourceAttr("tencentcloud_kubernetes_addon_attachment.cos", "version", "1.0.0"),
2121
),
2222
},
2323
},
@@ -26,67 +26,72 @@ func TestAccTencentCloudTkeAddonAttachmentResource(t *testing.T) {
2626

2727
func testAccTkeAddonAttachment() string {
2828
return `
29-
variable "availability_zone" {
30-
default = "ap-guangzhou-3"
31-
}
32-
33-
variable "cluster_cidr" {
34-
default = "172.16.0.0/16"
35-
}
29+
//variable "availability_zone" {
30+
// default = "ap-guangzhou-3"
31+
//}
32+
//
33+
//variable "cluster_cidr" {
34+
// default = "172.16.0.0/16"
35+
//}
36+
//
37+
//variable "default_instance_type" {
38+
// default = "S1.SMALL1"
39+
//}
40+
//
41+
//data "tencentcloud_images" "default" {
42+
// image_type = ["PUBLIC_IMAGE"]
43+
// os_name = "centos"
44+
//}
45+
//
46+
//
47+
//data "tencentcloud_vpc_subnets" "vpc" {
48+
// is_default = true
49+
// availability_zone = var.availability_zone
50+
//}
51+
//
52+
//resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
53+
// vpc_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.vpc_id
54+
// cluster_cidr = "10.31.0.0/16"
55+
// cluster_max_pod_num = 32
56+
// cluster_name = "keep"
57+
// cluster_desc = "test cluster desc"
58+
// cluster_version = "1.20.6"
59+
// cluster_max_service_num = 32
60+
// cluster_os = "tlinux2.2(tkernel3)x86_64"
61+
//
62+
// worker_config {
63+
// count = 1
64+
// availability_zone = var.availability_zone
65+
// instance_type = var.default_instance_type
66+
// system_disk_type = "CLOUD_SSD"
67+
// system_disk_size = 60
68+
// internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
69+
// internet_max_bandwidth_out = 100
70+
// public_ip_assigned = true
71+
// subnet_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.subnet_id
72+
//
73+
// data_disk {
74+
// disk_type = "CLOUD_PREMIUM"
75+
// disk_size = 50
76+
// }
77+
//
78+
// enhanced_security_service = false
79+
// enhanced_monitor_service = false
80+
// user_data = "dGVzdA=="
81+
// password = "ZZXXccvv1212"
82+
// }
83+
//
84+
// cluster_deploy_type = "MANAGED_CLUSTER"
85+
//}
3686
37-
variable "default_instance_type" {
38-
default = "S1.SMALL1"
87+
data "tencentcloud_kubernetes_clusters" "cls" {
88+
cluster_name = "keep"
3989
}
4090
41-
data "tencentcloud_images" "default" {
42-
image_type = ["PUBLIC_IMAGE"]
43-
os_name = "centos"
44-
}
45-
46-
47-
data "tencentcloud_vpc_subnets" "vpc" {
48-
is_default = true
49-
availability_zone = var.availability_zone
50-
}
51-
52-
resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
53-
vpc_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.vpc_id
54-
cluster_cidr = "10.31.0.0/16"
55-
cluster_max_pod_num = 32
56-
cluster_name = "keep"
57-
cluster_desc = "test cluster desc"
58-
cluster_version = "1.20.6"
59-
cluster_max_service_num = 32
60-
cluster_os = "tlinux2.2(tkernel3)x86_64"
61-
62-
worker_config {
63-
count = 1
64-
availability_zone = var.availability_zone
65-
instance_type = var.default_instance_type
66-
system_disk_type = "CLOUD_SSD"
67-
system_disk_size = 60
68-
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
69-
internet_max_bandwidth_out = 100
70-
public_ip_assigned = true
71-
subnet_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.subnet_id
72-
73-
data_disk {
74-
disk_type = "CLOUD_PREMIUM"
75-
disk_size = 50
76-
}
77-
78-
enhanced_security_service = false
79-
enhanced_monitor_service = false
80-
user_data = "dGVzdA=="
81-
password = "ZZXXccvv1212"
82-
}
83-
84-
cluster_deploy_type = "MANAGED_CLUSTER"
85-
}
8691
87-
resource "tencentcloud_kubernetes_addon_attachment" "cbs" {
88-
cluster_id = tencentcloud_kubernetes_cluster.managed_cluster.id
89-
name = "cbs"
92+
resource "tencentcloud_kubernetes_addon_attachment" "cos" {
93+
cluster_id = data.tencentcloud_kubernetes_clusters.cls.list.0.cluster_id // tencentcloud_kubernetes_cluster.managed_cluster.id
94+
name = "cos"
9095
version = "1.0.0"
9196
}
9297
`

0 commit comments

Comments
 (0)