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

61 lines
1.3 KiB
Bash
Raw 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'
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}"
}
: "${GITHUB_OUTPUT:?GITHUB_OUTPUT 未设置}" >/dev/null
verify_cluster() {
local timeout=30
if timeout "$timeout" kubectl cluster-info >/dev/null 2>&1; then
log_success "kubectl 集群连接验证通过"
log_info "集群信息:"
kubectl cluster-info
log_info "尝试获取节点信息"
if kubectl get nodes >/dev/null 2>&1; then
kubectl get nodes
else
log_warning "无法获取节点信息(可能权限不足)"
fi
local current_context
current_context=$(kubectl config current-context 2>/dev/null || echo "unknown")
log_info "当前上下文: $current_context"
printf 'current-context=%s\n' "$current_context" >> "$GITHUB_OUTPUT"
else
log_error "kubectl 集群连接验证失败"
kubectl cluster-info || true
exit 1
fi
}
trap 'log_error "kubectl 验证失败,退出码: $?"' ERR
main() {
verify_cluster
}
main "$@"