Repository navigation
Kubernetes
kubectl, kubecolor (k) and helm are zsh functions (kube-guard.zsh) that ask a Go binary (.config/kube-guard) before running the real command. On a production context, a command that changes the cluster asks first:
? kube-guard: prod-eu is production. kubectl delete pod x? [y/N]
-
Asks for: apply, create, delete, edit, patch, replace, scale, set, label, annotate, taint, drain, cordon, uncordon, autoscale, expose, run, exec, debug, cp,
rollout restart|undo|pause|resume;helm install|upgrade|uninstall|rollback. -
Passes: reads (get, describe, logs, diff,
rollout status,helm template,helm diff), and a kubectl plugin's own arguments (kubectl who-can delete podsis a read). -
The context is the one the command runs on:
--context/--kube-contextanywhere on the line, else the current one of--kubeconfigwhen given, else ofKUBECONFIG/~/.kube/config. A flag's value before the subcommand (--as admin delete,-v 6 delete) is not mistaken for it. - The answer is read from the terminal, so
cat x.yaml | kubectl apply -f -still gets its manifest. -
Skip it once:
KUBE_GUARD=0 kubectl apply -f x.yaml. - Scripts call the real binaries and are not guarded; only your interactive shell is.
- Until the binary is built (
dotfiles-sync), everykubectl/helmcommand asks.
A context is production when its name matches the production regex, in any case. In order:
-
KUBE_PROD_REGEX, in a shell (e.g. set in~/.zshrc.local) - the first line of
~/.config/kube-guard/prod-regexon this machine (never committed, backed up bylocal-backup) prod|prd
Use the file for a machine's own naming: it is the only one SketchyBar and launchd see, as they never read ~/.zshrc.local.
echo 'prod|prd|live' > ~/.config/kube-guard/prod-regexk9s and sofka open with --readonly on a production context; --write opens them writable. A context switched to inside them keeps the mode they started with. sofka also has guardrails of its own that block deletes on production contexts (.config/sofka/conf.d/00-k9s.yaml). References: k9s README, sofka README (plugins, Shift-digit views).
⌘K opens sofka in a tmux or Rex popup on the kube context of the pane it was pressed in, a ktx -s shell's own included: each interactive shell writes its KUBECONFIG to ~/.cache/kube-pane when it changes.
-
ktx(kubectx):ktx <ctx>switches,ktx -s <ctx>gives this shell a context of its own,ktx -rread-only -
kns(kubens): no argument picks with fzf,-the previous one - The SketchyBar kube item shows
context/namespace, red on production. - Tab completion covers the krew plugins (
kubectl neat,tree,view-secret,who-can). -
logclireads Loki's logs (deleted pods and finished jobs too), port-forwarding toLOKI_SERVICEwhenLOKI_ADDRis unset.