Last active
March 15, 2024 15:16
-
-
Save mhewedy/0fd922d367d2fdb609bddf70bf771c4d to your computer and use it in GitHub Desktop.
create a proxy and do port forward on top of kubernetes/openshift
This file contains 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 | |
doPortForward() { | |
ns=$1 | |
ip=$2 | |
port=$3 | |
listen_port=`awk -F ':' '{print $1}' <<< "$port"` | |
container_port=`awk -F ':' '{print $2}' <<< "$port"` | |
container_port=${container_port:-$listen_port} | |
target_port=$4 || `shuf -i 2000-65000 -n 1` | |
ipescaped=`echo $ip | sed 's/\./-/g'` | |
oc login --username mhewedy | |
oc project apps-tamt-$ns | |
cat <<EOF | oc apply -f - | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: socat-$ipescaped-$container_port | |
spec: | |
containers: | |
- name: socat-$ipescaped-$container_port | |
image: alpine/socat | |
command: ["socat", "-dd", "tcp4-listen:$container_port,fork,reuseaddr", "tcp4:$ip:$listen_port"] | |
ports: | |
- containerPort: $container_port | |
EOF | |
oc port-forward socat-$ipescaped-$container_port $target_port:$container_port | |
} | |
case $1 in | |
dbdev) | |
doPortForward dev 100.3.66.9 1433 11433 | |
;; | |
dbqa) | |
doPortForward qa 120.3.193.14 1433 11433 | |
;; | |
*) | |
if [ "$#" -ne 4 ]; then | |
echo -e "Usage \n$(basename $0) <dbdev|dbqa>" | |
echo "$(basename $0) - <ns> <ip> <port>" | |
echo "$(basename $0) - <ns> <ip> <listen_port>:<container_port>" | |
exit -1 | |
fi | |
doPortForward $2 $3 $4 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment