devcontainer: wire dev.sh to the Proxmox k3s cluster

- drop stale libvirt socket mount + LIBVIRT_DEFAULT_URI (old KVM cluster)
- auto-open an SSH tunnel (local :16443 -> primary 127.0.0.1:6443 over the
  pfSense SSH NAT) on each run so kubectl/flux/helm reach the cluster with no
  manual setup; DEV_NO_TUNNEL=1 opts out, K3S_SSH_* env overrides the NAT front
- refresh header/usage comments (bpg/proxmox, not dmacvicar/libvirt)

Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
jakman85andClaude Opus 4.8 committed 2026-08-25 14:59:19 -05:00
1 parent 21669da075
commit 0d7495486e
1 file changed
+26 -10
+26 -10
View File
@@ -1,14 +1,16 @@
#!/usr/bin/env bash
# infra/devcontainer/dev.sh — build & enter the pinned dev toolbox.
#
# Mounts the host libvirt socket and the repo so OpenTofu (dmacvicar/libvirt) and Ansible
# drive the local KVM cluster from inside the container. Rootless podman by default
# (ENGINE=docker to switch). SELinux label is disabled for the run so the container can
# use the host libvirt socket without relabeling it.
# Mounts the repo so OpenTofu (bpg/proxmox) and Ansible drive the Proxmox-hosted k3s cluster
# from inside the container. Rootless podman by default (ENGINE=docker to switch); SELinux
# label is disabled for the run. On each run it also opens a best-effort SSH tunnel to the
# cluster's kube-API over the pfSense SSH NAT, so kubectl/flux/helm reach the Proxmox cluster
# transparently (the API port itself is not NAT-forwarded). Set DEV_NO_TUNNEL=1 to skip it.
#
# ./dev.sh build # build the image only
# ./dev.sh # build (if needed) + open a shell
# ./dev.sh run tofu -version # run one command in the toolbox
# ./dev.sh build # build the image only
# ./dev.sh # build (if needed) + open a shell (kube tunnel up)
# ./dev.sh run tofu -version # run one command in the toolbox
# ./dev.sh run kubectl get nodes # talks to the Proxmox k3s cluster via the tunnel
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${HERE}/../.." && pwd)"
@@ -17,6 +19,15 @@ ENGINE="${ENGINE:-podman}"
# shellcheck disable=SC1091
source "${HERE}/versions.env"
# k3s API access: the dev cluster runs on Proxmox (nodes on 192.168.1.0/24), whose kube-API
# port is NOT directly routable from here — dev.sh opens an SSH tunnel over the pfSense SSH NAT
# (local :${K3S_API_LOCAL_PORT} -> primary node 127.0.0.1:6443) so kubectl/flux/helm work
# transparently. infra/ansible/.cache/dev.kubeconfig targets https://127.0.0.1:${K3S_API_LOCAL_PORT}.
K3S_SSH_HOST="${K3S_SSH_HOST:-192.168.2.102}" # pfSense NAT front for the primary node SSH
K3S_SSH_PORT="${K3S_SSH_PORT:-2250}" # forwards to k3s-dev-1 (192.168.1.50):22
K3S_SSH_USER="${K3S_SSH_USER:-k3s}"
K3S_API_LOCAL_PORT="${K3S_API_LOCAL_PORT:-16443}"
build() {
"$ENGINE" build \
--build-arg FEDORA_VERSION="$FEDORA_VERSION" \
@@ -36,17 +47,22 @@ run() {
# SOPS-encrypted secret). SOPS_AGE_KEY_FILE defaults to ~/.config/sops/age/keys.txt,
# which is this mount. Create it first so the bind source exists.
mkdir -p "${HOME}/.config/sops/age"
# In-container prelude (runs before your command): bring up the kube-API SSH tunnel so
# kubectl/flux/helm reach the Proxmox cluster. Best-effort — if the cluster is unreachable
# it warns and continues (tofu/ansible don't need it). Skip with DEV_NO_TUNNEL=1.
local prelude=":"
if [ "${DEV_NO_TUNNEL:-0}" != "1" ]; then
prelude="if [ -f /root/.ssh/home_network ]; then ssh -fN -L ${K3S_API_LOCAL_PORT}:127.0.0.1:6443 -p ${K3S_SSH_PORT} -i /root/.ssh/home_network -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ExitOnForwardFailure=yes -o ConnectTimeout=5 -o ServerAliveInterval=30 ${K3S_SSH_USER}@${K3S_SSH_HOST} 2>/dev/null || echo '[dev.sh] k3s API tunnel not up (cluster down or NAT changed?) — kubectl/flux/helm will not reach the cluster' >&2; fi"
fi
"$ENGINE" run --rm -it \
--network host \
--security-opt label=disable \
-v /run/libvirt:/run/libvirt \
-v "${REPO_ROOT}:/work" -w /work \
-v "${HOME}/.ssh:/root/.ssh:ro" \
-v "${HOME}/.config/sops:/root/.config/sops" \
-v "${HOME}/.config/home-k3s/ansible-vault-pass:/root/.config/home-k3s/ansible-vault-pass:ro" \
-e LIBVIRT_DEFAULT_URI=qemu:///system \
-e KUBECONFIG="${DEV_KUBECONFIG:-/work/infra/ansible/.cache/dev.kubeconfig}" \
"$IMAGE" "$@"
"$IMAGE" bash -lc "${prelude}"'; if [ "$#" -gt 0 ]; then exec "$@"; else exec bash -l; fi' dev.sh "$@"
}
case "${1:-shell}" in