This project makes it easy to switch between different k8s environments depending on the current working dir.
When you enter a directory with this enabled, it will set $KUBECONFIG, and create a directory for each namespace found on the cluster.
Each directory will contain another direnv and a kubectl-wrapper, that points to the specific namespace. When entering one of these directories, that kubectl will end up first in $PATH.
These kubectl's are created each time the root environment is loaded. Technically the same can be achieved just by using aliases containing an environment variable, but I found that broke tab completion.
Furthermore, $KUBECONFIG_NAMESPACE will also be set inside these directories, to make it easy to include the namespace name in the shell prompt.
This script will use the basename of the current working directory as a name for a kube config. That is, given a directory /home/adam/k8s/prod, it will export KUBECONFIG=/home/atu/.kube/prod.
- install direnv and make your shell load it (see https://direnv.net/)
- create a kubeconfig named
$namein~/.kube/ - create a directory named
$name - create a symlink from
create-environment.shto$name/.envrc - cd into the directory, and notice that direnv complains about the env not being allowed yet
- READ THROUGH THE ENTIRE
.envrc(orcreate-environment.sh) SCRIPT, AND CONFIRM THAT IT DOESN'T DO ANYTHING NASTY! - if happy with the above, run
direnv allow .
Repeat the above steps for each k8s environment you want to access.
- source
zsh-integrationfrom your theme - add
${k8s_info}somewhere in the PROMPT-definition
This will add something like k8s:cluster/namespace to the prompt. Colors can be controlled by writing the desired color name (e.g. "red") to ~/.config/direnv-k8s/$name/color, where $name has the same meaning as in the previous sections.
Done.
My theme is based on the "ys" theme, with the k8s info added just before the git part of the prompt.


