Skip to content

Commit

Permalink
OpenShiftP-247: add loadbalancer logic into terraform
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Bastide <pbastide@us.ibm.com>
  • Loading branch information
prb112 committed Jan 31, 2025
1 parent 6959b99 commit 6df1eb8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Local .terraform directories
_debug
**/.terraform/*
_scratch

Expand Down
52 changes: 52 additions & 0 deletions modules/1_vpc_support/find_lbs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
################################################################
# Copyright 2025 - IBM Corporation. All rights reserved
# SPDX-License-Identifier: Apache-2.0
################################################################

# ibm_is_lbs - https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/is_lb#private_ip-6
# in the resource provider that uses this data, we must add `lifecycle { ignore_changes = all }`

data "ibm_resource_group" "group" {
name = data.ibm_is_vpc.vpc.resource_group_name
}

# only get the subnets in the vpc
data "ibm_is_subnets" "subnets" {
vpc = data.ibm_is_vpc.vpc.id
resource_group = data.ibm_resource_group.group.id
}

# gets all lbs in the region
data "ibm_is_lbs" "lbs" {
// Empty
}

locals {
subnets_used_in_vpc = [for sn in data.ibm_is_subnets.subnets.subnets: sn.id]

// Load Balancer Id
subnets_from_lbs_with_ips = flatten([
for lb in data.ibm_is_lbs.lbs.load_balancers:
flatten([
for sn in lb.subnets[*]:
flatten([
for snx in data.ibm_is_subnets.subnets.subnets:
sn.id == snx.id && snx.vpc == data.ibm_is_vpc.vpc.id && length(lb.private_ip) > 0 ?
[{
id = lb.id
lb_name = lb.name
sn = sn
vpc = data.ibm_is_vpc.vpc.id
private_ip = lb.private_ip[*].address
}]
: []
])
])
])

load_balancer_ips = distinct(flatten([for slip in local.subnets_from_lbs_with_ips:
!strcontains(slip.lb_name, "-api-")?
flatten(
[for pip in slip.private_ip: pip]):
[]]))
}
5 changes: 5 additions & 0 deletions modules/1_vpc_support/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ output "transit_gateway_name" {
output "transit_gateway_status" {
value = !var.setup_transit_gateway ? module.existing_gateway[0].existing_tg_status : module.transit_gateway[0].new_tg_status
}

// Highly unlikely this is empty or zero, best to throw an error
output "load_balancer_ips" {
value = load_balancer_ips[0]
}

0 comments on commit 6df1eb8

Please sign in to comment.