Skip to content

Commit b10dd9f

Browse files
authored
feat: enable autoscaling on a given cluster (#138)
* feat: enable autoscaling on a given cluster * fix: pre commit failures * fix: add lifecycle to ignore addons * fix: add validation for cluster_autoscaler_version * fix: update the example * fix: update examples * fix: get addon list from user * fix: remove autoscaling example and update logic * docs: update docs * fix: doc update
1 parent ec13c1c commit b10dd9f

File tree

9 files changed

+235
-34
lines changed

9 files changed

+235
-34
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ A module for provisioning an IBM Cloud Red Hat OpenShift cluster on VPC Gen2. Th
1616
- Make sure that you have a recent version of the [IBM Cloud CLI](https://cloud.ibm.com/docs/cli?topic=cli-getting-started)
1717
- Make sure that you have a recent version of the [IBM Cloud Kubernetes service CLI](https://cloud.ibm.com/docs/containers?topic=containers-kubernetes-service-cli)
1818

19-
20-
2119
## Usage
2220
```hcl
2321
# Replace "master" with a GIT release version to lock into a specific release
@@ -125,7 +123,7 @@ Optionally, you need the following permissions to attach Access Management tags
125123
- [ Apply Taints Example](examples/apply_taints)
126124
- [ Existing COS](examples/existing_cos)
127125
- [ 2 MZR clusters in same VPC](examples/multiple_mzr_clusters)
128-
- [ Single Zone Cluster](examples/single_zone_cluster)
126+
- [ Single zone cluster example](examples/single_zone_cluster)
129127
- [ Standard Example With User Managed Boot Volume Encryption](examples/standard)
130128
<!-- END EXAMPLES HOOK -->
131129

@@ -136,7 +134,9 @@ Optionally, you need the following permissions to attach Access Management tags
136134
|------|---------|
137135
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
138136
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | >= 1.51.0 |
137+
| <a name="requirement_kubernetes"></a> [kubernetes](#requirement\_kubernetes) | 2.16.1 |
139138
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 3.2.1 |
139+
| <a name="requirement_time"></a> [time](#requirement\_time) | 0.9.1 |
140140

141141
## Modules
142142

@@ -148,14 +148,17 @@ Optionally, you need the following permissions to attach Access Management tags
148148

149149
| Name | Type |
150150
|------|------|
151+
| [ibm_container_addons.addons](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/container_addons) | resource |
151152
| [ibm_container_vpc_cluster.autoscaling_cluster](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/container_vpc_cluster) | resource |
152153
| [ibm_container_vpc_cluster.cluster](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/container_vpc_cluster) | resource |
153154
| [ibm_container_vpc_worker_pool.autoscaling_pool](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/container_vpc_worker_pool) | resource |
154155
| [ibm_container_vpc_worker_pool.pool](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/container_vpc_worker_pool) | resource |
155156
| [ibm_resource_tag.cluster_access_tag](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_tag) | resource |
156157
| [ibm_resource_tag.cos_access_tag](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_tag) | resource |
158+
| [kubernetes_config_map_v1_data.set_autoscaling](https://registry.terraform.io/providers/hashicorp/kubernetes/2.16.1/docs/resources/config_map_v1_data) | resource |
157159
| [null_resource.confirm_network_healthy](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
158160
| [null_resource.reset_api_key](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
161+
| [time_sleep.wait_operators](https://registry.terraform.io/providers/hashicorp/time/0.9.1/docs/resources/sleep) | resource |
159162
| [ibm_container_cluster_config.cluster_config](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/container_cluster_config) | data source |
160163
| [ibm_container_cluster_versions.cluster_versions](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/container_cluster_versions) | data source |
161164

@@ -164,6 +167,7 @@ Optionally, you need the following permissions to attach Access Management tags
164167
| Name | Description | Type | Default | Required |
165168
|------|-------------|------|---------|:--------:|
166169
| <a name="input_access_tags"></a> [access\_tags](#input\_access\_tags) | A list of access tags to apply to the resources created by the module, see https://cloud.ibm.com/docs/account?topic=account-access-tags-tutorial for more details | `list(string)` | `[]` | no |
170+
| <a name="input_addons"></a> [addons](#input\_addons) | List of all addons supported by the ocp cluster. | <pre>object({<br> alb-oauth-proxy = optional(string)<br> debug-tool = optional(string)<br> image-key-synchronizer = optional(string)<br> istio = optional(string)<br> openshift-data-foundation = optional(string)<br> static-route = optional(string)<br> cluster-autoscaler = optional(string)<br> vpc-block-csi-driver = optional(string)<br> })</pre> | `null` | no |
167171
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | The name that will be assigned to the provisioned cluster | `string` | n/a | yes |
168172
| <a name="input_cluster_ready_when"></a> [cluster\_ready\_when](#input\_cluster\_ready\_when) | The cluster is ready when one of the following: MasterNodeReady (not recommended), OneWorkerNodeReady, Normal, IngressReady | `string` | `"IngressReady"` | no |
169173
| <a name="input_cos_name"></a> [cos\_name](#input\_cos\_name) | Name of the COS instance to provision. New instance only provisioned if `use_existing_cos = false`. Default: `<cluster_name>_cos` | `string` | `null` | no |
@@ -182,7 +186,7 @@ Optionally, you need the following permissions to attach Access Management tags
182186
| <a name="input_verify_worker_network_readiness"></a> [verify\_worker\_network\_readiness](#input\_verify\_worker\_network\_readiness) | By setting this to true, a script will run kubectl commands to verify that all worker nodes can communicate successfully with the master. If the runtime does not have access to the kube cluster to run kubectl commands, this should be set to false. | `bool` | `true` | no |
183187
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | Id of the VPC instance where this cluster will be provisioned | `string` | n/a | yes |
184188
| <a name="input_vpc_subnets"></a> [vpc\_subnets](#input\_vpc\_subnets) | Metadata that describes the VPC's subnets. Obtain this information from the VPC where this cluster will be created | <pre>map(list(object({<br> id = string<br> zone = string<br> cidr_block = string<br> })))</pre> | n/a | yes |
185-
| <a name="input_worker_pools"></a> [worker\_pools](#input\_worker\_pools) | List of worker pools | <pre>list(object({<br> subnet_prefix = optional(string)<br> vpc_subnets = optional(list(object({<br> id = string<br> zone = string<br> cidr_block = string<br> })))<br> pool_name = string<br> machine_type = string<br> workers_per_zone = number<br> resource_group_id = optional(string)<br> labels = optional(map(string))<br> boot_volume_encryption_kms_config = optional(object({<br> crk = string<br> kms_instance_id = string<br> kms_account_id = optional(string)<br> }))<br> }))</pre> | n/a | yes |
189+
| <a name="input_worker_pools"></a> [worker\_pools](#input\_worker\_pools) | List of worker pools | <pre>list(object({<br> subnet_prefix = optional(string)<br> vpc_subnets = optional(list(object({<br> id = string<br> zone = string<br> cidr_block = string<br> })))<br> pool_name = string<br> machine_type = string<br> workers_per_zone = number<br> resource_group_id = optional(string)<br> labels = optional(map(string))<br> minSize = optional(number)<br> maxSize = optional(number)<br> enableAutoscaling = optional(bool)<br> boot_volume_encryption_kms_config = optional(object({<br> crk = string<br> kms_instance_id = string<br> kms_account_id = optional(string)<br> }))<br> }))</pre> | n/a | yes |
186190
| <a name="input_worker_pools_taints"></a> [worker\_pools\_taints](#input\_worker\_pools\_taints) | Optional, Map of lists containing node taints by node-pool name | `map(list(object({ key = string, value = string, effect = string })))` | `null` | no |
187191

188192
## Outputs
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Single Zone Cluster
1+
# Single zone cluster example
22

3-
This is an example of creating a single zone VPC Gen2 OpenShift cluster
3+
This example creates a VPC with a Red Hat OpenShift cluster in a single zone. The example includes autoscaling for the cluster.

examples/single_zone_cluster/main.tf

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ module "vpc" {
2929
# Base OCP
3030
###############################################################################
3131
locals {
32+
addons = {
33+
"cluster-autoscaler" = "1.0.8"
34+
}
3235

3336
cluster_vpc_subnets = {
3437
default = module.vpc.subnet_detail_map.zone-1
@@ -39,7 +42,26 @@ locals {
3942
pool_name = "default" # ibm_container_vpc_cluster automatically names default pool "default" (See https://github.com/IBM-Cloud/terraform-provider-ibm/issues/2849)
4043
machine_type = "bx2.4x16"
4144
workers_per_zone = 2
42-
}]
45+
},
46+
{
47+
subnet_prefix = "default"
48+
pool_name = "logging"
49+
machine_type = "bx2.4x16"
50+
workers_per_zone = 2
51+
minSize = 1
52+
maxSize = 6
53+
enableAutoscaling = true
54+
},
55+
{
56+
subnet_prefix = "default"
57+
pool_name = "sample"
58+
machine_type = "bx2.4x16"
59+
workers_per_zone = 4
60+
minSize = 1
61+
maxSize = 6
62+
enableAutoscaling = true
63+
}
64+
]
4365
}
4466

4567
module "ocp_base" {
@@ -54,6 +76,13 @@ module "ocp_base" {
5476
ocp_version = var.ocp_version
5577
tags = var.resource_tags
5678
worker_pools = local.sz_pool
79+
addons = local.addons
80+
}
81+
82+
data "ibm_container_cluster_config" "cluster_config" {
83+
cluster_name_id = module.ocp_base.cluster_id
84+
resource_group_id = module.ocp_base.resource_group_id
85+
5786
}
5887

5988
##############################################################################

examples/single_zone_cluster/provider.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ provider "ibm" {
99
}
1010

1111
##############################################################################
12+
13+
provider "kubernetes" {
14+
host = data.ibm_container_cluster_config.cluster_config.host
15+
token = data.ibm_container_cluster_config.cluster_config.token
16+
cluster_ca_certificate = data.ibm_container_cluster_config.cluster_config.ca_certificate
17+
}

examples/single_zone_cluster/version.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ terraform {
66
source = "ibm-cloud/ibm"
77
version = "1.51.0"
88
}
9+
kubernetes = {
10+
source = "hashicorp/kubernetes"
11+
version = ">= 2.16.1"
12+
}
913
}
1014
}
1115

main.tf

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ locals {
2424
# tflint-ignore: terraform_unused_declarations
2525
validate_check = regex("^${local.validate_msg}$", (!local.validate_condition ? local.validate_msg : ""))
2626

27+
addons_list = var.addons != null ? { for k, v in var.addons : k => v if v != null } : {}
28+
addons = lookup(local.addons_list, "vpc-block-csi-driver", null) == null ? merge(local.addons_list, { vpc-block-csi-driver = "5.0" }) : local.addons_list
29+
2730
delete_timeout = "2h"
2831
create_timeout = "3h"
2932
update_timeout = "3h"
@@ -352,3 +355,51 @@ resource "null_resource" "confirm_network_healthy" {
352355
}
353356
}
354357
}
358+
359+
360+
resource "ibm_container_addons" "addons" {
361+
cluster = local.cluster_id
362+
resource_group_id = var.resource_group_id
363+
364+
dynamic "addons" {
365+
for_each = local.addons
366+
content {
367+
name = addons.key
368+
version = addons.value
369+
}
370+
}
371+
}
372+
373+
resource "time_sleep" "wait_operators" {
374+
depends_on = [ibm_container_addons.addons]
375+
create_duration = "5s"
376+
}
377+
378+
locals {
379+
worker_pool_config = [
380+
for worker in var.worker_pools :
381+
{
382+
name = worker.pool_name
383+
minSize = worker.minSize
384+
maxSize = worker.maxSize
385+
enabled = worker.enableAutoscaling
386+
} if worker.enableAutoscaling != null && worker.minSize != null && worker.maxSize != null
387+
]
388+
389+
}
390+
391+
resource "kubernetes_config_map_v1_data" "set_autoscaling" {
392+
count = !(var.disable_public_endpoint) && lookup(local.addons_list, "cluster-autoscaler", null) != null ? 1 : 0
393+
depends_on = [time_sleep.wait_operators]
394+
395+
metadata {
396+
name = "iks-ca-configmap"
397+
namespace = "kube-system"
398+
}
399+
400+
data = {
401+
"workerPoolsConfig.json" = jsonencode(local.worker_pool_config)
402+
}
403+
404+
force = true
405+
}

0 commit comments

Comments
 (0)