Skip to content

Instantly share code, notes, and snippets.

@msoranno
Last active January 3, 2021 19:53
Show Gist options
  • Save msoranno/d1a0ac5f3aade98345bf0b809c7831ce to your computer and use it in GitHub Desktop.
Save msoranno/d1a0ac5f3aade98345bf0b809c7831ce to your computer and use it in GitHub Desktop.
Create a kubeconfig file from the ./kube/config
change-context(){
export KUBECONFIG=""
TARGET_FOLDER="/tmp/kube"
SERVICE_ACCOUNT_NAME="default"
NAMESPACE="default"
mkdir -p "${TARGET_FOLDER}"
kubectl config use-context $1
p=`echo $1 | sed 's,/, ,g' | awk '{print $1}'`
context=$(kubectl config current-context)
KUBECFG_FILE_NAME="$TARGET_FOLDER/$p.kubeconfig"
CLUSTER_NAME=$(kubectl config get-contexts "$context" | awk '{print $3}' | tail -n 1)
echo "Cluster name: $CLUSTER_NAME"
ENDPOINT=$(kubectl config view \
-o jsonpath="{.clusters[?(@.name == \"${CLUSTER_NAME}\")].cluster.server}")
echo "Endpoint: $ENDPOINT"
CA=$(kubectl config view \
-o jsonpath="{.clusters[?(@.name == \"${CLUSTER_NAME}\")].cluster.certificate-authority}")
#echo "CA: $CA"
USER_CONTEXT=$(kubectl config view \
-o jsonpath="{.contexts[?(@.name == \"${CLUSTER_NAME}\")].context.user}")
#echo "User context: $USER_CONTEXT"
USER_ID_TOKEN=$(kubectl config view \
-o jsonpath="{.users[?(@.name == \"${USER_CONTEXT}\")].user.auth-provider.config.id-token}")
#echo "User id token: $USER_ID_TOKEN"
REFRESH_TOKEN=$(kubectl config view \
-o jsonpath="{.users[?(@.name == \"${USER_CONTEXT}\")].user.auth-provider.config.refresh-token}")
#echo "Refresh token: $REFRESH_TOKEN"
cat <<EOF > $KUBECFG_FILE_NAME
apiVersion: v1
clusters:
- cluster:
certificate-authority: $CA
server: $ENDPOINT
name: $CLUSTER_NAME
contexts:
- context:
cluster: $CLUSTER_NAME
namespace: default
user: $USER_CONTEXT
name: $CLUSTER_NAME
current-context: $CLUSTER_NAME
kind: Config
preferences: {}
users:
- name: $USER_CONTEXT
user:
auth-provider:
config:
client-id: kube
client-secret: kube
id-token: $USER_ID_TOKEN
idp-issuer-url: https://iam.cloud.ibm.com/identity
refresh-token: $REFRESH_TOKEN
name: oidc
EOF
#kubectl config unset current-context
export KUBECONFIG="${KUBECFG_FILE_NAME}"
kubectl get nodes
if [ $? -eq 0 ]; then
changePrompt "$OLD_OLD_PROMPT$p: "
else
kubectl config unset current-context
export KUBECONFIG=""
fi
}
get-contexts(){
export KUBECONFIG=""
kubectl config unset current-context
sleep 1
changePrompt "$OLD_OLD_PROMPT"
echo ; echo ; echo "missing context, please choose one of these:" ; echo
echo "Available contexts:"
echo "-------------------------------------------------------------------"
echo
kubectl config get-contexts | awk '{print $1}' | sed '1d'
echo
}
changePrompt(){
current_shell=$(ps -p $$ -oargs=)
case "$current_shell" in
"zsh" | "/usr/bin/zsh")
PROMPT="${1} "
;;
"bash")
PS1="$1"
;;
*)
echo ; echo ; "Shell [ $current_shell ] could not be handled."
;;
esac
}
cxt(){
if [ -z $OLD_OLD_PROMPT ]; then
current_shell=$(ps -p $$ -oargs=)
case "$current_shell" in
"zsh" | "/usr/bin/zsh")
export OLD_OLD_PROMPT="${PROMPT} "
;;
"bash")
export OLD_OLD_PROMPT="${PS1}"
;;
*)
echo ; echo ; "Shell [ $current_shell ] could not be handled."
;;
esac
fi
case "$1" in
"")
get-contexts
;;
*)
change-context "$1"
;;
esac
}
@msoranno
Copy link
Author

msoranno commented Dec 29, 2020

  • This will work on cluster created by ibmcloud context.
  • Works with bash and zsh

Download the raw gist to your home

curl -s -O [RAW URL]

Just add either to your .bashrc or .zshrc

source set-kubectl-context.sh

To invoke it just:

cxt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment