From c24204b546045fd4a1c809e90fe53fb485d38745 Mon Sep 17 00:00:00 2001 From: Jason Zhang Date: Thu, 18 Mar 2021 17:06:35 +0800 Subject: [PATCH] support tags in cce node pool --- docs/resources/cce_node_pool.md | 2 + .../resource_huaweicloud_cce_node_pool.go | 35 ++++++ ...resource_huaweicloud_cce_node_pool_test.go | 107 ++++++++++++++++++ .../openstack/cce/v3/nodepools/requests.go | 2 +- .../openstack/cce/v3/nodes/results.go | 7 ++ .../networking/v1/subnets/results.go | 3 + 6 files changed, 155 insertions(+), 1 deletion(-) diff --git a/docs/resources/cce_node_pool.md b/docs/resources/cce_node_pool.md index 2a3a7f0f4a1..53b0ec06e01 100644 --- a/docs/resources/cce_node_pool.md +++ b/docs/resources/cce_node_pool.md @@ -102,6 +102,8 @@ The following arguments are supported: * `labels` - (Optional, Map, ForceNew) Tags of a Kubernetes node, key/value pair format. Changing this parameter will create a new resource. +* `tags` - (Optional, Map) Tags of a VM node, key/value pair format. Changing this parameter will create a new resource. + * `root_volume` - (Required, List, ForceNew) It corresponds to the system disk related configuration. Changing this parameter will create a new resource. * `data_volumes` - (Required, List, ForceNew) Represents the data disk to be created. Changing this parameter will create a new resource. diff --git a/huaweicloud/resource_huaweicloud_cce_node_pool.go b/huaweicloud/resource_huaweicloud_cce_node_pool.go index 1a70eb0a664..fd10dd6781e 100644 --- a/huaweicloud/resource_huaweicloud_cce_node_pool.go +++ b/huaweicloud/resource_huaweicloud_cce_node_pool.go @@ -11,6 +11,7 @@ import ( "github.com/huaweicloud/golangsdk" "github.com/huaweicloud/golangsdk/openstack/cce/v3/nodepools" "github.com/huaweicloud/golangsdk/openstack/cce/v3/nodes" + "github.com/huaweicloud/golangsdk/openstack/common/tags" ) func ResourceCCENodePool() *schema.Resource { @@ -166,6 +167,7 @@ func ResourceCCENodePool() *schema.Resource { }, }}, }, + "tags": tagsSchema(), "billing_mode": { Type: schema.TypeInt, Computed: true, @@ -240,6 +242,11 @@ func ResourceCCENodePool() *schema.Resource { } } +func resourceCCENodePoolTags(d *schema.ResourceData) []tags.ResourceTag { + tagRaw := d.Get("tags").(map[string]interface{}) + return expandResourceTags(tagRaw) +} + func resourceCCENodePoolCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) nodePoolClient, err := config.CceV3Client(GetRegion(d, config)) @@ -286,6 +293,7 @@ func resourceCCENodePoolCreate(d *schema.ResourceData, meta interface{}) error { }, ExtendParam: resourceCCEExtendParam(d), Taints: resourceCCETaint(d), + UserTags: resourceCCENodePoolTags(d), }, Autoscaling: nodepools.AutoscalingSpec{ Enable: d.Get("scall_enable").(bool), @@ -412,6 +420,13 @@ func resourceCCENodePoolRead(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("[DEBUG] Error saving root Volume to state for HuaweiCloud Node Pool (%s): %s", d.Id(), err) } + tagmap := tagsToMap(s.Spec.NodeTemplate.UserTags) + // ignore "CCE-Dynamic-Provisioning-Node" + delete(tagmap, "CCE-Dynamic-Provisioning-Node") + if err := d.Set("tags", tagmap); err != nil { + return fmt.Errorf("Error saving tags to state for CCE Node Pool(%s): %s", d.Id(), err) + } + d.Set("status", s.Status.Phase) return nil @@ -425,6 +440,17 @@ func resourceCCENodePoolUpdate(d *schema.ResourceData, meta interface{}) error { } initialNodeCount := d.Get("initial_node_count").(int) + var loginSpec nodes.LoginSpec + if hasFilledOpt(d, "key_pair") { + loginSpec = nodes.LoginSpec{SshKey: d.Get("key_pair").(string)} + } else if hasFilledOpt(d, "password") { + loginSpec = nodes.LoginSpec{ + UserPassword: nodes.UserPassword{ + Username: "root", + Password: d.Get("password").(string), + }, + } + } updateOpts := nodepools.UpdateOpts{ Kind: "NodePool", @@ -441,6 +467,15 @@ func resourceCCENodePoolUpdate(d *schema.ResourceData, meta interface{}) error { ScaleDownCooldownTime: d.Get("scale_down_cooldown_time").(int), Priority: d.Get("priority").(int), }, + NodeTemplate: nodes.Spec{ + Flavor: d.Get("flavor_id").(string), + Az: d.Get("availability_zone").(string), + Login: loginSpec, + RootVolume: resourceCCERootVolume(d), + DataVolumes: resourceCCEDataVolume(d), + Count: 1, + UserTags: resourceCCENodePoolTags(d), + }, Type: d.Get("type").(string), }, } diff --git a/huaweicloud/resource_huaweicloud_cce_node_pool_test.go b/huaweicloud/resource_huaweicloud_cce_node_pool_test.go index 487737b2f33..6f8548a4d74 100644 --- a/huaweicloud/resource_huaweicloud_cce_node_pool_test.go +++ b/huaweicloud/resource_huaweicloud_cce_node_pool_test.go @@ -58,6 +58,41 @@ func TestAccCCENodePool_basic(t *testing.T) { }) } +func TestAccCCENodePool_tags(t *testing.T) { + var nodePool nodepools.NodePool + + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5)) + resourceName := "huaweicloud_cce_node_pool.test" + //clusterName here is used to provide the cluster id to fetch cce node pool. + clusterName := "huaweicloud_cce_cluster.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCCENodePoolDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCCENodePool_tags(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCCENodePoolExists(resourceName, clusterName, &nodePool), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "tags.test1", "val1"), + resource.TestCheckResourceAttr(resourceName, "tags.test2", "val2"), + ), + }, + { + Config: testAccCCENodePool_tags_update(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCCENodePoolExists(resourceName, clusterName, &nodePool), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "tags.test1", "val1_update"), + resource.TestCheckResourceAttr(resourceName, "tags.test2_update", "val2_update"), + ), + }, + }, + }) +} + func testAccCheckCCENodePoolDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) cceClient, err := config.CceV3Client(HW_REGION_NAME) @@ -258,3 +293,75 @@ resource "huaweicloud_cce_node_pool" "test" { } `, testAccCCENodePool_Base(rName), rName) } + +func testAccCCENodePool_tags(rName string) string { + return fmt.Sprintf(` +%s + +resource "huaweicloud_cce_node_pool" "test" { + cluster_id = huaweicloud_cce_cluster.test.id + name = "%s" + os = "EulerOS 2.5" + flavor_id = "s6.large.2" + initial_node_count = 1 + availability_zone = data.huaweicloud_availability_zones.test.names[0] + key_pair = huaweicloud_compute_keypair.test.name + scall_enable = false + min_node_count = 0 + max_node_count = 0 + scale_down_cooldown_time = 0 + priority = 0 + type = "vm" + + root_volume { + size = 40 + volumetype = "SSD" + } + data_volumes { + size = 100 + volumetype = "SSD" + } + + tags = { + test1 = "val1" + test2 = "val2" + } +} +`, testAccCCENodePool_Base(rName), rName) +} + +func testAccCCENodePool_tags_update(rName string) string { + return fmt.Sprintf(` +%s + +resource "huaweicloud_cce_node_pool" "test" { + cluster_id = huaweicloud_cce_cluster.test.id + name = "%s" + os = "EulerOS 2.5" + flavor_id = "s6.large.2" + initial_node_count = 1 + availability_zone = data.huaweicloud_availability_zones.test.names[0] + key_pair = huaweicloud_compute_keypair.test.name + scall_enable = false + min_node_count = 0 + max_node_count = 0 + scale_down_cooldown_time = 0 + priority = 0 + type = "vm" + + root_volume { + size = 40 + volumetype = "SSD" + } + data_volumes { + size = 100 + volumetype = "SSD" + } + + tags = { + test1 = "val1_update" + test2_update = "val2_update" + } +} +`, testAccCCENodePool_Base(rName), rName) +} diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/cce/v3/nodepools/requests.go b/vendor/github.com/huaweicloud/golangsdk/openstack/cce/v3/nodepools/requests.go index 8ff594aa7e5..0d68467a773 100644 --- a/vendor/github.com/huaweicloud/golangsdk/openstack/cce/v3/nodepools/requests.go +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/cce/v3/nodepools/requests.go @@ -181,7 +181,7 @@ type UpdateMetaData struct { // UpdateSpec describes Node pools update specification type UpdateSpec struct { // Node type. Currently, only VM nodes are supported. - Type string `json:"type"` + Type string `json:"type,omitempty"` // Node template NodeTemplate nodes.Spec `json:"nodeTemplate"` // Initial number of expected nodes diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/cce/v3/nodes/results.go b/vendor/github.com/huaweicloud/golangsdk/openstack/cce/v3/nodes/results.go index 5a4667a0d54..63010ce0638 100644 --- a/vendor/github.com/huaweicloud/golangsdk/openstack/cce/v3/nodes/results.go +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/cce/v3/nodes/results.go @@ -73,6 +73,8 @@ type Spec struct { UserTags []tags.ResourceTag `json:"userTags,omitempty"` // Tag of a Kubernetes node, key value pair format K8sTags map[string]string `json:"k8sTags,omitempty"` + // The runtime spec + RunTime *RunTimeSpec `json:"runtime,omitempty"` // taints to created nodes to configure anti-affinity Taints []TaintSpec `json:"taints,omitempty"` } @@ -158,6 +160,11 @@ type EipSpec struct { Bandwidth BandwidthOpts `json:"bandwidth,omitempty"` } +type RunTimeSpec struct { + // the name of runtime: docker or containerd + Name string `json:"name,omitempty"` +} + type BandwidthOpts struct { ChargeMode string `json:"chargemode,omitempty"` Size int `json:"size,omitempty"` diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/subnets/results.go b/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/subnets/results.go index 733678d4be6..0a9af5441fd 100644 --- a/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/subnets/results.go +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/subnets/results.go @@ -52,6 +52,9 @@ type Subnet struct { //Specifies the subnet ID. SubnetId string `json:"neutron_subnet_id"` + //Specifies the subnet ID of the IPv6 subnet. + IPv6SubnetId string `json:"neutron_subnet_id_v6"` + //Specifies the extra dhcp opts. ExtraDhcpOpts []ExtraDhcp `json:"extra_dhcp_opts"` }