evolved-npcs-infra/control_plane/main.tf
2025-09-28 17:52:27 -07:00

103 lines
No EOL
2.8 KiB
HCL

terraform {
required_version = ">= 1.0"
required_providers {
talos = {
source = "siderolabs/talos"
version = "~> 0.4"
}
}
}
# 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
}
variable "talos_version" {
description = "The version of Talos to use"
type = string
default = "v1.11.1"
}
resource "null_resource" "talos_cluster" {
triggers = {
cluster_name = var.cluster_name
memory_mb = var.memory_mb
vcpu_count = var.vcpu_count
talos_version = var.talos_version
config_hash = sha1(join("", [
var.cluster_name,
tostring(var.memory_mb),
tostring(var.vcpu_count),
var.talos_version,
filesha1("${path.module}/cleanup.sh") # Recreate if cleanup script changes
]))
}
# Download Talos kernel and initramfs
provisioner "local-exec" {
command = <<EOT
mkdir -p _out/
curl -L -o _out/vmlinuz-arm64 https://github.com/siderolabs/talos/releases/download/${self.triggers.talos_version}/vmlinuz-arm64
curl -L -o _out/initramfs-arm64.xz https://github.com/siderolabs/talos/releases/download/${self.triggers.talos_version}/initramfs-arm64.xz
EOT
}
provisioner "local-exec" {
command = <<EOT
set -e # Exit on error
# Cleaning up any existing configs for this cluster
chmod +x ${path.module}/cleanup.sh
${path.module}/cleanup.sh ${self.triggers.cluster_name}
# Now create the cluster
echo "Creating new cluster..."
sudo --preserve-env=HOME talosctl cluster create \
--provisioner=qemu \
--name ${self.triggers.cluster_name} \
--talosconfig ${path.module}/config/talosconfig \
--controlplanes 1 \
--workers 0 \
--memory ${self.triggers.memory_mb} \
--cpus ${self.triggers.vcpu_count}
# Change ownership of the generated files in ~/.talos and ~/.kube
sudo chown -R $(id -u):$(id -g) $HOME/.talos
sudo chown -R $(id -u):$(id -g) $HOME/.kube
EOT
}
provisioner "local-exec" {
when = destroy
command = <<EOT
chmod +x ${path.module}/cleanup.sh
${path.module}/cleanup.sh ${self.triggers.cluster_name}
EOT
}
}
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
}