From 34d734a5e13db2ec6f0516795f8b80b9d73acca9 Mon Sep 17 00:00:00 2001 From: George Taylor Date: Mon, 2 Sep 2024 18:44:26 +0100 Subject: [PATCH] :wrench: Add scripts to easily port-forward non k8s services --- tools/scripts/amq-connect.sh | 62 +++++++++++++++++++++++++++ tools/scripts/opensearch-connect.sh | 48 +++++++++++++++++++++ tools/scripts/rds-connect.sh | 66 +++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100755 tools/scripts/amq-connect.sh create mode 100755 tools/scripts/opensearch-connect.sh create mode 100755 tools/scripts/rds-connect.sh diff --git a/tools/scripts/amq-connect.sh b/tools/scripts/amq-connect.sh new file mode 100755 index 0000000..6a41c39 --- /dev/null +++ b/tools/scripts/amq-connect.sh @@ -0,0 +1,62 @@ +#!/bin/bash#!/bin/bash + +# trap ctrl-c and call ctrl_c() +trap ctrl_c INT +# trap fail and call fail() +trap fail ERR + +main() { + env=$1 + if [ "$env" == "poc" ]; then + namespace="hmpps-delius-alfrsco-${env}" + else + namespace="hmpps-delius-alfresco-${env}" + fi + echo "Connecting to AMQ Console in namespace $namespace" + + # get amq connection url + URL=$(kubectl get secrets amazon-mq-broker-secret --namespace ${namespace} -o json | jq -r ".data.BROKER_CONSOLE_URL | @base64d") + # extract host and port + HOST=$(echo $URL | cut -d '/' -f 3 | cut -d ':' -f 1) + # extract protocol + PROTOCOL=$(echo $URL | awk -F'://' '{print $1}') + # extract remote port + REMOTE_PORT=$(echo $URL | cut -d '/' -f 3 | cut -d ':' -f 2) + # if custom local port not provided, use remote port + if [ -z "$2" ]; then + LOCAL_PORT=$REMOTE_PORT + else + LOCAL_PORT=$2 + fi + # generate random hex string + RANDOM_HEX=$(openssl rand -hex 4) + # start port forwarding + kubectl run port-forward-pod-${RANDOM_HEX} --image=ghcr.io/ministryofjustice/hmpps-delius-alfresco-port-forward-pod:latest --port ${LOCAL_PORT} --env="REMOTE_HOST=$HOST" --env="LOCAL_PORT=$LOCAL_PORT" --env="REMOTE_PORT=$REMOTE_PORT" --namespace ${namespace}; + # wait for pod to start + kubectl wait --for=condition=ready pod/port-forward-pod-${RANDOM_HEX} --timeout=30s --namespace ${namespace} + printf "\nPort forwarding started, connecting to $HOST:$REMOTE_PORT \n" + printf "\n****************************************************\n" + printf "Connect to ${PROTOCOL}://localhost:$LOCAL_PORT locally\n" + printf "Press Ctrl+C to stop port forwarding \n" + printf "****************************************************\n\n" + # start the local port forwarding session + kubectl port-forward --namespace ${namespace} port-forward-pod-${RANDOM_HEX} $LOCAL_PORT:$LOCAL_PORT; +} + +fail() { + echo "\n\nPort forwarding failed" + kubectl delete pod port-forward-pod-${RANDOM_HEX} --force --grace-period=0 --namespace ${namespace} + exit 1 +} +ctrl_c() { + echo "\n\nStopping port forwarding" + kubectl delete pod port-forward-pod-${RANDOM_HEX} --force --grace-period=0 --namespace ${namespace} + exit 0 +} + +if [ -z "$1" ]; then + echo "env not provided" + echo "Usage: amq-connect.sh " + exit 1 +fi +main $1 $2 diff --git a/tools/scripts/opensearch-connect.sh b/tools/scripts/opensearch-connect.sh new file mode 100755 index 0000000..6ff5773 --- /dev/null +++ b/tools/scripts/opensearch-connect.sh @@ -0,0 +1,48 @@ +#!/bin/bash#!/bin/bash + +# trap ctrl-c and call ctrl_c() +trap ctrl_c INT +# trap fail and call fail() +trap fail ERR + +main() { + env=$1 + + if [ "$env" == "poc" ]; then + namespace="hmpps-delius-alfrsco-${env}" + else + namespace="hmpps-delius-alfresco-${env}" + fi + echo "Connecting to Opensearch in namespace $namespace" + + if [ -z "$2" ]; then + PORT=8080 + else + PORT=$2 + fi + + # get opensearch proxy pod name + OPENSEARCH_PROXY_POD=$(kubectl get pods | grep 'opensearch-proxy-cloud-platform' | awk '{print $1}') + printf "\n****************************************************\n" + printf "Connect to http://localhost:$PORT locally\n" + printf "Press Ctrl+C to stop port forwarding \n" + printf "****************************************************\n\n" + # start the local port forwarding session + kubectl port-forward $OPENSEARCH_PROXY_POD $PORT:8080 --namespace ${namespace} +} + +fail() { + echo "\n\nPort forwarding failed" + exit 1 +} +ctrl_c() { + echo "\n\nStopping port forwarding" + exit 0 +} + +if [ -z "$1" ]; then + echo "env not provided" + echo "Usage: opensearch-connect.sh " + exit 1 +fi +main $1 $2 diff --git a/tools/scripts/rds-connect.sh b/tools/scripts/rds-connect.sh new file mode 100755 index 0000000..65a62a5 --- /dev/null +++ b/tools/scripts/rds-connect.sh @@ -0,0 +1,66 @@ +#!/bin/bash#!/bin/bash + +# trap ctrl-c and call ctrl_c() +trap ctrl_c INT +# trap fail and call fail() +trap fail ERR + +main() { + env=$1 + + if [ "$env" == "poc" ]; then + namespace="hmpps-delius-alfrsco-${env}" + else + namespace="hmpps-delius-alfresco-${env}" + fi + echo "Connecting to RDS in namespace $namespace" + + # get RDS connection url + URL=$(kubectl get secrets rds-instance-output --namespace ${namespace} -o json | jq -r ".data.RDS_JDBC_URL | @base64d") + # extract host and port + HOST=$(echo $URL | cut -d '/' -f 3 | cut -d ':' -f 1) + # extract database name + DATABASE_NAME=$(echo "$URL" | awk -F'/' '{print $NF}') + # extract protocol/jdbc + PROTOCOL=$(echo $URL | awk -F'://' '{print $1}') + # extract remote port + REMOTE_PORT=$(echo $URL | cut -d '/' -f 3 | cut -d ':' -f 2) + # if custom local port not provided, use remote port + if [ -z "$2" ]; then + LOCAL_PORT=$REMOTE_PORT + else + LOCAL_PORT=$2 + fi + # generate random hex string + RANDOM_HEX=$(openssl rand -hex 4) + # start port forwarding + kubectl run port-forward-pod-${RANDOM_HEX} --image=ghcr.io/ministryofjustice/hmpps-delius-alfresco-port-forward-pod:latest --port ${LOCAL_PORT} --env="REMOTE_HOST=$HOST" --env="LOCAL_PORT=$LOCAL_PORT" --env="REMOTE_PORT=$REMOTE_PORT" --namespace ${namespace}; + # wait for pod to start + kubectl wait --for=condition=ready pod/port-forward-pod-${RANDOM_HEX} --timeout=30s --namespace ${namespace} + printf "\nPort forwarding started, connecting to $HOST:$REMOTE_PORT \n" + printf "\n****************************************************\n" + printf "Connect to ${PROTOCOL}://localhost:$LOCAL_PORT/$DATABASE_NAME locally\n" + printf "Press Ctrl+C to stop port forwarding \n" + printf "****************************************************\n\n" + # start the local port forwarding session + kubectl port-forward --namespace ${namespace} port-forward-pod-${RANDOM_HEX} $LOCAL_PORT:$LOCAL_PORT; +} + +fail() { + echo "\n\nPort forwarding failed" + kubectl delete pod port-forward-pod-${RANDOM_HEX} --force --grace-period=0 --namespace ${namespace} + exit 1 +} + +ctrl_c() { + echo "\n\nStopping port forwarding" + kubectl delete pod port-forward-pod-${RANDOM_HEX} --force --grace-period=0 --namespace ${namespace} + exit 0 +} + +if [ -z "$1" ]; then + echo "env not provided" + echo "Usage: rds-connect.sh " + exit 1 +fi +main $1 $2