Skip to content

Commit

Permalink
Fix cluster primary nodepool scaling bug (#63)
Browse files Browse the repository at this point in the history
* Fix cluster primary nodepool scaling bug

* Change validation method for "num_target_nodes"
  • Loading branch information
zulh-civo committed Aug 18, 2021
1 parent 0760056 commit 6d4088f
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions civo/resource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/civo/terraform-provider-civo/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

// Kubernetes Cluster resource, with this you can manage all cluster from terraform
Expand All @@ -37,10 +38,11 @@ func resourceKubernetesCluster() *schema.Resource {
Description: "The network for the cluster, if not declare we use the default one",
},
"num_target_nodes": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "the number of instances to create (optional, the default at the time of writing is 3)",
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "the number of instances to create (optional, the default at the time of writing is 3)",
ValidateFunc: validation.IntAtLeast(1),
},
"target_nodes_size": {
Type: schema.TypeString,
Expand Down Expand Up @@ -378,8 +380,26 @@ func resourceKubernetesClusterUpdate(d *schema.ResourceData, m interface{}) erro
}

if d.HasChange("num_target_nodes") {
config.NumTargetNodes = d.Get("num_target_nodes").(int)
numTargetNodes := d.Get("num_target_nodes").(int)

config.Region = apiClient.Region
kubernetesCluster, err := apiClient.FindKubernetesCluster(d.Id())
if err != nil {
return err
}

targetNodePool := ""
nodePools := []civogo.KubernetesClusterPoolConfig{}
for _, v := range kubernetesCluster.Pools {
nodePools = append(nodePools, civogo.KubernetesClusterPoolConfig{ID: v.ID, Count: v.Count, Size: v.Size})

if targetNodePool == "" && v.Size == d.Get("target_nodes_size").(string) {
targetNodePool = v.ID
}
}

nodePools = updateNodePool(nodePools, targetNodePool, numTargetNodes)
config.Pools = nodePools
}

if d.HasChange("kubernetes_version") {
Expand All @@ -402,6 +422,7 @@ func resourceKubernetesClusterUpdate(d *schema.ResourceData, m interface{}) erro
}

log.Printf("[INFO] updating the kubernetes cluster %s", d.Id())
log.Printf("[DEBUG] KubernetesClusterConfig: %+v\n", config)
_, err := apiClient.UpdateKubernetesCluster(d.Id(), config)
if err != nil {
return fmt.Errorf("[ERR] failed to update kubernetes cluster: %s", err)
Expand Down

0 comments on commit 6d4088f

Please sign in to comment.