diff --git a/README.md b/README.md index 94d825f..30de236 100644 --- a/README.md +++ b/README.md @@ -1,46 +1 @@ # ktx - -> Plant a Tree, Have a Baby, Build a k8s context switcher - - -## Motivation -Like every good YAML Engineer, I'm constantly switching between half a dozen k8s clusters with a big list of namespaces in each of them. This gets cumbersume with just plain kubectl, so a big ecosystem of context switchers has evolved. I tried some of them, but I had at least one problem with each of them, so I started my own to fullfill my needs. My personal requirements for a context switcher are: - * When opening a new shell I want to be in the context I used last - * When switching context or namespace in one shell, all other shells should not be affected - -## Installation - -Requirements: - * kubectl, fzf and jq are installed - * Your global k8s config is in ~/.kube/config - -Setup: - -```shell -# Clone the repo somewhere -cd ~/repos && git clone https://git.rainbownerds.de/fleaz/ktx - -# Source the ktx script in your shell config -echo "source ~/repos/ktx/ktx" >> .zshrc - -# Create the required directory -mkdir ~/.kube/ktx -``` - -Now open a new shell and you should have `ktx` and `kn` available - -## Usage - -```shell -# ktx alone to get a list of all your contexts to choose from -ktx -# ktx with the name of a context to directly switch to it -ktx prod-ams1 -# ktx with the name of a context and a namespace -ktx prod-ams1 monitoring - -# kn alone to get a list of all namespaces in the current cluster -kn -# kn with the name of a namespace to directly switch to it -``` - diff --git a/ktx b/ktx old mode 100755 new mode 100644 index 0facef8..f3a76fc --- a/ktx +++ b/ktx @@ -2,16 +2,6 @@ CUR_FILE="$HOME/.kube/current" -_create_file_and_switch() { - CONTEXT=${1} - NAMESPACE=${2} - FILENAME="$HOME/.kube/ktx/${CONTEXT}_${NAMESPACE}.conf" - kubectl config view --minify --flatten --context ${CONTEXT} > ${FILENAME} - export KUBECONFIG=${FILENAME} - kubectl config set-context --current --namespace=${NAMESPACE} - ln -sf ${FILENAME} ${CUR_FILE} -} - # Switch context # Usage: ktx [cluster] [namespace] @@ -21,18 +11,24 @@ ktx() { NAMESPACE=${2:-} if [ -z $CONTEXT ]; then - # unset KUBECONFIG= variable here to always start with the global config - SELECTION=$(KUBECONFIG= kubectl config view -o json | jq -r '.contexts[] | "\(.name)|\(.context.namespace // "default")"' | sort -r | fzf) + SELECTION=$(kubectl config get-contexts | awk '{print $2"|"$4}' | tail +2 | sort -r | fzf) CONTEXT=$(echo $SELECTION | cut -d"|" -f1) DEFAULT_NS=$(echo $SELECTION | cut -d"|" -f2) fi - # Take default namespace if user didn't supplied a specific one if [ -z $NAMESPACE ]; then NAMESPACE=${DEFAULT_NS} fi - _create_file_and_switch ${CONTEXT} ${NAMESPACE} + FILENAME="$HOME/.kube/frickel/${CONTEXT}_${NAMESPACE}.conf" + + kubectl config view --minify --flatten --context $CONTEXT > ${FILENAME} + + export KUBECONFIG=${FILENAME} + + kubectl config set-context --current --namespace=$NAMESPACE + + ln -sf ${FILENAME} ${CUR_FILE} } # Switch namespace @@ -41,11 +37,10 @@ kn() { NAMESPACE=${1:-} if [ -z $NAMESPACE ]; then - NAMESPACE=$(kubectl get namespace --no-headers | awk '{print $1}' | sort -r | fzf) + NAMESPACE=$(kubectl get namespace | awk '{ print $1 }' | tail +2 | sort -r | fzf) fi - - CONTEXT=$(kubectl config get-contexts | grep "*" | awk '{print $2}') - _create_file_and_switch ${CONTEXT} ${NAMESPACE} + + kubectl config set-context --current --namespace=$NAMESPACE } if [ -f ${CUR_FILE} ]; then