prole/etc/init_k8s.sh
chrisfu 5618b662dd Remove prole-db-manager; simplify deployment via prole-authority; fix pg18 downgrade & cluster name
Summary:
Removed the prole-db-manager microservice and simplified deployment to use
prole-authority as the internal management and authorization point. Fixed two
blocking bugs that prevented silent install from completing on knoe-dev-cluster.

Removed: prole-db-manager
- Deleted db-manager-deployment.yaml and db-manager-service.yaml from opentofu manifests
- Deleted src/db-manager/ (Dockerfile, server.js, package.json, tests)
- Removed prole-db-manager port-forward mapping from installer/core/env.py
- Removed init_db_manager.sh from Initialization Scripts (milestones.py, actions.py)
- Removed init_certmgr.sh and init_db_manager.sh tabs from services screen (services.py)
- Removed live k8s Deployment/Service from knoe-dev-cluster

Fixed: PostgreSQL version downgrade error (pg17 -> pg18)
- Created conf/postgresql/.version with value 18
- Updated k8s/prole/prole-db.yaml and prole-db-recovery.yaml.tpl imageName to prole-db:18-089
- Fixed _init_database_options_state() to restore saved version_type from prole.cfg
  so db_version_type defaults to v18 (pg18) instead of silently reverting to pg17
- Added database_options.* keys to _collect_input_snapshot() in cfg.py so
  distribution, version_type, and all extension toggles persist to prole.cfg

Fixed: Cluster name inconsistency
- Removed stale prole-dev-cluster references; all scripts now use knoe-dev-cluster
- Added knoe-dev-cluster to mode-detection case in etc/prole_cfg.sh

Config: conf/prole.cfg
- Set kerberos_config.enabled = False, KERBEROS_AUTO_ENABLED = False
- Added database_options.distribution = percona, version_type = v18
- Added all 13 extension flags set to True (postgis, pgvector, pgcrypto, pgaudit,
  pg_repack, pg_stat_statements, pg_buffercache, pg_freespacemap, pgrowlocks,
  postgres_fdw, dblink, pg_stat_monitor, pgbadger)

Verification:
./install.py -s -l -v -c conf/prole.cfg completed successfully.
CNPG deployed prole-db:18-089 to knoe-dev-cluster; all milestones passed.

Co-authored-by: Junie <junie@jetbrains.com>
2026-03-01 20:40:44 -08:00

253 lines
5.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# etc/init_k8s.sh
# Purpose: Manage k3d/k8s environment
# Initialize SCRIPT_DIR
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# Load environment and config via prole_cfg.sh
# shellcheck disable=SC1090
source "$SCRIPT_DIR/prole_cfg.sh"
# Default values
VERBOSE=false
ENVIRONMENT="prod"
HOST=""
CLUSTER_NAME_DEFAULT="knoe-dev-cluster"
CLUSTER_PORT_DEFAULT="6443"
usage() {
cat <<EOF
Usage: $0 [initialize|start|stop|restart|status] [options]
Actions:
initialize Create a k3d cluster (dev) or fetch kubeconfig (service/prod)
start Start the k3d cluster
stop Stop the k3d cluster
restart Restart the k3d cluster
status Show status of Docker and k3d cluster
Options:
-v, --verbose Enable verbose output
-m, --mode k3d|k3s|k8s (maps to dev|service|prod)
-e, --environment dev|service|prod (default: $ENVIRONMENT)
-h, --host Remote host for service/prod environments
-n, --name k3d cluster name
EOF
exit 1
}
log() {
echo "[INFO] $*"
}
debug() {
if [[ "$VERBOSE" == "true" ]]; then
echo "[DEBUG] $*"
fi
}
# Parse options
POSITIONAL_ARGS=()
CLUSTER_NAME=""
while [[ $# -gt 0 ]]; do
case $1 in
-v|--verbose)
VERBOSE=true
shift
;;
-m|--mode)
prole_set_mode "${2:-}"
case "${PROLE_MODE:-}" in
k3d) ENVIRONMENT="dev" ;;
k3s) ENVIRONMENT="service" ;;
k8s) ENVIRONMENT="prod" ;;
esac
shift 2
;;
--mode=*|-m=*)
prole_set_mode "${1#*=}"
case "${PROLE_MODE:-}" in
k3d) ENVIRONMENT="dev" ;;
k3s) ENVIRONMENT="service" ;;
k8s) ENVIRONMENT="prod" ;;
esac
shift
;;
-e|--environment)
ENVIRONMENT="$2"
shift 2
;;
-h|--host)
HOST="$2"
shift 2
;;
-n|--name)
CLUSTER_NAME="$2"
shift 2
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
ACTION=${1:-}
if [[ -z "$ACTION" ]]; then
usage
fi
ensure_tools() {
if [[ "$ENVIRONMENT" == "dev" ]]; then
for t in k3d docker; do
command -v "$t" >/dev/null || { echo "ERROR: Missing required tool: $t" >&2; exit 1; }
done
else
command -v kubectl >/dev/null || { echo "ERROR: Missing required tool: kubectl" >&2; exit 1; }
fi
}
ensure_docker() {
if [[ "$ENVIRONMENT" != "dev" ]]; then
return 0
fi
if ! docker info >/dev/null 2>&1; then
echo "ERROR: Docker is not running." >&2
exit 1
fi
}
initialize() {
ensure_tools
ensure_docker
if [[ "$ENVIRONMENT" == "dev" ]]; then
if [[ -z "$CLUSTER_NAME" ]]; then
if [[ -t 0 ]]; then
read -p "Enter k3d cluster name [$CLUSTER_NAME_DEFAULT]: " CLUSTER_NAME
CLUSTER_NAME=${CLUSTER_NAME:-$CLUSTER_NAME_DEFAULT}
else
CLUSTER_NAME=$CLUSTER_NAME_DEFAULT
fi
fi
if k3d cluster list "$CLUSTER_NAME" >/dev/null 2>&1; then
log "Cluster '$CLUSTER_NAME' already exists."
else
data_dir="${PROLE_DATA:-}"
if [[ -z "$data_dir" && -f "$SCRIPT_DIR/../env.sh" ]]; then
# shellcheck disable=SC1090
source "$SCRIPT_DIR/../env.sh"
data_dir="${PROLE_DATA:-}"
fi
if [[ -z "$data_dir" ]]; then
data_dir="$SCRIPT_DIR/../data"
fi
mkdir -p "$data_dir" >/dev/null 2>&1 || true
volume_args=()
if [[ -n "$data_dir" ]]; then
volume_args=(--volume "${data_dir}:/var/lib/rancher/k3s/storage@all")
fi
# Generate a registries.yaml so k3d nodes can pull from the local
# registry over plain HTTP (insecure). The file is written to a
# temp location and passed via --registry-config at creation time.
registry_config_args=()
local reg_cfg
reg_cfg=$(mktemp "${TMPDIR:-/tmp}/k3d-registries-XXXXXX.yaml")
cat > "$reg_cfg" <<'REGEOF'
mirrors:
"k3d-prole-registry:5000":
endpoint:
- "http://k3d-prole-registry:5000"
REGEOF
registry_config_args=(--registry-config "$reg_cfg")
log "Creating k3d cluster '$CLUSTER_NAME'..."
k3d cluster create "$CLUSTER_NAME" \
--api-port "0.0.0.0:${CLUSTER_PORT_DEFAULT}" \
--agents 2 \
-p "0.0.0.0:${CLUSTER_PORT_DEFAULT}:6443@server:0" \
"${volume_args[@]}" \
"${registry_config_args[@]}"
rm -f "$reg_cfg" 2>/dev/null || true
fi
else
if [[ -n "$HOST" ]]; then
log "Environment is $ENVIRONMENT. Host is $HOST."
log "TODO: Fetch kubeconfig from $HOST (placeholder)."
else
log "Environment is $ENVIRONMENT. Use -h|--host to specify the remote cluster host."
fi
fi
}
status() {
ensure_tools
log "Checking Docker status..."
if docker info >/dev/null 2>&1; then
echo "Docker is running."
else
echo "Docker is NOT running."
fi
log "Listing k3d clusters..."
k3d cluster list
}
get_cluster_name() {
# If a name was provided or exists in env, use it.
# Otherwise, if there is only one cluster, use it.
# Finally, use default.
if [[ -n "${CLUSTER_NAME:-}" ]]; then
echo "$CLUSTER_NAME"
return
fi
local clusters
clusters=$(k3d cluster list --no-headers | awk '{print $1}')
local count
count=$(echo "$clusters" | grep -c . || true)
if [[ "$count" -eq 1 ]]; then
echo "$clusters"
else
echo "$CLUSTER_NAME_DEFAULT"
fi
}
case "$ACTION" in
initialize)
initialize
;;
status)
status
;;
start)
ensure_tools
CLUSTER_NAME=$(get_cluster_name)
log "Starting k3d cluster '$CLUSTER_NAME'..."
k3d cluster start "$CLUSTER_NAME"
;;
stop)
ensure_tools
CLUSTER_NAME=$(get_cluster_name)
log "Stopping k3d cluster '$CLUSTER_NAME'..."
k3d cluster stop "$CLUSTER_NAME"
;;
restart)
ensure_tools
CLUSTER_NAME=$(get_cluster_name)
log "Restarting k3d cluster '$CLUSTER_NAME'..."
k3d cluster stop "$CLUSTER_NAME"
k3d cluster start "$CLUSTER_NAME"
;;
*)
usage
;;
esac