Skip to content

Commit

Permalink
Add Labels and Taints to Node Pool configurations (#198)
Browse files Browse the repository at this point in the history
* Add Labels and Taints
  • Loading branch information
uzaxirr committed Mar 19, 2024
1 parent 2a1d762 commit cd13982
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 52 deletions.
48 changes: 8 additions & 40 deletions civo/kubernetes/datasource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
Expand Down
36 changes: 31 additions & 5 deletions civo/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions civo/kubernetes/resource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit cd13982

Please sign in to comment.