Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sort subnets to calculate by their netmask to efficiently use ip space #130

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion modules/calculate_subnets/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ locals {
}
]])

# map of subnet names to netmask values for looking up netmask by name
netmasks_for_subnets = { for subnet in local.calculated_subnet_objects : subnet.name => subnet.netmask }

# sorted list of netmasks from largest to smallest so we can efficiently use the ip address space
sorted_subnet_netmasks = reverse(distinct(sort([
for subnet in local.calculated_subnet_objects : format("%05d", subnet.netmask)
])))

# list of subnet names sorted based on their netmask value (large to small)
sorted_subnet_names = compact(flatten([
for netmask in local.sorted_subnet_netmasks : [
for subnet in local.calculated_subnet_objects :
subnet.name if subnet.netmask == tonumber(netmask)
]
]))

# list of subnet the original calculated subnet objects, but sorted based on their netmask value (large to small)
sorted_subnet_objects = [
for name in local.sorted_subnet_names : {
name = name
netmask = local.netmasks_for_subnets[name]
}
]

# map of explicit cidrs to az
explicit_cidrs_grouped = { for _, type in local.types_with_explicit : type => zipmap(var.azs, var.subnets[type].cidrs[*]) }
}
Expand All @@ -27,6 +51,6 @@ module "subnet_calculator" {
version = "1.0.2"

base_cidr_block = var.cidr
networks = local.calculated_subnet_objects
networks = local.sorted_subnet_objects
}