Skip to content

Commit 5d8d321

Browse files
committed
fix: Enhance default route handling in locals.tf for improved reliability
This commit updates the logic in locals.tf to enhance the handling of default routes for the private interface. The changes include a more robust method for checking existing routes and modifying them based on the current metric, ensuring that the configuration adapts to varying network conditions. This improvement addresses potential issues with route persistence and error handling, contributing to a more stable network setup in response to recent DHCP behavior changes.
1 parent ea5c8c5 commit 5d8d321

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

locals.tf

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ locals {
100100
join("\n", [
101101
"# Ensure persistent private-network default route (Hetzner DHCP change Aug 11, 2025)",
102102
"set +e # Allow idempotent network adjustments",
103-
"METRIC=512",
103+
"METRIC=30000",
104104
"",
105105
"# Determine the private interface dynamically (no hardcoded eth1)",
106106
"PRIV_IF=$(ip -4 route show ${var.network_ipv4_cidr} 2>/dev/null | awk '{for(i=1;i<=NF;i++) if($i==\"dev\"){print $(i+1); exit}}' | head -n 1)",
@@ -116,8 +116,19 @@ locals {
116116
" if [ -n \"$NM_CONN\" ]; then",
117117
" # Persist a default route via the private gateway with higher metric than public NICs",
118118
" ROUTE_READY=0",
119-
" if nmcli -g ipv4.routes connection show \"$NM_CONN\" | grep -qE \"^0\\.0\\.0\\.0/0[[:space:]]+${local.network_gw_ipv4}([[:space:]]|$)\"; then",
120-
" ROUTE_READY=1",
119+
" ROUTE_LINE=$(nmcli -g ipv4.routes connection show \"$NM_CONN\" | tr ',' '\\n' | awk '$1==\"0.0.0.0/0\" && $2==\"${local.network_gw_ipv4}\"{print $0; exit}')",
120+
" if [ -n \"$ROUTE_LINE\" ]; then",
121+
" CUR_ROUTE_METRIC=$(echo \"$ROUTE_LINE\" | awk '{print $3}')",
122+
" if [ -z \"$CUR_ROUTE_METRIC\" ] || [ \"$CUR_ROUTE_METRIC\" != \"$METRIC\" ]; then",
123+
" nmcli connection modify \"$NM_CONN\" -ipv4.routes \"$ROUTE_LINE\" >/dev/null 2>&1 || true",
124+
" if nmcli connection modify \"$NM_CONN\" +ipv4.routes \"0.0.0.0/0 ${local.network_gw_ipv4} $METRIC\" >/dev/null 2>&1; then",
125+
" ROUTE_READY=1",
126+
" else",
127+
" echo \"Warning: Failed to update default route metric on $PRIV_IF. Node may be affected by Hetzner DHCP changes.\" >&2",
128+
" fi",
129+
" else",
130+
" ROUTE_READY=1",
131+
" fi",
121132
" else",
122133
" if nmcli connection modify \"$NM_CONN\" +ipv4.routes \"0.0.0.0/0 ${local.network_gw_ipv4} $METRIC\" >/dev/null 2>&1; then",
123134
" ROUTE_READY=1",
@@ -134,7 +145,13 @@ locals {
134145
" fi",
135146
" fi",
136147
" # Runtime guard to cover current leases before dispatcher hooks fire",
137-
" if ! ip -4 route show default | grep -qE \" via ${local.network_gw_ipv4} dev $PRIV_IF([[:space:]]|$)\" ; then",
148+
" EXISTING_RT=$(ip -4 route show default dev \"$PRIV_IF\" | awk '$3==\"${local.network_gw_ipv4}\"{print $0; exit}')",
149+
" if [ -n \"$EXISTING_RT\" ]; then",
150+
" CUR_RT_METRIC=$(echo \"$EXISTING_RT\" | awk 'match($0,/metric ([0-9]+)/,m){print m[1]}')",
151+
" if [ -z \"$CUR_RT_METRIC\" ] || [ \"$CUR_RT_METRIC\" != \"$METRIC\" ]; then",
152+
" ip -4 route change default via ${local.network_gw_ipv4} dev \"$PRIV_IF\" metric $METRIC 2>/dev/null || true",
153+
" fi",
154+
" else",
138155
" ip -4 route add default via ${local.network_gw_ipv4} dev \"$PRIV_IF\" metric $METRIC 2>/dev/null || true",
139156
" fi",
140157
"else",

0 commit comments

Comments
 (0)