forked from KnVerey/k8s-workflow-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchctx
executable file
·49 lines (42 loc) · 1.48 KB
/
chctx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/zsh#!/bin/bash
set -eo pipefail
# Fuzzy matches available kubernetes context names for fast context switching
# E.g. if "my-context2" is the only context with "2" in its name, `chctx 2` will switch to it
# Optional second argument to switch namespace at the same time using chns
# Author: Katrina Verey (github.com/KnVerey)
# Suggestion: Add the following to your zshrc and install the completions
# To get fast namespace listing and switching via context names
# Warning: Think through your context names first. E.g. if you're using minikube, you'll need to exclude or unalias it
# or else you'll have a conflict with the executable
#
# local contexts; contexts=($(kubectl config get-contexts -o name))
# for context in $contexts; do
# alias $context="chctx $context"
# done
#
if [ $1 ]; then
CONTEXT=$1
else
name=`basename "$0"`
echo "Usage: $name CONTEXT [NAMESPACE]"
exit 1;
fi
if ! [ "$(kubectl config get-contexts ${CONTEXT} -o name 2>/dev/null)" ]; then
ALL_CONTEXTS=$(kubectl config get-contexts -o name)
GUESSES=$(echo "$ALL_CONTEXTS" | grep $CONTEXT) || true
NUM_GUESSES=$(echo "$GUESSES" | wc -w)
if [ $NUM_GUESSES -eq 1 ]; then
CONTEXT=$GUESSES
elif [ $NUM_GUESSES -gt 1 ]; then
echo -e "\033[0;35mName '$CONTEXT' is ambiguous. Matching contexts:\033[0m"
echo "$GUESSES"
exit 1;
else
echo -e "\033[0;31mString '$CONTEXT' does not match any available context.\033[0m"
exit 1;
fi
fi
kubectl config use-context $CONTEXT
if [ $2 ]; then
chns $2
fi