Table of Contents
The showcase demonstrates the multiple sessions per shell feature, selecting items and fuzzy searching for them. The same features apply for handling namespace as well
This BLAZINGLY FAST plugin written in Rust makes it possible to have one context per shell active.
Why isolated context? Typically when you switch context in kubectl (with ex. kubectx), the change happens on all terminal instances. That is because the change is saved in your $HOME/.kube/config file and is read on every interaction with kubectl.
This will lead to the inevitable scenario of working on a local cluster, and needing to do something quickly in production. You open another terminal, switch context, do your work and then go right back to your old terminal. The issue is that the prompt has not visually refreshed to the actual context. Often the following command you type will not be dangerous, and you will notice that you did it in production, but sometimes the damage is severe💥.
We will use the config merge capability of kubectl to override the current-context setting. By creating a file with the correct context and prepend it to the KUBECONFIG environment variable
The program will output the SESSION_CONTEXT and the alias created in Installation will do the prepending
export KUBECONFIG=$SESSION_CONTEXT:$KUBECONFIG
Tool: custom script
Command | kubesess | kubectx |
---|---|---|
20 runs with no ctx switch and no kubectl calls | .024931342 | 1.744966963 |
20 runs with ctx switch and no kubectl calls | .049247181 | 3.775905777 |
20 runs with ctx switch and calling kubectl get nodes | 11.167763585 | 15.265837926 |
I am using the input argument variant for both tools, using fzf or tab completion is harder to do.
Tool: hyperfine
Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
---|---|---|---|---|
kubesess -v docker-desktop context |
1.3 ± 0.2 | 1.0 | 2.2 | 1.00 |
kubectx docker-desktop |
91.8 ± 3.3 | 85.1 | 100.7 | 71.23 ± 13.64 |
Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
---|---|---|---|---|
kubesess -v monitoring namespace |
0.8 ± 0.1 | 0.7 | 0.9 | 1.00 |
kubens monitoring |
215.3 ± 27.0 | 199.5 | 288.9 | 270.22 ± 38.34 |
* Disclaimer * kubectx and kubens are a wonderful tools, been using it allot but my workflow is different and thus this tool was created. Probably most of the speed gains are because I am bypassing kubectl and just editing config files.
- No dependencies
Download and extract the binary.
KUBESESS_VERSION=2.0.2 && \
KUBESESS_OS=x86_64-unknown-linux-gnu && \
wget "https://github.com/Ramilito/kubesess/releases/download/${KUBESESS_VERSION}/kubesess_${KUBESESS_VERSION}_${KUBESESS_OS}.tar.gz" && \
mkdir -p $HOME/.kube/kubesess && tar zxpf kubesess_${KUBESESS_VERSION}_${KUBESESS_OS}.tar.gz -C $HOME/.kube/kubesess && \
sudo mv ~/.kube/kubesess/target/${KUBESESS_OS}/release/kubesess /usr/local/bin/kubesess
A script wrapper called kubesess.sh is provided for easier use, source the script wrapper in your .bashrc, .zshrc.
source ~/.kube/kubesess/scripts/sh/kubesess.sh
For zsh users, source the completion script to your .zsh/.zshrc file
source ~/.kube/kubesess/scripts/sh/completion.sh
For fish users, copy functions and completion scripts in your fish config.
rsync -a ~/.kube/kubesess/scripts/fish/ ~/.config/fish/
brew install kubesess
Add the following to your .zshrc:
source ${HOMEBREW_PREFIX}/share/zsh/site-functions/kubesess.sh
source ${HOMEBREW_PREFIX}/opt/kubesess/etc/bash_completion.d/completion.sh
Or if you use fish:
cp /$HOMEBREW_PREFIX/share/fish/vendor_functions.d/{kcd.fish,kc.fish,knd.fish,kn.fish} ~/.config/fish/functions/
See the available commands by running kubesess -h, output from the program needs to be added to $KUBECONFIG env variable.
kc #kube_context: Sets session context
kcd #kube_context_default: Sets context across all shells
kn #kube_namespace: Sets namespace
knd #kube_namespace_default: Sets namespace across all shells
We have two ways of handling multiple config files, the first one is to use kubectl recommended way of adding multiconfig files found here. Example:
export KUBECONFIG=$HOME/.kube/config:$HOME/.kube/config-demo:$HOME/.kube/config-demo-2
Note
*The order is important: the first file will be the master config!
The second way is to let Kubesess handle it by adding one or more config files under the $HOME/.kube folder and it will be automatically merged.
Warning
Currently there is a bug that requires us to have this env variable: export KUBECONFIG=$(find ~/.kube/ -maxdepth 1 -type f -printf ":%p" | sed 's/^://')
prompt_context() {
KUBE_CTX=$(kubesess -c context)
KUBE_NS=$(kubesess -c namespace)
if [[ $KUBE_CTX == *"dev"* ]]; then
echo "❗%{$fg[yellow]%}|$KUBE_CTX%{$reset_color%}:%F{6}$KUBE_NS%f"
elif [[ $KUBE_CTX == *"prod"* ]]; then
echo "⛔%{$fg[red]%}|$KUBE_CTX%{$reset_color%}:%F{6}$KUBE_NS%f"
elif [[ $KUBE_CTX == *"staging"* ]]; then
echo "⛔%{$fg[red]%}|$KUBE_CTX%{$reset_color%}:%F{6}$KUBE_NS%f"
else
echo "✅️%{$fg[green]%}|$KUBE_CTX%{$reset_color%}:%F{6}$KUBE_NS%f"
fi
}
RPROMPT='$(prompt_context)'
- List all contexts
- Present it with fzf
- Write selection to file
- Output link to file
- Add alias to handle output
- Cleanup after use
- clean prepended env variable
- output files to $HOME/.cache/kubesess
- Handle different namespaces per context
- Use rust tui instead of fzf
- Add option to make changes stick (default-context)
- Add option to make changes stick (default-namespace)
- Add tests
- Add tab completion - clap-rs/clap#1232
- Add to brew
- Add support for multiple .kube/config files
- Add support for multiple namespace per session
- Add error handling
A hard reset will fix most issues, to do that just remove the ~/.kube/kubesess/cache
folder.
*The downside is that your last visited namespace per context will be lost.
There are only two places that can go wrong, either the $KUBECONFIG
env is
not set correctly or the generated file is corrupt.
This is how the $KUBECONFIG
should look like (replace ${USER}
with your user name):
/home/${USER}/.kube/kubesess/cache/docker-desktop:/home/${USER}/.kube/config
This is how the generated file should look like:
clusters:
- name: docker-desktop
cluster:
server: https://kubernetes.docker.internal:6443
certificate-authority-data: REDACTED
users:
- name: docker-desktop
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
contexts:
- name: docker-desktop
context:
cluster: docker-desktop
user: docker-desktop
namespace: default
current-context: docker-desktop