initial scaffold
Some checks failed
terraform / validate (push) Waiting to run
build-hello-app / build-and-push (push) Has been cancelled

This commit is contained in:
CodeGit 2026-08-18 20:22:42 +01:00
commit 5c2080a73b
31 changed files with 1244 additions and 0 deletions

95
terraform/main.tf Normal file
View file

@ -0,0 +1,95 @@
locals {
server_node = [for name, n in var.nodes : n if n.role == "server"][0]
server_ip = local.server_node.ip
prefix_length = split("/", var.network_cidr)[1]
}
resource "libvirt_pool" "k3s" {
name = var.storage_pool
type = "dir"
path = var.storage_pool_path
}
resource "libvirt_volume" "base" {
name = "k3s-base.qcow2"
pool = libvirt_pool.k3s.name
source = var.base_image_url
format = "qcow2"
}
# A dedicated, isolated NAT network so this project can't collide with
# anything else already using the host's default libvirt network. DHCP is
# off every node gets a static IP via cloud-init instead.
resource "libvirt_network" "k3s" {
name = "k3s-homelab"
mode = "nat"
domain = "k3s.local"
addresses = [var.network_cidr]
dhcp {
enabled = false
}
dns {
enabled = true
}
}
resource "libvirt_volume" "node" {
for_each = var.nodes
name = "${each.key}.qcow2"
pool = libvirt_pool.k3s.name
base_volume_id = libvirt_volume.base.id
size = each.value.disk_gb * 1024 * 1024 * 1024
format = "qcow2"
}
resource "libvirt_cloudinit_disk" "node" {
for_each = var.nodes
name = "${each.key}-cloudinit.iso"
pool = libvirt_pool.k3s.name
user_data = templatefile("${path.module}/cloud-init/${each.value.role}.yaml.tpl", {
hostname = each.key
ssh_public_key = var.ssh_public_key
k3s_token = var.k3s_token
server_ip = local.server_ip
k8s_api_hostname = var.k8s_api_hostname
})
network_config = templatefile("${path.module}/cloud-init/network-config.yaml.tpl", {
ip = each.value.ip
prefix_length = local.prefix_length
gateway = var.gateway_ip
})
}
resource "libvirt_domain" "node" {
for_each = var.nodes
name = each.key
vcpu = each.value.vcpu
memory = each.value.memory
cloudinit = libvirt_cloudinit_disk.node[each.key].id
network_interface {
network_id = libvirt_network.k3s.id
wait_for_lease = false
}
disk {
volume_id = libvirt_volume.node[each.key].id
}
# Serial console only, no display this box doesn't need a GUI hop for
# a couple of small demo VMs.
console {
type = "pty"
target_type = "serial"
target_port = "0"
}
# Terraform brings all VMs up in parallel; the agent cloud-init script
# (cloud-init/agent.yaml.tpl) retries the join until the server's API is
# reachable, so node boot order doesn't matter.
}