made cluster name prefix configurable and tweaked ways the agents random pets are created

small tweaks

small tweaks
This commit is contained in:
Karim Naufal 2022-03-05 05:33:29 +01:00
parent b93087d896
commit e6b8249846
7 changed files with 21 additions and 11 deletions

2
.gitignore vendored
View File

@ -6,4 +6,4 @@ kubeconfig.yaml-e
terraform.tfvars terraform.tfvars
plans-custom.yaml plans-custom.yaml
traefik-custom.yaml traefik-custom.yaml
kured-custom.yaml kured-custom.yaml

View File

@ -1,6 +1,6 @@
resource "random_pet" "agents" { resource "random_pet" "agents" {
for_each = local.agent_nodepools count = length(local.agent_nodepools)
length = 1 length = 1
} }
module "agents" { module "agents" {
@ -8,7 +8,7 @@ module "agents" {
for_each = local.agent_nodepools for_each = local.agent_nodepools
name = var.use_cluster_name_in_node_name ? "k3s-${random_pet.cluster.id}-${each.value.nodepool_name}-${random_pet.agents[each.key].id}" : "${each.value.nodepool_name}-${random_pet.agents[each.key].id}" name = var.use_cluster_name_in_node_name ? "${random_pet.cluster.id}-${each.value.nodepool_name}-${random_pet.agents[each.value.index].id}" : "${each.value.nodepool_name}-${random_pet.agents[each.value.index].id}"
ssh_keys = [hcloud_ssh_key.k3s.id] ssh_keys = [hcloud_ssh_key.k3s.id]
public_key = var.public_key public_key = var.public_key
private_key = var.private_key private_key = var.private_key

View File

@ -7,7 +7,7 @@ module "control_planes" {
source = "./modules/host" source = "./modules/host"
count = var.control_plane_count count = var.control_plane_count
name = var.use_cluster_name_in_node_name ? "k3s-${random_pet.cluster.id}-control-plane-${random_pet.control_planes[count.index].id}" : "control-plane-${random_pet.control_planes[count.index].id}" name = var.use_cluster_name_in_node_name ? "${random_pet.cluster.id}-control-plane-${random_pet.control_planes[count.index].id}" : "control-plane-${random_pet.control_planes[count.index].id}"
ssh_keys = [hcloud_ssh_key.k3s.id] ssh_keys = [hcloud_ssh_key.k3s.id]
public_key = var.public_key public_key = var.public_key
private_key = var.private_key private_key = var.private_key

View File

@ -94,7 +94,7 @@ resource "null_resource" "kustomization" {
content = local.is_single_node_cluster ? "" : templatefile( content = local.is_single_node_cluster ? "" : templatefile(
"${path.module}/templates/traefik_config.yaml.tpl", "${path.module}/templates/traefik_config.yaml.tpl",
{ {
name = "k3s-${random_pet.cluster.id}-traefik" name = "${random_pet.cluster.id}-traefik"
load_balancer_disable_ipv6 = var.load_balancer_disable_ipv6 load_balancer_disable_ipv6 = var.load_balancer_disable_ipv6
load_balancer_type = var.load_balancer_type load_balancer_type = var.load_balancer_type
location = var.location location = var.location

11
main.tf
View File

@ -1,5 +1,6 @@
resource "random_pet" "cluster" { resource "random_pet" "cluster" {
length = 1 length = 1
prefix = var.cluster_prefix
} }
resource "random_password" "k3s_token" { resource "random_password" "k3s_token" {
@ -8,12 +9,12 @@ resource "random_password" "k3s_token" {
} }
resource "hcloud_ssh_key" "k3s" { resource "hcloud_ssh_key" "k3s" {
name = "k3s-${random_pet.cluster.id}" name = random_pet.cluster.id
public_key = local.ssh_public_key public_key = local.ssh_public_key
} }
resource "hcloud_network" "k3s" { resource "hcloud_network" "k3s" {
name = "k3s-${random_pet.cluster.id}" name = random_pet.cluster.id
ip_range = var.network_ipv4_range ip_range = var.network_ipv4_range
} }
@ -26,7 +27,7 @@ resource "hcloud_network_subnet" "subnet" {
} }
resource "hcloud_firewall" "k3s" { resource "hcloud_firewall" "k3s" {
name = "k3s-${random_pet.cluster.id}" name = random_pet.cluster.id
dynamic "rule" { dynamic "rule" {
for_each = concat(local.base_firewall_rules, var.extra_firewall_rules) for_each = concat(local.base_firewall_rules, var.extra_firewall_rules)
@ -41,7 +42,7 @@ resource "hcloud_firewall" "k3s" {
} }
resource "hcloud_placement_group" "k3s" { resource "hcloud_placement_group" "k3s" {
name = "k3s-${random_pet.cluster.id}" name = random_pet.cluster.id
type = "spread" type = "spread"
labels = { labels = {
"provisioner" = "terraform", "provisioner" = "terraform",
@ -51,7 +52,7 @@ resource "hcloud_placement_group" "k3s" {
data "hcloud_load_balancer" "traefik" { data "hcloud_load_balancer" "traefik" {
count = local.is_single_node_cluster ? 0 : 1 count = local.is_single_node_cluster ? 0 : 1
name = "k3s-${random_pet.cluster.id}-traefik" name = "${random_pet.cluster.id}-traefik"
depends_on = [null_resource.kustomization] depends_on = [null_resource.kustomization]
} }

View File

@ -80,6 +80,9 @@ load_balancer_type = "lb11"
# 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 in the node name, i.e. add the prefix k3s-(cluster_name)- to the nodes? The default is "true".
# use_cluster_name_in_node_name = false # use_cluster_name_in_node_name = false
# Prefix for the cluster name, by default "k3s"
# cluster_prefix = ""
# Adding extra firewall rules, like opening a port # 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 # In this example with allow port TCP 5432 for a Postgres service we will open via a nodeport
# More info on the format here https://registry.terraform.io/providers/hetznercloud/hcloud/latest/docs/resources/firewall # More info on the format here https://registry.terraform.io/providers/hetznercloud/hcloud/latest/docs/resources/firewall

View File

@ -126,3 +126,9 @@ variable "use_cluster_name_in_node_name" {
default = true default = true
description = "Whether to use the cluster name in the node name" description = "Whether to use the cluster name in the node name"
} }
variable "cluster_prefix" {
type = string
default = "k3s"
description = "Prefix for the cluster name"
}