fix(k8s): validate Mathmast username input and enhance error handling in kubectl config clearing

Signed-off-by: 孙振宇 <>
This commit is contained in:
孙振宇 2025-01-15 03:24:48 +08:00
parent 614f6b8683
commit 293860028f

View File

@ -186,7 +186,15 @@ setup_kubelogin() {
prompt_username() {
echo "[PROMPT] Please enter your Mathmast account name (ending with @mathmast.com, eg. jack@mathmast.com):"
read -r username
# While loop to check if username is valid
while ! echo "${username}" | grep -qE '^[a-zA-Z0-9._%+-]+@mathmast.com$'; do
echo "[ERROR] Username invalid, please enter a valid Mathmast account name (ending with @mathmast.com, eg. jack@mathmast.com):"
read -r username
done
echo "[PROMPT] Username: ${username}"
}
@ -260,10 +268,25 @@ clear_auth() {
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
if ! kubectl config delete-user "${username}" > /dev/null 2>&1; then
echo "[ERROR] User ${username} not found in kubectl config."
exit 1
fi
if ! kubectl config delete-context "${username}@freeleaps-cluster" > /dev/null 2>&1; then
echo "[ERROR] Context ${username}@freeleaps-cluster not found in kubectl config."
exit 1
fi
if ! kubectl config delete-cluster freeleaps-cluster > /dev/null 2>&1; then
echo "[ERROR] Cluster freeleaps-cluster not found in kubectl config."
exit 1
fi
if ! kubectl config unset current-context > /dev/null 2>&1; then
echo "[ERROR] Unable to unset current context in kubectl config."
exit 1
fi
echo "[CLEAR] kubectl authentication cleared successfully."
}