diff --git a/civo/kubernetes/datasource_kubernetes_cluster.go b/civo/kubernetes/datasource_kubernetes_cluster.go index bfc4318..b47817e 100644 --- a/civo/kubernetes/datasource_kubernetes_cluster.go +++ b/civo/kubernetes/datasource_kubernetes_cluster.go @@ -75,7 +75,13 @@ func DataSourceKubernetesCluster() *schema.Resource { Description: "A list of application installed", }, "installed_applications": dataSourceApplicationSchema(), - "pools": dataSourcenodePoolSchema(), + "pools": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: nodePoolSchema(false), + }, + }, "status": { Type: schema.TypeString, Computed: true, @@ -115,44 +121,6 @@ func DataSourceKubernetesCluster() *schema.Resource { } } -// schema for the node pool in the cluster -func dataSourcenodePoolSchema() *schema.Schema { - return &schema.Schema{ - Type: schema.TypeList, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "label": { - Type: schema.TypeString, - Computed: true, - Description: "Node pool label, if you don't provide one, we will generate one for you", - }, - "node_count": { - Type: schema.TypeInt, - Computed: true, - Description: "The size of the pool", - }, - "size": { - Type: schema.TypeString, - Computed: true, - Description: "The size of each node inside the pool", - }, - "instance_names": { - Type: schema.TypeSet, - Computed: true, - Description: "A list of the instance in the pool", - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "public_ip_node_pool": { - Type: schema.TypeBool, - Computed: true, - Description: "Node pool belongs to the public ip node pool", - }, - }, - }, - } -} - // schema for the application in the cluster func dataSourceApplicationSchema() *schema.Schema { return &schema.Schema{ @@ -188,7 +156,7 @@ func dataSourceApplicationSchema() *schema.Schema { func dataSourceKubernetesClusterRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { apiClient := m.(*civogo.Client) - // overwrite the region if is define in the datasource + // overwrite the region if it is defined in the datasource if region, ok := d.GetOk("region"); ok { apiClient.Region = region.(string) } diff --git a/civo/kubernetes/kubernetes.go b/civo/kubernetes/kubernetes.go index 04ad94e..0de2f38 100644 --- a/civo/kubernetes/kubernetes.go +++ b/civo/kubernetes/kubernetes.go @@ -4,9 +4,9 @@ import ( "github.com/civo/civogo" "github.com/civo/terraform-provider-civo/internal/utils" "github.com/google/uuid" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + corev1 "k8s.io/api/core/v1" ) // nodePoolSchema function to define the node pool schema @@ -134,7 +134,7 @@ func flattenInstalledApplication(apps []civogo.KubernetesInstalledApplication) [ return flattenedInstalledApplication } -// exapandNodePools function to expand the node pools +// expandNodePools function to expand the node pools func expandNodePools(nodePools []interface{}) []civogo.KubernetesClusterPoolConfig { expandedNodePools := make([]civogo.KubernetesClusterPoolConfig, 0, len(nodePools)) for _, rawPool := range nodePools { @@ -145,10 +145,36 @@ func expandNodePools(nodePools []interface{}) []civogo.KubernetesClusterPoolConf poolID = pool["label"].(string) } + // Initialize labels map only if they are provided and valid + var labels map[string]string + if rawLabels, ok := pool["labels"].(map[string]interface{}); ok { + labels = make(map[string]string, len(rawLabels)) + for k, v := range rawLabels { + if strVal, ok := v.(string); ok { + labels[k] = strVal + } + } + } + + // Initialize taints slice only if they are provided and valid + var taints []corev1.Taint + if taintSet, ok := pool["taint"].(*schema.Set); ok { + for _, taintInterface := range taintSet.List() { + taintMap := taintInterface.(map[string]interface{}) + taints = append(taints, corev1.Taint{ + Key: taintMap["key"].(string), + Value: taintMap["value"].(string), + Effect: corev1.TaintEffect(taintMap["effect"].(string)), + }) + } + } + cr := civogo.KubernetesClusterPoolConfig{ - ID: poolID, - Size: pool["size"].(string), - Count: pool["node_count"].(int), + ID: poolID, + Size: pool["size"].(string), + Count: pool["node_count"].(int), + Labels: labels, + Taints: taints, } if pool["public_ip_node_pool"].(bool) { diff --git a/civo/kubernetes/resource_kubernetes_cluster.go b/civo/kubernetes/resource_kubernetes_cluster.go index 93af4b5..9cbeb79 100644 --- a/civo/kubernetes/resource_kubernetes_cluster.go +++ b/civo/kubernetes/resource_kubernetes_cluster.go @@ -194,7 +194,7 @@ func applicationSchema() *schema.Schema { func resourceKubernetesClusterCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { apiClient := m.(*civogo.Client) - // overwrite the region if is define in the datasource + // overwrite the region if is defined in the datasource if region, ok := d.GetOk("region"); ok { apiClient.Region = region.(string) } @@ -303,7 +303,7 @@ func resourceKubernetesClusterCreate(ctx context.Context, d *schema.ResourceData func resourceKubernetesClusterRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { apiClient := m.(*civogo.Client) - // overwrite the region if is define in the datasource + // overwrite the region if it is defined in the datasource if region, ok := d.GetOk("region"); ok { apiClient.Region = region.(string) } @@ -352,7 +352,7 @@ func resourceKubernetesClusterRead(_ context.Context, d *schema.ResourceData, m func resourceKubernetesClusterUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { apiClient := m.(*civogo.Client) - // overwrite the region if is define in the datasource + // overwrite the region if it is defined in the datasource if region, ok := d.GetOk("region"); ok { apiClient.Region = region.(string) } @@ -391,7 +391,7 @@ func resourceKubernetesClusterUpdate(ctx context.Context, d *schema.ResourceData targetNodePool := "" nodePools := []civogo.KubernetesClusterPoolConfig{} for _, v := range kubernetesCluster.Pools { - nodePools = append(nodePools, civogo.KubernetesClusterPoolConfig{ID: v.ID, Count: v.Count, Size: v.Size}) + nodePools = append(nodePools, civogo.KubernetesClusterPoolConfig{ID: v.ID, Count: v.Count, Size: v.Size, Labels: v.Labels, Taints: v.Taints}) if targetNodePool == "" && v.ID == newPool["label"].(string) { targetNodePool = v.ID } @@ -439,7 +439,7 @@ func resourceKubernetesClusterUpdate(ctx context.Context, d *schema.ResourceData func resourceKubernetesClusterDelete(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { apiClient := m.(*civogo.Client) - // overwrite the region if is define in the datasource + // overwrite the region if it is defined in the datasource if region, ok := d.GetOk("region"); ok { apiClient.Region = region.(string) } diff --git a/go.mod b/go.mod index 1bb078f..3c60a9c 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/civo/terraform-provider-civo require ( - github.com/civo/civogo v0.3.58 + github.com/civo/civogo v0.3.65 github.com/google/uuid v1.3.1 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/terraform-plugin-sdk/v2 v2.31.0 @@ -58,7 +58,7 @@ require ( github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect github.com/zclconf/go-cty v1.14.1 // indirect - golang.org/x/mod v0.14.0 // indirect + golang.org/x/mod v0.16.0 // indirect golang.org/x/net v0.20.0 // indirect golang.org/x/sys v0.16.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/go.sum b/go.sum index 87bcab9..f3256c9 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ github.com/civo/civogo v0.3.57 h1:KotP07TE6ZbYPmo+oOty5YAPDD8CCxLwmD8Ii10D6Ns= github.com/civo/civogo v0.3.57/go.mod h1:54lv/FOf7gh6wE9ZwVdw4yBehk8V1CvU9aWa4v6dvW0= github.com/civo/civogo v0.3.58 h1:FikCbwAKB5ovD8+NmSi7rzYBSpC4nUpGVpCyngEkTo4= github.com/civo/civogo v0.3.58/go.mod h1:54lv/FOf7gh6wE9ZwVdw4yBehk8V1CvU9aWa4v6dvW0= +github.com/civo/civogo v0.3.65 h1:FZpkaVoTxcc4lAgRbh5fo65EPDBu/0xmJsoql5xEBHk= +github.com/civo/civogo v0.3.65/go.mod h1:S/iYmGvQOraxdRtcXeq/2mVX01/ia2qfpQUp2SsTLKA= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= @@ -301,6 +303,8 @@ golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= +golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=