Skip to content

Commit

Permalink
Add k8s cluster CIDR (#221)
Browse files Browse the repository at this point in the history
* allow to set cluster_cidr in k8s rs

* make err message of cluster_cidr immutability clear

* fix cannot set cluster_cidr on k8s creation

Because HasChange = true on k8s creation

* update k8s resource doc

* clarify value of cluster cidr when it is empty
  • Loading branch information
nvthongswansea authored Mar 14, 2023
1 parent c483fb7 commit c52dc9b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
40 changes: 40 additions & 0 deletions gridscale/resource_gridscale_k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -195,6 +196,12 @@ func resourceGridscaleK8s() *schema.Resource {
Optional: true,
Default: true,
},
"cluster_cidr": {
Type: schema.TypeString,
Description: "The cluster CIDR that will be used to generate the CIDR of nodes, services, and pods. The allowed CIDR prefix length is /16. If this field is empty, the default value is \"10.244.0.0/16\"",
Optional: true,
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -302,6 +309,11 @@ func resourceGridscaleK8sRead(d *schema.ResourceData, meta interface{}) error {
"storage": props.Parameters["k8s_worker_node_storage"],
"storage_type": props.Parameters["k8s_worker_node_storage_type"],
}
// Set cluster CIDR if it is set
if _, isClusterCIDRSet := props.Parameters["k8s_cluster_cidr"]; isClusterCIDRSet {
nodePool["cluster_cidr"] = props.Parameters["k8s_cluster_cidr"]
}

// Surge node feature is enable if k8s_surge_node_count > 0
if surgeNodeCount, ok := props.Parameters["k8s_surge_node_count"].(float64); ok {
nodePool["surge_node"] = surgeNodeCount > 0
Expand Down Expand Up @@ -388,6 +400,10 @@ func resourceGridscaleK8sCreate(d *schema.ResourceData, meta interface{}) error
params["k8s_worker_node_count"] = d.Get("node_pool.0.node_count")
params["k8s_worker_node_storage"] = d.Get("node_pool.0.storage")
params["k8s_worker_node_storage_type"] = d.Get("node_pool.0.storage_type")
// Set cluster CIDR if it is set
if clusterCIDR, isClusterCIDRSet := d.GetOk("node_pool.0.cluster_cidr"); isClusterCIDRSet {
params["k8s_cluster_cidr"] = clusterCIDR
}
isSurgeNodeEnabled := d.Get("node_pool.0.surge_node").(bool)
if isSurgeNodeEnabled {
params["k8s_surge_node_count"] = 1
Expand Down Expand Up @@ -585,6 +601,30 @@ func validateK8sParameters(d *schema.ResourceDiff, template gsclient.PaaSTemplat
}
}

cluster_cidr_template, cluster_cidr_template_ok := template.Properties.ParametersSchema["k8s_cluster_cidr"]
if cluster_cidr, ok := d.GetOk("node_pool.0.cluster_cidr"); ok {
// if the template doesn't support cluster_cidr, return error if it is set
if !cluster_cidr_template_ok {
errorMessages = append(errorMessages, "The template doesn't support cluster_cidr. Please remove it from your configuration.\n")
} else {
// if the template supports cluster_cidr, validate the value
if cluster_cidr.(string) != "" {
_, _, err := net.ParseCIDR(cluster_cidr.(string))
if err != nil {
errorMessages = append(errorMessages, fmt.Sprintf("Invalid 'node_pool.0.cluster_cidr' value. Value must be a valid CIDR.\n"))
}
}
// if cluster_cidr_template is immutable, return error if it is set during k8s creation
// and it is changed during k8s update
if cluster_cidr_template.Immutable {
oldClusterCIDR, _ := d.GetChange("node_pool.0.cluster_cidr")
if oldClusterCIDR != "" && d.HasChange("node_pool.0.cluster_cidr") {
errorMessages = append(errorMessages, "Cannot change parameter cluster_cidr, because it is immutable.\n")
}
}
}
}

if len(errorMessages) != 0 {
return errors.New(strings.Join(errorMessages, ""))
}
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/k8s.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The following arguments are supported:
* `storage` - (Immutable) Storage per worker node (in GiB).
* `storage_type` - (Immutable) Storage type (one of storage, storage_high, storage_insane).
* `surge_node` - Enable surge node to avoid resources shortage during the cluster upgrade (Default: true).
* `cluster_cidr` - The cluster CIDR that will be used to generate the CIDR of nodes, services, and pods. The allowed CIDR prefix length is /16. If the cluster CIDR is not set, the cluster will use "10.244.0.0/16" as it default (even though the `cluster_cidr` in the k8s resource is empty).

## Timeouts

Expand Down Expand Up @@ -84,6 +85,7 @@ This resource exports the following attributes:
* `storage` - See Argument Reference above.
* `storage_type` - See Argument Reference above.
* `surge_node` - See Argument Reference above.
* `cluster_cidr` - See Argument Reference above.
* `usage_in_minutes` - The amount of minutes the IP address has been in use.
* `create_time` - The time the object was created.
* `change_time` - Defines the date and time of the last object change.
Expand Down

0 comments on commit c52dc9b

Please sign in to comment.