kubectx
#
ahmetb/kubectx
# 安装
brew install kubectx
alias kx=kubectx
alias kn=kubens
# 展示当前可用集群列表
kx
# 选中某集群
kx xxx
# 展示 namespace 列表
kn
# 选中某 ns
kn xxx
kubemerge
#
# vim ~/.zshrc
kubemerge() {
if [ $# -eq 0 ]; then
echo "Usage: kubemerge <path-to-kubeconfig1> [<path-to-kubeconfig2> ...]"
return 1
fi
# Ensure ~/.kube directory exists
mkdir -p ~/.kube
# Ensure ~/.kube/config exists and is not empty
if [ ! -f ~/.kube/config ] || [ ! -s ~/.kube/config ]; then
touch ~/.kube/config
fi
for NEW_KUBECONFIG in "$@"; do
if [[ "$NEW_KUBECONFIG" == "cache" || "$NEW_KUBECONFIG" == "config_temp" || "$NEW_KUBECONFIG" == "kubectx" ]]; then
echo "Skipping $NEW_KUBECONFIG"
continue
fi
if [ ! -f "$NEW_KUBECONFIG" ]; then
echo "File not found: $NEW_KUBECONFIG"
continue
fi
local CONTEXT_NAME=$(basename "$NEW_KUBECONFIG")
# Check if the context name already exists
if kubectl config get-contexts -o name | grep -q "^$CONTEXT_NAME$"; then
echo "Context \"$CONTEXT_NAME\" already exists. Skipping merge and rename."
else
# Merge the new kubeconfig with the existing one
if KUBECONFIG=~/.kube/config:$NEW_KUBECONFIG kubectl config view --merge --flatten >~/.kube/config_temp; then
mv ~/.kube/config_temp ~/.kube/config
echo "Merged $NEW_KUBECONFIG successfully."
# Extract new contexts from the new kubeconfig file
NEW_CONTEXTS=$(kubectl --kubeconfig="$NEW_KUBECONFIG" config get-contexts -o name)
for CONTEXT in $NEW_CONTEXTS; do
kubectl config rename-context "$CONTEXT" "$CONTEXT_NAME"
done
else
echo "Failed to merge $NEW_KUBECONFIG due to invalid format or content."
return 1
fi
fi
done
}
叶王 © 2013-2024 版权所有。如果本文档对你有所帮助,可以请作者喝饮料。