Skip to content
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

Fix cluster primary nodepool scaling bug #63

Merged
merged 2 commits into from
Aug 18, 2021
Merged
Changes from all 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
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 @@ -377,8 +379,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 @@ -401,6 +421,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