2021-11-29 08:07:17 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2022-03-11 18:38:32 +00:00
|
|
|
source .utils
|
2021-11-29 08:07:17 +00:00
|
|
|
|
2022-03-11 18:38:32 +00:00
|
|
|
NAMESPACE="$(kubectl config view --minify --output 'jsonpath={..namespace}' &>/dev/null)"
|
|
|
|
set -e
|
|
|
|
NAMESPACE=${NAMESPACE:-default}
|
2021-11-29 08:07:17 +00:00
|
|
|
POD=""
|
|
|
|
CMD="sh"
|
|
|
|
|
|
|
|
print_usage() {
|
2022-03-11 18:38:32 +00:00
|
|
|
blue "kube-exec - execute commands within a pod"
|
2021-11-29 08:07:17 +00:00
|
|
|
echo " "
|
2022-03-11 18:38:32 +00:00
|
|
|
underline "Usage:"
|
2021-11-29 08:07:17 +00:00
|
|
|
echo "kube-exec [options]"
|
|
|
|
echo " "
|
2022-03-11 18:38:32 +00:00
|
|
|
underline "Options:"
|
2021-11-29 08:07:17 +00:00
|
|
|
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
|