Created
May 28, 2019 12:29
-
-
Save mgurov/005ddb506a7e1a28b2d68725a2bb687a to your computer and use it in GitHub Desktop.
connect to a postgres instance behind kubectl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -eux | |
CONTEXT=pro | |
APP=purple | |
exec 3< <( | |
pgid=$! | |
echo "kill $pgid" | |
kubectl --context=${CONTEXT} --namespace=${APP} port-forward ${APP}-${APP}-db-0 :5432 | |
) | |
while true; do | |
read <&3 line; | |
if [[ $line == *"kill"* ]]; then | |
killLine=$line | |
killJob() { | |
echo "cleaning up ${killLine}" | |
${killLine} | |
} | |
trap killJob EXIT SIGINT | |
fi | |
if [[ $line == *"Forwarding from 127.0.0.1"* ]]; then | |
port=$(echo "$line" | cut -d : -f 2 | cut -d ' ' -f 1) | |
PGOPTIONS="-c search_path=${APP}" psql -h localhost -p ${port} -d ${APP} | |
exit | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment