Skip to content

Commit 8a04b29

Browse files
author
Jordan Wilson
committed
Add no globbing fallback for cluster context switching
1 parent 479837f commit 8a04b29

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

kubectl-switch

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@ function list_clusters() {
3737
### Switch cluster context
3838
function switch_cluster() {
3939
# Verify it's a valid cluster name with kubectl
40-
CLUSTER_NAME=$(kubectl config get-clusters | grep "$CLUSTER$") # Allow fuzzying on cluster name
41-
40+
CLUSTER_NAME=$(kubectl config get-clusters | grep "$CLUSTER$")
4241
# Fallback in case the user has renamed their context for the cluster.
4342
if [ -z "${CLUSTER_NAME}" ]; then
44-
CLUSTER_NAME=$(kubectl config get-contexts -o name | grep $CLUSTER)
45-
# If both lookups have failed, exit 1.
43+
# Allow fuzzying on context name
44+
CLUSTER_NAME=($(kubectl config get-contexts -o name | grep $CLUSTER))
45+
# If globbing gives more than one match, discard those and look for an exact match
46+
if [ ${#CLUSTER_NAME[@]} -ne 1 ]; then
47+
unset CLUSTER_NAME
48+
CLUSTER_NAME=$(kubectl config get-contexts -o name | grep $CLUSTER$)
49+
fi
50+
# If all lookups have failed, exit 1.
4651
if [ -z "${CLUSTER_NAME}" ]; then
4752
RED=$(echo -en '\033[00;31m')
4853
YELLOW=$(echo -en '\033[00;33m')

0 commit comments

Comments
 (0)