feat(k8s): enhance freeleaps-cluster-authenticator with sub-command support and detailed help message

Signed-off-by: 孙振宇 <>
This commit is contained in:
孙振宇 2025-01-14 17:18:27 +08:00
parent 940c0cc318
commit 02a06cc6f8

View File

@ -17,6 +17,31 @@ KUBELOGIN_VERSION=${KUBELOGIN_VERSION:-v1.31.1}
AUTO_INSTALL_KUBECTL=${AUTO_INSTALL_KUBECTL:-true}
AUTO_INSTALL_KUBELOGIN=${AUTO_INSTALL_KUBELOGIN:-true}
help() {
echo "Freeleaps Cluster Authenticator"
echo ""
echo "Usage: freeleaps-cluster-authenticator <sub-command>"
echo ""
echo "Sub Commands:"
echo " auth Setup kubectl for freeleaps cluster with Mathmast account."
echo " clear Clear authentication for freeleaps cluster."
echo " doctor Check if all the required tools are installed."
echo " help,-h,--help Show this help message."
echo ""
echo "Environment Options:"
echo " CLUSTER_API_LB_IP: IP address of the cluster API load balancer."
echo " CLUSTER_API_LB_PORT: Port of the cluster API load balancer."
echo " MICROSOFT_ENTRA_ID_TENANT: Microsoft Entra ID tenant."
echo " MICROSOFT_ENTRA_ID_CLIENT_ID: Microsoft Entra ID client ID."
echo " MICROSOFT_ENTRA_ID_CLIENT_SECRET: Microsoft Entra ID client secret."
echo " MICROSOFT_ENTRA_ID_ISSUER: Microsoft Entra ID issuer URL."
echo " OS: Operating system (linux or darwin). Default: auto"
echo " ARCH: Architecture (amd64 or arm64). Default: auto"
echo " KUBECTL_VERSION: Version of kubectl to install. Default: v1.30.3"
echo " KUBELOGIN_VERSION: Version of kubelogin to install. Default: v1.31.1"
echo " AUTO_INSTALL_KUBECTL: Automatically install kubectl if not found. Default: true"
}
gather_os_environment() {
echo "[GATHER] Checking OS and architecture..."
@ -89,6 +114,9 @@ ensure_kubelogin() {
gather_prerequisites() {
gather_os_environment
echo "[INFO] OS: ${OS}"
echo "[INFO] Architecture: ${ARCH}"
echo "[PREREQUISITES] Checking for required tools..."
if ! command -v curl > /dev/null; then
@ -211,12 +239,8 @@ check_whoami() {
kubectl auth whoami
}
main() {
auth() {
gather_prerequisites
echo "[INFO] OS: ${OS}"
echo "[INFO] Architecture: ${ARCH}"
setup_kubelogin
prompt_username
set_kubectl_credentials
@ -229,4 +253,48 @@ main() {
echo "[INFO] Now you can try to using kubectl to interact with the cluster."
}
clear_auth() {
prompt_username
echo "[CLEAR] Clearing kubectl authentication..."
kubectl config delete-user "${username}"
kubectl config delete-context "${username}@freeleaps-cluster"
kubectl config delete-cluster freeleaps-cluster
kubectl config unset current-context
echo "[CLEAR] kubectl authentication cleared successfully."
}
main() {
if [ $# -lt 1 ]; then
echo "[ERROR] No sub-command provided."
echo ""
help
exit 1
fi
subcommand="$1"
case "${subcommand}" in
auth)
auth
;;
clear)
clear_auth
;;
doctor)
gather_prerequisites
;;
help|-h|--help)
help
;;
*)
echo "[ERROR] Invalid sub-command: ${subcommand}"
help
exit 1
;;
esac
}
main "$@"