Skip to content

Commit

Permalink
feat: Create server
Browse files Browse the repository at this point in the history
  • Loading branch information
mrclrchtr committed Mar 16, 2024
1 parent 73d7d31 commit 9678b92
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 2 deletions.
109 changes: 109 additions & 0 deletions terraform-hcloud-talos/server.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
data "hcloud_image" "arm" {
with_selector = "os=talos"
with_architecture = "arm"
most_recent = true
}

data "hcloud_image" "x86" {
with_selector = "os=talos"
with_architecture = "x86"
most_recent = true
}

locals {
control_plane_image_id = substr(var.control_plane_server_type, 0, 3) == "cax" ? data.hcloud_image.arm.id : data.hcloud_image.x86.id
worker_image_id = substr(var.worker_server_type, 0, 3) == "cax" ? data.hcloud_image.arm.id : data.hcloud_image.x86.id
}

resource "hcloud_ssh_key" "this" {
name = "default"
public_key = var.ssh_public_key
}

resource "hcloud_primary_ip" "control_planes" {
count = var.control_plane_count
name = "control-plane-${count.index + 1}"
datacenter = data.hcloud_datacenter.this.name
type = "ipv4"
assignee_type = "server"
auto_delete = false
}

resource "hcloud_server" "control_planes" {
count = var.control_plane_count
datacenter = data.hcloud_datacenter.this.name
name = "control-plane-${count.index + 1}"
image = local.control_plane_image_id
server_type = var.control_plane_server_type
user_data = data.talos_machine_configuration.controlplane[count.index].machine_configuration
ssh_keys = [hcloud_ssh_key.this.id]
placement_group_id = hcloud_placement_group.control_plane.id

labels = {
"role" = "control-plane"
}

firewall_ids = [
hcloud_firewall.this.id
]

public_net {
ipv4_enabled = true
ipv4 = hcloud_primary_ip.control_planes[count.index].id
ipv6_enabled = true
}

network {
network_id = hcloud_network_subnet.control_plane.network_id
ip = local.control_plane_ips[count.index]
}

depends_on = [
hcloud_network_subnet.control_plane,
data.talos_machine_configuration.controlplane
]
}

resource "hcloud_primary_ip" "workers" {
count = var.worker_count
name = "worker-${count.index + 1}"
datacenter = data.hcloud_datacenter.this.name
type = "ipv4"
assignee_type = "server"
auto_delete = false
}

resource "hcloud_server" "workers" {
count = var.worker_count
datacenter = data.hcloud_datacenter.this.name
name = "worker-${count.index + 1}"
image = local.worker_image_id
server_type = var.worker_server_type
user_data = data.talos_machine_configuration.worker[count.index].machine_configuration
ssh_keys = [hcloud_ssh_key.this.id]
placement_group_id = hcloud_placement_group.worker.id

labels = {
"role" = "worker"
}

firewall_ids = [
hcloud_firewall.this.id
]

public_net {
ipv4_enabled = true
ipv4 = hcloud_primary_ip.workers[count.index].id
ipv6_enabled = true
}

network {
network_id = hcloud_network_subnet.worker.network_id
ip = local.worker_ips[count.index]
}

depends_on = [
hcloud_network_subnet.worker,
data.talos_machine_configuration.worker
]
}
16 changes: 15 additions & 1 deletion terraform-hcloud-talos/talos.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,24 @@ data "talos_machine_configuration" "worker" {
examples = false
}

resource "talos_machine_bootstrap" "this" {
client_configuration = talos_machine_secrets.this.client_configuration
endpoint = hcloud_server.control_planes[0].ipv4_address
node = hcloud_server.control_planes[0].ipv4_address
}

data "talos_client_configuration" "this" {
cluster_name = var.cluster_name
client_configuration = talos_machine_secrets.this.client_configuration
endpoints = [
for controlplane_ip in local.control_plane_ips : controlplane_ip
for server in hcloud_server.control_planes : server.ipv4_address
]
}

data "talos_cluster_kubeconfig" "this" {
client_configuration = talos_machine_secrets.this.client_configuration
node = hcloud_server.control_planes[0].ipv4_address
depends_on = [
talos_machine_bootstrap.this
]
}
47 changes: 46 additions & 1 deletion terraform-hcloud-talos/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ variable "talos_version" {
description = "The version of Talos to use for the cluster."
}

variable "ssh_public_key" {
description = <<EOF
The public key to be set in the servers. It is not used in any way.
Unfortunately, it is still required, otherwise the Hetzner will sen E-Mails with login credentials.
EOF
type = string
sensitive = true
}

variable "control_plane_count" {
type = number
description = <<EOF
Expand All @@ -85,7 +94,43 @@ variable "control_plane_count" {
}
}

variable "control_plane_server_type" {
type = string
description = <<EOF
The server type to use for the control plane nodes.
Possible values: cx11, cx21, cx31, cx41, cx51, cpx11, cpx21, cpx31, cpx41,
cpx51, cax11, cax21, cax31, cax41, ccx13, ccx23, ccx33, ccx43, ccx53, ccx63
EOF
validation {
condition = contains([
"cx11", "cx21", "cx31", "cx41", "cx51",
"cpx11", "cpx21", "cpx31", "cpx41", "cpx51",
"cax11", "cax21", "cax31", "cax41",
"ccx13", "ccx23", "ccx33", "ccx43", "ccx53", "ccx63"
], var.control_plane_server_type)
error_message = "Invalid control plane server type."
}
}

variable "worker_count" {
type = number
description = "The number of worker nodes to create."
}
}

variable "worker_server_type" {
type = string
description = <<EOF
The server type to use for the worker nodes.
Possible values: cx11, cx21, cx31, cx41, cx51, cpx11, cpx21, cpx31, cpx41,
cpx51, cax11, cax21, cax31, cax41, ccx13, ccx23, ccx33, ccx43, ccx53, ccx63
EOF
validation {
condition = contains([
"cx11", "cx21", "cx31", "cx41", "cx51",
"cpx11", "cpx21", "cpx31", "cpx41", "cpx51",
"cax11", "cax21", "cax31", "cax41",
"ccx13", "ccx23", "ccx33", "ccx43", "ccx53", "ccx63"
], var.worker_server_type)
error_message = "Invalid worker server type."
}
}

0 comments on commit 9678b92

Please sign in to comment.