dotfiles/home/.bin/kube-logs

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=""
ARGS=""
print_usage() {
blue "kube-logs - tail logs from a pod"
echo " "
underline "Usage:"
echo "kube-logs [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 to get logs for"
echo "-a, --args additional arguments to pass to kubectl logs command"
}
while test $# -gt 0; do
case "$1" in
-n|--namespace)
shift
NAMESPACE=$1
shift
;;
-p|--pod)
shift
POD=$1
shift
;;
-a|--args)
shift
ARGS=$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 -o wide | _fzf | awk '{print $1, $2}'))
POD=${pod[1]}
NAMESPACE=${pod[0]}
fi
echo kubectl logs -f $ARGS --namespace $NAMESPACE $POD
kubectl logs -f $ARGS --namespace $NAMESPACE $POD