Skip to content

Commit

Permalink
Improve subnet CIDR calculation (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsgaspar authored Sep 7, 2023
1 parent 0eb5f7f commit 5304503
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 13 deletions.
2 changes: 2 additions & 0 deletions modules/terraform-cdp-aws-pre-reqs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module "aws_cdp_vpc" {
env_prefix = var.env_prefix
tags = local.env_tags

private_cidr_range = var.private_cidr_range
public_cidr_range = var.public_cidr_range
}

# ------- Security Groups -------
Expand Down
6 changes: 6 additions & 0 deletions modules/terraform-cdp-aws-pre-reqs/modules/vpc/defaults.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ locals {
public = (var.deployment_template == "private") ? (var.private_network_extensions ? 1 : 0) : length(local.zones_in_region)
private = (var.deployment_template == "public") ? 0 : length(local.zones_in_region)
}

# Extract the VPC CIDR range from the user-provided CIDR
vpc_cidr_range = split("/", var.vpc_cidr)[1]

# Calculate the first suitable CIDR range for public subnets after private subnets have been allocated (normalize the offset, expressed as a multiplier of public subnet ranges)
public_subnet_offset = ceil(local.subnets_required.private * pow(2, 32 - var.private_cidr_range) / pow(2, 32 - var.public_cidr_range))
}
4 changes: 2 additions & 2 deletions modules/terraform-cdp-aws-pre-reqs/modules/vpc/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module "cdp_vpc" {
private_subnets = (local.subnets_required.private == 0 ?
[] :
[
for i in range(local.subnets_required.private) : cidrsubnet(var.vpc_cidr, ceil(log(local.subnets_required.total, 2)), local.subnets_required.public + i)
for i in range(local.subnets_required.private) : cidrsubnet(var.vpc_cidr, var.private_cidr_range - local.vpc_cidr_range, i)
]
)
private_subnet_tags = {
Expand All @@ -33,7 +33,7 @@ module "cdp_vpc" {
public_subnets = (local.subnets_required.public == 0 ?
[] :
[
for i in range(local.subnets_required.public) : cidrsubnet(var.vpc_cidr, ceil(log(local.subnets_required.total, 2)), i)
for i in range(local.subnets_required.public) : cidrsubnet(var.vpc_cidr, var.public_cidr_range - local.vpc_cidr_range, i + local.public_subnet_offset)
]
)

Expand Down
14 changes: 13 additions & 1 deletion modules/terraform-cdp-aws-pre-reqs/modules/vpc/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ variable "vpc_cidr" {

}

variable "private_cidr_range" {
type = number
description = "Size of each private subnet"
}

variable "public_cidr_range" {
type = number
description = "Size of each public subnet"
}

variable "tags" {
type = map(any)
description = "Tags applied to provised resources"
Expand Down Expand Up @@ -45,4 +55,6 @@ variable "private_network_extensions" {

description = "Enable creation of resources for connectivity to CDP Control Plane (public subnet and NAT Gateway) for Private Deployment. Only relevant for private deployment template."

}
}


16 changes: 15 additions & 1 deletion modules/terraform-cdp-aws-pre-reqs/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,25 @@ variable "create_vpc" {

variable "vpc_cidr" {
type = string
description = "VPC CIDR Block"
description = "VPC CIDR Block. Required if create_vpc is true."

default = "10.10.0.0/16"
}

variable "private_cidr_range" {
type = number
description = "Size of each private subnet. Required if create_vpc is true. Number of subnets will be automatically selected to match on the number of Availability Zones in the selected AWS region. (Depending on the selected deployment pattern, one subnet will be created per region.)"

default = 19
}

variable "public_cidr_range" {
type = number
description = "Size of each public subnet. Required if create_vpc is true. Number of subnets will be automatically selected to match on the number of Availability Zones in the selected AWS region. (Depending on the selected deployment pattern, one subnet will be created per region.)"

default = 24
}

variable "private_network_extensions" {
type = bool

Expand Down
4 changes: 4 additions & 0 deletions modules/terraform-cdp-azure-pre-reqs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ module "azure_cdp_vnet" {

env_prefix = var.env_prefix
tags = local.env_tags

cdp_subnet_range = var.cdp_subnet_range
gateway_subnet_range = var.gateway_subnet_range

}


Expand Down
11 changes: 9 additions & 2 deletions modules/terraform-cdp-azure-pre-reqs/modules/vnet/defaults.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ locals {
# cidr = cidrsubnet(var.vnet_cidr, ceil(log(var.subnet_count, 2)), idx)
}

# Extract the VNet CIDR range from the user-provided CIDR
vnet_cidr_range = split("/", var.vnet_cidr)[1]

# Calculate the first suitable CIDR range for public subnets after private subnets have been allocated (normalize the offset, expressed as a multiplier of gateway subnet ranges)
gateway_subnet_offset = ceil(local.subnets_required.cdp_subnets * pow(2, 32 - var.cdp_subnet_range) / pow(2, 32 - var.gateway_subnet_range))


# Network infrastructure for CDP resources
cdp_subnets = [
for idx in range(local.subnets_required.cdp_subnets) :
{
name = "${var.env_prefix}-sbnt-${format("%02d", idx + 1)}"
cidr = cidrsubnet(var.vnet_cidr, ceil(log(local.subnets_required.total, 2)), idx)
cidr = cidrsubnet(var.vnet_cidr, var.cdp_subnet_range - local.vnet_cidr_range, idx)
}
]

Expand All @@ -37,7 +44,7 @@ locals {
for idx in range(local.subnets_required.gateway_subnets) :
{
name = "${var.env_prefix}-gw-sbnt-${format("%02d", idx + 1)}"
cidr = cidrsubnet(var.vnet_cidr, ceil(log(local.subnets_required.total, 2)), local.subnets_required.cdp_subnets + idx)
cidr = cidrsubnet(var.vnet_cidr, var.gateway_subnet_range - local.vnet_cidr_range, idx + local.gateway_subnet_offset)
}
]

Expand Down
12 changes: 12 additions & 0 deletions modules/terraform-cdp-azure-pre-reqs/modules/vnet/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ variable "vnet_cidr" {

}

variable "cdp_subnet_range" {
type = number
description = "Size of each (internal) cluster subnet"

}

variable "gateway_subnet_range" {
type = number
description = "Size of each gateway subnet"

}

variable "vnet_region" {
type = string
description = "Region which VNet will be created"
Expand Down
22 changes: 15 additions & 7 deletions modules/terraform-cdp-azure-pre-reqs/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ variable "env_prefix" {
description = "Shorthand name for the environment. Used in resource descriptions"
}

# variable "public_key_text" {
# type = string

# description = "SSH Public key string for the nodes of the CDP environment"
# }
# ------- CDP Environment Deployment -------
variable "deployment_template" {
type = string

Expand Down Expand Up @@ -89,11 +83,25 @@ variable "vnet_name" {

variable "vnet_cidr" {
type = string
description = "VNet CIDR Block"
description = "VNet CIDR Block. Required if create_vpc is true."

default = "10.10.0.0/16"
}

variable "cdp_subnet_range" {
type = number
description = "Size of each (internal) cluster subnet. Required if create_vpc is true."

default = 19
}

variable "gateway_subnet_range" {
type = number
description = "Size of each gateway subnet. Required if create_vpc is true."

default = 24
}

variable "cdp_resourcegroup_name" {
type = string
description = "Pre-existing Resource Group for CDP environment. Required if create_vnet is false."
Expand Down

0 comments on commit 5304503

Please sign in to comment.