evolved-npcs-infra/main.tf
2025-09-27 21:34:23 -07:00

208 lines
No EOL
4.9 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 - FIXED: Use the correct data source
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
talos_version = "v1.11.2"
config_patches = [
yamlencode({
machine = {
install = {
disk = "/dev/vda"
}
}
})
]
}
data "talos_client_configuration" "this" {
cluster_name = "example-cluster"
client_configuration = talos_machine_secrets.this.client_configuration
nodes = ["10.5.0.2"]
}
# 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_configuration) # FIXED: Correct attribute
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]
}
resource "talos_machine_configuration_apply" "this" {
depends_on = [libvirt_domain.talos-controlplane]
client_configuration = talos_machine_secrets.this.client_configuration
machine_configuration_input = talos_machine_configuration.this.machine_configuration
node = "10.5.0.2"
}
resource "talos_machine_bootstrap" "this" {
depends_on = [
talos_machine_configuration_apply.this
]
node = "10.5.0.2"
client_configuration = talos_machine_secrets.this.client_configuration
}
resource "talos_cluster_kubeconfig" "this" {
depends_on = [
talos_machine_bootstrap.this
]
client_configuration = talos_machine_secrets.this.client_configuration
node = "10.5.0.2"
}
# Output important information
output "controlplane_ip" {
value = "10.5.0.2"
}
output "talos_client_config" {
value = data.talos_client_configuration.this.talos_config
sensitive = true
}
output "machine_config" {
value = data.talos_machine_configuration.controlplane.machine_configuration
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. Terraform will automatically apply the config and bootstrap the cluster
4. Check status with: talosctl --talosconfig talosconfig version --nodes 10.5.0.2
EOT
}