provisioning-catalog/components/postgresql/cluster/postgresql-lib.sh

128 lines
4.1 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
# Methods library for postgresql — sourced by run-*.sh scripts generated from manifest_plan.
# SCRIPT_DIR must be set by the caller before sourcing.
_kubectl() {
if command -v kubectl >/dev/null 2>&1; then
command kubectl "$@"
elif command -v k0s >/dev/null 2>&1; then
k0s kubectl "$@"
else
echo "ERROR: no kubectl or k0s binary found" >&2; exit 127
fi
}
_require_env() {
local var="$1"
if [ -z "${!var:-}" ]; then
echo "ERROR: required variable $var is not set" >&2; exit 1
fi
}
_pod_name() {
_kubectl get pod \
--namespace "$POSTGRESQL_NAMESPACE" \
--selector "app.kubernetes.io/name=postgresql" \
-o jsonpath='{.items[0].metadata.name}' 2>/dev/null
}
_wait_for_pod() {
local timeout="${1:-180}"
_kubectl wait pod \
--namespace "$POSTGRESQL_NAMESPACE" \
--selector "app.kubernetes.io/name=postgresql" \
--for=condition=Ready \
--timeout="${timeout}s"
}
# ── Manifest plan helpers ─────────────────────────────────────────────────────
_plan_apply() {
local file="$1"
local path="$SCRIPT_DIR/manifests/${file}.yaml"
[ -f "$path" ] || { echo " [skip] $file.yaml not in manifests/"; return 0; }
_kubectl apply -f "$path"
}
_plan_apply_skip() {
local file="$1"
local path="$SCRIPT_DIR/manifests/${file}.yaml"
[ -f "$path" ] || { echo " [skip] $file.yaml not in manifests/"; return 0; }
if _kubectl get -f "$path" >/dev/null 2>&1; then
echo " [skip] $file already exists in cluster"
return 0
fi
_kubectl apply -f "$path"
}
_plan_rollout_restart() {
local file="$1"
local path="$SCRIPT_DIR/manifests/${file}.yaml"
[ -f "$path" ] || { echo " [skip] $file.yaml not in manifests/"; return 0; }
_kubectl rollout restart -f "$path"
}
_plan_delete() {
local file="$1"
local path="$SCRIPT_DIR/manifests/${file}.yaml"
[ -f "$path" ] || { echo " [skip] $file.yaml not in manifests/"; return 0; }
_kubectl delete -f "$path" --ignore-not-found
}
_plan_recreate() {
local file="$1"
local path="$SCRIPT_DIR/manifests/${file}.yaml"
[ -f "$path" ] || { echo " [skip] $file.yaml not in manifests/"; return 0; }
_kubectl delete -f "$path" --ignore-not-found
_kubectl apply -f "$path"
}
# ── Component methods ─────────────────────────────────────────────────────────
_method_create-credentials() {
[ -f "${SCRIPT_DIR}/_credentials.env" ] && set -a && source "${SCRIPT_DIR}/_credentials.env" && set +a
_require_env POSTGRES_PASSWORD
_kubectl create secret generic postgresql-credentials \
--namespace "$POSTGRESQL_NAMESPACE" \
--from-literal=POSTGRES_PASSWORD="$POSTGRES_PASSWORD" \
--dry-run=client -o yaml | _kubectl apply -f -
}
_method_wait-ready() {
_wait_for_pod 180
}
_method_post-install() {
local post_install="$SCRIPT_DIR/manifests/post-install.sh"
if [ ! -f "$post_install" ]; then
echo " [skip] post-install.sh not found in manifests/"
return 0
fi
bash "$post_install"
}
_method_protect-volume() {
local pvc="${PLAN_PARAM_PVC:-postgresql-data}"
echo " waiting for PVC '$pvc' to bind..."
_kubectl wait pvc "$pvc" \
--namespace "$POSTGRESQL_NAMESPACE" \
--for=jsonpath='{.status.phase}'=Bound \
--timeout=120s
local pv_name
pv_name=$(_kubectl get pvc "$pvc" \
--namespace "$POSTGRESQL_NAMESPACE" \
-o jsonpath='{.spec.volumeName}')
if [ -z "$pv_name" ]; then
echo " [warn] could not resolve PV name for PVC '$pvc' — skipping volume protection"
return 0
fi
echo " enabling Hetzner delete-protection on volume '$pv_name'..."
if command -v hcloud >/dev/null 2>&1; then
hcloud volume enable-protection "$pv_name" delete 2>/dev/null \
&& echo " [ok] volume '$pv_name' protected against deletion" \
|| echo " [warn] hcloud protect failed — enable manually: hcloud volume enable-protection $pv_name delete"
else
echo " [warn] hcloud CLI not found — enable manually: hcloud volume enable-protection $pv_name delete"
fi
}