From f20ac7c8d5c5c3ba8f46ea7828ca0e627958ab37 Mon Sep 17 00:00:00 2001 From: Karim Naufal Date: Wed, 9 Mar 2022 05:19:06 +0100 Subject: [PATCH 1/4] changed the cluster name to manual --- agents.tf | 2 +- control_planes.tf | 2 +- init.tf | 2 +- main.tf | 15 +++++---------- output.tf | 2 +- terraform.tfvars.example | 6 +++--- variables.tf | 9 +++++++-- 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/agents.tf b/agents.tf index a8e5423..36a62a7 100644 --- a/agents.tf +++ b/agents.tf @@ -3,7 +3,7 @@ module "agents" { for_each = local.agent_nodepools - name = "${var.use_cluster_name_in_node_name ? "${random_pet.cluster.id}-" : ""}${each.value.nodepool_name}" + name = "${var.use_cluster_name_in_node_name ? "${var.cluster_name}-" : ""}${each.value.nodepool_name}" ssh_keys = [hcloud_ssh_key.k3s.id] public_key = var.public_key private_key = var.private_key diff --git a/control_planes.tf b/control_planes.tf index e3130c4..a19b212 100644 --- a/control_planes.tf +++ b/control_planes.tf @@ -2,7 +2,7 @@ module "control_planes" { source = "./modules/host" count = var.control_plane_count - name = "${var.use_cluster_name_in_node_name ? "${random_pet.cluster.id}-" : ""}control-plane" + name = "${var.use_cluster_name_in_node_name ? "${var.cluster_name}-" : ""}control-plane" ssh_keys = [hcloud_ssh_key.k3s.id] public_key = var.public_key private_key = var.private_key diff --git a/init.tf b/init.tf index e07bc45..4b64e15 100644 --- a/init.tf +++ b/init.tf @@ -94,7 +94,7 @@ resource "null_resource" "kustomization" { content = local.is_single_node_cluster ? "" : templatefile( "${path.module}/templates/traefik_config.yaml.tpl", { - name = "${random_pet.cluster.id}-traefik" + name = "${var.cluster_name}-traefik" load_balancer_disable_ipv6 = var.load_balancer_disable_ipv6 load_balancer_type = var.load_balancer_type location = var.location diff --git a/main.tf b/main.tf index b78a41d..0652399 100644 --- a/main.tf +++ b/main.tf @@ -1,20 +1,15 @@ -resource "random_pet" "cluster" { - length = 1 - prefix = var.cluster_prefix -} - resource "random_password" "k3s_token" { length = 48 special = false } resource "hcloud_ssh_key" "k3s" { - name = random_pet.cluster.id + name = var.cluster_name public_key = local.ssh_public_key } resource "hcloud_network" "k3s" { - name = random_pet.cluster.id + name = var.cluster_name ip_range = var.network_ipv4_range } @@ -37,7 +32,7 @@ resource "hcloud_network_subnet" "subnet" { } resource "hcloud_firewall" "k3s" { - name = random_pet.cluster.id + name = var.cluster_name dynamic "rule" { for_each = concat(local.base_firewall_rules, var.extra_firewall_rules) @@ -52,7 +47,7 @@ resource "hcloud_firewall" "k3s" { } resource "hcloud_placement_group" "k3s" { - name = random_pet.cluster.id + name = var.cluster_name type = "spread" labels = { "provisioner" = "terraform", @@ -62,7 +57,7 @@ resource "hcloud_placement_group" "k3s" { data "hcloud_load_balancer" "traefik" { count = local.is_single_node_cluster ? 0 : 1 - name = "${random_pet.cluster.id}-traefik" + name = "${var.cluster_name}-traefik" depends_on = [null_resource.kustomization] } diff --git a/output.tf b/output.tf index 9337e1d..e0089fd 100644 --- a/output.tf +++ b/output.tf @@ -1,5 +1,5 @@ output "cluster_name" { - value = random_pet.cluster.id + value = var.cluster_name description = "Shared suffix for all resources belonging to this cluster." } diff --git a/terraform.tfvars.example b/terraform.tfvars.example index 1688b31..60c41a9 100644 --- a/terraform.tfvars.example +++ b/terraform.tfvars.example @@ -77,11 +77,11 @@ load_balancer_type = "lb11" # Allows you to specify either stable, latest, or testing (defaults to stable), see https://rancher.com/docs/k3s/latest/en/upgrades/basic/ # initial_k3s_channel = "latest" -# Whether to use the cluster name in the node name, i.e. add the prefix k3s-(cluster_name)- to the nodes? The default is "true". +# Whether to use the cluster name to in the node name, the default is true # use_cluster_name_in_node_name = false -# Prefix for the cluster name, by default "k3s" -# cluster_prefix = "" +# The cluster name, by default "k3s" +# cluster_name = "" # Adding extra firewall rules, like opening a port # In this example with allow port TCP 5432 for a Postgres service we will open via a nodeport diff --git a/variables.tf b/variables.tf index 49d0e51..787240e 100644 --- a/variables.tf +++ b/variables.tf @@ -127,10 +127,15 @@ variable "use_cluster_name_in_node_name" { description = "Whether to use the cluster name in the node name" } -variable "cluster_prefix" { +variable "cluster_name" { type = string default = "k3s" - description = "Prefix for the cluster name" + description = "Name of the cluster" + + validation { + condition = can(regex("^[a-z1-9\\-]+$", var.cluster_name)) + error_message = "The cluster name must be in the form of lowercase alphanumeric characters and/or dashes." + } } variable "traefik_additional_options" { From 2d913e710a4a68cd303e845d631a839efe592fe0 Mon Sep 17 00:00:00 2001 From: Karim Naufal Date: Wed, 9 Mar 2022 05:22:44 +0100 Subject: [PATCH 2/4] tweaked tfvars.example --- terraform.tfvars.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terraform.tfvars.example b/terraform.tfvars.example index 60c41a9..08a646a 100644 --- a/terraform.tfvars.example +++ b/terraform.tfvars.example @@ -77,7 +77,7 @@ load_balancer_type = "lb11" # Allows you to specify either stable, latest, or testing (defaults to stable), see https://rancher.com/docs/k3s/latest/en/upgrades/basic/ # initial_k3s_channel = "latest" -# Whether to use the cluster name to in the node name, the default is true +# Whether to use the cluster name in the node name, the default is "true". # use_cluster_name_in_node_name = false # The cluster name, by default "k3s" From af592adb9b1b7e8c7b647b7f64ef95ad8ba6fca1 Mon Sep 17 00:00:00 2001 From: Karim Naufal Date: Wed, 9 Mar 2022 05:42:56 +0100 Subject: [PATCH 3/4] moved to random string for the node name --- modules/host/main.tf | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/modules/host/main.tf b/modules/host/main.tf index a4de8c6..70b2aaa 100644 --- a/modules/host/main.tf +++ b/modules/host/main.tf @@ -1,5 +1,10 @@ -resource "random_pet" "server" { - length = 1 +resource "random_string" "server" { + length = 3 + lower = true + special = false + number = false + upper = false + keepers = { # We re-create the id (and server) whenever one of those attributes # changes. This should include all input variables to this module, From 863fa15207c642fc42735d6390075330cd8584fe Mon Sep 17 00:00:00 2001 From: Karim Naufal Date: Wed, 9 Mar 2022 05:50:12 +0100 Subject: [PATCH 4/4] small fix --- modules/host/locals.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/host/locals.tf b/modules/host/locals.tf index 46e0f81..4edc40e 100644 --- a/modules/host/locals.tf +++ b/modules/host/locals.tf @@ -12,5 +12,5 @@ locals { ssh_args = "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ${local.ssh_identity_file}" # the hosts name with its unique suffix attached - name = "${var.name}-${random_pet.server.id}" + name = "${var.name}-${random_string.server.id}" }