fix: persist edge route to Pimox Traefik
This commit is contained in:
parent
e240650bc8
commit
2ec6e53002
|
|
@ -23,6 +23,7 @@ mapping = {
|
|||
"network.lan_cidr": "LAB_LAN_CIDR",
|
||||
"network.lan_ip_prefix": "LAB_LAN_IP_PREFIX",
|
||||
"network.metallb.traefik_ip": "LAB_TRAEFIK_LB_IP",
|
||||
"network.tailscale_cgnat_cidr": "LAB_TAILSCALE_CGNAT_CIDR",
|
||||
"cluster.active_profile": "LAB_CLUSTER_PROFILE",
|
||||
"hosts.debian.user": "LAB_DEBIAN_USER",
|
||||
"hosts.debian.lan_ip": "LAB_DEBIAN_LAN_IP",
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ mapping = {
|
|||
"network.lan_cidr": "LAB_LAN_CIDR",
|
||||
"network.lan_ip_prefix": "LAB_LAN_IP_PREFIX",
|
||||
"network.metallb.traefik_ip": "LAB_TRAEFIK_LB_IP",
|
||||
"network.tailscale_cgnat_cidr": "LAB_TAILSCALE_CGNAT_CIDR",
|
||||
"cluster.active_profile": "LAB_CLUSTER_PROFILE",
|
||||
"hosts.debian.user": "LAB_DEBIAN_USER",
|
||||
"hosts.debian.lan_ip": "LAB_DEBIAN_LAN_IP",
|
||||
|
|
@ -6289,6 +6290,8 @@ ensure_edge_tailscale_routes() {
|
|||
local enabled="${LAB_EDGE_CONFIGURE_TAILSCALE_ROUTES:-true}"
|
||||
local route="${LAB_EDGE_TAILSCALE_SUBNET_ROUTE:-${LAB_LAN_CIDR:-192.168.100.0/24}}"
|
||||
local router="${LAB_EDGE_TAILSCALE_ROUTER:-debian}"
|
||||
local traefik_ip="${LAB_TRAEFIK_LB_IP:-}"
|
||||
local tailscale_cidr="${LAB_TAILSCALE_CGNAT_CIDR:-100.64.0.0/10}"
|
||||
local rpi_host="${LAB_RPI_HOST:-${LAB_RASPBERRY_HOST:-192.168.100.89}}"
|
||||
local rpi_user="${LAB_RPI_USER:-${LAB_RASPBERRY_USER:-jv}}"
|
||||
local rpi_key="${LAB_RPI_SSH_KEY_PATH:-${LAB_RASPBERRY_SSH_KEY_PATH:-/home/jv/.ssh/id_ed25519}}"
|
||||
|
|
@ -6307,6 +6310,18 @@ ensure_edge_tailscale_routes() {
|
|||
echo "Invalid LAB_EDGE_TAILSCALE_SUBNET_ROUTE '${route}'." >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ -z "${traefik_ip}" ]]; then
|
||||
echo "LAB_TRAEFIK_LB_IP is required to configure edge-to-Traefik routing." >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! validate_ipv4_cidr_or_host "${traefik_ip}" || [[ "${traefik_ip}" == */* ]]; then
|
||||
echo "Invalid LAB_TRAEFIK_LB_IP '${traefik_ip}'." >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! validate_ipv4_cidr_or_host "${tailscale_cidr}" || [[ "${tailscale_cidr}" != */* ]]; then
|
||||
echo "Invalid LAB_TAILSCALE_CGNAT_CIDR '${tailscale_cidr}'." >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ -z "${edge_host}" ]]; then
|
||||
echo "LAB_EDGE_HOST or TF_VAR_edge_host is required to configure edge Tailscale routes." >&2
|
||||
exit 1
|
||||
|
|
@ -6323,6 +6338,67 @@ ensure_edge_tailscale_routes() {
|
|||
printf '%s\n' 'net.ipv4.ip_forward = 1' | sudo tee /etc/sysctl.d/99-homelab-tailscale-subnet-router.conf >/dev/null
|
||||
sudo sysctl -w net.ipv4.ip_forward=1 >/dev/null
|
||||
sudo tailscale set --advertise-routes="${route}"
|
||||
sudo tee /usr/local/sbin/homelab-edge-traefik-route.sh >/dev/null <<ROUTE_SCRIPT_EOF
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
traefik_ip='${traefik_ip}'
|
||||
tailscale_cidr='${tailscale_cidr}'
|
||||
|
||||
sysctl_bin="\$(command -v sysctl 2>/dev/null || true)"
|
||||
if [ -z "\$sysctl_bin" ] && [ -x /usr/sbin/sysctl ]; then
|
||||
sysctl_bin=/usr/sbin/sysctl
|
||||
fi
|
||||
if [ -n "\$sysctl_bin" ]; then
|
||||
"\$sysctl_bin" -w net.ipv4.ip_forward=1 >/dev/null
|
||||
fi
|
||||
|
||||
lan_if="\$(ip route get "\$traefik_ip" | awk '{print \$5; exit}')"
|
||||
if [ -z "\$lan_if" ]; then
|
||||
echo "Could not resolve LAN interface for \$traefik_ip." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
iptables_bin="\$(command -v iptables 2>/dev/null || true)"
|
||||
if [ -z "\$iptables_bin" ] && [ -x /usr/sbin/iptables ]; then
|
||||
iptables_bin=/usr/sbin/iptables
|
||||
fi
|
||||
if [ -z "\$iptables_bin" ]; then
|
||||
echo 'iptables is required to configure edge-to-Traefik forwarding.' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
iptables_cmd() {
|
||||
"\$iptables_bin" "\$@"
|
||||
}
|
||||
|
||||
iptables_cmd -C FORWARD -i tailscale0 -o "\$lan_if" -s "\$tailscale_cidr" -d "\$traefik_ip" -m comment --comment homelab-edge-traefik-route -j ACCEPT 2>/dev/null ||
|
||||
iptables_cmd -I FORWARD 1 -i tailscale0 -o "\$lan_if" -s "\$tailscale_cidr" -d "\$traefik_ip" -m comment --comment homelab-edge-traefik-route -j ACCEPT
|
||||
|
||||
iptables_cmd -C FORWARD -i "\$lan_if" -o tailscale0 -s "\$traefik_ip" -d "\$tailscale_cidr" -m comment --comment homelab-edge-traefik-route -j ACCEPT 2>/dev/null ||
|
||||
iptables_cmd -I FORWARD 1 -i "\$lan_if" -o tailscale0 -s "\$traefik_ip" -d "\$tailscale_cidr" -m comment --comment homelab-edge-traefik-route -j ACCEPT
|
||||
|
||||
iptables_cmd -t nat -C POSTROUTING -s "\$tailscale_cidr" -d "\$traefik_ip" -o "\$lan_if" -m comment --comment homelab-edge-traefik-route -j MASQUERADE 2>/dev/null ||
|
||||
iptables_cmd -t nat -I POSTROUTING 1 -s "\$tailscale_cidr" -d "\$traefik_ip" -o "\$lan_if" -m comment --comment homelab-edge-traefik-route -j MASQUERADE
|
||||
ROUTE_SCRIPT_EOF
|
||||
sudo chmod 0755 /usr/local/sbin/homelab-edge-traefik-route.sh
|
||||
sudo tee /etc/systemd/system/homelab-edge-traefik-route.service >/dev/null <<'ROUTE_SERVICE_EOF'
|
||||
[Unit]
|
||||
Description=Homelab edge Tailscale route to Traefik MetalLB
|
||||
After=network-online.target tailscaled.service
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/local/sbin/homelab-edge-traefik-route.sh
|
||||
RemainAfterExit=yes
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
ROUTE_SERVICE_EOF
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable homelab-edge-traefik-route.service >/dev/null
|
||||
sudo systemctl restart homelab-edge-traefik-route.service
|
||||
;;
|
||||
rpi | rpi4 | raspberrypi)
|
||||
echo "Ensuring RPi advertises ${route} and OCI edge accepts Tailscale routes..."
|
||||
|
|
|
|||
Loading…
Reference in New Issue