Created
January 21, 2024 15:26
-
-
Save Itsdenty/f60145bbe7c7dae4bd0428e517b9b0e1 to your computer and use it in GitHub Desktop.
A simple bash utility to abstract kubernets access
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 | |
# set -x; | |
team=$(whoami) | |
# echo $team | |
all=0 | |
if [ $# -eq 0 ] | |
then | |
echo "No arguments supplied use the -h flag for help" | |
exit 1 | |
fi | |
if [ $1 = "list-server" ]; then | |
cat "/home/ubuntu/acl/${team}" | |
elif [ $1 = "open" ]; then | |
if [ "$2" ]; then | |
if grep -q "$2" "/home/ubuntu/acl/${team}"; then | |
export AWS_ACCESS_KEY_ID=<your-access-key> | |
export AWS_SECRET_ACCESS_KEY=<your-access-secret> | |
if [[ $2 == *"dev"* ]]; then | |
kubectl get pods --selector="app=${2}" -n my-app-development > pods.txt | |
aline=$(tail -n 1 pods.txt) | |
pod=$(echo "$aline" | awk '{print $1}') | |
kubectl exec -i -t -n my-app-development "$pod" -c "$2" "--" sh -c "clear; (bash || ash || sh)" | |
elif [[ $2 == *"qa"* ]]; then | |
kubectl get pods --selector="app=${2}" -n my-app-qa > pods.txt | |
aline=$(tail -n 1 pods.txt) | |
pod=$(echo "$aline" | awk '{print $1}') | |
kubectl exec -i -t -n my-app-qa "$pod" -c "$2" "--" sh -c "clear; (bash || ash || sh)" | |
elif [[ $2 == *"uat"* ]]; then | |
kubectl get pods --selector="app=${2}" -n my-app-qa > pods.txt | |
aline=$(tail -n 1 pods.txt) | |
pod=$(echo "$aline" | awk '{print $1}') | |
kubectl exec -i -t -n my-app-ust "$pod" -c "$2" "--" sh -c "clear; (bash || ash || sh)" | |
fi | |
else | |
echo "you do not have access to pod or invalid pod name supplied" | |
fi | |
else | |
echo "pod name must be supplied to open" | |
exit 1 | |
fi | |
else | |
echo "you did not include a command you can try -h for help" | |
fi | |
while getopts ":h" opt; do | |
case ${opt} in | |
h ) # process option a | |
echo -e "Kube access utility usage \n use list-server option to list the servers you have access to \n use open <servername> to open and gain access to your server" | |
;; | |
\? ) echo "Usage: cmd [-h] for help" | |
;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment