Skip to content

tencentcloud_kubernetes_cluster support labels #445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
## 1.37.0 (Unreleased)

ENHANCEMENTS:

* Resource: `tencentcloud_kubernetes_cluster` add new argument `labels`.
* Resource: `tencentcloud_kubernetes_as_scaling_group` add new argument `labels`.

## 1.36.0 (June 08, 2020)

FEATURES:
Expand Down
15 changes: 15 additions & 0 deletions examples/tencentcloud-tke/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
tags = {
"test" = "test"
}

labels = {
"test1" = "test1",
"test2" = "test2",
}
}

#examples for INDEPENDENT_CLUSTER cluster
Expand Down Expand Up @@ -89,6 +94,11 @@ resource "tencentcloud_kubernetes_cluster" "independing_cluster" {
password = "ZZXXccvv1212"
}

labels = {
"test1" = "test1",
"test2" = "test2",
}

cluster_deploy_type = "INDEPENDENT_CLUSTER"
}

Expand Down Expand Up @@ -168,4 +178,9 @@ resource "tencentcloud_kubernetes_as_scaling_group" "test" {
}

}

labels = {
"test1" = "test1",
"test2" = "test2",
}
}
11 changes: 11 additions & 0 deletions tencentcloud/internal/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/pkg/errors"
tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525"
)

// Generates a hash for the set hash function used by the IDs
Expand Down Expand Up @@ -40,6 +41,16 @@ func GetTags(d *schema.ResourceData, k string) map[string]string {
return tags
}

func GetLabels(d *schema.ResourceData, k string) []*tke.Label {
labels := make([]*tke.Label, 0)
if raw, ok := d.GetOk(k); ok {
for k, v := range raw.(map[string]interface{}) {
labels = append(labels, &tke.Label{Name: String(k), Value: String(v.(string))})
}
}
return labels
}

func BuildToken() string {
buf := make([]byte, 16)
_, _ = rand.Read(buf)
Expand Down
45 changes: 42 additions & 3 deletions tencentcloud/resource_tc_kubernetes_as_scaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ resource "tencentcloud_kubernetes_as_scaling_group" "test" {

auto_scaling_config {
configuration_name = "tf-guagua-as-config"
instance_type = "SN3ne.8XLARGE64"
instance_type = "S1.SMALL1"
project_id = 0
system_disk_type = "CLOUD_PREMIUM"
system_disk_size = "50"
Expand All @@ -52,6 +52,11 @@ resource "tencentcloud_kubernetes_as_scaling_group" "test" {
}

}

labels = {
"test1" = "test1",
"test1" = "test2",
}
}
```
*/
Expand Down Expand Up @@ -101,6 +106,12 @@ func ResourceTencentCloudKubernetesAsScalingGroup() *schema.Resource {
},
Description: "Auto scaling config parameters.",
},
"labels": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Description: "Labels of kubernetes AS Group created nodes.",
},
},
}
}
Expand Down Expand Up @@ -702,7 +713,33 @@ func resourceKubernetesAsScalingGroupRead(d *schema.ResourceData, meta interface
if number == 0 {
return nil
}
return nil

err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
clusterAsGroupSet, err := service.DescribeClusterAsGroupsByGroupId(ctx, clusterId, asGroupId)
if err != nil {
return retryError(err)
}

if clusterAsGroupSet == nil {
return nil
}

labels := clusterAsGroupSet.Labels
var labelsMap = make(map[string]string, len(labels))

if len(labels) == 0 {
d.Set("labels", labelsMap)
return nil
}

for _, v := range labels {
labelsMap[*v.Name] = *v.Value
}
d.Set("labels", labelsMap)
return nil
})

return err
}

func resourceKubernetesAsScalingGroupCreate(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -730,9 +767,11 @@ func resourceKubernetesAsScalingGroupCreate(d *schema.ResourceData, meta interfa
return err
}

labels := helper.GetLabels(d, "labels")

service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn}

asGroupId, err := service.CreateClusterAsGroup(ctx, clusterId, groupParaStr, configParaStr)
asGroupId, err := service.CreateClusterAsGroup(ctx, clusterId, groupParaStr, configParaStr, labels)
if err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion tencentcloud/resource_tc_kubernetes_as_scaling_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func TestAccTencentCloudTkeAsResource(t *testing.T) {
resource.TestCheckResourceAttrSet(testTkeClusterAsResourceKey, "cluster_id"),
resource.TestCheckResourceAttr(testTkeClusterAsResourceKey, "auto_scaling_group.#", "1"),
resource.TestCheckResourceAttr(testTkeClusterAsResourceKey, "auto_scaling_config.#", "1"),
resource.TestCheckResourceAttr(testTkeClusterAsResourceKey, "labels.test1", "test1"),
resource.TestCheckResourceAttr(testTkeClusterAsResourceKey, "labels.test2", "test2"),
),
},
},
Expand Down Expand Up @@ -116,7 +118,7 @@ data "tencentcloud_vpc_subnets" "vpc" {
}

variable "default_instance_type" {
default = "SN3ne.8XLARGE64"
default = "S1.SMALL1"
}

resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
Expand Down Expand Up @@ -199,6 +201,11 @@ resource "tencentcloud_kubernetes_as_scaling_group" "as_test" {
}

}

labels = {
"test1" = "test1",
"test2" = "test2",
}
}

`
Expand Down
26 changes: 23 additions & 3 deletions tencentcloud/resource_tc_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ variable "subnet" {
}

variable "default_instance_type" {
default = "SA1.LARGE8"
default = "S1.SMALL1"
}

#examples for MANAGED_CLUSTER cluster
resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
vpc_id = var.vpc
cluster_cidr = "10.1.0.0/16"
cluster_cidr = "10.31.0.0/16"
cluster_max_pod_num = 32
cluster_name = "test"
cluster_desc = "test cluster desc"
Expand Down Expand Up @@ -51,6 +51,11 @@ resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
password = "ZZXXccvv1212"
}

labels = {
"test1" = "test1",
"test2" = "test2",
}

cluster_deploy_type = "MANAGED_CLUSTER"
}

Expand Down Expand Up @@ -107,6 +112,11 @@ resource "tencentcloud_kubernetes_cluster" "independing_cluster" {
password = "ZZXXccvv1212"
}

labels = {
"test1" = "test1",
"test2" = "test2",
}

cluster_deploy_type = "INDEPENDENT_CLUSTER"
}
```
Expand Down Expand Up @@ -589,6 +599,12 @@ func resourceTencentCloudTkeCluster() *schema.Resource {
},
Description: "An information list of cvm within the 'WORKER' clusters. Each element contains the following attributes:",
},
"labels": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Description: "Labels of tke cluster nodes.",
},
}

for k, v := range tkeSecurityInfo() {
Expand Down Expand Up @@ -943,6 +959,9 @@ func resourceTencentCloudTkeClusterCreate(d *schema.ResourceData, meta interface

tags := helper.GetTags(d, "tags")

labels := helper.GetLabels(d, "labels")
iAdvanced.Labels = labels

service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn}

id, err := service.CreateCluster(ctx, basic, advanced, cvms, iAdvanced, cidrSet, tags)
Expand Down Expand Up @@ -1124,7 +1143,7 @@ func resourceTencentCloudTkeClusterRead(d *schema.ResourceData, meta interface{}

_, workers, err := service.DescribeClusterInstances(ctx, d.Id())
if err != nil {
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
err = resource.Retry(10*readRetryTimeout, func() *resource.RetryError {
_, workers, err = service.DescribeClusterInstances(ctx, d.Id())

if e, ok := err.(*errors.TencentCloudSDKError); ok {
Expand Down Expand Up @@ -1205,6 +1224,7 @@ func resourceTencentCloudTkeClusterRead(d *schema.ResourceData, meta interface{}
} else {
_ = d.Set("cluster_intranet", true)
}

return nil
}

Expand Down
20 changes: 18 additions & 2 deletions tencentcloud/resource_tc_kubernetes_cluster_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ variable "availability_zone" {
}

variable "cluster_cidr" {
default = "172.31.0.0/16"
default = "172.16.0.0/16"
}

variable "default_instance_type" {
default = "SA1.LARGE8"
default = "S1.SMALL1"
}

data "tencentcloud_images" "default" {
Expand Down Expand Up @@ -84,6 +84,11 @@ resource "tencentcloud_kubernetes_cluster_attachment" "test_attach" {
cluster_id = tencentcloud_kubernetes_cluster.managed_cluster.id
instance_id = tencentcloud_instance.foo.id
password = "Lo4wbdit"

labels = {
"test1" = "test1",
"test2" = "test2",
}
}
```
*/
Expand Down Expand Up @@ -141,6 +146,12 @@ func resourceTencentCloudTkeClusterAttachment() *schema.Resource {
Computed: true,
Description: "A list of security group ids after attach to cluster.",
},
"labels": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Description: "Labels of tke attachment exits cvm.",
},
}

return &schema.Resource{
Expand Down Expand Up @@ -270,6 +281,11 @@ func resourceTencentCloudTkeClusterAttachmentCreate(d *schema.ResourceData, meta
return fmt.Errorf("parameters `key_ids` and `password` must set and only set one")
}

request.InstanceAdvancedSettings = &tke.InstanceAdvancedSettings{}

labels := helper.GetLabels(d, "labels")
request.InstanceAdvancedSettings.Labels = labels

/*cvm has been attached*/
var err error
_, workers, err := tkeService.DescribeClusterInstances(ctx, *request.ClusterId)
Expand Down
15 changes: 12 additions & 3 deletions tencentcloud/resource_tc_kubernetes_cluster_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func TestAccTencentCloudTkeAttachResource(t *testing.T) {
testAccCheckTkeAttachExists("tencentcloud_kubernetes_cluster_attachment.test_attach"),
resource.TestCheckResourceAttrSet("tencentcloud_kubernetes_cluster_attachment.test_attach", "cluster_id"),
resource.TestCheckResourceAttrSet("tencentcloud_kubernetes_cluster_attachment.test_attach", "instance_id"),
resource.TestCheckResourceAttr("tencentcloud_kubernetes_cluster_attachment.test_attach", "labels.test1", "test1"),
resource.TestCheckResourceAttr("tencentcloud_kubernetes_cluster_attachment.test_attach", "labels.test2", "test2"),
),
},
},
Expand Down Expand Up @@ -131,11 +133,11 @@ variable "availability_zone" {
}

variable "cluster_cidr" {
default = "172.31.0.0/16"
default = "172.16.0.0/16"
}

variable "default_instance_type" {
default = "SA1.LARGE8"
default = "S1.SMALL1"
}

data "tencentcloud_images" "default" {
Expand Down Expand Up @@ -166,11 +168,13 @@ resource "tencentcloud_instance" "foo" {
instance_type = var.default_instance_type
system_disk_type = "CLOUD_PREMIUM"
system_disk_size = 50
vpc_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.vpc_id
subnet_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.subnet_id
}

resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
vpc_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.vpc_id
cluster_cidr = "10.1.0.0/16"
cluster_cidr = "10.31.0.0/16"
cluster_max_pod_num = 32
cluster_name = "keep"
cluster_desc = "test cluster desc"
Expand Down Expand Up @@ -205,6 +209,11 @@ resource "tencentcloud_kubernetes_cluster_attachment" "test_attach" {
cluster_id = tencentcloud_kubernetes_cluster.managed_cluster.id
instance_id = tencentcloud_instance.foo.id
password = "Lo4wbdit"

labels = {
"test1" = "test1",
"test2" = "test2",
}
}
`
}
Loading