evolved-npcs-infra/main.tf
2025-09-27 21:04:15 -07:00

170 lines
No EOL
3.7 KiB
HCL

terraform {
required_version = ">= 1.0"
required_providers {
talos = {
source = "siderolabs/talos"
version = "~> 0.4"
}
libvirt = {
source = "dmacvicar/libvirt"
version = "~> 0.7"
}
}
}
# Load variables from a .tfvars file
variable "cluster_name" {
description = "The name of the Talos cluster"
type = string
default = "evolved-npcs-cluster"
}
variable "memory_mb" {
description = "Memory for each VM in MB"
type = number
default = 2048
}
variable "vcpu_count" {
description = "Number of vCPUs for each VM"
type = number
default = 2
}
# Configure providers
provider "talos" {}
provider "libvirt" {
uri = "qemu:///system"
}
# Create a dedicated network for Talos
resource "libvirt_network" "talos_network" {
name = "talos-network"
mode = "nat"
domain = "talos.local"
addresses = ["10.5.0.0/24"]
dhcp {
enabled = true
}
dns {
enabled = true
}
}
# Generate machine secrets
resource "talos_machine_secrets" "this" {}
# Create control plane configuration
data "talos_machine_configuration" "controlplane" {
cluster_name = var.cluster_name
cluster_endpoint = "https://10.5.0.2:6443"
machine_type = "controlplane"
machine_secrets = talos_machine_secrets.this.machine_secrets
config_patches = [
yamlencode({
machine = {
install = {
disk = "/dev/vda"
}
}
})
]
}
# Download Talos kernel and initramfs
resource "null_resource" "download_talos_files" {
triggers = {
version = "v1.6.4"
}
provisioner "local-exec" {
command = <<EOT
mkdir -p _out/
curl -L -o _out/vmlinuz-amd64 https://github.com/siderolabs/talos/releases/download/${self.triggers.version}/vmlinuz-amd64
curl -L -o _out/initramfs-amd64.xz https://github.com/siderolabs/talos/releases/download/${self.triggers.version}/initramfs-amd64.xz
EOT
}
}
# Create a disk for Talos
resource "libvirt_volume" "talos-disk" {
name = "talos-disk"
pool = "default"
size = 10 * 1024 * 1024 * 1024 # 10GB
format = "qcow2"
}
# Create the QEMU domain with direct kernel boot
resource "libvirt_domain" "talos-controlplane" {
name = "talos-controlplane"
memory = var.memory_mb
vcpu = var.vcpu_count
# Use kernel and initramfs directly
kernel = "${path.module}/_out/vmlinuz-amd64"
initrd = "${path.module}/_out/initramfs-amd64.xz"
# Talos kernel parameters
cmdline = [
"talos.platform=metal",
"talos.config=${base64encode(data.talos_machine_configuration.controlplane.machine_config)}",
"ip=10.5.0.2::10.5.0.1:255.255.255.0::eth0:off",
"init_on_alloc=1",
"slab_nomerge",
"pti=on",
"console=tty0",
"console=ttyS0",
"printk.devkmsg=on"
]
disk {
volume_id = libvirt_volume.talos-disk.id
}
network_interface {
network_id = libvirt_network.talos_network.id
addresses = ["10.5.0.2"]
hostname = "controlplane"
}
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
depends_on = [null_resource.download_talos_files]
}
# Output important information
output "controlplane_ip" {
value = "10.5.0.2"
}
output "talosconfig" {
value = data.talos_machine_configuration.controlplane.machine_config
sensitive = true
}
output "next_steps" {
value = <<EOT
After applying:
1. The control plane will be available at 10.5.0.2
2. The configuration is embedded in the kernel parameters
3. Wait for Talos to install and reboot
4. Bootstrap the cluster:
talosctl bootstrap --nodes 10.5.0.2
5. Get kubeconfig:
talosctl kubeconfig --nodes 10.5.0.2
EOT
}