Skip to content

Instantly share code, notes, and snippets.

@wokalski
Created July 31, 2022 10:30
Show Gist options
  • Save wokalski/a6cac8ba08abb9816a4dab56607c9b48 to your computer and use it in GitHub Desktop.
Save wokalski/a6cac8ba08abb9816a4dab56607c9b48 to your computer and use it in GitHub Desktop.
Boundary Kubectl connect with persistent sessiosn
#!/bin/zsh
PID_FILE="$HOME/.boundary_session_ttcp_BRTTDbGBAK"
connect() {
echo "Inititating a boundary session to k8s..."
TMP_FILE=$(mktemp)
nohup -- boundary connect -target-id ttcp_BRTTDbGBAK -format json &> $TMP_FILE &
CONNECT_PID=$!
trap "kill -2 $CONNECT_PID" SIGINT
echo "{\"pid\":$CONNECT_PID,\"address\":\"\",\"port\":\"\"}" > "$PID_FILE"
while ! [ -s $TMP_FILE ]; do
sleep 1
done
trap - SIGINT
echo $TMP_FILE
if [[ `jq '.address' $TMP_FILE` == null ]]; then
if [[ `jq .status $TMP_FILE` == 401 ]]; then
echo "Authorizing"
boundary authenticate oidc -auth-method-id amoidc_mpfrIGSIjv
connect
else
cat $TMP_FILE
echo "Connection failed"
exit 1
fi
fi
jq "{\"pid\":$CONNECT_PID,\"address\":.address,\"port\":.port}" $TMP_FILE > "$PID_FILE"
}
if [[ -f "$PID_FILE" ]]; then
PID=$(jq -r '.pid' "$PID_FILE")
ps -p $PID >> /dev/null
RESULT=$?
if [[ $RESULT != 0 ]]; then
connect
else
while [[ `jq -r '.address' "$PID_FILE"` == "" ]]; do
sleep 1
done
fi
else
connect
fi
kubectl --server https://`jq -r '.address' "$PID_FILE"`:`jq -r '.port' "$PID_FILE"` --insecure-skip-tls-verify=true "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment