Skip to content

Commit 7cead90

Browse files
authored
fix: added a script that will ensure the "iks-ca-configmap" exists before enabling auto scaling (#356)
1 parent b56a6e1 commit 7cead90

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ Optionally, you need the following permissions to attach Access Management tags
195195
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | >= 1.62.0, < 2.0.0 |
196196
| <a name="requirement_kubernetes"></a> [kubernetes](#requirement\_kubernetes) | >= 2.16.1 |
197197
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 3.2.1 |
198-
| <a name="requirement_time"></a> [time](#requirement\_time) | >= 0.9.1 |
199198
200199
### Modules
201200
@@ -219,9 +218,9 @@ Optionally, you need the following permissions to attach Access Management tags
219218
| [ibm_resource_tag.cluster_access_tag](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_tag) | resource |
220219
| [ibm_resource_tag.cos_access_tag](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_tag) | resource |
221220
| [kubernetes_config_map_v1_data.set_autoscaling](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/config_map_v1_data) | resource |
221+
| [null_resource.config_map_status](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
222222
| [null_resource.confirm_network_healthy](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
223223
| [null_resource.reset_api_key](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
224-
| [time_sleep.wait_operators](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) | resource |
225224
| [ibm_container_addons.existing_addons](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/container_addons) | data source |
226225
| [ibm_container_cluster_config.cluster_config](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/container_cluster_config) | data source |
227226
| [ibm_container_cluster_versions.cluster_versions](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/container_cluster_versions) | data source |

main.tf

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ resource "ibm_container_vpc_cluster" "cluster" {
151151

152152
# copy of the cluster resource above which ignores changes to the worker pool for use in autoscaling scenarios
153153
resource "ibm_container_vpc_cluster" "autoscaling_cluster" {
154+
depends_on = [null_resource.reset_api_key]
154155
count = var.ignore_worker_pool_size_changes ? 1 : 0
155156
name = var.cluster_name
156157
vpc_id = var.vpc_id
@@ -253,7 +254,7 @@ resource "null_resource" "reset_api_key" {
253254
##############################################################################
254255

255256
data "ibm_container_cluster_config" "cluster_config" {
256-
count = var.verify_worker_network_readiness ? 1 : 0
257+
count = var.verify_worker_network_readiness || lookup(local.addons_list, "cluster-autoscaler", null) != null ? 1 : 0
257258
cluster_name_id = local.cluster_id
258259
config_dir = "${path.module}/kubeconfig"
259260
resource_group_id = var.resource_group_id
@@ -373,7 +374,7 @@ resource "null_resource" "confirm_network_healthy" {
373374
# Worker pool creation can start before the 'ibm_container_vpc_cluster' completes since there is no explicit
374375
# depends_on in 'ibm_container_vpc_worker_pool', just an implicit depends_on on the cluster ID. Cluster ID can exist before
375376
# 'ibm_container_vpc_cluster' completes, so hence need to add explicit depends on against 'ibm_container_vpc_cluster' here.
376-
depends_on = [ibm_container_vpc_cluster.cluster, ibm_container_vpc_worker_pool.pool, ibm_container_vpc_worker_pool.autoscaling_pool]
377+
depends_on = [ibm_container_vpc_cluster.cluster, ibm_container_vpc_cluster.autoscaling_cluster, ibm_container_vpc_worker_pool.pool, ibm_container_vpc_worker_pool.autoscaling_pool]
377378

378379
provisioner "local-exec" {
379380
command = "${path.module}/scripts/confirm_network_healthy.sh"
@@ -394,7 +395,7 @@ resource "ibm_container_addons" "addons" {
394395
# Worker pool creation can start before the 'ibm_container_vpc_cluster' completes since there is no explicit
395396
# depends_on in 'ibm_container_vpc_worker_pool', just an implicit depends_on on the cluster ID. Cluster ID can exist before
396397
# 'ibm_container_vpc_cluster' completes, so hence need to add explicit depends on against 'ibm_container_vpc_cluster' here.
397-
depends_on = [ibm_container_vpc_cluster.cluster, ibm_container_vpc_worker_pool.pool, ibm_container_vpc_worker_pool.autoscaling_pool, null_resource.confirm_network_healthy]
398+
depends_on = [ibm_container_vpc_cluster.cluster, ibm_container_vpc_cluster.autoscaling_cluster, ibm_container_vpc_worker_pool.pool, ibm_container_vpc_worker_pool.autoscaling_pool, null_resource.confirm_network_healthy]
398399

399400
cluster = local.cluster_id
400401
resource_group_id = var.resource_group_id
@@ -415,11 +416,6 @@ resource "ibm_container_addons" "addons" {
415416
}
416417
}
417418

418-
resource "time_sleep" "wait_operators" {
419-
depends_on = [ibm_container_addons.addons]
420-
create_duration = "5s"
421-
}
422-
423419
locals {
424420
worker_pool_config = [
425421
for worker in var.worker_pools :
@@ -433,9 +429,22 @@ locals {
433429

434430
}
435431

432+
resource "null_resource" "config_map_status" {
433+
count = lookup(local.addons_list, "cluster-autoscaler", null) != null ? 1 : 0
434+
depends_on = [ibm_container_addons.addons]
435+
436+
provisioner "local-exec" {
437+
command = "${path.module}/scripts/get_config_map_status.sh"
438+
interpreter = ["/bin/bash", "-c"]
439+
environment = {
440+
KUBECONFIG = data.ibm_container_cluster_config.cluster_config[0].config_file_path
441+
}
442+
}
443+
}
444+
436445
resource "kubernetes_config_map_v1_data" "set_autoscaling" {
437-
count = !(var.disable_public_endpoint) && lookup(local.addons_list, "cluster-autoscaler", null) != null ? 1 : 0
438-
depends_on = [time_sleep.wait_operators]
446+
count = lookup(local.addons_list, "cluster-autoscaler", null) != null ? 1 : 0
447+
depends_on = [null_resource.config_map_status]
439448

440449
metadata {
441450
name = "iks-ca-configmap"

scripts/get_config_map_status.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
CONFIGMAP_NAME="iks-ca-configmap"
6+
NAMESPACE="kube-system"
7+
COUNTER=0
8+
MAX_ATTEMPTS=40
9+
10+
while [[ $COUNTER -lt $MAX_ATTEMPTS ]] && ! kubectl get configmap $CONFIGMAP_NAME -n $NAMESPACE &>/dev/null; do
11+
COUNTER=$((COUNTER + 1))
12+
echo "Attempt $COUNTER: ConfigMap '$CONFIGMAP_NAME' not found in namespace '$NAMESPACE', retrying..."
13+
sleep 30
14+
done
15+
16+
if [[ $COUNTER -eq $MAX_ATTEMPTS ]]; then
17+
echo "ConfigMap '$CONFIGMAP_NAME' did not become available within $MAX_ATTEMPTS attempts."
18+
exit 1
19+
else
20+
echo "ConfigMap '$CONFIGMAP_NAME' is now available." >&2
21+
fi

scripts/reset_iks_api_key.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ done
3333
# run api-key reset command if apikey for given region + resource group does not already exist
3434
reset=true
3535
key_descriptions=()
36-
while IFS='' read -r line; do key_descriptions+=("$line"); done < <(ibmcloud iam api-keys --all --output json | jq -r --arg name "${APIKEY_KEY_NAME}"'.[] | select(.name == $name) | .description')
36+
while IFS='' read -r line; do key_descriptions+=("$line"); done < <(ibmcloud iam api-keys --all --output json | jq -r --arg name "${APIKEY_KEY_NAME}" '.[] | select(.name == $name) | .description')
3737
for i in "${key_descriptions[@]}"; do
3838
if [[ "$i" =~ ${REGION} ]] && [[ "$i" =~ ${RESOURCE_GROUP_ID} ]]; then
3939
echo "Found key named ${APIKEY_KEY_NAME} which covers clusters in ${REGION} and resource group ID ${RESOURCE_GROUP_ID}"

version.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ terraform {
1010
source = "hashicorp/null"
1111
version = ">= 3.2.1"
1212
}
13-
time = {
14-
source = "hashicorp/time"
15-
version = ">= 0.9.1"
16-
}
1713
kubernetes = {
1814
source = "hashicorp/kubernetes"
1915
version = ">= 2.16.1"

0 commit comments

Comments
 (0)