Skip to content

Commit 07a8bc4

Browse files
authored
feat: add script to check the status of the LB before attaching SG (#417)
1 parent 6657d46 commit 07a8bc4

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

.secrets.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "go.sum|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2024-02-29T19:31:09Z",
6+
"generated_at": "2024-04-22T04:36:25Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -82,7 +82,7 @@
8282
"hashed_secret": "dce1f02ca7cc4b63ac43008b7a3ce96e702a0c24",
8383
"is_secret": false,
8484
"is_verified": false,
85-
"line_number": 47,
85+
"line_number": 49,
8686
"type": "Secret Keyword",
8787
"verified_result": null
8888
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Optionally, the module supports advanced security group management for the worke
1717

1818
- Ensure that you have an up-to-date version of the [IBM Cloud CLI](https://cloud.ibm.com/docs/cli?topic=cli-getting-started).
1919
- Ensure that you have an up-to-date version of the [IBM Cloud Kubernetes service CLI](https://cloud.ibm.com/docs/containers?topic=containers-kubernetes-service-cli).
20+
- Ensure that you have an up-to-date version of the [IBM Cloud VPC Infrastructure service CLI](https://cloud.ibm.com/docs/vpc?topic=vpc-vpc-reference). Only required if providing additional security groups with the `var.additional_lb_security_group_ids`.
21+
- Ensure that you have an up-to-date version of the [jq](https://jqlang.github.io/jq)
2022

2123
<!-- Below content is automatically populated via pre-commit hook -->
2224
<!-- BEGIN OVERVIEW HOOK -->
@@ -221,6 +223,7 @@ Optionally, you need the following permissions to attach Access Management tags
221223
| [ibm_resource_tag.cos_access_tag](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_tag) | resource |
222224
| [kubernetes_config_map_v1_data.set_autoscaling](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/config_map_v1_data) | resource |
223225
| [null_resource.config_map_status](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
226+
| [null_resource.confirm_lb_active](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
224227
| [null_resource.confirm_network_healthy](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
225228
| [null_resource.reset_api_key](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
226229
| [ibm_container_addons.existing_addons](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/container_addons) | data source |

main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,21 @@ locals {
491491
lbs_associated_with_cluster = length(var.additional_lb_security_group_ids) > 0 ? [for lb in data.ibm_is_lbs.all_lbs[0].load_balancers : lb.id if strcontains(lb.name, local.cluster_id)] : []
492492
}
493493

494+
resource "null_resource" "confirm_lb_active" {
495+
count = length(var.additional_lb_security_group_ids)
496+
depends_on = [data.ibm_is_lbs.all_lbs]
497+
498+
provisioner "local-exec" {
499+
command = "${path.module}/scripts/confirm_lb_active.sh ${var.region} ${var.resource_group_id} ${local.lbs_associated_with_cluster[count.index]}"
500+
interpreter = ["/bin/bash", "-c"]
501+
environment = {
502+
IBMCLOUD_API_KEY = var.ibmcloud_api_key
503+
}
504+
}
505+
}
506+
494507
module "attach_sg_to_lb" {
508+
depends_on = [null_resource.confirm_lb_active]
495509
count = length(var.additional_lb_security_group_ids)
496510
source = "terraform-ibm-modules/security-group/ibm"
497511
version = "2.6.1"

scripts/confirm_lb_active.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
REGION="$1"
6+
RESOURCE_GROUP_ID="$2"
7+
LB_ID="$3"
8+
9+
# Expects the environment variable $IBMCLOUD_API_KEY to be set
10+
if [[ -z "${IBMCLOUD_API_KEY}" ]]; then
11+
echo "API key must be set with IBMCLOUD_API_KEY environment variable" >&2
12+
exit 1
13+
fi
14+
15+
if [[ -z "${REGION}" ]]; then
16+
echo "Region must be passed as first input script argument" >&2
17+
exit 1
18+
fi
19+
20+
if [[ -z "${RESOURCE_GROUP_ID}" ]]; then
21+
echo "Resource_group_id must be passed as second input script argument" >&2
22+
exit 1
23+
fi
24+
25+
# Login to ibmcloud with cli
26+
attempts=1
27+
until ibmcloud login -q -r "${REGION}" -g "${RESOURCE_GROUP_ID}" || [ $attempts -ge 3 ]; do
28+
attempts=$((attempts + 1))
29+
echo "Error logging in to IBM Cloud CLI..." >&2
30+
sleep 5
31+
done
32+
33+
lb_attempts=1
34+
while true; do
35+
status=$(ibmcloud is load-balancer "$LB_ID" --output json | jq -r .provisioning_status)
36+
echo "Load balancer status: $status"
37+
if [[ "$status" == "active" ]]; then
38+
break
39+
else
40+
lb_attempts=$((lb_attempts + 1))
41+
if [ $lb_attempts -ge 10 ]; then
42+
echo "Load balancer status: $status"
43+
break
44+
fi
45+
echo "Sleeping for 30 secs.."
46+
sleep 30
47+
fi
48+
status=""
49+
done

0 commit comments

Comments
 (0)