Skip to content

Instantly share code, notes, and snippets.

@nithu0115
Created December 5, 2019 02:57
Show Gist options
  • Save nithu0115/0450e50b886b42f1c3f42ad291a048d5 to your computer and use it in GitHub Desktop.
Save nithu0115/0450e50b886b42f1c3f42ad291a048d5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# If only one argument is given when calling this script, we do a namespace switch.
# If a second argument exists when calling this script, we do a cluster switch.
# When switching clusters, alert the user if that cluster is not found.
# The trap provides the current context on exit. The context names are sorted to provide consistency.
LRED=$(echo -en '\033[01;31m')
RESTORE=$(echo -en '\033[0m')
trap 'CURRENT_CONTEXT=$(kubectl config current-context) && echo && kubectl config get-contexts | head -n1 && kubectl config get-contexts | sed 1d | sort --key=1 -b | sed "s|\*\(.*\)${CURRENT_CONTEXT}\(.*\)$|${LRED}\*\1${CURRENT_CONTEXT}\2${RESTORE}|g"' 0 1 3 15
# Usage
usage() { echo -e "# Usage\n\tPrompt for Clusters: kubectl switch cluster -l\n\tSwitch Namespace: kubectl switch <namespace>\n\tSwitch Cluster: kubectl switch cluster <cluster_name>\n" && exit 0; }
[ $# -eq 0 ] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] && usage
NAMESPACE="$1"
CLUSTER="$2"
### List the clusters to choose from
function list_clusters() {
IFS=$'\n'
CLUSTERS=($(kubectl config get-clusters | grep -v NAME | sort))
count=1
for i in ${!CLUSTERS[@]}; do
echo "[$count]: ${CLUSTERS[$i]}"
let count=count+1
done
echo; read -p "Enter the number of the cluster to use: " SELECTION
SELECTION=$(($SELECTION - 1))
CLUSTER="${CLUSTERS[$SELECTION]}"
switch_cluster
}
### Switch cluster context
function switch_cluster() {
# Verify it's a valid cluster name with kubectl
CLUSTER_NAME=$(kubectl config get-clusters | grep "$CLUSTER$") # Allow fuzzying on cluster name
# Fallback in case the user has renamed their context for the cluster.
if [ -z "${CLUSTER_NAME}" ]; then
CLUSTER_NAME=$(kubectl config get-contexts -o name | grep $CLUSTER)
# If both lookups have failed, exit 1.
if [ -z "${CLUSTER_NAME}" ]; then
RED=$(echo -en '\033[00;31m')
YELLOW=$(echo -en '\033[00;33m')
RESTORE=$(echo -en '\033[0m')
echo -e "\n${RED}Error on lookup. Cannot find:${YELLOW} $CLUSTER${RESTORE}\n"
exit 1
fi
fi
CLUSTER="$CLUSTER_NAME"
kubectl config use-context $CLUSTER >/dev/null 2>&1
if [ $? -ne 0 ]; then
CLUSTER=$(kubectl config get-contexts | grep " $CLUSTER " | sed 's|\*||g' | awk '{print $1}') >/dev/null 2>&1
kubectl config use-context $CLUSTER >/dev/null 2>&1
fi
[ $? -ne 0 ] && echo -e "\n####### TYPO! Please re-check the spelling for cluster: $2" && exit 1
}
### Switch namespace for current context
function switch_namespace() {
kubectl config set-context $(kubectl config current-context) --namespace="$NAMESPACE" 1>/dev/null
exit 0
}
if [[ "$@" =~ " -l" ]]; then
list_clusters
elif [[ "$@" =~ "cluster" ]]; then
switch_cluster
elif [[ ! "$@" =~ "cluster" ]]; then
switch_namespace
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment