Created
March 5, 2020 08:33
-
-
Save MLukman/0d898875974e109eeceac27e4da4cdb3 to your computer and use it in GitHub Desktop.
Script to easily switch between namespaces within TM Kubix clusters
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 | |
################################################################################ | |
# Script Name : kubix.sh | |
# Description : GUI menu to switch Kubix cluster and namespace | |
# Args : kubix.sh [clustername [namespacename]] | |
# Author : Muhammad Lukman bin Nasaruddin | |
# Email : lukman.nasaruddin@tm.com.my | |
################################################################################ | |
unset CLUSTERS | |
declare -A CLUSTERS | |
export CLUSTERS | |
if [[ -f ~/.kubix ]]; then source ~/.kubix; fi | |
################################################################################ | |
# Either modify the following line to declare list of Kubix clusters | |
# Or write them into a separate file .kubix inside your home folder | |
# Format: CLUSTERS[clustername]=pks.api.url | |
################################################################################ | |
################################################################################ | |
# Nothing needs modification beyond here. Modify at your own risk! | |
################################################################################ | |
if [[ ! -n $(which dialog 2> /dev/null) ]]; then echo Required dialog utility is not found. Please install it first.; kill -INT $$; fi | |
(return 0 2>/dev/null) && sourced=1 | |
if [[ "$sourced" -ne "1" ]]; then | |
read -p "It is recommended to run this script prefixed with 'source' or '.'. Press ENTER to proceed. " dummy | |
fi | |
function pks_login() { | |
CLUSTER_=$1 | |
if [[ ! -n "${CLUSTERS[$CLUSTER_]}" ]]; then | |
echo "Cluster '$_CLUSTER_' is not defined in the \$CLUSTERS array" | |
kill -INT $$ | |
fi | |
PKSRES=1 | |
while [[ $PKSRES -ne 0 ]]; do | |
# Username | |
BUTTON=0 | |
while [[ ! -n "$PKS_USR" && $BUTTON -eq 0 ]]; do | |
PKS_USR=$(dialog --keep-tite \ | |
--backtitle "Login to PKS (set PKS_USR environment variable to skip this next time)" \ | |
--title "PKS Login Required" --inputbox "PKS Username " 8 60 2>&1 > /dev/tty) | |
BUTTON=$? | |
done | |
if [[ $BUTTON -ne 0 ]]; then echo 'You did not provide PKS username'; kill -INT $$; fi | |
export PKS_USR=$PKS_USR | |
# Password | |
BUTTON=0 | |
while [[ ! -n "$PKS_PWD" && $BUTTON -eq 0 ]]; do | |
PKS_PWD=$(dialog --keep-tite \ | |
--backtitle "Login to PKS (set PKS_PWD environment variable to skip this next time)" \ | |
--title "PKS Login Required" --insecure --passwordbox "PKS Password for $PKS_USR " 8 60 2>&1 > /dev/tty) | |
BUTTON=$? | |
done | |
if [[ $BUTTON -ne 0 ]]; then echo 'You did not provide PKS password'; kill -INT $$; fi | |
export PKS_PWD=$PKS_PWD | |
ERR=$(pks get-kubeconfig $CLUSTER_ -u $PKS_USR -p $PKS_PWD -a ${CLUSTERS[$CLUSTER_]} -k 2>&1) | |
PKSRES=$? | |
echo $PKRES | |
if [[ $PKSRES -eq 1 ]]; then | |
if [[ $ERR =~ "redential" ]]; then | |
export PKS_USR= | |
export PKS_PWD= | |
fi | |
ERR=$(echo $ERR) | |
dialog --keep-tite --backtitle "Login to PKS" \ | |
--yesno "${ERR}\nTry again?" 10 60 | |
if [[ $? -ne 0 ]]; then echo "Failed to login to PKS cluster $CLUSTER_"; kill -INT $$; fi | |
if [[ $ERR =~ "not found" ]]; then | |
CLUSTER_=$(dialog --keep-tite \ | |
--backtitle "Login to PKS" \ | |
--title "The specified PKS cluster not found" --inputbox "PKS Cluster " 8 60 "$CLUSTER_" 2>&1 > /dev/tty) | |
if [[ $? -ne 0 ]] || [[ ! -n "$CLUSTER_" ]]; then echo 'You cancelled'; kill -INT $$; fi | |
echo Switching to cluster $CLUSTER_ ... | |
fi | |
fi | |
done | |
} | |
function choose_cluster () { | |
CLMENU=() | |
for CL in "${!CLUSTERS[@]}"; do | |
CLMENU+=("$CL" "$CL") | |
done | |
CLCHOOSE=$(dialog --keep-tite --clear --no-tags \ | |
--backtitle "Please choose a Kubix Cluster" \ | |
--title "[ Choose cluster (Current: ${CLUSTER:-N/A}) ]" \ | |
--default-item "$CLUSTER" \ | |
--menu 'Use the UP/DOWN arrow keys & ENTER key to choose cluster' 15 50 0 "${CLMENU[@]}" 2>&1 > /dev/tty) | |
if [[ ! -n "$CLCHOOSE" ]]; then echo "No cluster has been chosen."; return 1; fi | |
switch_cluster $CLCHOOSE | |
} | |
function switch_cluster() { | |
CLUSTER_=$1 | |
echo Switching to cluster $CLUSTER_ ... | |
pks_login $CLUSTER_ | |
kubectl config use-context $CLUSTER_ > /dev/null | |
export CLUSTER=$CLUSTER_ | |
} | |
function choose_namespace () { | |
export CLUSTER=$(kubectl config get-contexts | grep '*' | awk '{ print $3 }') | |
if [[ ! -n "$CLUSTER" ]]; then | |
choose_cluster | |
if [[ ! -n "$CLUSTER" ]]; then return 0; fi | |
fi | |
export NS=$(kubectl config get-contexts | grep '*' | awk '{ print $5 }') | |
echo Connecting to cluster $CLUSTER ... | |
NSS=$(kubectl get ns -o name 2> /dev/null | cut -d'/' -f2) | |
if [[ ! -n "$NSS" ]]; then | |
echo Login required. Attempting to auto-login ... | |
pks_login $CLUSTER && choose_namespace | |
return | |
fi | |
NSMENU=() | |
while read -r line; do | |
if [[ -n "$line" ]]; then NSMENU+=("$line" "$line"); fi | |
done <<< "$NSS" | |
if [[ ${#NSMENU[@]} -eq 0 ]]; then kill -INT $$; fi | |
EXTRABUTTON= | |
if [[ ${#CLUSTERS[@]} -gt 1 ]]; then EXTRABUTTON='--extra-button --extra-label Clusters'; fi | |
NSCHOOSE=$(dialog --keep-tite --clear --no-tags \ | |
$EXTRABUTTON \ | |
--backtitle "Welcome to Kubix Cluster $CLUSTER" \ | |
--title "[ Switch namespace (Current: ${NS:-N/A}) ]" \ | |
--default-item "$NS" \ | |
--menu 'Use the UP/DOWN arrow keys & ENTER key to switch namespace' 15 50 0 "${NSMENU[@]}" 2>&1 > /dev/tty) | |
BUTTON=$? | |
if [[ ${#CLUSTERS[@]} -gt 1 ]] && [[ $BUTTON -eq 3 ]]; then choose_cluster; choose_namespace; return 0; fi | |
RET=0 | |
if [[ -n "$NSCHOOSE" ]]; then | |
switch_namespace $NSCHOOSE | |
elif [[ -n "$NS" ]]; then | |
echo "No change to current namespace $NS @ $CLUSTER" | |
else | |
echo "No namespace has been chosen." | |
RET=1 | |
fi | |
if [[ ! -d "$HOME/$CLUSTER" ]]; then mkdir "$HOME/$CLUSTER"; fi | |
if [[ $(pwd) != "$HOME/$CLUSTER" ]]; then | |
pushd "$HOME/$CLUSTER" > /dev/null | |
echo "Navigated to " $(pwd) " path. Use popd to back to previous path." | |
fi | |
echo "Execute '$BASH_SOURCE' to bring up this menu again." | |
return $RET | |
} | |
function switch_namespace () { | |
NS_=$1 | |
if [[ -n "$(kubectl get ns $NS_ 2> /dev/null)" ]]; then | |
kubectl config set-context $CLUSTER --namespace $NS_ > /dev/null | |
echo Namespace ${NS_} @ ${CLUSTER} chosen. | |
export NS=$NS_ | |
else | |
echo Namespace ${NS_} not found in cluster ${CLUSTER}! | |
fi | |
} | |
CLUSTER=$(kubectl config get-contexts | grep '*' | awk '{ print $3 }') | |
if [ "$#" -gt 0 ]; then | |
if [[ "$1" = '-' ]]; then | |
choose_cluster | |
kill -INT $$ | |
fi | |
if ! [[ "$1" = "$CLUSTER" ]]; then | |
switch_cluster $1 | |
fi | |
if [[ -n "$2" ]]; then | |
switch_namespace $2 | |
else | |
choose_namespace | |
fi | |
else | |
if [[ -n "$CLUSTER" ]]; then | |
choose_namespace | |
elif [[ ${#CLUSTERS[@]} -eq 0 ]]; then | |
echo "Please modify CLUSTERS array near the top of $BASH_SOURCE or set it as environment variable or specify a cluster name as the 1st argument to the command, e.g. '. $BASH_SOURCE clustername'" | |
kill -INT $$ | |
elif [[ ${#CLUSTERS[@]} -eq 1 ]]; then | |
switch_cluster ${CLUSTERS[0]} | |
choose_namespace | |
else | |
choose_namespace | |
fi | |
fi | |
echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment