154 lines
No EOL
4.2 KiB
HCL
154 lines
No EOL
4.2 KiB
HCL
terraform {
|
|
required_version = ">= 1.0"
|
|
required_providers {
|
|
talos = {
|
|
source = "siderolabs/talos"
|
|
version = "~> 0.4"
|
|
}
|
|
}
|
|
}
|
|
|
|
provider "talos" {
|
|
# Configuration options
|
|
}
|
|
|
|
# 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} \
|
|
--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
|
|
}
|
|
}
|
|
|
|
resource "talos_machine_secrets" "this" {}
|
|
|
|
data "talos_machine_configuration" "this" {
|
|
cluster_name = "${var.cluster_name}"
|
|
machine_type = "controlplane"
|
|
cluster_endpoint = "https://10.5.0.1:6443"
|
|
machine_secrets = talos_machine_secrets.this.machine_secrets
|
|
}
|
|
|
|
data "talos_client_configuration" "this" {
|
|
cluster_name = "${var.cluster_name}"
|
|
client_configuration = talos_machine_secrets.this.client_configuration
|
|
nodes = ["10.5.0.2"]
|
|
}
|
|
|
|
resource "talos_machine_configuration_apply" "this" {
|
|
client_configuration = talos_machine_secrets.this.client_configuration
|
|
machine_configuration_input = data.talos_machine_configuration.this.machine_configuration
|
|
node = "10.5.0.2"
|
|
config_patches = [
|
|
yamlencode({
|
|
cluster = {
|
|
apiServer = {
|
|
certSANs = [
|
|
"- 10.5.0.1"
|
|
]
|
|
}
|
|
}
|
|
})
|
|
]
|
|
}
|
|
|
|
# resource "null_resource" "talos_cluster_patches" {
|
|
# depends_on = [null_resource.talos_cluster]
|
|
# triggers = {
|
|
# patches_hash = filesha1("${path.module}/patches/controlplane-patch.yaml")
|
|
# }
|
|
|
|
# provisioner "local-exec" {
|
|
# command = <<EOT
|
|
# # Apply ONLY your custom patches (not the full config)
|
|
# talosctl patch machineconfig \
|
|
# --patch-file patches/controlplane-patch.yaml \
|
|
# --nodes 10.5.0.2 \
|
|
# --mode reboot
|
|
# 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
|
|
} |