prole/etc/init_kong.sh
chrisfu b03efa8f69 Kong API gateway, docker-import preload, OpenTofu graceful fallback, milestone fix
Kong API Gateway (replacing prole nginx):
- Add etc/init_kong.sh provisioning script (DB-less mode, prole-db namespace)
- Add kong-deployment.yaml and kong-service.yaml manifests
- Rewire ingress rules (svc/git/api.prole.org) to prole-db-kong:8000
- Update kustomization.yaml to reference kong manifests

PostgREST & DB Manager in prole-db namespace:
- Add etc/init_postgrest.sh and etc/init_db_manager.sh scripts
- Add postgrest/db-manager deployment and service manifests
- Add src/db-manager/ Node.js REST endpoint for backup triggers
- Default NAMESPACE changed to prole-db in both scripts

Docker image pre-load from PROLE_DATA/docker-import:
- Add _preload_docker_images() to init_common_services.sh
- Scan for .tar files exported by final_deployment.sh
- Import via k3d image import (k3d) or ctr (k3s) before deployments
- Increase rollout timeouts to 300s (configurable via ROLLOUT_TIMEOUT) in init_openbao.sh, init_opentofu.sh, init_garage_store.sh, init_registry.sh

OpenTofu password resolution fix:
- Add Kubernetes secret fallback in resolve_admin_password()
- Change hard exit 1 to graceful return 1 with warning
- Wrap call in if-guard so set -e doesn't abort the script chain

Milestone fix (init scripts not running):
- Add init_kong.sh, init_postgrest.sh, init_db_manager.sh to InitializationScriptsMilestone.execute() script list and arg branches
- Previously only actions.py had these; milestones.py was missing them

Installer integration:
- Add Kong/PostgREST/DB Manager to silent installer _step_init_scripts
- Add corresponding tabs and execution blocks in UI services.py
2026-02-22 00:57:49 -08:00

191 lines
4.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# init_kong.sh
# Purpose:
# - Deploy Kong API Gateway (DB-less) into the prole-db namespace
# - Replaces the prole nginx deployment as the API endpoint
# - Routes /backup/* to prole-db-manager and /rest/* to prole-db-postgrest
# - Creates the kong declarative config as a ConfigMap
# - Applies the kong deployment and service manifests
# - Provides start/stop/status/restart actions
#
# Usage:
# ./init_kong.sh [--mode MODE] [-n NAMESPACE] <start|stop|status|restart>
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck disable=SC1090
source "$SCRIPT_DIR/prole_cfg.sh"
if [[ "${1:-}" == "--mode" || "${1:-}" == "-m" ]]; then
prole_set_mode "${2:-}"
shift 2
elif [[ "${1:-}" == --mode=* || "${1:-}" == -m=* ]]; then
prole_set_mode "${1#*=}"
shift
fi
NAMESPACE="${NAMESPACE:-default}"
while [[ $# -gt 0 ]]; do
case "$1" in
-n|--namespace)
shift
if [[ -z "${1:-}" ]]; then
echo "ERROR: -n/--namespace requires a value" >&2
exit 2
fi
NAMESPACE="$1"
;;
-n=*|--namespace=*)
NAMESPACE="${1#*=}"
;;
start|stop|status|restart)
ACTION="$1"
;;
-h|--help)
usage
;;
*)
echo "ERROR: Unknown argument: $1" >&2
exit 2
;;
esac
shift
done
ACTION="${ACTION:-start}"
PROLE_HOME=${PROLE_HOME:-$(cd "$SCRIPT_DIR/.." && pwd)}
KONG_IMAGE="${KONG_IMAGE:-kong:3.9}"
KONG_NAME="${KONG_NAME:-prole-db-kong}"
KONG_PROXY_PORT="${KONG_PROXY_PORT:-8000}"
KONG_ADMIN_PORT="${KONG_ADMIN_PORT:-8001}"
KONG_CONFIG_NAME="${KONG_CONFIG_NAME:-prole-db-kong-config}"
# Upstream service defaults
POSTGREST_SERVICE="${POSTGREST_SERVICE:-prole-db-postgrest}"
POSTGREST_PORT="${POSTGREST_PORT:-3000}"
DB_MANAGER_SERVICE="${DB_MANAGER_SERVICE:-prole-db-manager}"
DB_MANAGER_PORT="${DB_MANAGER_PORT:-80}"
usage() {
cat <<USAGE
Usage: $0 [--mode MODE] [-n NAMESPACE] <start|stop|status|restart>
Actions:
start Create ConfigMap and deploy Kong to the prole-db namespace
stop Remove Kong deployment, service, and ConfigMap
status Show Kong pod/service status
restart Restart Kong pods
USAGE
exit 1
}
ensure_tools() {
for t in kubectl; do
command -v "$t" >/dev/null || { echo "Missing required tool: $t" >&2; exit 1; }
done
}
ensure_namespace() {
if ! kubectl get namespace "$NAMESPACE" >/dev/null 2>&1; then
echo "Creating namespace '$NAMESPACE' ..."
kubectl create namespace "$NAMESPACE" >/dev/null 2>&1 || true
fi
}
create_kong_config() {
echo "Creating/updating Kong declarative config '$KONG_CONFIG_NAME' in namespace '$NAMESPACE' ..."
local kong_yml
kong_yml=$(cat <<EOF
_format_version: "3.0"
_transform: true
services:
- name: postgrest
url: http://${POSTGREST_SERVICE}.${NAMESPACE}.svc.cluster.local:${POSTGREST_PORT}
routes:
- name: postgrest-route
paths:
- /rest
strip_path: true
- name: db-manager
url: http://${DB_MANAGER_SERVICE}.${NAMESPACE}.svc.cluster.local:${DB_MANAGER_PORT}
routes:
- name: backup-route
paths:
- /backup
strip_path: false
EOF
)
kubectl create configmap "$KONG_CONFIG_NAME" \
--namespace="$NAMESPACE" \
--from-literal=kong.yml="$kong_yml" \
--dry-run=client -o yaml | kubectl apply -f -
echo "ConfigMap '$KONG_CONFIG_NAME' ready."
}
deploy() {
echo "Deploying $KONG_NAME to namespace '$NAMESPACE' ..."
local manifests_dir="$PROLE_HOME/deploy/opentofu/k3s/manifests/prole"
kubectl apply -f "$manifests_dir/kong-deployment.yaml" -n "$NAMESPACE"
kubectl apply -f "$manifests_dir/kong-service.yaml" -n "$NAMESPACE"
echo "Waiting for $KONG_NAME rollout ..."
kubectl rollout status deployment/"$KONG_NAME" -n "$NAMESPACE" --timeout=120s
echo "$KONG_NAME deployed successfully."
}
stop() {
echo "Removing $KONG_NAME from namespace '$NAMESPACE' ..."
kubectl delete deployment "$KONG_NAME" -n "$NAMESPACE" --ignore-not-found=true
kubectl delete service "$KONG_NAME" -n "$NAMESPACE" --ignore-not-found=true
kubectl delete configmap "$KONG_CONFIG_NAME" -n "$NAMESPACE" --ignore-not-found=true
echo "$KONG_NAME removed."
}
status() {
echo "=== $KONG_NAME pods ==="
kubectl get pods -n "$NAMESPACE" -l app="$KONG_NAME" 2>/dev/null || echo "No pods found"
echo ""
echo "=== $KONG_NAME service ==="
kubectl get svc "$KONG_NAME" -n "$NAMESPACE" 2>/dev/null || echo "No service found"
}
restart() {
echo "Restarting $KONG_NAME ..."
kubectl rollout restart deployment/"$KONG_NAME" -n "$NAMESPACE"
kubectl rollout status deployment/"$KONG_NAME" -n "$NAMESPACE" --timeout=120s
echo "$KONG_NAME restarted."
}
case "$ACTION" in
start)
ensure_tools
ensure_namespace
create_kong_config
deploy
;;
stop)
ensure_tools
stop
;;
status)
ensure_tools
status
;;
restart)
ensure_tools
restart
;;
*)
usage
;;
esac