From 1765917dba966e362042c0a7754dc5e8b27d38b6 Mon Sep 17 00:00:00 2001
From: Daniel Lundin <daniel@arity.se>
Date: Fri, 21 Aug 2020 09:09:08 +0200
Subject: [PATCH] Add kubectl wrapper for convenience and safety

---
 .zshrc              | 1 +
 bin/kubectl-wrapper | 9 +++++++++
 2 files changed, 10 insertions(+)
 create mode 100755 bin/kubectl-wrapper

diff --git a/.zshrc b/.zshrc
index 5cd845d..83d8f92 100644
--- a/.zshrc
+++ b/.zshrc
@@ -259,6 +259,7 @@ alias ag='ag --pager less'
 alias cdiff='colordiff -u'
 alias dotgit='git --work-tree $HOME --git-dir $HOME/.dot_git'
 alias hs='history -a; history -n'
+alias kubectl='kubectl-wrapper'
 alias lower="tr '[:upper:]' '[:lower:]'"
 alias pstree="pstree -Auh | less"
 alias tail='tail -n $LINES'
diff --git a/bin/kubectl-wrapper b/bin/kubectl-wrapper
new file mode 100755
index 0000000..7e3a091
--- /dev/null
+++ b/bin/kubectl-wrapper
@@ -0,0 +1,9 @@
+#!/bin/bash
+cfg=.kubeconfig
+
+path=$(pwd)
+while [[ "$path" != "" && ! -e "$path/$cfg" ]]; do
+  path=${path%/*}
+done
+
+exec env KUBECONFIG="$path/$cfg:$KUBECONFIG:$HOME/.kube/config" kubectl "$@"