Files
home-network-k3s-impl/infra/tofu/main.tf
T
jakman85andClaude Opus 5 c1f24e4a6d fix(dns): resilient node resolvers — mechanism-detecting, per-environment (R-DNS-01/03)
Re-authors the fix stranded on origin/fix/node-dns-resilient-upstream (7a34590). That
commit was NOT landed: it wrote a netplan drop-in and ran `netplan apply`, but netplan
does not exist on these nodes any more (Debian 13 trixie generic cloud image; not in
node_baseline_packages). Landing it would have failed the handler with command-not-found
and BROKEN node-baseline.yml on every node — worse than not landing it. Its dev-LAN
literals also sat in role defaults, which prod inherits, pointing prod nodes at a PiHole
on a network they cannot reach.

The underlying problem is real and verified: a SINGLE upstream resolver is a hard SPOF.
PiHole answers jackson.lan instantly but its external recursion flaps — 0/25 for public
names (registry-1.docker.io, quay.io, ghcr.io) while gitea.jackson.lan resolved 5/5 —
breaking containerd image pulls and CoreDNS `forward . /etc/resolv.conf`.

MECHANISM DETECTION (new `dns` tag in node_baseline). Five read-only probes feed one
set_fact yielding three outcomes, because the two environments no longer share a resolver
stack (dev-1 is behind resolved's 127.0.0.53 stub; prod-1 has a direct resolv.conf and no
cloud-init at all):
  * systemd-resolved — resolved running AND owning /etc/resolv.conf (proven by the
    symlink pointing into /run/systemd/resolve/, or by every nameserver being a resolved
    loopback). Writes resolved.conf.d/99-k3s-dns.conf with an ordered DNS=, Domains=, and
    FallbackDNS= EXPLICITLY EMPTIED so systemd's compiled-in public resolvers can never
    silently bypass the local zone and ad-blocking.
  * resolv-conf-file — plain directly-managed file (prod's shape). Ordered nameserver
    lines + options timeout:2 attempts:2, backup: true. Deliberately NO `rotate`, which
    would destroy PiHole-first ordering.
  * contended — resolvconf/openresolv present or NetworkManager active. Writes NOTHING
    and fails loudly: a change that silently gets reverted is worse than no change.
Resolved-running-but-not-owning-resolv.conf correctly falls through to the file branch.
Ordering is the policy that keeps the local zone working — the second server is only
consulted after the first fails, so a gateway NXDOMAIN can never beat PiHole to a
jackson.lan answer.

SEAMS. Dev values in role defaults ([192.168.1.18, 192.168.1.1], search jackson.lan);
prod values in inventory/group_vars/k3s_prod/vars.yml. NOT cluster-vars — that ConfigMap
is consumed by Flux postBuild for platform/ and apps/, and no Ansible code reads it.

PROD'S SECOND UPSTREAM IS AN OPEN OPERATOR DECISION and is left as a loud
BLOCKED-pending-second-upstream placeholder, the same precedent as the node_baseline disk
devices, with a dns-tagged assert that stops the play before any file is written. A
public resolver was NOT invented as a default: it would bypass the local zone, which is a
decision, not a default. PROD-PROVISIONING.md §4 was rewritten (the old bullet pointed at
the now-dropped branch) to state the choice, including that prod routing to the dev
PiHole is unverified across the pfSense boundary.

tofu: servers = [var.dns_server, var.network_gateway] — the one hunk of the old branch
worth taking as-is. Dev-only by nature.

ALSO FIXES A FALSE WARN THIS WOULD OTHERWISE CREATE: node_health_collect.py parsed
/etc/resolv.conf only, so on dev it read the 127.0.0.53 stub and would report a permanent
single-upstream WARN even after a correct fix. It now asks resolvectl for the real
upstreams when it sees the stub — and if that fails, nameservers is NULL with the reason
recorded, never the stub and never a guess. assess.yml turns null into SKIP, not WARN and
not PASS. The honesty discipline is preserved exactly.

Verified with NO play run against any node: syntax-check + yamllint + tofu fmt clean;
--tags packages still selects ONLY the apt task (the prod package-floor escape hatch
bypasses DNS entirely); --list-hosts unchanged; -c local simulation driving the REAL
detection expression and REAL file bodies extracted from the role by script (so the test
cannot drift) passes all 5 scenarios — dev stub -> systemd-resolved, prod shape ->
resolv-conf-file, NetworkManager and resolvconf -> contended; dev defaults pass the
assert byte-identically while prod overrides fail it by name; collector unit-tested
against 8 resolver shapes.

Serves: R-DNS-01, R-DNS-03, R-ENV-02, R-ENV-03.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-09-01 00:11:49 -05:00

184 lines
8.2 KiB
Terraform

# main.tf — 3 cloud-init'd Proxmox VE VMs (k3s-dev-1/2/3), each with an imported
# OS disk (from the Debian cloud image) and a blank Longhorn data disk (R-ENV-01).
#
# Node lifecycle stage 1 ("Create") per docs/02. cloud-init (infra/cloud-init/)
# does stage 2 ("Baseline"); Ansible (infra/ansible/) does stage 3 ("Cluster-join").
#
# Provider: bpg/proxmox. VMs land on the Proxmox node `var.proxmox_node`, disks on
# `var.datastore` (ZFS "Storage"), NIC on `var.bridge` (vmbr0), static IPs via the
# native cloud-init `initialization` block. Native cloud-init (login user + SSH keys,
# static IP/gateway/DNS) is set via the Proxmox 8006 API. The rich first-boot baseline
# (packages, kernel modules, Longhorn mount, swap-off) is delivered by the Ansible
# `node_baseline` role (infra/ansible), run before k3s — not as a cloud-init snippet.
locals {
# Per-node identity/addressing, KEYED BY NODE NAME (map, not list).
# for_each over this map gives every node a stable, name-based state address
# (…vm.node["k3s-dev-2"]) instead of a positional one. Positional count indexes
# are brittle under -target/-replace: a single-node destroy on a count-indexed
# resource can cascade to siblings (F1 finding). Name keys are individually
# addressable, so one node can be replaced/destroyed without disturbing the others.
nodes = {
for i in range(var.node_count) : "${var.node_name_prefix}${i + 1}" => {
name = "${var.node_name_prefix}${i + 1}"
ip = var.node_ips[i]
fqdn = "${var.node_name_prefix}${i + 1}.${var.node_domain}"
is_first = i == 0
}
}
# Ordered (node1..nodeN) list + primary view for outputs that need positional
# access — the resource for_each uses the map above; these preserve ordering for
# the Ansible inventory (primary = cluster-init server, rest = secondaries).
node_list = [for i in range(var.node_count) : local.nodes["${var.node_name_prefix}${i + 1}"]]
primary = local.nodes["${var.node_name_prefix}1"]
}
# --- base OS cloud image (downloaded once to `local`, imported per node) -----
# content_type "import" (PVE 8.2+): the image must be on `import`-content storage to
# be usable as a VM disk `import_from` (uncompressed qcow2 only — ours qualifies).
# OPERATOR ACTION: enable the "Import" content type on var.image_datastore (Datacenter
# -> Storage -> local -> Content -> check "Import"), else the disk import fails with
# "wrong type 'iso' - needs to be 'images' or 'import'".
#
# The Debian cloud image is a bpg `download_file` resource. This intentionally uses the
# LONG (deprecated) type name `proxmox_virtual_environment_download_file`: the R2 backend
# state still holds this resource under the long name, and bpg >= 0.111 dropped the
# cross-type `moved{}` linkage to the short `proxmox_download_file` (ADR-007) — a `moved`
# block between the two now fails plan with "Resource type mismatch" (this broke plan on
# main entirely). Keeping the long name makes config match state, so the plan is clean and
# the ~450 MB image is NOT re-downloaded. Migrating to the short name is a SEPARATE,
# deliberate step (`tofu state mv proxmox_virtual_environment_download_file.debian_cloud_image
# proxmox_download_file.debian_cloud_image`, then rename here) — not bundled with the
# etcd-disk change so this bounce stays focused and low-risk.
resource "proxmox_virtual_environment_download_file" "debian_cloud_image" {
content_type = "import"
datastore_id = var.image_datastore
node_name = var.proxmox_node
url = var.image_source
file_name = var.image_file_name
overwrite = false
}
# --- cloud-init: NATIVE Proxmox cloud-init only (API-driven) ------------------
# The rich first-boot baseline (packages, kernel modules, Longhorn disk, swap-off)
# is NOT delivered as a custom snippet any more — custom snippets require SSH/SFTP
# to the Proxmox host, which is not reachable here. Instead the VM's `initialization`
# block below uses bpg's native user_account/ip_config/dns, which Proxmox sets via
# the built-in cloud-init API options (ciuser/sshkeys/ipconfig0/nameserver). The
# baseline moved to the Ansible `node_baseline` role (infra/ansible), run before k3s.
# --- the VMs ----------------------------------------------------------------
resource "proxmox_virtual_environment_vm" "node" {
for_each = local.nodes
name = each.value.name
node_name = var.proxmox_node
tags = ["k3s", "k3s-dev", "opentofu"]
description = "k3s dev node ${each.value.name} (${each.value.fqdn}) — provisioned by OpenTofu."
on_boot = true
stop_on_destroy = true
# `host` CPU type passes through the physical CPU so nested virt is available for
# KubeVirt in Phase 5.
cpu {
cores = var.vcpu
type = var.cpu_type
}
memory {
dedicated = var.memory_mib
}
# qemu-guest-agent is installed by the Ansible node_baseline role now (not cloud-init),
# so don't block VM create on the agent — static IPs are assigned, not discovered.
agent {
enabled = false
}
operating_system {
type = "l26"
}
# Serial console (socket): Debian cloud images boot with console=ttyS0 and can panic
# on import+resize when no serial device is present (bpg #1639). Standard + harmless
# for cloud images; the default VGA display is kept so the Proxmox UI console works too.
serial_device {
device = "socket"
}
# OS disk (virtio0 -> /dev/vda, boot): a copy of the imported cloud image, grown
# to os_disk_gib. Longhorn data disk (virtio1 -> /dev/vdb): blank, data_disk_gib.
# virtio* keeps the guest device names (/dev/vda, /dev/vdb) the cloud-init
# fs_setup/mount for /var/lib/longhorn already expects.
disk {
datastore_id = var.datastore
import_from = proxmox_virtual_environment_download_file.debian_cloud_image.id
interface = "virtio0"
size = var.os_disk_gib
discard = "on"
}
disk {
datastore_id = var.datastore
interface = "virtio1"
size = var.data_disk_gib
discard = "on"
}
# Dedicated SSD etcd disk (virtio2 -> /dev/vdc). Isolates etcd WAL fsync from the HDD
# `Storage` pool where the OS + Longhorn disks live — the node_baseline role formats
# it and mounts it at /var/lib/rancher/k3s/server/db before k3s starts. On the SSD
# `local-zfs` (rpool/data) datastore: an enterprise Micron SATA SSD mirror WITH
# power-loss protection, so cache=none (honor guest fsync straight through to the ZFS
# ZIL) is both durable and fast. Kept last (ascending interface) so bpg won't reorder
# the existing virtio0/virtio1 disks. See docs/plans/ssd-etcd-isolation.md.
disk {
datastore_id = var.etcd_ssd_datastore
interface = "virtio2"
size = var.etcd_disk_gib
discard = "on"
cache = "none"
iothread = false
}
network_device {
bridge = var.bridge
}
# Static addressing + DNS come from cloud-init (bpg generates the network-config),
# so the libvirt netplan template is gone. Native cloud-init (login user + SSH keys)
# comes from user_account below; the OS baseline is the Ansible node_baseline role.
initialization {
datastore_id = var.datastore
ip_config {
ipv4 {
address = "${each.value.ip}/${var.network_prefix}"
gateway = var.network_gateway
}
}
# DNS resilience (dev only — tofu provisions the dev VMs; prod is bare metal with no
# cloud-init). A SINGLE upstream was a hard SPOF: PiHole (var.dns_server) answers the
# local jackson.lan zone but its external recursion flaps, which broke containerd image
# pulls and CoreDNS forwarding. PiHole stays FIRST — the local zone and network
# ad-blocking are unchanged — and the pfSense gateway (var.network_gateway) is added
# purely as a fallback for external recursion. Order is the policy, not a preference.
# This keeps a freshly built node's cloud-init in parity with what the Ansible
# node_baseline role's `dns`-tagged block configures on an already-running node
# (node_baseline_dns_servers), so a rebuild does not regress to one resolver.
dns {
servers = [var.dns_server, var.network_gateway]
}
user_account {
username = var.login_user
keys = [trimspace(var.ssh_authorized_key)]
}
}
# Boot from the OS disk only.
boot_order = ["virtio0"]
}