Files
xgj/setup-env/scripts/verify-kubectl.sh

77 lines
1.6 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -euo pipefail
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 输出函数
log_info() {
echo -e "${BLUE} $1${NC}"
}
log_success() {
echo -e "${GREEN}$1${NC}"
}
log_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
log_error() {
echo -e "${RED}$1${NC}"
}
# 验证 kubectl 连接
verify_kubectl_connection() {
log_info "验证 kubectl 连接..."
# 设置超时时间
TIMEOUT=30
# 检查集群连接
if timeout $TIMEOUT kubectl cluster-info >/dev/null 2>&1; then
log_success "集群连接成功"
# 显示集群信息
log_info "集群信息:"
kubectl cluster-info
# 显示节点信息
log_info "集群节点:"
if kubectl get nodes >/dev/null 2>&1; then
kubectl get nodes
else
log_warning "无法获取节点信息(可能权限不足)"
fi
# 显示当前上下文
CURRENT_CONTEXT=$(kubectl config current-context 2>/dev/null || echo "未知")
log_info "当前上下文: $CURRENT_CONTEXT"
# 显示 kubectl 版本
log_info "kubectl 版本信息:"
kubectl version --short 2>/dev/null || kubectl version --client
else
log_error "kubectl 连接验证失败"
log_info "尝试显示详细错误信息..."
kubectl cluster-info || true
exit 1
fi
}
# 主函数
main() {
verify_kubectl_connection
}
# 错误处理
trap 'log_error "kubectl 验证失败,退出码: $?"' ERR
# 执行主函数
main "$@"