Skip to content

Commit 031e074

Browse files
committed
add a psql subcommand to cncluster script for convenient DB inspection
[static] Signed-off-by: Mateusz Błażejewski <mateusz.blazejewski@digitalasset.com>
1 parent 22582e9 commit 031e074

File tree

1 file changed

+133
-1
lines changed

1 file changed

+133
-1
lines changed

build-tools/cncluster

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,8 @@ function subcmd_debug_shell() {
14581458
fi
14591459
repo="ghcr.io/digital-asset/decentralized-canton-sync-dev/docker"
14601460

1461+
local SHELL_COMMAND="${1:-/bin/bash}"
1462+
14611463
_info "Deploying debug pod on version $VERSION_NUMBER"
14621464
kubectl apply --wait -f - <<EOF
14631465
apiVersion: v1
@@ -1479,10 +1481,11 @@ spec:
14791481
effect: "NoSchedule"
14801482
EOF
14811483

1484+
# shellcheck disable=SC2086
14821485
( _info "Waiting for debug pod to become ready" && \
14831486
kubectl wait --for=condition=Ready pod splice-debug --timeout=30s && \
14841487
_info "Opening terminal on debug pod" && \
1485-
kubectl exec -it splice-debug -- /bin/bash ) || true
1488+
kubectl exec -it splice-debug -- $SHELL_COMMAND ) || true
14861489

14871490
_info "Deleting debug pod"
14881491
kubectl delete pod splice-debug
@@ -2172,6 +2175,135 @@ EOF
21722175
_update_cluster_config "$deployment_config" "operatorDeployment"
21732176
}
21742177

2178+
subcommand_whitelist[psql]=""
2179+
function subcmd_psql() {
2180+
_cluster_must_exist
2181+
2182+
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
2183+
_error "Usage: $SCRIPTNAME psql <namespace> <application> [search_path]"
2184+
exit 1
2185+
fi
2186+
2187+
local NAMESPACE="$1"
2188+
local APPLICATION="$2"
2189+
local SEARCH_PATH=${3:-}
2190+
2191+
_info "Retrieving DB connection info from pod description..."
2192+
local DB_INIT_COMMAND
2193+
DB_INIT_COMMAND=$(
2194+
kubectl describe pod \
2195+
--namespace "${NAMESPACE}" \
2196+
--selector "app=${APPLICATION}" \
2197+
| grep -i psql \
2198+
| sed -nE 's/^.*(psql.?*) 2>&1.*$/\1/p'
2199+
)
2200+
2201+
if [ -z "${DB_INIT_COMMAND}" ]; then
2202+
_error "Application ${APPLICATION} in namespace ${NAMESPACE} does not have an associated database."
2203+
exit 1
2204+
fi
2205+
2206+
# shellcheck disable=SC2086
2207+
set -- $DB_INIT_COMMAND
2208+
while [ $# -gt 0 ]; do
2209+
case "$1" in
2210+
-h|--host)
2211+
local DB_HOST="$2"
2212+
shift 2
2213+
;;
2214+
-h=*|--host=*)
2215+
local DB_HOST="${1#*=}"
2216+
shift
2217+
;;
2218+
-p|--port)
2219+
local DB_PORT="$2"
2220+
shift 2
2221+
;;
2222+
-p=*|--port=*)
2223+
local DB_PORT="${1#*=}"
2224+
shift
2225+
;;
2226+
-U|--username)
2227+
local DB_USERNAME="$2"
2228+
shift 2
2229+
;;
2230+
-U=*|--username=*)
2231+
local DB_USERNAME="${1#*=}"
2232+
shift
2233+
;;
2234+
-d|--dbname)
2235+
local DB_NAME="$2"
2236+
shift 2
2237+
;;
2238+
-d=*|--dbname=*)
2239+
local DB_NAME="${1#*=}"
2240+
shift
2241+
;;
2242+
*)
2243+
shift
2244+
;;
2245+
esac
2246+
done
2247+
2248+
if [ -z "${DB_HOST:-}" ]; then
2249+
_error "Failed to retrieve DB hostname from pod description."
2250+
exit 1
2251+
fi
2252+
2253+
if [ -z "${DB_PORT:-}" ]; then
2254+
_error "Failed to retrieve DB port from pod description."
2255+
exit 1
2256+
fi
2257+
2258+
if [ -z "${DB_USERNAME:-}" ]; then
2259+
_error "Failed to retrieve DB username from pod description."
2260+
exit 1
2261+
fi
2262+
2263+
if [ -z "${DB_NAME:-}" ]; then
2264+
_error "Failed to retrieve DB name from pod description."
2265+
exit 1
2266+
fi
2267+
2268+
local PASSWORD_REF
2269+
PASSWORD_REF=$(
2270+
kubectl get deployment \
2271+
--namespace sv-1 \
2272+
--output 'jsonpath={.spec.template.spec.initContainers[].env[?(@.name == "PGPASSWORD")].valueFrom.secretKeyRef}' \
2273+
"${APPLICATION}"
2274+
)
2275+
local SECRET_NAME
2276+
SECRET_NAME=$(jq -r '.name' <<< "$PASSWORD_REF")
2277+
local SECRET_KEY
2278+
SECRET_KEY=$(jq -r '.key' <<< "$PASSWORD_REF")
2279+
2280+
_info "Retrieving DB password from secret [${SECRET_NAME}]..."
2281+
local DB_PASSWORD
2282+
DB_PASSWORD=$(
2283+
kubectl get secret \
2284+
--namespace "${NAMESPACE}" \
2285+
--output "jsonpath={.data.${SECRET_KEY}}" \
2286+
"${SECRET_NAME}" \
2287+
| base64 --decode
2288+
)
2289+
2290+
if [ -z "${DB_PASSWORD}" ]; then
2291+
_error "Failed to retrieve DB password."
2292+
exit 1
2293+
fi
2294+
2295+
subcmd_debug_shell " \
2296+
/bin/env
2297+
PGPASSWORD=$DB_PASSWORD \
2298+
${SEARCH_PATH:+PGOPTIONS=--search_path=$SEARCH_PATH} \
2299+
psql \
2300+
--host=$DB_HOST \
2301+
--port=$DB_PORT \
2302+
--username=$DB_USERNAME \
2303+
--dbname=$DB_NAME \
2304+
"
2305+
}
2306+
21752307
################################
21762308
### Main
21772309
################################

0 commit comments

Comments
 (0)