diff --git a/gridscale/resource_gridscale_k8s.go b/gridscale/resource_gridscale_k8s.go index e13ed5ad2..7a60bd960 100644 --- a/gridscale/resource_gridscale_k8s.go +++ b/gridscale/resource_gridscale_k8s.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "net" "net/http" "strings" "time" @@ -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, + }, }, }, }, @@ -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 @@ -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 @@ -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, "")) } diff --git a/website/docs/r/k8s.html.md b/website/docs/r/k8s.html.md index c4bf9896b..8e98f5ef5 100644 --- a/website/docs/r/k8s.html.md +++ b/website/docs/r/k8s.html.md @@ -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 @@ -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.