automated the creation of the subnets

This commit is contained in:
Karim Naufal 2022-03-09 02:07:24 +01:00
parent 56fc95763a
commit a72facdd1a
6 changed files with 15 additions and 42 deletions

View File

@ -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",

View File

@ -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",

View File

@ -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)]
}

16
main.tf
View File

@ -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" {

View File

@ -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 = ""

View File

@ -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