#!/usr/bin/env bash set -euo pipefail ####################################################################### ### 1) CONFIGURATION ####################################################################### # Change these values as needed. NAMESPACE="firezone" # The namespace where everything will live. CLUSTER_NAME="cluster-firezone" # The name for the CloudNativePG Cluster CR. SECRET_USER_NAME="firezone" # The name of the Secret for the normal DB user. SECRET_SUPERUSER_NAME="izadmin" # The name of the Secret for the Postgres superuser. SECRET_FIREZONE="firezone-database" # The name of the Secret that Firezone will use. # CloudNativePG cluster settings. POSTGRES_IMAGE="ghcr.io/cloudnative-pg/postgresql:16.2" STORAGE_CLASS="ceph-block" STORAGE_SIZE="20Gi" # Database and Firezone credentials. # Firezone (or your application) is expecting a database with this name. DB_NAME="firezone" # This is the database created during bootstrap (initdb). DB_APP_USER="firezone" # The database owner (normal DB user). DB_SUPERUSER="postgres" # The typical Postgres superuser name. # Additional PostgreSQL parameters. MAX_CONNECTIONS="200" SHARED_BUFFERS="256MB" # Generate random passwords for demo purposes. # If you want fixed passwords, you can set these manually. DB_APP_PASSWORD="$(openssl rand -hex 16)" DB_SUPERUSER_PASSWORD="$(openssl rand -hex 16)" ####################################################################### ### 2) CREATE/UPDATE SECRETS FOR CLOUDNATIVE-PG ####################################################################### echo "Creating/updating secrets for CloudNativePG..." # Secret for the normal DB user (used during initdb bootstrap). oc -n "$NAMESPACE" create secret generic "$SECRET_USER_NAME" \ --type=kubernetes.io/basic-auth \ --from-literal=username="$DB_APP_USER" \ --from-literal=password="$DB_APP_PASSWORD" \ --dry-run=client -o yaml | oc apply -f - # Secret for the Postgres superuser. oc -n "$NAMESPACE" create secret generic "$SECRET_SUPERUSER_NAME" \ --type=kubernetes.io/basic-auth \ --from-literal=username="$DB_SUPERUSER" \ --from-literal=password="$DB_SUPERUSER_PASSWORD" \ --dry-run=client -o yaml | oc apply -f - echo "Secrets for CloudNativePG created/updated." ####################################################################### ### 3) HANDLE THE CLOUDNATIVE-PG CLUSTER CR (INITDB) ####################################################################### # IMPORTANT: The bootstrap (initdb) phase runs only when the cluster is first created. # If the cluster already exists, then changes to bootstrap.initdb will not re-run. # To force a re‑initialization (to create the DB as specified), we delete the # existing cluster CR before re‑creating it. if oc get cluster "$CLUSTER_NAME" -n "$NAMESPACE" >/dev/null 2>&1; then echo "CloudNativePG Cluster '$CLUSTER_NAME' already exists." echo "Deleting the existing cluster to force re‑initialization (initdb)..." oc delete cluster "$CLUSTER_NAME" -n "$NAMESPACE" # Wait until the cluster CR is fully deleted. echo "Waiting for cluster '$CLUSTER_NAME' to be deleted..." while oc get cluster "$CLUSTER_NAME" -n "$NAMESPACE" >/dev/null 2>&1; do sleep 5 done echo "Existing cluster deleted." fi echo "Creating CloudNativePG Cluster '$CLUSTER_NAME' with initdb bootstrap..." cat <