|
| 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 |
0 commit comments