From 2b7d2722c55c901979da1197a2061bd1b2af69d3 Mon Sep 17 00:00:00 2001 From: Karim Naufal Date: Wed, 9 Mar 2022 03:15:15 +0100 Subject: [PATCH] changed the agent_nodepools to be a list --- agents.tf | 4 ++-- locals.tf | 8 ++++---- terraform.tfvars.example | 20 ++++++++++++-------- variables.tf | 4 ++-- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/agents.tf b/agents.tf index ce75203..6732d30 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[index(keys(var.agent_nodepools), each.value.nodepool_name) + 2].id + ipv4_subnet_id = hcloud_network_subnet.subnet[[for i, v in var.agent_nodepools : i if v.name == each.value.nodepool_name][0] + 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(local.network_ipv4_subnets[index(keys(var.agent_nodepools), each.value.nodepool_name) + 2], each.value.index + 101) + private_ipv4 = cidrhost(local.network_ipv4_subnets[[for i, v in var.agent_nodepools : i if v.name == each.value.nodepool_name][0] + 2], each.value.index + 101) labels = { "provisioner" = "terraform", diff --git a/locals.tf b/locals.tf index 45116c6..13ea3db 100644 --- a/locals.tf +++ b/locals.tf @@ -1,6 +1,6 @@ locals { # if we are in a single cluster config, we use the default klipper lb instead of Hetzner LB - is_single_node_cluster = var.control_plane_count + length(keys(var.agent_nodepools)) == 1 + is_single_node_cluster = var.control_plane_count + length(var.agent_nodepools) == 1 ssh_public_key = trimspace(file(var.public_key)) # ssh_private_key is either the contents of var.private_key or null to use a ssh agent. ssh_private_key = var.private_key == null ? null : trimspace(file(var.private_key)) @@ -170,10 +170,10 @@ locals { install_k3s_agent = concat(local.common_commands_install_k3s, ["curl -sfL https://get.k3s.io | INSTALL_K3S_SKIP_START=true INSTALL_K3S_SKIP_SELINUX_RPM=true INSTALL_K3S_CHANNEL=${var.initial_k3s_channel} INSTALL_K3S_EXEC=agent sh -"], local.apply_k3s_selinux) agent_nodepools = merge([ - for nodepool_name, nodepool_obj in var.agent_nodepools : { + for nodepool_obj in var.agent_nodepools : { for index in range(nodepool_obj.count) : - format("%s-%s", nodepool_name, index) => { - nodepool_name : nodepool_name, + format("%s-%s", nodepool_obj.name, index) => { + nodepool_name : nodepool_obj.name, server_type : nodepool_obj.server_type, index : index } diff --git a/terraform.tfvars.example b/terraform.tfvars.example index 9dd4a5e..9484d24 100644 --- a/terraform.tfvars.example +++ b/terraform.tfvars.example @@ -28,17 +28,21 @@ control_plane_server_type = "cpx11" # As for the agent nodepools, below is just an example, if you do not want nodepools, just use one, # and change the name to what you want, it need not be "agent-big" or "agent-small", also give them the subnet prefer. -# For single node clusters set this equal to {} -agent_nodepools = { - agent-big = { +# For single node clusters set this equal to [] or just set the counts to 0 +# IMPORTANT: Once the cluster is created, you can change the count, and even set it to 0, but do not remove a nodepool from the list. +# You can add others at the end of the list if you want. +agent_nodepools = [ + { + name = "agent-small", + server_type = "cpx11", + count = 2 + }, + { + name = "agent-large", server_type = "cpx21", count = 1 } - agent-small = { - server_type = "cpx11", - count = 2 - } -} +] # That will depend on how much load you want it to handle, see https://www.hetzner.com/cloud/load-balancer load_balancer_type = "lb11" diff --git a/variables.tf b/variables.tf index 90e0955..3460697 100644 --- a/variables.tf +++ b/variables.tf @@ -53,8 +53,8 @@ variable "load_balancer_disable_ipv6" { variable "agent_nodepools" { description = "Number of agent nodes." - type = map(any) - default = {} + type = list(any) + default = [] } variable "hetzner_ccm_version" {