Skip to content

Commit aaccad2

Browse files
committed
Add pg-client.sh and pg-logs.sh
1 parent 1672ce4 commit aaccad2

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

pg-client.sh

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env bash
2+
3+
src_dir=$(realpath $(dirname $0))
4+
source ${src_dir}/functions
5+
6+
usage() {
7+
echo "Usage: $(basename "$0") --namespace <ns> --cluster <cluster> --endpoint <service>"
8+
exit 1
9+
}
10+
11+
main() {
12+
13+
local cluster=""
14+
local namespace=""
15+
local username="postgres"
16+
local password=""
17+
local endpoint=""
18+
local pod=""
19+
local port=""
20+
21+
while [[ $# -gt 0 ]]; do
22+
key="$1"
23+
case "${key}" in
24+
-h | --help)
25+
usage
26+
exit 0
27+
;;
28+
-c | --cluster)
29+
cluster="$2"
30+
shift
31+
shift
32+
;;
33+
-n | --namespace)
34+
namespace="$2"
35+
shift
36+
shift
37+
;;
38+
-u | --user)
39+
username="$2"
40+
shift
41+
shift
42+
;;
43+
-p | --password)
44+
password="$2"
45+
shift
46+
shift
47+
;;
48+
-e | --endpoint)
49+
endpoint="$2"
50+
shift
51+
shift
52+
;;
53+
-o | --pod)
54+
pod="$2"
55+
shift
56+
shift
57+
;;
58+
-P | --port)
59+
port="$2"
60+
shift
61+
shift
62+
;;
63+
*)
64+
echo "unknown flag or option ${key}"
65+
usage
66+
exit 1
67+
;;
68+
esac
69+
done
70+
71+
if [[ -z ${namespace} ]]; then
72+
namespace=$(kubectl config view --minify --output 'jsonpath={..namespace}')
73+
fi
74+
75+
if [[ -z ${cluster} ]]; then
76+
cluster=$(kubectl get pg --output name ${namespace:+--namespace $namespace} 2>/dev/null | sed 's:^perconapgcluster.pg.percona.com/::')
77+
if [ "$(echo "${cluster}" | wc -l)" -gt 1 ]; then
78+
echo "There's more than one cluster, please specify --cluster <cluster> !"
79+
exit 1
80+
elif [ -z "${cluster}" ]; then
81+
echo "No cluster available in the namespace!"
82+
exit 1
83+
fi
84+
fi
85+
86+
if [[ -z "${pod}" ]]; then
87+
if [[ -z "${endpoint}" ]]; then
88+
# endpoint=$(kubectl get pg "${cluster}" -ojsonpath='{.status.host}')
89+
endpoint=$(kubectl get secrets ${cluster}-pguser-${username} --template='{{.data.host | base64decode}}{{"\n"}}')
90+
fi
91+
else
92+
endpoint="127.0.0.1"
93+
port="5432"
94+
fi
95+
96+
if [[ -z "${port}" ]]; then
97+
port="5432"
98+
fi
99+
100+
if [[ -z ${password} ]]; then
101+
# password=$(kubectl get secrets $(kubectl get ps "${cluster}" -ojsonpath='{.spec.secretsName}') -otemplate='{{.data.'"${username}"' | base64decode}}')
102+
password=$(kubectl get secrets ${cluster}-pguser-${username} --template='{{.data.password | base64decode}}{{"\n"}}')
103+
fi
104+
105+
if [[ -z ${pod} ]]; then
106+
echo -e "### Connecting to PostgreSQL at host: ${endpoint}:${port} ###\n"
107+
kubectl run -it --rm percona-client-${RANDOM} --image=perconalab/percona-distribution-postgresql:15 --restart=Never -- bash -c "PGPASSWORD=${password} psql -h${endpoint} -p${port} -U${username}"
108+
else
109+
echo -e "### Connecting to PostgreSQL from inside pod: ${pod} ###\n"
110+
kubectl exec -it "${pod}" -c mysql -- PGPASSWORD='${password}' psql -h"${endpoint}" -P"${port}" -U"${username}"
111+
fi
112+
}
113+
114+
main "$@" || exit 1

pg-logs.sh

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
3+
usage() {
4+
echo "Usage: $(basename "$0") --namespace <namespace>"
5+
exit 1
6+
}
7+
8+
main() {
9+
local namespace=""
10+
local pod=""
11+
12+
while [[ $# -gt 0 ]]; do
13+
key="$1"
14+
case "${key}" in
15+
-h | --help)
16+
usage
17+
exit 0
18+
;;
19+
-n | --namespace)
20+
namespace="$2"
21+
shift
22+
shift
23+
;;
24+
*)
25+
echo "unknown flag or option ${key}"
26+
usage
27+
exit 1
28+
;;
29+
esac
30+
done
31+
32+
pod=$(kubectl get pods -l app.kubernetes.io/name=percona-postgresql-operator --output name ${namespace:+--namespace $namespace})
33+
if [ -z "${pod}" ]; then
34+
pod=$(kubectl get pods -l app.kubernetes.io/name=pg-operator --output name ${namespace:+--namespace $namespace})
35+
fi
36+
if [ -n "${pod}" ]; then
37+
kubectl logs "${pod}" ${namespace:+--namespace $namespace}
38+
else
39+
echo "Operator pod is not found in the namespace!"
40+
exit 1
41+
fi
42+
}
43+
44+
main "$@" || exit 1

0 commit comments

Comments
 (0)