Replace auto-carved private subnets with explicit admin-supplied CIDRs

private_supernet/private_subnet_prefix_length/private_interface_count
(which auto-derived per-router-per-role micro-subnets via cidrsubnet())
are replaced by a single required variable, private_network_cidrs: one
CIDR per shared private network that router VMs get an interface into,
supplied explicitly by the admin - no auto-carving. Each router gets its
own port/IP inside every listed network (cidrhost(cidr, router_index+2)),
closer to the original lan_net design but generalized to N networks and
N routers. network-init.sh.tpl needed no changes - it already matches
interfaces by CIDR membership regardless of whether the CIDR is shared.

Also fixes a testing gap found along the way: `terraform validate` does
not enforce variable validation{} blocks for externally-supplied values
in this terraform version - only `plan`/`apply` do. The test suite now
exercises those validations for real via `terraform plan` against an
isolated, provider-free copy of variables.tf.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011hXR2ftXZZhJ4Y3XuSoR8r
This commit is contained in:
ayurishchevandClaude Sonnet 5 committed 2026-09-04 10:49:24 +03:00
1 parent 5979a9a58b
commit 1ef4b12143
8 files changed
+290 -150

No files matched your search

+23 -24
View File
@@ -105,53 +105,52 @@ resource "vkcs_networking_secgroup_rule" "router_ipsec_nat_t" {
sdn = "sprut"
}
# Per-router private interface subnets: each router gets `private_roles` isolated
# micro-subnets (no shared LAN, no VRRP), carved out of private_supernet.
# Per-router private interfaces: one shared private network per admin-supplied
# CIDR in private_network_cidrs (no shared LAN with the priv_srv_* segment,
# no VRRP), each router getting its own port/IP inside every such network.
locals {
private_roles = [for i in range(var.private_interface_count) : "priv${i + 1}"]
private_roles = [for idx in range(length(var.private_network_cidrs)) : "priv${idx + 1}"]
router_private_subnets = {
for pair in setproduct(range(var.router_count), local.private_roles) :
"router${pair[0] + 1}-${pair[1]}" => {
router_index = pair[0]
role = pair[1]
cidr = cidrsubnet(
var.private_supernet,
var.private_subnet_prefix_length - tonumber(split("/", var.private_supernet)[1]),
pair[0] * length(local.private_roles) + index(local.private_roles, pair[1])
)
}
private_network_cidr = {
for idx, cidr in var.private_network_cidrs :
"priv${idx + 1}" => cidr
}
}
resource "vkcs_networking_network" "router_priv_net" {
for_each = local.router_private_subnets
for_each = local.private_network_cidr
name = "router-${each.key}-net"
sdn = "sprut"
admin_state_up = true
}
resource "vkcs_networking_subnet" "router_priv_subnet" {
for_each = local.router_private_subnets
for_each = local.private_network_cidr
network_id = vkcs_networking_network.router_priv_net[each.key].id
name = "router-${each.key}-subnet"
cidr = each.value.cidr
gateway_ip = cidrhost(each.value.cidr, 1)
cidr = each.value
gateway_ip = cidrhost(each.value, 1)
sdn = "sprut"
}
resource "vkcs_networking_port" "router_priv_port" {
for_each = local.router_private_subnets
for_each = {
for pair in setproduct(range(var.router_count), local.private_roles) :
"router${pair[0] + 1}-${pair[1]}" => {
router_index = pair[0]
role = pair[1]
}
}
name = "router-${each.key}-port"
network_id = vkcs_networking_network.router_priv_net[each.key].id
network_id = vkcs_networking_network.router_priv_net[each.value.role].id
admin_state_up = true
port_security_enabled = false
full_security_groups_control = true
security_group_ids = []
sdn = "sprut"
fixed_ip {
subnet_id = vkcs_networking_subnet.router_priv_subnet[each.key].id
ip_address = cidrhost(each.value.cidr, 2)
subnet_id = vkcs_networking_subnet.router_priv_subnet[each.value.role].id
ip_address = cidrhost(local.private_network_cidr[each.value.role], each.value.router_index + 2)
}
}
@@ -176,7 +175,7 @@ resource "vkcs_compute_instance" "router" {
private_interfaces = [
for role in local.private_roles : {
name = "eth${index(local.private_roles, role) + 1}"
cidr = local.router_private_subnets["router${count.index + 1}-${role}"].cidr
cidr = local.private_network_cidr[role]
}
]
})
@@ -186,7 +185,7 @@ resource "vkcs_compute_instance" "router" {
uuid = data.vkcs_networking_network.extnet.id
}
# Private: one pre-created isolated port per role
# Private: one pre-created port per role, into that role's shared network
dynamic "network" {
for_each = local.private_roles
content {