#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
ENV_FILE="${1:-$ROOT/k8s/regional/regions.env}"
OUT="${2:-$ROOT/k8s/regional/rendered}"

"$ROOT/scripts/render-regional-k8s.sh" "$ENV_FILE" "$OUT"
set -a
# shellcheck disable=SC1090
source "$ENV_FILE"
set +a

kubectl apply -f "$OUT/service.yaml"
kubectl apply -f "$OUT/pdb.yaml"

shopt -s nullglob
deployments=("$OUT"/deployment-*.yaml)
hpas=("$OUT"/hpa-*.yaml)
((${#deployments[@]} > 0)) || { echo "No regional deployments rendered" >&2; exit 2; }

for manifest in "${deployments[@]}"; do kubectl apply -f "$manifest"; done
for manifest in "${hpas[@]}"; do kubectl apply -f "$manifest"; done
for manifest in "${deployments[@]}"; do kubectl rollout status -f "$manifest" --timeout=10m; done

kubectl -n "$K8S_NAMESPACE" get pods -l "app=${APP_NAME}-regional" -o wide
