diff --git a/agents.tf b/agents.tf index 1cda0ea..4211d81 100644 --- a/agents.tf +++ b/agents.tf @@ -12,11 +12,9 @@ module "agents" { placement_group_id = var.placement_group_disable ? 0 : element(hcloud_placement_group.agent.*.id, ceil(each.value.index / 10)) location = each.value.location server_type = each.value.server_type - ipv4_subnet_id = hcloud_network_subnet.subnet[[for i, v in var.agent_nodepools : i if v.name == each.value.nodepool_name][0] + length(var.control_plane_nodepools) + 1].id + ipv4_subnet_id = hcloud_network_subnet.agent[[for i, v in var.agent_nodepools : i if v.name == each.value.nodepool_name][0] + 1].id - # We leave some room so 100 eventual Hetzner LBs that can be created perfectly safely - # It leaves the subnet with 254 x 254 - 100 = 64416 IPs to use, so probably enough. - private_ipv4 = cidrhost(local.network_ipv4_subnets[[for i, v in var.agent_nodepools : i if v.name == each.value.nodepool_name][0] + length(var.control_plane_nodepools) + 1], each.value.index + 101) + private_ipv4 = cidrhost(hcloud_network_subnet.agent[[for i, v in var.agent_nodepools : i if v.name == each.value.nodepool_name][0] + 1].ip_range, each.value.index + 101) labels = { "provisioner" = "terraform", diff --git a/control_planes.tf b/control_planes.tf index d73d6c8..d3bc0f3 100644 --- a/control_planes.tf +++ b/control_planes.tf @@ -12,11 +12,11 @@ module "control_planes" { placement_group_id = var.placement_group_disable ? 0 : element(hcloud_placement_group.control_plane.*.id, ceil(each.value.index / 10)) location = each.value.location server_type = each.value.server_type - ipv4_subnet_id = hcloud_network_subnet.subnet[[for i, v in var.control_plane_nodepools : i if v.name == each.value.nodepool_name][0] + 1].id + ipv4_subnet_id = hcloud_network_subnet.control_plane[[for i, v in var.control_plane_nodepools : i if v.name == each.value.nodepool_name][0]].id # We leave some room so 100 eventual Hetzner LBs that can be created perfectly safely # It leaves the subnet with 254 x 254 - 100 = 64416 IPs to use, so probably enough. - private_ipv4 = cidrhost(local.network_ipv4_subnets[[for i, v in var.control_plane_nodepools : i if v.name == each.value.nodepool_name][0] + 1], each.value.index + 101) + private_ipv4 = cidrhost(hcloud_network_subnet.control_plane[[for i, v in var.control_plane_nodepools : i if v.name == each.value.nodepool_name][0]].ip_range, each.value.index + 101) labels = { "provisioner" = "terraform", diff --git a/locals.tf b/locals.tf index addbbcb..c71cd25 100644 --- a/locals.tf +++ b/locals.tf @@ -205,7 +205,7 @@ locals { # The first two subnets are respectively the default subnet 10.0.0.0/16 use for potientially anything and 10.1.0.0/16 used for control plane nodes. # the rest of the subnets are for agent nodes in each nodepools. - network_ipv4_subnets = [for index in range(length(var.control_plane_nodepools) + length(var.agent_nodepools) + 1) : cidrsubnet(local.network_ipv4_cidr, 8, index)] + network_ipv4_subnets = [for index in range(256) : cidrsubnet(local.network_ipv4_cidr, 8, index)] # disable k3s extras disable_extras = concat(["local-storage"], local.is_single_node_cluster ? [] : ["servicelb"], var.traefik_enabled ? [] : ["traefik"], var.metrics_server_enabled ? [] : ["metrics-server"]) diff --git a/main.tf b/main.tf index c09a324..dd27a1d 100644 --- a/main.tf +++ b/main.tf @@ -13,7 +13,18 @@ resource "hcloud_network" "k3s" { ip_range = local.network_ipv4_cidr } -resource "hcloud_network_subnet" "subnet" { +# We start from the end of the subnets cird array, +# as we would have fewer control plane nodepools, than angent ones. +resource "hcloud_network_subnet" "control_plane" { + count = length(local.control_plane_nodepools) + network_id = hcloud_network.k3s.id + type = "cloud" + network_zone = var.network_region + ip_range = local.network_ipv4_subnets[255 - count.index] +} + +# Here we start at the beginning of the subnets cird array +resource "hcloud_network_subnet" "agent" { count = length(local.network_ipv4_subnets) network_id = hcloud_network.k3s.id type = "cloud"