Compare commits

...

2 commits

Author SHA1 Message Date
fleaz
7357a8fe76
Fill README with content 2025-05-31 22:07:02 +02:00
fleaz
f52e7625c3
Restructuring and some bugfixing 2025-05-31 21:52:45 +02:00
2 changed files with 63 additions and 13 deletions

View file

@ -1 +1,46 @@
# 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
```

31
ktx Normal file → Executable file
View file

@ -2,6 +2,16 @@
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]
@ -11,24 +21,18 @@ ktx() {
NAMESPACE=${2:-}
if [ -z $CONTEXT ]; then
SELECTION=$(kubectl config get-contexts | awk '{print $2"|"$4}' | tail +2 | sort -r | fzf)
# 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)
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
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}
_create_file_and_switch ${CONTEXT} ${NAMESPACE}
}
# Switch namespace
@ -37,10 +41,11 @@ kn() {
NAMESPACE=${1:-}
if [ -z $NAMESPACE ]; then
NAMESPACE=$(kubectl get namespace | awk '{ print $1 }' | tail +2 | sort -r | fzf)
NAMESPACE=$(kubectl get namespace --no-headers | awk '{print $1}' | sort -r | fzf)
fi
kubectl config set-context --current --namespace=$NAMESPACE
CONTEXT=$(kubectl config get-contexts | grep "*" | awk '{print $2}')
_create_file_and_switch ${CONTEXT} ${NAMESPACE}
}
if [ -f ${CUR_FILE} ]; then