diff --git a/k8s/apps/pasarguard/configmap-scripts.yaml b/k8s/apps/pasarguard/configmap-scripts.yaml index 763f456..20f27cb 100644 --- a/k8s/apps/pasarguard/configmap-scripts.yaml +++ b/k8s/apps/pasarguard/configmap-scripts.yaml @@ -236,29 +236,52 @@ data: cd /app - # Start main process in background - ./main & - MAIN_PID=$! - - # Start continuous port monitoring in background + write_xray_api_port() { + API_PORT="$1" + case "$API_PORT" in + ""|*[!0-9]*) + return + ;; + esac + + CURRENT_PORT="" + if [ -f /shared/xray-api-port ]; then + CURRENT_PORT=$(cat /shared/xray-api-port) + fi + + if [ "$API_PORT" != "$CURRENT_PORT" ]; then + echo "Found xray API port: $API_PORT" + echo -n "$API_PORT" > /shared/xray-api-port + fi + } + + LOG_PIPE="/tmp/pasarguard-main.log" + rm -f "$LOG_PIPE" + mkfifo "$LOG_PIPE" + + # Capture main logs so the Xray API listener is not confused with Xray's metrics listener. { - 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 + while IFS= read -r line; do + echo "$line" + case "$line" in + *"transport/internet/tcp: listening TCP on 127.0.0.1:"*) + API_PORT=$(echo "$line" | sed -n 's/.*listening TCP on 127\.0\.0\.1:\([0-9][0-9]*\).*/\1/p') + write_xray_api_port "$API_PORT" + ;; + esac done - } & - PORT_MONITOR_PID=$! - + } < "$LOG_PIPE" & + LOG_READER_PID=$! + + # Start main process in background + ./main > "$LOG_PIPE" 2>&1 & + MAIN_PID=$! + # Wait for main process to finish wait $MAIN_PID - - # Clean up port monitor - kill $PORT_MONITOR_PID 2>/dev/null + MAIN_STATUS=$? + + # Clean up log reader + wait $LOG_READER_PID 2>/dev/null + rm -f "$LOG_PIPE" + exit $MAIN_STATUS diff --git a/k8s/apps/pasarguard/daemonset.yaml b/k8s/apps/pasarguard/daemonset.yaml index 8039126..0d077cd 100644 --- a/k8s/apps/pasarguard/daemonset.yaml +++ b/k8s/apps/pasarguard/daemonset.yaml @@ -116,14 +116,20 @@ spec: - name: metrics containerPort: 9550 protocol: TCP - livenessProbe: + startupProbe: httpGet: path: /scrape port: metrics + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 36 + livenessProbe: + tcpSocket: + port: metrics initialDelaySeconds: 60 periodSeconds: 30 timeoutSeconds: 10 - failureThreshold: 3 + failureThreshold: 6 readinessProbe: httpGet: path: /scrape