dotfiles/home/.bin/kube-exec
Marcus Noble 381adefe87
Make things pretty
Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
2022-03-11 18:38:32 +00:00

66 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env bash
source .utils
NAMESPACE="$(kubectl config view --minify --output 'jsonpath={..namespace}' &>/dev/null)"
set -e
NAMESPACE=${NAMESPACE:-default}
POD=""
CMD="sh"
print_usage() {
blue "kube-exec - execute commands within a pod"
echo " "
underline "Usage:"
echo "kube-exec [options]"
echo " "
underline "Options:"
echo "-h, --help show this help text"
echo "-n, --namespace the namespace the pod is in"
echo "-p, --pod the name of the pod"
echo "-c, --command the command to run in the pod (default: sh)"
}
while test $# -gt 0; do
case "$1" in
-n|--namespace)
shift
NAMESPACE=$1
shift
;;
-p|--pod)
shift
POD=$1
shift
;;
-c|--command)
shift
CMD=$1
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
if [[ "$POD" == "" ]]; then
which fzf &>/dev/null || (
echo "If no pod provided, fzf is required to select pods"
echo ""
print_usage
exit 1
)
pod=($(kubectl get pods --all-namespaces -owide | fzf | awk '{print $1, $2}'))
POD=$pod[1]
NAMESPACE=$pod[0]
fi
echo kubectl exec -it --namespace $NAMESPACE $POD $CMD
kubectl exec -it --namespace $NAMESPACE $POD $CMD