Files
ilkerandClaude Opus 5.5 0c11a45bc3 terraform: a libvirt tenant module, and the layout both backends share
The cluster's names, MACs, addresses and the head's vendor data move out
of the Proxmox module into modules/layout, which every backend's module
uses. Warewulf matches nodes by MAC and Slurm by name, so a second copy
would drift into clusters whose nodes boot and are never recognised. The
MAC prefix becomes a layout input; Proxmox keeps BC:24:11:E1.

modules/libvirt and envs/libvirt build the same cluster on one libvirt
host: an isolated network (no forward, no addresses, so no dnsmasq to
answer the head's PXE clients), a qcow2 overlay on a per-build head
image, a NoCloud seed carrying the login account and the layout's vendor
data, and diskless compute nodes booting from the network, with PCI
devices as managed hostdevs. dmacvicar/libvirt is pinned to 0.9.9.

Found by building on a nested-KVM test host (Debian 13, libvirt 11.3):
  - a disk given as a pool volume gets an AppArmor profile without its
    backing file; given by path it starts
  - a guest with no video device triple-faults after GRUB (it executes
    the empty VGA window at 0xa0000), looping with a silent console
  - cloud-init 24.4 on Rocky 9 cannot parse `to: default` in a v2 route
    and then applies no network config at all
  - without ACPI a q35 guest sees one CPU

Checked there: the head came up at its static address with eth0 on the
management bridge and eth1 on the fabric, its inventory written; the
second apply (started=true) started the compute nodes in place and the
plan after it was empty; a compute node's PXE requests reached the head
and none reached the LAN; destroy left no domain, network or volume.
The Proxmox environment still plans byte-identical resources.

Not yet: discovery and setup for libvirt, and the head image build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 12:38:14 +01:00

311 lines
10 KiB
Terraform

# One tenant HPC cluster: an isolated bridge, a head node, and N diskless
# compute nodes that PXE boot from it.
#
# This mirrors what scripts/00-proxmox-provision.sh builds by hand, with the
# per-tenant parameterisation the portal needs.
# Names, MACs, addresses and the head's vendor data, shared with every other
# backend's tenant module: see ../layout.
module "layout" {
source = "../layout"
cluster_id = var.cluster_id
tenant_id = var.tenant_id
compute = var.compute
mgmt_ip_cidr = var.mgmt_ip_cidr
apps = var.apps
control_plane_cert = var.control_plane_cert
}
locals {
# The vnet name, which is what a guest attaches to.
bridge = module.layout.bridge
subnet = module.layout.subnet
head_vmid = var.vmid_base
head_mgmt_mac = module.layout.head_mgmt_mac
head_clu_mac = module.layout.head_clu_mac
head_name = module.layout.head_name
compute_nodes = module.layout.compute_nodes
self_configuring = var.snippet_datastore_id != null
# Where each guest actually runs. Falling back to node_name keeps a
# single-hypervisor site from having to spell out a decision there is
# only one answer to.
head_host = var.head_host != "" ? var.head_host : var.node_name
# A vxlan fabric loses 50 bytes to encapsulation, so its bridge is 1450.
# mtu = 1 tells Proxmox to give the guest the bridge's MTU through virtio;
# without it the guest assumes 1500, and every full-sized frame - an image
# download, an MPI message - is dropped at the bridge without a word.
fabric_mtu = var.sdn_zone_type == "vxlan" ? 1 : null
compute_hosts = length(var.compute_hosts) > 0 ? var.compute_hosts : [for i in range(var.compute.count) : var.node_name]
}
# The tenant's own inventory, delivered with the VM rather than pushed to it
# afterwards.
#
# Configuration used to run from the control plane over SSH, one round trip per
# task. Measured on a 65-task provision, that transport cost ~60s of a ~100s
# run, for work that touches nothing but this host. The same roles run locally
# on the head in ~39s, and start as soon as the machine is up instead of when
# something remote gets around to connecting.
resource "proxmox_virtual_environment_file" "vendor_data" {
count = local.self_configuring ? 1 : 0
content_type = "snippets"
datastore_id = var.snippet_datastore_id
node_name = var.node_name
source_raw {
file_name = format("hpcod-c%03d-vendor.yaml", var.cluster_id)
data = module.layout.vendor_data
}
}
# The cluster's private fabric, as an SDN vnet.
#
# This used to be a bridge stanza appended to /etc/network/interfaces over
# SSH. It worked on one hypervisor and was a poor way to work on any: it
# edited a file by hand on a machine running other people's guests, and an
# early version wrote a comment without its leading '#', which would have
# taken the host's networking down at the next reload. Proxmox has a managed
# mechanism for exactly this, so the provider owns creation and removal.
#
# The zone is estate infrastructure and is referenced, not created here.
# Every cluster shares it, and having each one declare it would leave
# several Terraform states each believing they own the same object.
#
# tag is what lets this span hypervisors later. A simple zone ignores it and
# gives every node its own isolated bridge of the same name - right for a
# single-node site, wrong the moment a cluster spans two. Changing the zone
# to vlan or vxlan then needs configuration, not a rewrite.
resource "proxmox_sdn_vnet" "fabric" {
id = local.bridge
zone = var.sdn_zone
tag = var.sdn_zone_type == "simple" ? null : var.cluster_id
alias = "hpcod cluster ${var.cluster_id} (${var.tenant_name})"
# See the "down" applier below for why the vnet depends on something that
# does nothing when it is created.
depends_on = [proxmox_sdn_applier.down]
}
# SDN changes are staged and applied cluster-wide; nothing exists on a node
# until an apply runs. Separate from the vnet because Proxmox applies the
# whole pending configuration at once, not one object at a time.
#
# Two appliers, because one cannot be in the right place in the graph twice.
# An apply has to happen after the vnet is created and again after it is
# removed, and Terraform destroys in reverse dependency order - so a single
# applier that depends on the vnet is destroyed BEFORE it, and the removal
# is never applied.
#
# That is not hypothetical. Destroying c002 left the vnet gone from
# vnets.cfg but still present in the applied configuration, so Proxmox went
# on believing the fabric existed and the bridge stayed up on the host.
# Applies the creation. Depends on the vnet, so it runs after it.
resource "proxmox_sdn_applier" "up" {
depends_on = [proxmox_sdn_vnet.fabric]
on_create = true
on_destroy = false
}
# Applies the removal. The vnet depends on THIS one, which is what puts it
# later in the teardown: Terraform destroys the vnet first and this
# afterwards. On the way up it does nothing at all.
resource "proxmox_sdn_applier" "down" {
on_create = false
on_destroy = true
}
resource "proxmox_virtual_environment_vm" "head" {
name = local.head_name
description = "hpcod cluster ${var.cluster_id}, tenant ${var.tenant_id} (${var.tenant_name}) head node: Warewulf, DHCP/TFTP/PXE, Slurm controller, NAT gateway for ${local.subnet}.0/24"
tags = ["hpcod", format("cluster-%03d", var.cluster_id), format("tenant-%03d", var.tenant_id), "head"]
node_name = local.head_host
vm_id = local.head_vmid
on_boot = true
cpu {
cores = var.head.cores
type = "host"
}
memory {
dedicated = var.head.memory_gb * 1024
}
agent {
enabled = true
}
# net0: management. net1: the tenant fabric.
network_device {
bridge = var.mgmt_bridge
mac_address = local.head_mgmt_mac
model = "virtio"
}
network_device {
bridge = local.bridge
mac_address = local.head_clu_mac
model = "virtio"
mtu = local.fabric_mtu
}
# Cloning a prebuilt template is the fast path: the head node comes up with
# Warewulf, OpenHPC and Slurm already installed, so the tenant playbook only
# has to write configuration. Building from the cloud image is the fallback,
# and costs the tenant roughly twelve minutes of dnf.
dynamic "clone" {
for_each = var.head_template_vmid == null ? [] : [var.head_template_vmid]
content {
vm_id = clone.value
full = true
}
}
dynamic "disk" {
for_each = var.head_template_vmid == null ? [1] : []
content {
datastore_id = var.datastore_id
import_from = var.image_file_id
interface = "scsi0"
size = var.head.disk_gb
discard = "on"
ssd = true
iothread = true
}
}
# The clone needs one too, or head.disk_gb silently does nothing and every
# tenant gets whatever the template happens to be - which is how it behaved
# until this was noticed. A clone starts at the template's size and can
# only grow, so disk_gb is validated against it rather than quietly
# ignored when it is smaller.
#
# Space is reserved, not thin: this pool has no sparse flag, so a zvol
# holds its full size from creation. That is affordable because a head is
# per-tenant and goes away with it - verified: destroying a tenant leaves
# no orphaned volumes behind.
dynamic "disk" {
for_each = var.head_template_vmid == null ? [] : [1]
content {
datastore_id = var.datastore_id
interface = "scsi0"
size = var.head.disk_gb
discard = "on"
ssd = true
iothread = true
}
}
scsi_hardware = "virtio-scsi-single"
depends_on = [proxmox_sdn_applier.up]
initialization {
datastore_id = var.datastore_id
ip_config {
ipv4 {
address = var.mgmt_ip_cidr
gateway = var.mgmt_gateway
}
}
ip_config {
ipv4 {
address = "${local.subnet}.1/24"
}
}
user_account {
username = var.ci_user
keys = var.ssh_public_keys
}
vendor_data_file_id = local.self_configuring ? proxmox_virtual_environment_file.vendor_data[0].id : null
}
serial_device {}
operating_system {
type = "l26"
}
lifecycle {
# cloud-init regenerates this on every apply and it is not meaningful.
ignore_changes = [initialization[0].user_account[0].password]
}
}
# Diskless compute nodes.
#
# No disk block at all and boot_order fixed to the network device: the root
# filesystem is fetched from the head node at every boot and unpacked into RAM,
# which is why the caller sizes memory with a reserve on top of what the user
# asked for.
resource "proxmox_virtual_environment_vm" "compute" {
count = var.compute.count
name = local.compute_nodes[count.index].name
description = "hpcod cluster ${var.cluster_id}, tenant ${var.tenant_id} (${var.tenant_name}) compute node ${count.index + 1}: diskless, PXE-provisioned by the head node over ${local.bridge}"
tags = ["hpcod", format("tenant-%03d", var.tenant_id), "compute"]
node_name = local.compute_hosts[count.index]
vm_id = var.vmid_base + count.index + 1
on_boot = false
started = var.started
cpu {
cores = var.compute.cores
type = "host"
}
memory {
dedicated = var.compute.memory_gb * 1024
# Ballooning would fight a root filesystem that lives in RAM.
floating = 0
}
network_device {
bridge = local.bridge
mac_address = local.compute_nodes[count.index].mac
model = "virtio"
mtu = local.fabric_mtu
}
boot_order = ["net0"]
lifecycle {
precondition {
condition = length(var.compute_hosts) == 0 || length(var.compute_hosts) == var.compute.count
error_message = "compute_hosts has ${length(var.compute_hosts)} entries for ${var.compute.count} compute nodes. Give one per node or none at all: a short list silently sends the rest to the default hypervisor, which is how a GPU job ends up on the machine without the GPU."
}
}
depends_on = [proxmox_sdn_applier.up]
# Placement decides which physical devices this node gets.
dynamic "hostpci" {
for_each = lookup(var.hostpci, tostring(count.index + 1), [])
content {
device = format("hostpci%d", hostpci.key)
id = hostpci.value
pcie = true
}
}
serial_device {}
vga {
type = "serial0"
}
operating_system {
type = "l26"
}
}