diff --git a/k8s/apps/pasarguard/configmap-scripts.yaml b/k8s/apps/pasarguard/configmap-scripts.yaml index 860349c..fb87b64 100644 --- a/k8s/apps/pasarguard/configmap-scripts.yaml +++ b/k8s/apps/pasarguard/configmap-scripts.yaml @@ -172,31 +172,43 @@ data: mv /tmp/v2ray-exporter /usr/local/bin/v2ray-exporter chmod +x /usr/local/bin/v2ray-exporter - # Wait for xray API port file from pasarguard-node container - echo "Waiting for xray API port file..." + # Wait for initial API port file + echo "Waiting for initial xray API port file..." + while [ ! -f /shared/xray-api-port ]; do + echo "Waiting for API port file..." + sleep 2 + done + + # Main loop - restart exporter if it crashes or port changes while true; do if [ -f /shared/xray-api-port ]; then API_PORT=$(cat /shared/xray-api-port) if [ -n "$API_PORT" ]; then - echo "Got xray API port from shared volume: $API_PORT" + echo "Starting v2ray-exporter with endpoint 127.0.0.1:$API_PORT" + /usr/local/bin/v2ray-exporter --v2ray-endpoint "127.0.0.1:$API_PORT" --listen ":9550" & + EXPORTER_PID=$! - # Verify the port is working - if curl -s -o /dev/null -w "%{http_code}" --max-time 2 "127.0.0.1:$API_PORT" 2>&1 | grep -q "Received HTTP/0.9"; then - echo "Verified API port: $API_PORT" - break - else - echo "Port verification failed, waiting..." - fi + # Wait for exporter to exit or port file to change + while kill -0 $EXPORTER_PID 2>/dev/null; do + if [ -f /shared/xray-api-port ]; then + NEW_PORT=$(cat /shared/xray-api-port) + if [ "$NEW_PORT" != "$API_PORT" ]; then + echo "API port changed from $API_PORT to $NEW_PORT, restarting exporter" + kill $EXPORTER_PID 2>/dev/null + wait $EXPORTER_PID 2>/dev/null + break + fi + fi + sleep 5 + done + + echo "Exporter stopped, restarting..." + wait $EXPORTER_PID 2>/dev/null fi fi - echo "Waiting for valid xray API port... retrying in 5 seconds" - sleep 5 + sleep 2 done - # Start exporter - echo "Starting v2ray-exporter with endpoint 127.0.0.1:$API_PORT" - exec /usr/local/bin/v2ray-exporter --v2ray-endpoint "127.0.0.1:$API_PORT" --listen ":9550" - pasarguard-start.sh: | #!/bin/sh # Read API_KEY from shared volume created by init container @@ -213,21 +225,25 @@ data: ./main & MAIN_PID=$! - # Wait a bit for xray to start - sleep 10 + # Start continuous port monitoring in background + { + sleep 10 # Wait for xray to start initially + LAST_PORT="" + + while true; do + API_PORT=$(netstat -tlpn | grep xray | grep 127.0.0.1 | awk '{print $4}' | cut -d: -f2 | head -1) + if [ -n "$API_PORT" ] && [ "$API_PORT" != "$LAST_PORT" ]; then + echo "Found xray API port: $API_PORT" + echo -n "$API_PORT" > /shared/xray-api-port + LAST_PORT="$API_PORT" + fi + sleep 5 # Check every 5 seconds + done + } & + PORT_MONITOR_PID=$! - # Find xray API port and save to shared volume - echo "Looking for xray API port..." - for i in {1..60}; do - API_PORT=$(netstat -tlpn | grep xray | grep 127.0.0.1 | awk '{print $4}' | cut -d: -f2 | head -1) - if [ -n "$API_PORT" ]; then - echo "Found xray API port: $API_PORT" - echo -n "$API_PORT" > /shared/xray-api-port - break - fi - echo "Waiting for xray API port... ($i/60)" - sleep 1 - done - - # Continue running main process + # Wait for main process to finish wait $MAIN_PID + + # Clean up port monitor + kill $PORT_MONITOR_PID 2>/dev/null