#!/bin/bash # Exit on any error set -e # Color codes for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo -e "${GREEN}=== Port Forward Setup Script ===${NC}\n" # Define port forwards: LOCAL_PORT:NAMESPACE:SERVICE:REMOTE_PORT:DESCRIPTION PORT_FORWARDS=( "55432:paradedb:paradedb-rw:5432:PostgreSQL" "45249:sientia-tracker:sientia-tracker-mlflow-tracking:80:MLflow" "37463:temporal:temporal-frontend:7233:Temporal" "8080:temporal:temporal-web:8080:Temporal UI" "42297:mongodb:my-release-mongodb:27017:MongoDB" "36577:minio:minio:9000:MinIO" ) # Step 1: Kill existing port-forward jobs for these services echo -e "${YELLOW}Step 1: Checking for existing port-forward jobs...${NC}" for pf in "${PORT_FORWARDS[@]}"; do IFS=':' read -r local_port namespace service remote_port description <<< "$pf" # Check if there's a job with this service name existing_jobs=$(jobs -l | grep "kubectl.*port-forward.*svc/$service" || true) if [ -n "$existing_jobs" ]; then echo -e "${YELLOW} Found existing port-forward for $description ($service)${NC}" # Extract PIDs and kill them pids=$(echo "$existing_jobs" | awk '{print $2}') for pid in $pids; do echo -e "${YELLOW} Killing job with PID $pid${NC}" kill "$pid" 2>/dev/null || true done fi done # Wait a moment for ports to be released sleep 1 echo -e "${GREEN} Cleanup complete${NC}\n" # Step 2: Check if any of the ports are already in use echo -e "${YELLOW}Step 2: Checking if ports are available...${NC}" ports_in_use=() for pf in "${PORT_FORWARDS[@]}"; do IFS=':' read -r local_port namespace service remote_port description <<< "$pf" # Check if port is in use using lsof or netstat if command -v lsof &> /dev/null; then if lsof -Pi :$local_port -sTCP:LISTEN -t >/dev/null 2>&1; then ports_in_use+=("$local_port:$description") fi elif command -v netstat &> /dev/null; then if netstat -tuln | grep -q ":$local_port "; then ports_in_use+=("$local_port:$description") fi elif command -v ss &> /dev/null; then if ss -tuln | grep -q ":$local_port "; then ports_in_use+=("$local_port:$description") fi fi done # If any ports are in use, report and exit if [ ${#ports_in_use[@]} -gt 0 ]; then echo -e "${RED}ERROR: The following ports are already in use:${NC}" for port_info in "${ports_in_use[@]}"; do IFS=':' read -r port desc <<< "$port_info" echo -e "${RED} - Port $port (for $desc)${NC}" done echo -e "\n${RED}Please free these ports before running this script.${NC}" exit 1 fi echo -e "${GREEN} All ports are available${NC}\n" # Step 3: Create all port forwards echo -e "${YELLOW}Step 3: Creating port forwards...${NC}" for pf in "${PORT_FORWARDS[@]}"; do IFS=':' read -r local_port namespace service remote_port description <<< "$pf" echo -e "${GREEN} Starting port-forward: $description${NC}" echo -e " Local port: $local_port -> $namespace/$service:$remote_port" kubectl -n "$namespace" port-forward "svc/$service" "$local_port:$remote_port" & # Give it a moment to start sleep 0.5 done echo -e "\n${GREEN}=== All port forwards created successfully ===${NC}" echo -e "\n${YELLOW}Active port forwards:${NC}" for pf in "${PORT_FORWARDS[@]}"; do IFS=':' read -r local_port namespace service remote_port description <<< "$pf" echo -e " - ${GREEN}localhost:$local_port${NC} -> $description ($namespace/$service)" done echo -e "\n${YELLOW}To stop all port forwards, run:${NC}" echo -e " jobs -p | xargs kill" echo -e "\n${YELLOW}To view active port forwards:${NC}" echo -e " jobs -l"