Displays active kubeconfig namespace and context, also allows you to switch namespace or context.
#!/usr/bin/env bash
# <xbar.title>Kubeconfig Namespace & Context Switcher</xbar.title>
# <xbar.version>v1.2</xbar.version>
# <xbar.author>Elias Abacioglu</xbar.author>
# <xbar.author.github>Raboo</xbar.author.github>
# <xbar.desc>Displays active kubeconfig namespace and context, also allows you to switch namespace or context.</xbar.desc>
# <xbar.dependencies>bash,kubectl</xbar.dependencies>
# <xbar.image>https://i.imgur.com/AmBpdmp.png</xbar.image>
PATH=/opt/homebrew/bin:/usr/local/bin:$PATH
KUBECTL=$(command -v kubectl)
KUBERNETES_LOGO='iVBORw0KGgoAAAANSUhEUgAAABwAAAAbCAYAAABvCO8sAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAACXBIWXMAABGIAAARiAE3kcxvAAABWWlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iWE1QIENvcmUgNS40LjAiPgogICA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczp0aWZmPSJodHRwOi8vbnMuYWRvYmUuY29tL3RpZmYvMS4wLyI+CiAgICAgICAgIDx0aWZmOk9yaWVudGF0aW9uPjE8L3RpZmY6T3JpZW50YXRpb24+CiAgICAgIDwvcmRmOkRlc2NyaXB0aW9uPgogICA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgpMwidZAAAGTUlEQVRIDYVWaUiWWRS+nzmWTWI1VqJFE5qWYaZpZZlaudBQ/giS8scQQZBDEP0I+lHMj3BahAaRpIWxaVA0p8VAECpbsMi0bSYpimgxy3Jtcf+WM89z/N63L5lmDpz3nnvu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XkmTx5ssMbb5OQkKCTUMZMb2+vQcjNlClTVMczts5t/Pjx5uDBg2ZwcNBkZmbqOBJGWxQIa+Eeb44kMqR1x48fZwhYlOXYsWNMY8Ei7Aj29fXJ48ePhVdlNPH1YChRUewh3ldejTNnzjBbtcjzToMaNKRXr17lirQSuFwu09LSYnD/THNzM/Vm165dJjo62lRVVWmfH0xWGcnCozDr1q3TPl4WU1hYaABokES0cyAafufPnxf4jqWi/vTp05ztREjY2pybm4th0XtHvZUUhw4dkj179ugYFqf23BEJZ2vP5xxmPX2zUoH+ImAquJtVggMEZf1klnICyxbp40cWi89Zy7FHjx6pjuWNxKJOPestC35WVpb6zM/PZ9ViEc/GOCxENuEBlezsbBq4mMZz584VVB11cPjwYfpTQlbK7du3hWfne24XL15UWxZxViq+IPTFV+Pdu3ec+xOxbIKigC8AFJ5p06a5+cAuWbJEZs2apY6YNMhWQbYpMD8scUwu1k/uCHP17nGhOHP9M+AzBiryBdIaBWUA+E+UJU50oh56LCcVFRXCnW3ZskWdskjk5eWpTBDcY7l79672Way9fwLDSEaC8RH+loBoR+ohBP1pQhsKbuQjTFA+U7RjEb906ZLKoaGh2lLPsLPlQ0vnqJ+C+0vdMJ83EEvP9+gT7IsfM1uBgSRw24EDBzjRyXNlqeMcvvo8X77sXAxlMtJex60k2blzp3g8ni74SfWCffFzRp0SDPSnB20uLvvw+vXrFXTSpEls9alBa8kKYuknTpyotjx3b9HYhDFuZAzYfu1HI9OpcTgcVTCKRAUqQAj9ecf4bLEoeMl2QGd8qvB/Kih9/vv37zchISG/wMfvXluBzMX8O8GBvpF0BP4Z3I6KMYC2F2Hin1if1VL29i0dw8gaZkXK/k200P4BmoPJHd1nlIYAAAAASUVORK5CYII='
# Scan for any custom KUBECONFIG in all the normal places and apply to this script it if it is set.
eval $(grep -hs KUBECONFIG ~/.profile ~/.bash_profile ~/.bashrc ~/.zshrc, ~/.zshenv ~/.zprofile)
currentNamespace=$(kubectl config view --minify --output 'jsonpath={..namespace}')
currentContext=$(kubectl config current-context)
namespaces=$(kubectl get namespaces --no-headers -o custom-columns=NAME:.metadata.name)
contexts=$(kubectl config get-contexts --no-headers -o name)
echo "|image=${KUBERNETES_LOGO}"
echo "---"
echo "Namespace: ${currentNamespace}"
for item in ${namespaces}; do
echo -n "--${item} | bash=${KUBECTL} param1=config param2=set-context param3=--current param4='--namespace=${item}' terminal=false refresh=true"
if [ "${item}" == "${currentNamespace}" ]; then
echo ' color=red'
else
echo
fi
done
echo "Context: ${currentContext}"
for item in ${contexts}; do
echo -n "--${item} | bash=${KUBECTL} param1=config param2=use-context param3=${item} terminal=false refresh=true"
if [ "${item}" == "${currentContext}" ]; then
echo ' color=red'
else
echo
fi
done