merge master into automatic-subnets

This commit is contained in:
Karim Naufal 2022-03-09 09:38:10 +01:00
commit b7c6bbbe8f
9 changed files with 36 additions and 23 deletions

View File

@ -3,7 +3,7 @@ module "agents" {
for_each = local.agent_nodepools 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] 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

@ -2,7 +2,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 ? "${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] 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 = "${random_pet.cluster.id}-traefik" name = "${var.cluster_name}-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

25
main.tf
View File

@ -1,21 +1,24 @@
resource "random_pet" "cluster" {
length = 1
prefix = var.cluster_prefix
}
resource "random_password" "k3s_token" { resource "random_password" "k3s_token" {
length = 48 length = 48
special = false special = false
} }
resource "hcloud_ssh_key" "k3s" { resource "hcloud_ssh_key" "k3s" {
name = random_pet.cluster.id name = var.cluster_name
public_key = local.ssh_public_key public_key = local.ssh_public_key
} }
resource "hcloud_network" "k3s" { resource "hcloud_network" "k3s" {
name = random_pet.cluster.id name = var.cluster_name
ip_range = local.network_ipv4_cidr 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"
} }
resource "hcloud_network_subnet" "subnet" { resource "hcloud_network_subnet" "subnet" {
@ -27,7 +30,7 @@ resource "hcloud_network_subnet" "subnet" {
} }
resource "hcloud_firewall" "k3s" { resource "hcloud_firewall" "k3s" {
name = random_pet.cluster.id name = var.cluster_name
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)
@ -42,7 +45,7 @@ resource "hcloud_firewall" "k3s" {
} }
resource "hcloud_placement_group" "k3s" { resource "hcloud_placement_group" "k3s" {
name = random_pet.cluster.id name = var.cluster_name
type = "spread" type = "spread"
labels = { labels = {
"provisioner" = "terraform", "provisioner" = "terraform",
@ -52,7 +55,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 = "${random_pet.cluster.id}-traefik" name = "${var.cluster_name}-traefik"
depends_on = [null_resource.kustomization] depends_on = [null_resource.kustomization]
} }

View File

@ -12,5 +12,5 @@ locals {
ssh_args = "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ${local.ssh_identity_file}" ssh_args = "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ${local.ssh_identity_file}"
# the hosts name with its unique suffix attached # the hosts name with its unique suffix attached
name = "${var.name}-${random_pet.server.id}" name = "${var.name}-${random_string.server.id}"
} }

View File

@ -1,5 +1,10 @@
resource "random_pet" "server" { resource "random_string" "server" {
length = 1 length = 3
lower = true
special = false
number = false
upper = false
keepers = { keepers = {
# We re-create the id (and server) whenever one of those attributes # We re-create the id (and server) whenever one of those attributes
# changes. This should include all input variables to this module, # changes. This should include all input variables to this module,

View File

@ -1,5 +1,5 @@
output "cluster_name" { output "cluster_name" {
value = random_pet.cluster.id value = var.cluster_name
description = "Shared suffix for all resources belonging to this cluster." description = "Shared suffix for all resources belonging to this cluster."
} }

View File

@ -67,11 +67,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/ # 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" # 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 in the node name, 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" # The cluster name, by default "k3s"
# cluster_prefix = "" # cluster_name = ""
# 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

View File

@ -116,10 +116,15 @@ variable "use_cluster_name_in_node_name" {
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" { variable "cluster_name" {
type = string type = string
default = "k3s" 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" { variable "traefik_additional_options" {