From 58c630ba78a2f539e3dd0276e8a1c9efccb3e178 Mon Sep 17 00:00:00 2001 From: jodhi Date: Thu, 24 Feb 2022 22:49:36 +0700 Subject: [PATCH 1/2] not hardcode the ip allocation for agents --- agents.tf | 3 +-- locals.tf | 2 +- modules/host/main.tf | 2 +- modules/host/variables.tf | 1 + variables.tf | 10 ++++++---- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/agents.tf b/agents.tf index be36867..7647cc0 100644 --- a/agents.tf +++ b/agents.tf @@ -12,7 +12,6 @@ module "agents" { placement_group_id = hcloud_placement_group.k3s.id location = var.location network_id = hcloud_network.k3s.id - ip = cidrhost(hcloud_network_subnet.k3s.ip_range, 513 + each.value.index) server_type = each.value.server_type labels = { @@ -45,7 +44,7 @@ resource "null_resource" "agents" { token = random_password.k3s_token.result kubelet-arg = "cloud-provider=external" flannel-iface = "eth1" - node-ip = cidrhost(hcloud_network_subnet.k3s.ip_range, 513 + each.value.index) + node-ip = module.agents[each.key].ipv4_address node-label = var.automatically_upgrade_k3s ? ["k3s_upgrade=true"] : [] }) destination = "/tmp/config.yaml" diff --git a/locals.tf b/locals.tf index 69b0bda..7932296 100644 --- a/locals.tf +++ b/locals.tf @@ -33,7 +33,7 @@ locals { agent_nodepools = merge([ for nodepool_name, nodepool_obj in var.agent_nodepools : { - for index in range(nodepool_obj.count) : + for index in range(lookup(nodepool_obj, "count", var.agents_num)) : format("%s-%s", nodepool_name, index) => { index : index, # just for the compatibility with previous structure server_type : nodepool_obj.server_type diff --git a/modules/host/main.tf b/modules/host/main.tf index 0aa14cb..56e794e 100644 --- a/modules/host/main.tf +++ b/modules/host/main.tf @@ -14,7 +14,7 @@ resource "hcloud_server" "server" { network { network_id = var.network_id - ip = var.ip + ip = try(var.ip, null) } connection { diff --git a/modules/host/variables.tf b/modules/host/variables.tf index b336fc5..9c328b1 100644 --- a/modules/host/variables.tf +++ b/modules/host/variables.tf @@ -60,6 +60,7 @@ variable "network_id" { } variable "ip" { + default = null description = "The IP" type = string nullable = true diff --git a/variables.tf b/variables.tf index ad2e053..73d7301 100644 --- a/variables.tf +++ b/variables.tf @@ -52,12 +52,14 @@ variable "servers_num" { type = number } +variable "agents_num" { + description = "Default agent server type" + type = number +} + variable "agent_nodepools" { description = "Number of agent nodes." - type = map(object({ - server_type = string - count = number - })) + type = map(any) } variable "hetzner_ccm_version" { From bb5ab6bd9a7e8ce68a47b45ccf0d4213b06784c6 Mon Sep 17 00:00:00 2001 From: jodhi Date: Thu, 24 Feb 2022 23:19:31 +0700 Subject: [PATCH 2/2] make sure control-plane ip dynamic --- servers.tf | 1 - terraform.tfvars.example | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/servers.tf b/servers.tf index a10c7d7..acfc3f2 100644 --- a/servers.tf +++ b/servers.tf @@ -12,7 +12,6 @@ module "control_planes" { placement_group_id = hcloud_placement_group.k3s.id location = var.location network_id = hcloud_network.k3s.id - ip = cidrhost(hcloud_network_subnet.k3s.ip_range, 257 + count.index) server_type = var.control_plane_server_type labels = { diff --git a/terraform.tfvars.example b/terraform.tfvars.example index 57a85f5..93945b2 100644 --- a/terraform.tfvars.example +++ b/terraform.tfvars.example @@ -16,16 +16,16 @@ lb_server_type = "lb11" servers_num = 3 # For agent nodes, at least 2 is recommended for HA, but you can keep automatic upgrades. +agents_num = 2 + agent_nodepools = { big = { server_type = "cpx31", - count = 3 + count = 1 + } + small = { + server_type = "cpx21", } - # Will fail because of the ip address collision (we use index number) - # small = { - # server_type = "cpx31", - # count = 3 - # } } # If you want to use a specific Hetzner CCM and CSI version, set them below, otherwise leave as is for the latest versions