-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathforward-local-net.sh
More file actions
executable file
·75 lines (59 loc) · 2.46 KB
/
forward-local-net.sh
File metadata and controls
executable file
·75 lines (59 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
set -euo pipefail
echo "[INFO] Getting NodePort services..."
NODEPORTS=$(kubectl get svc --all-namespaces \
-o jsonpath='{range .items[?(@.spec.type=="NodePort")]}{.metadata.namespace}{" "}{.metadata.name}{" "}{.spec.ports[*].nodePort}{"\n"}{end}')
if [[ -z "$NODEPORTS" ]]; then
echo "[INFO] No NodePort services found."
exit 0
fi
echo ""
echo "[INFO] Found NodePort services:"
echo "$NODEPORTS"
echo ""
echo "$NODEPORTS" | while IFS= read -r line; do
[[ -z "$line" ]] && continue
NAMESPACE=$(echo "$line" | awk '{print $1}')
NAME=$(echo "$line" | awk '{print $2}')
PORTS=$(echo "$line" | cut -d' ' -f3-)
echo "────────────────────────────────────────────"
echo "[SERVICE] $NAMESPACE/$NAME (NodePort(s): $PORTS)"
for PORT in $PORTS; do
echo "[INFO] Starting tunnel for $NAME (NodePort: $PORT)..."
# Launch minikube tunnel in background and capture output
TMPFILE=$(mktemp)
(minikube service "$NAME" -n "$NAMESPACE" --url >"$TMPFILE") &
TUNNEL_PID=$!
echo "[INFO] Waiting for tunnel to start..."
i=0
while ! grep -q "http://" "$TMPFILE"; do
sleep 0.2
i=$((i + 1))
if [[ $i -gt 50 ]]; then
echo " [ERROR] Timed out waiting for tunnel to start"
kill $TUNNEL_PID >/dev/null 2>&1 || true
rm -f "$TMPFILE"
continue 2
fi
done
RAW_URL=$(cat "$TMPFILE")
TUNNELED_PORT=$(echo "$RAW_URL" | sed -E 's|.*:([0-9]+)$|\1|')
echo "[DEBUG] RAW_URL = '$RAW_URL'"
echo "[INFO] Minikube is forwarding to: $RAW_URL"
echo "[INFO] We will bind localhost:$PORT → localhost:$TUNNELED_PORT"
if lsof -iTCP:$PORT >/dev/null 2>&1; then
echo " [SKIP] Port $PORT already in use locally"
else
echo "[INFO] Launching socat..."
nohup socat TCP4-LISTEN:$PORT,fork,reuseaddr TCP4:127.0.0.1:$TUNNELED_PORT \
>/tmp/socat-tunnel-$PORT.log 2>&1 &
SOCAT_PID=$!
echo "[OK] socat launched with PID $SOCAT_PID"
echo "[INFO] Log file: /tmp/socat-tunnel-$PORT.log"
fi
rm -f "$TMPFILE"
echo "[INFO] Leaving Minikube tunnel running in background (PID: $TUNNEL_PID)"
done
done
echo ""
echo "[DONE] All NodePorts are now mapped to localhost properly."