Skip to content

Commit

Permalink
resource/alicloud_cs_kubernetes_node_pool: support desired size of no…
Browse files Browse the repository at this point in the history
…de pool
  • Loading branch information
saberrey committed Feb 24, 2022
1 parent dc01ad3 commit ab5b8a8
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 23 deletions.
37 changes: 31 additions & 6 deletions alicloud/resource_alicloud_cs_kubernetes_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ func resourceAlicloudCSKubernetesNodePool() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ConflictsWith: []string{"instances"},
ConflictsWith: []string{"instances", "desired_size"},
Deprecated: "Field 'node_count' has been deprecated from provider version 1.158.0. New field 'desired_size' instead.",
},
"desired_size": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ConflictsWith: []string{"instances", "node_count"},
},
"vpc_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -395,7 +402,7 @@ func resourceAlicloudCSKubernetesNodePool() *schema.Resource {
Type: schema.TypeString,
},
MaxItems: 100,
ConflictsWith: []string{"node_count", "scaling_config"},
ConflictsWith: []string{"node_count", "scaling_config", "desired_size"},
},
"keep_instance_name": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -518,7 +525,6 @@ func resourceAlicloudCSNodePoolUpdate(d *schema.ResourceData, meta interface{})

if d.HasChange("node_count") {
oldV, newV := d.GetChange("node_count")

oldValue, ok := oldV.(int)
if ok != true {
return WrapErrorf(fmt.Errorf("node_count old value can not be parsed"), "parseError %d", oldValue)
Expand Down Expand Up @@ -574,6 +580,12 @@ func resourceAlicloudCSNodePoolUpdate(d *schema.ResourceData, meta interface{})
}
}

if d.HasChange("desired_size") {
update = true
size := int64(d.Get("desired_size").(int))
args.ScalingGroup.DesiredSize = &size
}

if v, ok := d.GetOk("install_cloud_monitor"); ok && v != nil {
args.CmsEnabled = v.(bool)
}
Expand Down Expand Up @@ -740,7 +752,7 @@ func resourceAlicloudCSNodePoolUpdate(d *schema.ResourceData, meta interface{})
addDebug("UpdateKubernetesNodePool", resoponse, resizeRequestMap)
}

stateConf := BuildStateConf([]string{"scaling", "updating"}, []string{"active"}, d.Timeout(schema.TimeoutUpdate), 10*time.Second, csService.CsKubernetesNodePoolStateRefreshFunc(d.Id(), []string{"deleting", "failed"}))
stateConf := BuildStateConf([]string{"scaling", "updating", "removing"}, []string{"active"}, d.Timeout(schema.TimeoutUpdate), 10*time.Second, csService.CsKubernetesNodePoolStateRefreshFunc(d.Id(), []string{"deleting", "failed"}))

if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
Expand Down Expand Up @@ -773,7 +785,7 @@ func resourceAlicloudCSNodePoolUpdate(d *schema.ResourceData, meta interface{})
return resource.NonRetryableError(err)
}

if nodePoolDetail.TotalNodes != d.Get("node_count").(int) {
if nodePoolDetail.TotalNodes != d.Get("node_count").(int) && nodePoolDetail.TotalNodes != d.Get("desired_size").(int) {
time.Sleep(20 * time.Second)
return resource.RetryableError(Error("[ERROR] The number of nodes is inconsistent %s", d.Id()))
}
Expand Down Expand Up @@ -827,6 +839,11 @@ func resourceAlicloudCSNodePoolRead(d *schema.ResourceData, meta interface{}) er
d.Set("runtime_name", object.Runtime)
d.Set("runtime_version", object.RuntimeVersion)
d.Set("deployment_set_id", object.DeploymentSetId)

if object.DesiredSize != nil {
d.Set("desired_size", *object.DesiredSize)
}

if object.InstanceChargeType == "PrePaid" {
d.Set("period", object.Period)
d.Set("period_unit", object.PeriodUnit)
Expand Down Expand Up @@ -960,7 +977,6 @@ func buildNodePoolArgs(d *schema.ResourceData, meta interface{}) (*cs.CreateNode

creationArgs := &cs.CreateNodePoolRequest{
RegionId: common.Region(client.RegionId),
Count: int64(d.Get("node_count").(int)),
NodePoolInfo: cs.NodePoolInfo{
Name: d.Get("name").(string),
NodePoolType: "ess", // hard code the type
Expand All @@ -982,6 +998,15 @@ func buildNodePoolArgs(d *schema.ResourceData, meta interface{}) (*cs.CreateNode
},
}

if v, ok := d.GetOkExists("node_count"); ok {
creationArgs.Count = int64(v.(int))
}

if v, ok := d.GetOkExists("desired_size"); ok {
size := int64(v.(int))
creationArgs.DesiredSize = &size
}

setNodePoolDataDisks(&creationArgs.ScalingGroup, d)
setNodePoolTags(&creationArgs.ScalingGroup, d)
setNodePoolTaints(&creationArgs.KubernetesConfig, d)
Expand Down
141 changes: 136 additions & 5 deletions alicloud/resource_alicloud_cs_kubernetes_node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestAccAlicloudCSKubernetesNodePool_basic(t *testing.T) {
"cluster_id": "${alicloud_cs_managed_kubernetes.default.0.id}",
"vswitch_ids": []string{"${local.vswitch_id}"},
"instance_types": []string{"${data.alicloud_instance_types.default.instance_types.0.id}"},
"node_count": "1",
"desired_size": "1",
"key_name": "${alicloud_key_pair.default.key_name}",
"system_disk_category": "cloud_efficiency",
"system_disk_size": "40",
Expand All @@ -64,7 +64,7 @@ func TestAccAlicloudCSKubernetesNodePool_basic(t *testing.T) {
"cluster_id": CHECKSET,
"vswitch_ids.#": "1",
"instance_types.#": "1",
"node_count": "1",
"desired_size": "1",
"key_name": CHECKSET,
"system_disk_category": "cloud_efficiency",
"system_disk_size": "40",
Expand Down Expand Up @@ -95,6 +95,125 @@ func TestAccAlicloudCSKubernetesNodePool_basic(t *testing.T) {
ImportStateVerifyIgnore: []string{"password"},
},
// check: scale out
{
Config: testAccConfig(map[string]interface{}{
"desired_size": "2",
"system_disk_size": "80",
"data_disks": []map[string]string{{"size": "40", "category": "cloud"}},
"management": []map[string]string{{"auto_repair": "true", "auto_upgrade": "true", "surge": "1", "max_unavailable": "1"}},
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"desired_size": "2",
"system_disk_size": "80",
"data_disks.#": "1",
"data_disks.0.size": "40",
"data_disks.0.category": "cloud",
"management.#": "1",
"management.0.auto_repair": "true",
"management.0.auto_upgrade": "true",
"management.0.surge": "1",
"management.0.max_unavailable": "1",
}),
),
},
// check: remove nodes
{
Config: testAccConfig(map[string]interface{}{
"desired_size": "1",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"desired_size": "1",
}),
),
},
},
})
}

func TestAccAlicloudCSKubernetesNodePoolWithNodeCount_basic(t *testing.T) {
var v *cs.NodePoolDetail

resourceId := "alicloud_cs_kubernetes_node_pool.with_node_count"
ra := resourceAttrInit(resourceId, csdKubernetesNodePoolBasicMap)

serviceFunc := func() interface{} {
return &CsService{testAccProvider.Meta().(*connectivity.AliyunClient)}
}
rc := resourceCheckInit(resourceId, &v, serviceFunc)

rac := resourceAttrCheckInit(rc, ra)

testAccCheck := rac.resourceAttrMapUpdateSet()
rand := acctest.RandIntRange(1000000, 9999999)
name := fmt.Sprintf("tf-testAccNodePool-%d", rand)
testAccConfig := resourceTestAccConfigFunc(resourceId, name, resourceCSNodePoolConfigDependence)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
// module name
IDRefreshName: resourceId,
Providers: testAccProviders,
CheckDestroy: rac.checkResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{
"name": name,
"cluster_id": "${alicloud_cs_managed_kubernetes.default.0.id}",
"vswitch_ids": []string{"${local.vswitch_id}"},
"instance_types": []string{"${data.alicloud_instance_types.default.instance_types.0.id}"},
"node_count": "1",
"key_name": "${alicloud_key_pair.default.key_name}",
"system_disk_category": "cloud_efficiency",
"system_disk_size": "40",
"install_cloud_monitor": "false",
"data_disks": []map[string]string{{"size": "100", "category": "cloud_ssd"}},
"tags": map[string]interface{}{"Created": "TF", "Foo": "Bar"},
"management": []map[string]string{{"auto_repair": "true", "auto_upgrade": "true", "surge": "0", "max_unavailable": "0"}},
"security_group_ids": []string{"${alicloud_security_group.group.id}", "${alicloud_security_group.group1.id}"},
"runtime_name": "containerd",
"runtime_version": "1.4.8",
"image_type": "CentOS",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"name": name,
"cluster_id": CHECKSET,
"vswitch_ids.#": "1",
"instance_types.#": "1",
"node_count": "1",
"key_name": CHECKSET,
"system_disk_category": "cloud_efficiency",
"system_disk_size": "40",
"install_cloud_monitor": "false",
"data_disks.#": "1",
"data_disks.0.size": "100",
"data_disks.0.category": "cloud_ssd",
"tags.%": "2",
"tags.Created": "TF",
"tags.Foo": "Bar",
"management.#": "1",
"management.0.auto_repair": "true",
"management.0.auto_upgrade": "true",
"management.0.surge": "0",
"management.0.max_unavailable": "0",
"security_group_ids.#": "2",
"runtime_name": "containerd",
"runtime_version": "1.4.8",
"image_type": "CentOS",
}),
),
},
{
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password"},
},
// check: scale out
{
Config: testAccConfig(map[string]interface{}{
"node_count": "2",
Expand Down Expand Up @@ -128,6 +247,18 @@ func TestAccAlicloudCSKubernetesNodePool_basic(t *testing.T) {
}),
),
},
// check: change node_count to desire_size
{
Config: testAccConfig(map[string]interface{}{
"node_count": "#REMOVEKEY",
"desired_size": "1",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"desired_size": "1",
}),
),
},
},
})
}
Expand Down Expand Up @@ -200,7 +331,7 @@ func TestAccAlicloudCSKubernetesNodePool_autoScaling(t *testing.T) {
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password", "node_count"},
ImportStateVerifyIgnore: []string{"password", "node_count", "desired_size"},
},
// check: update config
{
Expand Down Expand Up @@ -375,7 +506,7 @@ func TestAccAlicloudCSKubernetesNodePool_Spot(t *testing.T) {
"system_disk_size": "120",
"resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.groups.0.id}",
"password": "Terraform1234",
"node_count": "1",
"desired_size": "1",
"install_cloud_monitor": "false",
"internet_charge_type": "PayByTraffic",
"internet_max_bandwidth_out": "5",
Expand All @@ -397,7 +528,7 @@ func TestAccAlicloudCSKubernetesNodePool_Spot(t *testing.T) {
"system_disk_size": "120",
"resource_group_id": CHECKSET,
"password": CHECKSET,
"node_count": "1",
"desired_size": "1",
"install_cloud_monitor": "false",
"internet_charge_type": "PayByTraffic",
"internet_max_bandwidth_out": "5",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/aliyun/fc-go-sdk v0.0.0-20200925033337-c013428cbe21
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/deckarep/golang-set v1.8.0
github.com/denverdino/aliyungo v0.0.0-20211216040745-6cc94847413f
github.com/denverdino/aliyungo v0.0.0-20220218034049-d2eb7f483ee8
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
github.com/gogap/errors v0.0.0-20160523102334-149c546090d0 // indirect
github.com/gogap/stack v0.0.0-20150131034635-fef68dddd4f8 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set v1.8.0 h1:sk9/l/KqpunDwP7pSjUg0keiOOLEnOBHzykLrsPppp4=
github.com/deckarep/golang-set v1.8.0/go.mod h1:5nI87KwE7wgsBU1F4GKAw2Qod7p5kyS383rP6+o6qqo=
github.com/denverdino/aliyungo v0.0.0-20211216040745-6cc94847413f h1:umutxmMv53KWjVv2JsJQzC7kcPAkzifajj6jflUiWfk=
github.com/denverdino/aliyungo v0.0.0-20211216040745-6cc94847413f/go.mod h1:VVxx1gyGhdt369208nKOYLI0PVgrZqbU+EuWBZJQ1ZQ=
github.com/denverdino/aliyungo v0.0.0-20220218034049-d2eb7f483ee8 h1:Y86vVLW1CMFbrS2/S7EeGrsP/q3Ulljnszu4ju3dq+E=
github.com/denverdino/aliyungo v0.0.0-20220218034049-d2eb7f483ee8/go.mod h1:VVxx1gyGhdt369208nKOYLI0PVgrZqbU+EuWBZJQ1ZQ=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/denverdino/aliyungo/cs/node_pool.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ github.com/cenkalti/backoff
github.com/davecgh/go-spew/spew
# github.com/deckarep/golang-set v1.8.0
github.com/deckarep/golang-set
# github.com/denverdino/aliyungo v0.0.0-20211216040745-6cc94847413f
# github.com/denverdino/aliyungo v0.0.0-20220218034049-d2eb7f483ee8
github.com/denverdino/aliyungo/cdn
github.com/denverdino/aliyungo/common
github.com/denverdino/aliyungo/cs
Expand Down

0 comments on commit ab5b8a8

Please sign in to comment.