#!/usr/bin/env bash usage() { cat <"$CACHE/README.txt" get_data kubectl get -o custom-columns=':.metadata.name' --no-headers ns >"$CACHE/$CLUSTER" echo done } # Test if cache exists for current cluster, and if not, create it test_cache() { [[ -f "$CACHE/$CLUSTER" ]] || update_cache } # Print namespaces of current cluster from local cache print_namespaces() { sed "s/^/ /;\|^ $NAMESPACE$|s/ /*/" "$CACHE/$CLUSTER" } # List namespaces from local cache list_namespaces() { get_data test_cache print_namespaces } # Set namespace of current context to a new namespace (from local cache) set_namespace() { which fzf >/dev/null || { fzf_missing; exit 1; } get_data test_cache local ns=$(print_namespaces | fzf -e | sed 's/^..//') if [[ -z "$ns" ]]; then echo "Aborted. Nothing has been changed." else kubectl config set-context --current --namespace "$ns" >/dev/null echo "Switched to namespace \"$ns\"." fi } case "$1" in "") set_namespace;; -l) list_namespaces;; -u) update_cache;; *) usage;; esac