kubeconfig
#
切换 kubeconfig
#
k8s() {
# 无参:看当前
if (( $# == 0 )); then
print -r -- "Current KUBECONFIG: ${KUBECONFIG:-$HOME/.kube/config (default)}"
return 0
fi
# 切回默认
if [[ "$1" = "-" || "$1" = "--default" ]]; then
unset KUBECONFIG
print -r -- "Switched to default: $HOME/.kube/config"
return 0
fi
# 解析路径:支持 ~ 和相对路径;不做任何“猜测/回落”
local in="$1" p
p="${~in}" # 展开 ~ 变量
[[ "$p" != /* ]] && p="$PWD/$p"
p="${p:A}" # 转绝对路径(zsh 自带)
if [[ -d "$p" ]]; then
print -r -- "Error: '$p' 是目录,请指定文件(例如 ~/.kube/xxx.yaml)"
return 1
fi
if [[ ! -f "$p" ]]; then
print -r -- "Error: 找不到 kubeconfig 文件:$p"
return 1
fi
export KUBECONFIG="$p"
print -r -- "Switched kubeconfig to: $KUBECONFIG"
}
叶王 © 2013-2024 版权所有。如果本文档对你有所帮助,可以请作者喝饮料。