Skip to content
thomaschaplinPublic

About

Per-directory Kubernetes context and namespace, from a small file your team commits.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

kdir logo: a folder with a ship's wheel

kdir

Per-directory Kubernetes context and namespace, from a small file your team commits.

cd into a project and kubectl points at the cluster and namespace in its .kubectx. cd out and everything is back to normal. Only the current shell changes.

# .kubectx: safe to commit, contains no credentials
server: https://ABCD1234.gr7.eu-west-1.eks.amazonaws.com
namespace: payments

Think .nvmrc for clusters, hooked into the shell like direnv.

Terminal demo: cd into a project, kdir allow switches the prompt from prod-eu to staging-eu:payments, cd out switches it back, and a project whose context is missing disables kubectl

Why use it

  • Committable. The file has no credentials, so the whole team can share it.
  • Per shell. Other terminals keep their own context.
  • Fail-closed. If you don't have the cluster the project asks for, kubectl refuses to run instead of quietly using whatever was active (maybe production).
  • Safe by default. A cloned repo's .kubectx does nothing until you kdir allow it. Your own kubeconfig is never written.

Quick start

  1. Install

    brew install thomaschaplin/tap/kdir                         # macOS and Linux
    go install github.com/thomaschaplin/kdir/cmd/kdir@latest    # or with Go

    Or grab a binary from the releases page.

  2. Hook your shell, then open a new one:

    eval "$(kdir hook zsh)"     # ~/.zshrc
    eval "$(kdir hook bash)"    # ~/.bashrc
    kdir hook fish | source     # ~/.config/fish/config.fish
  3. Set up a project using the context you're on now:

    cd ~/src/payments
    kdir init --server --namespace payments
    git add .kubectx

Teammates who clone the repo run kdir allow once.

The .kubectx file

Pick one way to find the context, and optionally a namespace:

context: staging-eu          # exact context name, or
# contextRegex: "^.*staging$"  # a regex, or
# server: https://...        # the API server URL (best for teams)

namespace: payments          # optional
protect: false               # optional: confirm before delete/apply/etc.

kdir uses the nearest .kubectx above the current directory. If the matcher finds no context, or more than one, kubectl is disabled in that directory until it's fixed.

Commands

Command What it does
kdir init Create a .kubectx from your current context and allow it
kdir allow / kdir deny Trust or stop trusting a .kubectx
kdir status Show what's active here and why
kdir status --short context:namespace, for your prompt
kdir exec -- <cmd> Run one command with this directory's context (scripts, CI)
kdir hook <shell> Print the shell hook
kdir gc Clean up files left by exited shells

Prompt tools like starship and kube-ps1 pick up the right context with no extra setup.

Learn more

Supports macOS and Linux with zsh, bash and fish.

License

MIT

About

Per-directory Kubernetes context and namespace, from a small file your team commits.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages