Skip to content

Instantly share code, notes, and snippets.

@mgurov
Created May 28, 2019 12:29
Show Gist options
  • Save mgurov/005ddb506a7e1a28b2d68725a2bb687a to your computer and use it in GitHub Desktop.
Save mgurov/005ddb506a7e1a28b2d68725a2bb687a to your computer and use it in GitHub Desktop.
connect to a postgres instance behind kubectl
#!/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