From a72facdd1a2e389294c1ea0ebd8cf31672b4a603 Mon Sep 17 00:00:00 2001 From: Karim Naufal Date: Wed, 9 Mar 2022 02:07:24 +0100 Subject: [PATCH] automated the creation of the subnets --- agents.tf | 4 ++-- control_planes.tf | 4 ++-- locals.tf | 10 ++++++++-- main.tf | 16 +++------------- terraform.tfvars.example | 12 ------------ variables.tf | 11 ----------- 6 files changed, 15 insertions(+), 42 deletions(-) diff --git a/agents.tf b/agents.tf index a8e5423..ce75203 100644 --- a/agents.tf +++ b/agents.tf @@ -12,11 +12,11 @@ module "agents" { placement_group_id = hcloud_placement_group.k3s.id location = var.location server_type = each.value.server_type - ipv4_subnet_id = hcloud_network_subnet.subnet[each.value.subnet].id + ipv4_subnet_id = hcloud_network_subnet.subnet[index(keys(var.agent_nodepools), each.value.nodepool_name) + 2].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(var.network_ipv4_subnets[each.value.subnet], each.value.index + 101) + private_ipv4 = cidrhost(local.network_ipv4_subnets[index(keys(var.agent_nodepools), each.value.nodepool_name) + 2], each.value.index + 101) labels = { "provisioner" = "terraform", diff --git a/control_planes.tf b/control_planes.tf index e3130c4..a8957aa 100644 --- a/control_planes.tf +++ b/control_planes.tf @@ -11,11 +11,11 @@ module "control_planes" { placement_group_id = hcloud_placement_group.k3s.id location = var.location server_type = var.control_plane_server_type - ipv4_subnet_id = hcloud_network_subnet.subnet["control_plane"].id + ipv4_subnet_id = hcloud_network_subnet.subnet[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(var.network_ipv4_subnets["control_plane"], count.index + 101) + private_ipv4 = cidrhost(local.network_ipv4_subnets[1], count.index + 101) labels = { "provisioner" = "terraform", diff --git a/locals.tf b/locals.tf index a01b5cd..45116c6 100644 --- a/locals.tf +++ b/locals.tf @@ -23,7 +23,7 @@ locals { hetzner_cloud_api_ipv4 = "213.239.246.1/32" whitelisted_ips = [ - var.network_ipv4_range, + local.network_ipv4_cidr, local.hetzner_metadata_service_ipv4, local.hetzner_cloud_api_ipv4, "127.0.0.1/32", @@ -175,9 +175,15 @@ locals { format("%s-%s", nodepool_name, index) => { nodepool_name : nodepool_name, server_type : nodepool_obj.server_type, - subnet : nodepool_obj.subnet, index : index } } ]...) + + # The main network cidr that all subnets will be created upon + network_ipv4_cidr = "10.0.0.0/8" + + # 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.agent_nodepools) + 2) : cidrsubnet(local.network_ipv4_cidr, 8, index)] } diff --git a/main.tf b/main.tf index b78a41d..ff61cbd 100644 --- a/main.tf +++ b/main.tf @@ -15,25 +15,15 @@ resource "hcloud_ssh_key" "k3s" { resource "hcloud_network" "k3s" { name = random_pet.cluster.id - ip_range = var.network_ipv4_range -} - -# This is the default subnet to be used by the load balancer. -resource "hcloud_network_subnet" "default" { - network_id = hcloud_network.k3s.id - type = "cloud" - network_zone = var.network_region - ip_range = "10.0.0.0/16" + ip_range = local.network_ipv4_cidr } resource "hcloud_network_subnet" "subnet" { - for_each = var.network_ipv4_subnets + count = length(local.network_ipv4_subnets) network_id = hcloud_network.k3s.id type = "cloud" network_zone = var.network_region - ip_range = each.value - - depends_on = [hcloud_network_subnet.default] + ip_range = local.network_ipv4_subnets[count.index] } resource "hcloud_firewall" "k3s" { diff --git a/terraform.tfvars.example b/terraform.tfvars.example index 1688b31..cd3f5dd 100644 --- a/terraform.tfvars.example +++ b/terraform.tfvars.example @@ -18,15 +18,6 @@ private_key = "/home/username/.ssh/id_ed25519" location = "fsn1" # change to `ash` for us-east Ashburn, Virginia location network_region = "eu-central" # change to `us-east` if location is ash -# You can have up to as many subnets as you want (preferably if the form of 10.X.0.0/16), -# their primary use is to logically separate the nodes. -# The control_plane network is mandatory. -network_ipv4_subnets = { - control_plane = "10.1.0.0/16" - agent_big = "10.2.0.0/16" - agent_small = "10.3.0.0/16" -} - # At least 3 server nodes is recommended for HA, otherwise you need to turn off automatic upgrade (see ReadMe). # As per rancher docs, it must be always an odd number, never even! See https://rancher.com/docs/k3s/latest/en/installation/ha-embedded/ # For instance, 1 is ok (non-HA), 2 not ok, 3 is ok (becomes HA). @@ -56,9 +47,6 @@ load_balancer_type = "lb11" ### The following values are fully optional -# It's best to leave the network range as is, unless you know what you are doing. The default is "10.0.0.0/8". -# network_ipv4_range = "10.0.0.0/8" - # If you want to use a specific Hetzner CCM and CSI version, set them below, otherwise leave as is for the latest versions # hetzner_ccm_version = "" # hetzner_csi_version = "" diff --git a/variables.tf b/variables.tf index 49d0e51..90e0955 100644 --- a/variables.tf +++ b/variables.tf @@ -30,17 +30,6 @@ variable "network_region" { type = string } -variable "network_ipv4_range" { - description = "Default IPv4 range for network" - type = string - default = "10.0.0.0/8" -} - -variable "network_ipv4_subnets" { - description = "Subnets definition for default network" - type = map(string) -} - variable "control_plane_server_type" { description = "Default control plane server type" type = string