Implement oc smart login
This commit is contained in:
6 files changed
+1701
No files matched your search
@@ -0,0 +1,283 @@
|
||||
#!/bin/bash
|
||||
|
||||
################################################################################
|
||||
# OCP Smart Login/Switch Helper Functions
|
||||
#
|
||||
# Purpose: Intelligente OCP Cluster-Verwaltung
|
||||
# - Erkennt ob bereits angemeldet & Token noch gültig
|
||||
# - Falls ja: nur Context wechseln (schnell)
|
||||
# - Falls nein: Full Login durchführen (Browser)
|
||||
#
|
||||
# Replaces manual switching between ocLogin* und ocAWS-*/ocSC-* aliases
|
||||
################################################################################
|
||||
|
||||
# Color codes
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
################################################################################
|
||||
# Check if we're logged in to any cluster
|
||||
################################################################################
|
||||
|
||||
is_oc_logged_in() {
|
||||
# Try to get current context - if it fails, we're not logged in
|
||||
oc config current-context &>/dev/null
|
||||
return $?
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Check if current OC session/token is still valid
|
||||
################################################################################
|
||||
|
||||
is_oc_token_valid() {
|
||||
# Try a simple command that requires auth (list projects)
|
||||
# If it succeeds, token is still valid
|
||||
oc get projects &>/dev/null
|
||||
return $?
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Get remaining token expiry time in seconds (if available)
|
||||
################################################################################
|
||||
|
||||
get_token_expiry_info() {
|
||||
# Try to extract token expiry from kubeconfig
|
||||
# This is cluster/auth-method specific, so we do a simple check
|
||||
|
||||
if ! oc whoami &>/dev/null; then
|
||||
echo "Not authenticated"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# If we got here, token is valid
|
||||
local current_user=$(oc whoami)
|
||||
local current_context=$(oc config current-context)
|
||||
|
||||
echo "✓ Authenticated as: $current_user"
|
||||
echo "✓ Current context: $current_context"
|
||||
return 0
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Smart OCP Cluster Switch Function
|
||||
#
|
||||
# Usage: oc_smart_switch "aws01t" "AWS-Test1" "context-name" "project" "token-var-name" "login-url"
|
||||
#
|
||||
# Example: oc_smart_switch "aws01t" "AWS-Test1" \
|
||||
# "kihub-dev/api-aws01t-sbb-aws-test-net:6443/u241100" \
|
||||
# "kihub-dev" \
|
||||
# "OPENSHIFT_TOKEN_AWS_TEST" \
|
||||
# "https://api.aws01t.sbb-aws-test.net:6443" \
|
||||
# "https://oauth-openshift.apps.aws01t.sbb-aws-test.net/oauth/token/request"
|
||||
################################################################################
|
||||
|
||||
oc_smart_switch() {
|
||||
local cluster_short="$1"
|
||||
local cluster_display="$2"
|
||||
local context_name="$3"
|
||||
local project_name="$4"
|
||||
local token_var_name="$5"
|
||||
local server_url="$6"
|
||||
local browser_url="$7"
|
||||
|
||||
echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
|
||||
echo -e "${BLUE}OCP Smart Switch: ${cluster_display}${NC}"
|
||||
echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
|
||||
|
||||
# Step 1: Check if logged in at all
|
||||
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"
|
||||
return $?
|
||||
fi
|
||||
|
||||
# Step 2: Check if token is still valid
|
||||
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"
|
||||
return $?
|
||||
fi
|
||||
|
||||
# Step 3: Token is valid - check if we're already on target cluster
|
||||
local current_context=$(oc config current-context 2>/dev/null)
|
||||
|
||||
if [[ "$current_context" == "$context_name" ]]; then
|
||||
echo -e "${GREEN}✓ Already on ${cluster_display} (context: $current_context)${NC}"
|
||||
echo -e "${GREEN}✓ Using current token${NC}"
|
||||
_oc_verify_project "$project_name"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Step 4: Token valid but different cluster - quick context switch
|
||||
echo -e "${GREEN}✓ Token valid, switching context...${NC}"
|
||||
echo -e " From: $current_context"
|
||||
echo -e " To: $context_name"
|
||||
|
||||
if oc config use-context "$context_name" &>/dev/null; then
|
||||
echo -e "${GREEN}✓ Context switched successfully${NC}"
|
||||
_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"
|
||||
return $?
|
||||
fi
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Internal: Perform full login
|
||||
################################################################################
|
||||
|
||||
_oc_perform_login() {
|
||||
local cluster_short="$1"
|
||||
local token_var_name="$2"
|
||||
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}→ Opening browser for manual token entry...${NC}"
|
||||
|
||||
# Try to find browser
|
||||
if command -v open &>/dev/null; then
|
||||
open "$browser_url"
|
||||
elif command -v xdg-open &>/dev/null; then
|
||||
xdg-open "$browser_url"
|
||||
else
|
||||
echo -e "${RED}✗ Could not open browser. Please manually visit:${NC}"
|
||||
echo " $browser_url"
|
||||
fi
|
||||
|
||||
# Prompt for token
|
||||
echo -e "${YELLOW}Please copy the token from the browser and paste it here:${NC}"
|
||||
read -p "Token: " manual_token
|
||||
|
||||
if [[ -z "$manual_token" ]]; then
|
||||
echo -e "${RED}✗ No token provided${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
oc login --token="$manual_token" --server="$server_url"
|
||||
else
|
||||
# Try login with stored token
|
||||
echo -e "${BLUE}→ Using token from ${token_var_name}...${NC}"
|
||||
if oc login --token="$token_value" --server="$server_url" &>/dev/null; then
|
||||
echo -e "${GREEN}✓ Login successful with stored token${NC}"
|
||||
return 0
|
||||
else
|
||||
echo -e "${YELLOW}→ Stored token invalid, opening browser...${NC}"
|
||||
|
||||
# Open browser for new token
|
||||
if command -v open &>/dev/null; then
|
||||
open "$browser_url"
|
||||
elif command -v xdg-open &>/dev/null; then
|
||||
xdg-open "$browser_url"
|
||||
else
|
||||
echo -e "${RED}✗ Could not open browser. Please manually visit:${NC}"
|
||||
echo " $browser_url"
|
||||
fi
|
||||
|
||||
echo -e "${YELLOW}Please copy the new token and paste it here:${NC}"
|
||||
read -p "Token: " manual_token
|
||||
|
||||
if [[ -z "$manual_token" ]]; then
|
||||
echo -e "${RED}✗ No token provided${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
oc login --token="$manual_token" --server="$server_url"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Internal: Verify/switch to target project
|
||||
################################################################################
|
||||
|
||||
_oc_verify_project() {
|
||||
local project_name="$1"
|
||||
|
||||
if [[ -z "$project_name" ]]; then
|
||||
return 0 # No project to switch
|
||||
fi
|
||||
|
||||
# Check if we're already in the right project
|
||||
local current_project=$(oc project -q 2>/dev/null)
|
||||
|
||||
if [[ "$current_project" == "$project_name" ]]; then
|
||||
echo -e "${GREEN}✓ Already in project: $project_name${NC}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Try to switch project
|
||||
if oc project "$project_name" &>/dev/null; then
|
||||
echo -e "${GREEN}✓ Switched to project: $project_name${NC}"
|
||||
return 0
|
||||
else
|
||||
echo -e "${YELLOW}⚠ Could not switch to project: $project_name${NC}"
|
||||
echo -e "${YELLOW} Available projects: $(oc get projects -o name | tr '\n' ',' | sed 's/,$//')${NC}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Convenience: Get cluster status
|
||||
################################################################################
|
||||
|
||||
oc_status() {
|
||||
echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
|
||||
echo -e "${BLUE}OCP Status${NC}"
|
||||
echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
|
||||
|
||||
if ! is_oc_logged_in; then
|
||||
echo -e "${RED}✗ Not logged in to any cluster${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if ! is_oc_token_valid; then
|
||||
echo -e "${RED}✗ Token expired or invalid${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
get_token_expiry_info
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# List all available contexts
|
||||
################################################################################
|
||||
|
||||
oc_contexts() {
|
||||
echo -e "${BLUE}Available OCP Contexts:${NC}"
|
||||
oc config get-contexts
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Logout from current cluster
|
||||
################################################################################
|
||||
|
||||
oc_logout() {
|
||||
echo -e "${YELLOW}Logging out from current cluster...${NC}"
|
||||
oc logout
|
||||
echo -e "${GREEN}✓ Logged out${NC}"
|
||||
}
|
||||
|
||||
export -f is_oc_logged_in
|
||||
export -f is_oc_token_valid
|
||||
export -f get_token_expiry_info
|
||||
export -f oc_smart_switch
|
||||
export -f _oc_perform_login
|
||||
export -f _oc_verify_project
|
||||
export -f oc_status
|
||||
export -f oc_contexts
|
||||
export -f oc_logout
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
# ============================================================================
|
||||
# OCP Smart Login/Context Switch Aliases
|
||||
# ============================================================================
|
||||
# These aliases intelligently switch between OCP clusters:
|
||||
# - If logged in with valid token: just switch context (fast)
|
||||
# - If token expired or not logged in: full login with browser (safe)
|
||||
#
|
||||
# Source this after .bash_aliases to override the old context-only aliases
|
||||
# ============================================================================
|
||||
|
||||
# First, source the helper functions
|
||||
source ~/.bash_oc_smart.sh
|
||||
|
||||
# ============================================================================
|
||||
# AWS Clusters - Smart Switch Aliases
|
||||
# ============================================================================
|
||||
|
||||
# AWS-Test1 (aws01t)
|
||||
alias ocAWS-Test1='oc_smart_switch \
|
||||
"aws01t" \
|
||||
"AWS-Test1" \
|
||||
"kihub-u241100-dev/api-aws01t-sbb-aws-test-net:6443/u241100" \
|
||||
"kihub-dev-u241100" \
|
||||
"OPENSHIFT_TOKEN_AWS_TEST" \
|
||||
"https://api.aws01t.sbb-aws-test.net:6443" \
|
||||
"https://oauth-openshift.apps.aws01t.sbb-aws-test.net/oauth/token/request"'
|
||||
|
||||
# AWS-Test2 (aws02t)
|
||||
alias ocAWS-Test2='oc_smart_switch \
|
||||
"aws02t" \
|
||||
"AWS-Test2" \
|
||||
"kihub-u241100-dev/api-aws02t-sbb-aws-test-net:6443/u241100" \
|
||||
"kihub-dev-u241100" \
|
||||
"OPENSHIFT_TOKEN_AWS_TEST" \
|
||||
"https://api.aws02t.sbb-aws-test.net:6443" \
|
||||
"https://oauth-openshift.apps.aws02t.sbb-aws-test.net/oauth/token/request"'
|
||||
|
||||
# AWS-Dev (Development on aws01t)
|
||||
alias ocAWS-Dev='oc_smart_switch \
|
||||
"aws01t" \
|
||||
"AWS-Dev" \
|
||||
"kihub-u241100-dev/api-aws01t-sbb-aws-test-net:6443/u241100" \
|
||||
"kihub-dev-u241100" \
|
||||
"OPENSHIFT_TOKEN_AWS_TEST" \
|
||||
"https://api.aws01t.sbb-aws-test.net:6443" \
|
||||
"https://oauth-openshift.apps.aws01t.sbb-aws-test.net/oauth/token/request"'
|
||||
|
||||
# AWS-RT (Runtime on aws01t)
|
||||
alias ocAWS-RT='oc_smart_switch \
|
||||
"aws01t" \
|
||||
"AWS-RT" \
|
||||
"kihub-rt/api-aws01t-sbb-aws-test-net:6443/u241100" \
|
||||
"kihub-rt" \
|
||||
"OPENSHIFT_TOKEN_AWS_TEST" \
|
||||
"https://api.aws01t.sbb-aws-test.net:6443" \
|
||||
"https://oauth-openshift.apps.aws01t.sbb-aws-test.net/oauth/token/request"'
|
||||
|
||||
# AWS-Test (Test on aws01t)
|
||||
alias ocAWS-Test='oc_smart_switch \
|
||||
"aws01t" \
|
||||
"AWS-Test" \
|
||||
"kihub-test/api-aws01t-sbb-aws-test-net:6443/u241100" \
|
||||
"kihub-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"'
|
||||
|
||||
# AWS-Int (Integration on Lisa)
|
||||
alias ocAWS-Int='oc_smart_switch \
|
||||
"lisa" \
|
||||
"AWS-Int" \
|
||||
"kihub-int/api-lisa-sbb-aws-net:6443/u241100" \
|
||||
"kihub-int" \
|
||||
"OPENSHIFT_TOKEN_AWS_PROD" \
|
||||
"https://api.lisa.sbb-aws.net:6443" \
|
||||
"https://oauth-openshift.apps.lisa.sbb-aws.net/oauth/token/request"'
|
||||
|
||||
# AWS-Prod (Production on Lisa)
|
||||
alias ocAWS-Prod='oc_smart_switch \
|
||||
"lisa" \
|
||||
"AWS-Prod" \
|
||||
"kihub-prod/api-lisa-sbb-aws-net:6443/u241100" \
|
||||
"kihub-prod" \
|
||||
"OPENSHIFT_TOKEN_AWS_PROD" \
|
||||
"https://api.lisa.sbb-aws.net:6443" \
|
||||
"https://oauth-openshift.apps.lisa.sbb-aws.net/oauth/token/request"'
|
||||
|
||||
# AWS-Lisa (Production cluster)
|
||||
alias ocAWS-Lisa='oc_smart_switch \
|
||||
"lisa" \
|
||||
"AWS-Lisa" \
|
||||
"kihub-prod/api-lisa-sbb-aws-net:6443/u241100" \
|
||||
"kihub-prod" \
|
||||
"OPENSHIFT_TOKEN_AWS_PROD" \
|
||||
"https://api.lisa.sbb-aws.net:6443" \
|
||||
"https://oauth-openshift.apps.lisa.sbb-aws.net/oauth/token/request"'
|
||||
|
||||
# AWS-Maggie (Production cluster)
|
||||
alias ocAWS-Maggie='oc_smart_switch \
|
||||
"maggie" \
|
||||
"AWS-Maggie" \
|
||||
"kihub-prod/api-maggie-sbb-aws-net:6443/u241100" \
|
||||
"kihub-prod" \
|
||||
"OPENSHIFT_TOKEN_AWS_PROD" \
|
||||
"https://api.maggie.sbb-aws.net:6443" \
|
||||
"https://oauth-openshift.apps.maggie.sbb-aws.net/oauth/token/request"'
|
||||
|
||||
# AWS-WZU
|
||||
alias ocAWS-WZU='oc_smart_switch \
|
||||
"wzu01t" \
|
||||
"AWS-WZU" \
|
||||
"kihub-test/api-wzu01t-ocp-test-sbb-aws-net:6443/u241100" \
|
||||
"" \
|
||||
"OPENSHIFT_TOKEN_WZU" \
|
||||
"https://api.wzu01t.ocp-test.sbb-aws.net:6443" \
|
||||
"https://oauth-openshift.apps.wzu01t.ocp-test.sbb-aws.net/oauth/token/request"'
|
||||
|
||||
# ============================================================================
|
||||
# SC (OTC) Clusters - Smart Switch Aliases
|
||||
# ============================================================================
|
||||
|
||||
# SC-Dev (Development on sc02t)
|
||||
alias ocSC-Dev='oc_smart_switch \
|
||||
"sc02t" \
|
||||
"SC-Dev" \
|
||||
"kihub-u241100-dev/api-sc02t-otc-test-sbb-ch:6443/u241100" \
|
||||
"kihub-dev-u241100" \
|
||||
"OPENSHIFT_TOKEN_SC02" \
|
||||
"https://api.sc02t.otc-test.sbb.ch:6443" \
|
||||
"https://oauth-openshift.apps.sc02t.otc-test.sbb.ch/oauth/token/request"'
|
||||
|
||||
# SC-RT (Runtime on sc02t)
|
||||
alias ocSC-RT='oc_smart_switch \
|
||||
"sc02t" \
|
||||
"SC-RT" \
|
||||
"kihub-rt/api-sc02t-otc-test-sbb-ch:6443/u241100" \
|
||||
"kihub-rt" \
|
||||
"OPENSHIFT_TOKEN_SC02" \
|
||||
"https://api.sc02t.otc-test.sbb.ch:6443" \
|
||||
"https://oauth-openshift.apps.sc02t.otc-test.sbb.ch/oauth/token/request"'
|
||||
|
||||
# SC-Test (Test on sc02t)
|
||||
alias ocSC-Test='oc_smart_switch \
|
||||
"sc02t" \
|
||||
"SC-Test" \
|
||||
"kihub-test/api-sc02t-otc-test-sbb-ch:6443/u241100" \
|
||||
"kihub-test" \
|
||||
"OPENSHIFT_TOKEN_SC02" \
|
||||
"https://api.sc02t.otc-test.sbb.ch:6443" \
|
||||
"https://oauth-openshift.apps.sc02t.otc-test.sbb.ch/oauth/token/request"'
|
||||
|
||||
# SC-Int (Integration on Sputnix)
|
||||
alias ocSC-Int='oc_smart_switch \
|
||||
"sputnix" \
|
||||
"SC-Int" \
|
||||
"kihub-int/api-sputnix-otc-sbb-ch:6443/u241100" \
|
||||
"kihub-int" \
|
||||
"OPENSHIFT_TOKEN_SCSputnix" \
|
||||
"https://api.sputnix.otc.sbb.ch:6443" \
|
||||
"https://oauth-openshift.apps.sputnix.otc.sbb.ch/oauth/token/request"'
|
||||
|
||||
# SC-Prod (Production on Sputnix)
|
||||
alias ocSC-Prod='oc_smart_switch \
|
||||
"sputnix" \
|
||||
"SC-Prod" \
|
||||
"kihub-prod/api-sputnix-otc-sbb-ch:6443/u241100" \
|
||||
"kihub-prod" \
|
||||
"OPENSHIFT_TOKEN_SCSputnix" \
|
||||
"https://api.sputnix.otc.sbb.ch:6443" \
|
||||
"https://oauth-openshift.apps.sputnix.otc.sbb.ch/oauth/token/request"'
|
||||
|
||||
# ============================================================================
|
||||
# Convenience Aliases for Helper Functions
|
||||
# ============================================================================
|
||||
|
||||
alias ocStatus='oc_status'
|
||||
alias ocCtx='oc_contexts'
|
||||
alias ocLogout='oc_logout'
|
||||
alias ocWhoami='oc whoami'
|
||||
|
||||
# ============================================================================
|
||||
# Notes on Usage
|
||||
# ============================================================================
|
||||
#
|
||||
# Old way (still works):
|
||||
# ocLoginAWS-Test1 → Full login every time
|
||||
# ocAWS-Dev → Only context switch (fails if not logged in)
|
||||
#
|
||||
# New way (smart):
|
||||
# ocAWS-Dev → Auto-detect & do the right thing
|
||||
# - Logged in? → Switch context only (FAST)
|
||||
# - Not logged in? → Full login (SAFE)
|
||||
#
|
||||
# Helper commands:
|
||||
# ocStatus → Show current auth status
|
||||
# ocCtx → List all available contexts
|
||||
# ocLogout → Logout from current cluster
|
||||
# ocWhoami → Show current user
|
||||
#
|
||||
# Example workflow:
|
||||
# 1. ocAWS-Test1 → Switch to Test1, full login if needed
|
||||
# 2. ocAWS-Int → Switch to Int, just context switch (fast!)
|
||||
# 3. ocAWS-Prod → Switch to Prod, just context switch (fast!)
|
||||
# 4. ocSC-Int → Switch to SC Int, full login if different token
|
||||
#
|
||||
|
||||
@@ -203,3 +203,13 @@ export SDKMAN_DIR="$HOME/.sdkman"
|
||||
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
|
||||
export PATH="$(go env GOPATH)/bin:$PATH"
|
||||
eval "$(direnv hook zsh)"
|
||||
|
||||
# ============================================================================
|
||||
# OCP Smart Login/Context Switch
|
||||
# ============================================================================
|
||||
# Source OCP smart login aliases (MUST be AFTER .bash_aliases)
|
||||
# Provides intelligent cluster switching with auto-login/context-switch detection
|
||||
if [[ -f ~/.bash_oc_smart_aliases.sh ]]; then
|
||||
source ~/.bash_oc_smart_aliases.sh
|
||||
fi
|
||||
|
||||
Reference in new issue
Block a user