Improve oc smart login

This commit is contained in:
yves-sbb committed 2026-10-07 14:06:50 +02:00
1 parent eb82f66226
commit d3b8591fa1
4 files changed
+263 -152

No files matched your search

+30 -13
View File
@@ -64,12 +64,13 @@ get_token_expiry_info() {
################################################################################
# Smart OCP Cluster Switch Function
#
# Usage: oc_smart_switch "aws01t" "AWS-Test1" "context-name" "project" "token-var-name" "login-url"
# Usage: oc_smart_switch "aws01t" "AWS-Test1" "context-name" "project" \
# "token_value" "server_url" "browser_url"
#
# Example: oc_smart_switch "aws01t" "AWS-Test1" \
# "kihub-dev/api-aws01t-sbb-aws-test-net:6443/u241100" \
# "kihub-dev" \
# "OPENSHIFT_TOKEN_AWS_TEST" \
# "$OPENSHIFT_TOKEN_AWS_TEST" \
# "https://api.aws01t.sbb-aws-test.net:6443" \
# "https://oauth-openshift.apps.aws01t.sbb-aws-test.net/oauth/token/request"
################################################################################
@@ -79,7 +80,7 @@ oc_smart_switch() {
local cluster_display="$2"
local context_name="$3"
local project_name="$4"
local token_var_name="$5"
local token_value="$5" # <- Now direct token VALUE, not variable name!
local server_url="$6"
local browser_url="$7"
@@ -91,7 +92,7 @@ oc_smart_switch() {
if ! is_oc_logged_in; then
echo -e "${YELLOW}→ Not logged in to any cluster${NC}"
echo -e "${YELLOW}→ Performing full login...${NC}"
_oc_perform_login "$cluster_short" "$token_var_name" "$server_url" "$browser_url"
_oc_perform_login "$cluster_short" "$token_value" "$server_url" "$browser_url"
return $?
fi
@@ -99,7 +100,7 @@ oc_smart_switch() {
if ! is_oc_token_valid; then
echo -e "${YELLOW}→ Current token expired or invalid${NC}"
echo -e "${YELLOW}→ Performing new login...${NC}"
_oc_perform_login "$cluster_short" "$token_var_name" "$server_url" "$browser_url"
_oc_perform_login "$cluster_short" "$token_value" "$server_url" "$browser_url"
return $?
fi
@@ -120,13 +121,23 @@ oc_smart_switch() {
if oc config use-context "$context_name" &>/dev/null; then
echo -e "${GREEN}✓ Context switched successfully${NC}"
# Verify the token is still valid for this new context
if ! is_oc_token_valid; then
echo -e "${YELLOW}⚠ Token not valid for this cluster${NC}"
echo -e "${YELLOW}→ Previous login was for a different cluster${NC}"
echo -e "${YELLOW}→ Performing new login with provided token...${NC}"
_oc_perform_login "$cluster_short" "$token_value" "$server_url" "$browser_url"
return $?
fi
_oc_verify_project "$project_name"
return 0
else
echo -e "${RED}✗ Failed to switch context${NC}"
echo -e "${YELLOW}→ This might mean the cluster/context doesn't exist in kubeconfig${NC}"
echo -e "${YELLOW}→ Performing full login...${NC}"
_oc_perform_login "$cluster_short" "$token_var_name" "$server_url" "$browser_url"
_oc_perform_login "$cluster_short" "$token_value" "$server_url" "$browser_url"
return $?
fi
}
@@ -137,15 +148,13 @@ oc_smart_switch() {
_oc_perform_login() {
local cluster_short="$1"
local token_var_name="$2"
local token_value="$2" # <- Direct token VALUE (already expanded by alias)
local server_url="$3"
local browser_url="$4"
# Get token from environment variable
local token_value="${!token_var_name}"
if [[ -z "$token_value" ]]; then
echo -e "${YELLOW}→ Token environment variable '${token_var_name}' not set${NC}"
echo -e "${YELLOW}→ Token not provided or empty${NC}"
echo -e "${YELLOW}→ Opening browser for manual token entry...${NC}"
# Try to find browser
@@ -160,7 +169,11 @@ _oc_perform_login() {
# Prompt for token
echo -e "${YELLOW}Please copy the token from the browser and paste it here:${NC}"
read -p "Token: " manual_token
if [[ -n "${ZSH_VERSION}" ]]; then
read "?Token: " manual_token
else
read -p "Token: " manual_token
fi
if [[ -z "$manual_token" ]]; then
echo -e "${RED}✗ No token provided${NC}"
@@ -170,7 +183,7 @@ _oc_perform_login() {
oc login --token="$manual_token" --server="$server_url"
else
# Try login with stored token
echo -e "${BLUE}→ Using token from ${token_var_name}...${NC}"
echo -e "${BLUE}→ Using provided token...${NC}"
if oc login --token="$token_value" --server="$server_url" &>/dev/null; then
echo -e "${GREEN}✓ Login successful with stored token${NC}"
return 0
@@ -188,7 +201,11 @@ _oc_perform_login() {
fi
echo -e "${YELLOW}Please copy the new token and paste it here:${NC}"
read -p "Token: " manual_token
if [[ -n "${ZSH_VERSION}" ]]; then
read "?Token: " manual_token
else
read -p "Token: " manual_token
fi
if [[ -z "$manual_token" ]]; then
echo -e "${RED}✗ No token provided${NC}"