#!/usr/bin/env bash
# 用途：快速查看目前所有服務「怎麼連」
# 不修改任何資源，純顯示資訊

set -euo pipefail

echo
echo "=== Pods ==="
kubectl get pods -o wide

echo
echo "=== Services ==="
kubectl get svc

echo
echo "=== Node IPs ==="
kubectl get nodes -o wide | awk 'NR>1 {print $1 "\t" $6}'

echo
echo "=== NodePort Access ==="

NODE_IPS=$(kubectl get nodes -o jsonpath='{range .items[*]}{.status.addresses[?(@.type=="InternalIP")].address}{" "}{end}')
kubectl get svc --no-headers | awk '$2=="NodePort"' | while read -r name type clusterip external ports age; do
  PORTS=$(echo "$ports" | tr ',' '\n' | awk -F: '{print $NF}')
  for ip in $NODE_IPS; do
    for p in $PORTS; do
      echo "- $ip:$p ($name)"
    done
  done
done

echo
echo "=== Ingress (HTTP/HTTPS) ==="
kubectl get ingress --no-headers | while read -r name class hosts address ports age; do
  echo "- $name"
  kubectl get ingress "$name" -o jsonpath='{range .spec.rules[*]}{.host}{"\n"}{end}' | while read -r host; do
    TLS=$(kubectl get ingress "$name" -o jsonpath='{.spec.tls}')
    if [[ -n "$TLS" && "$TLS" != "[]" ]]; then
      echo "  https://$host"
    else
      echo "  http://$host"
    fi
  done
done

echo
echo "=== PVC ==="
kubectl get pvc

