11#! /bin/bash
22# Starts kubectl port-forward for mocked-server and sample-app services.
3- # Called by terraform null_resource after deployments are ready .
3+ # Includes keepalive monitoring to restart if port-forward drops .
44# Usage: ./port-forward.sh <kubeconfig> <namespace> <app_port>
55
66set -e
@@ -11,13 +11,60 @@ APP_PORT="$3"
1111
1212export KUBECONFIG
1313
14- kubectl -n " $NS " wait --for=condition=ready pod -l app=aoc --timeout=300s || true
14+ # Wait for collector pod (operator-managed or direct deployment)
15+ kubectl -n " $NS " wait --for=condition=ready pod -l app=aoc --timeout=300s 2> /dev/null || \
16+ kubectl -n " $NS " wait --for=condition=ready pod -l app.kubernetes.io/name=aoc-collector --timeout=300s 2> /dev/null || true
1517
16- nohup kubectl -n " $NS " port-forward svc/mocked-server 18081:80 > /dev/null 2>&1 &
17- echo $! > /tmp/pf_mocked.pid
18+ # Wait for sample app pod
19+ kubectl -n " $NS " wait --for=condition=ready pod -l app=sample-app --timeout=300s 2> /dev/null || true
1820
19- nohup kubectl -n " $NS " port-forward svc/sample-app 18080:" $APP_PORT " > /dev/null 2>&1 &
20- echo $! > /tmp/pf_sample.pid
21+ # Start port-forward with keepalive monitor
22+ start_port_forward () {
23+ local SVC=" $1 " LOCAL_PORT=" $2 " REMOTE_PORT=" $3 " PID_FILE=" $4 " LOG_FILE=" $5 "
2124
22- sleep 2
23- curl -sf http://localhost:18081/ > /dev/null || echo " WARN: mocked-server port-forward not ready"
25+ # Kill existing if any
26+ kill $( cat " $PID_FILE " 2> /dev/null) 2> /dev/null || true
27+
28+ nohup kubectl -n " $NS " port-forward " svc/$SVC " " $LOCAL_PORT :$REMOTE_PORT " > " $LOG_FILE " 2>&1 &
29+ echo $! > " $PID_FILE "
30+ }
31+
32+ # Start a background keepalive monitor that checks and restarts port-forwards
33+ start_keepalive () {
34+ (
35+ while true ; do
36+ sleep 10
37+ # Check sample-app port-forward
38+ if ! kill -0 $( cat /tmp/pf_sample.pid 2> /dev/null) 2> /dev/null; then
39+ echo " [keepalive] sample-app port-forward died, restarting..." >> /tmp/pf_keepalive.log
40+ start_port_forward " sample-app" 18080 " $APP_PORT " /tmp/pf_sample.pid /tmp/pf_sample.log
41+ sleep 2
42+ fi
43+ # Check mocked-server port-forward
44+ if ! kill -0 $( cat /tmp/pf_mocked.pid 2> /dev/null) 2> /dev/null; then
45+ echo " [keepalive] mocked-server port-forward died, restarting..." >> /tmp/pf_keepalive.log
46+ start_port_forward " mocked-server" 18081 80 /tmp/pf_mocked.pid /tmp/pf_mocked.log
47+ sleep 2
48+ fi
49+ done
50+ ) &
51+ echo $! > /tmp/pf_keepalive.pid
52+ }
53+
54+ # Initial start
55+ start_port_forward " mocked-server" 18081 80 /tmp/pf_mocked.pid /tmp/pf_mocked.log
56+ start_port_forward " sample-app" 18080 " $APP_PORT " /tmp/pf_sample.pid /tmp/pf_sample.log
57+
58+ # Start keepalive monitor
59+ start_keepalive
60+
61+ # Wait for port forwards to establish
62+ for i in $( seq 1 15) ; do
63+ if curl -sf http://localhost:18080/ > /dev/null 2>&1 ; then
64+ echo " INFO: sample-app port-forward ready"
65+ break
66+ fi
67+ sleep 2
68+ done
69+
70+ curl -sf http://localhost:18080/ > /dev/null 2>&1 || echo " WARN: sample-app port-forward not ready after 30s"
0 commit comments