Skip to content

Instantly share code, notes, and snippets.

@ottomata
Last active November 24, 2020 15:55
Show Gist options
  • Save ottomata/f089acd146ac87c0703d88ec85e99a5c to your computer and use it in GitHub Desktop.
Save ottomata/f089acd146ac87c0703d88ec85e99a5c to your computer and use it in GitHub Desktop.
#!/bin/bash
options=$(getopt -o s:c:p:b --long service:,cluster:,since:,pod:,bunyan -- "$@")
[ $? -eq 0 ] || {
echo "Incorrect options provided"
exit 1
}
eval set -- "$options"
while true; do
case "$1" in
-s|--service)
shift
SERVICE=$1
;;
-c|--cluster)
shift
CLUSTER=$1
;;
--since)
shift
SINCE=$1
;;
-p|--pod)
shift
POD=$1
;;
-b|--bunyan)
LOG_PARSE_CMD="nodejs /home/otto/node_modules/bunyan/bin/bunyan"
;;
--)
shift
break
;;
esac
shift
done
if [ -z "${SERVICE}" -a $(dirname $(pwd)) == "/srv/deployment-charts/helmfile.d/services" ]; then
SERVICE=$(basename $(pwd))
fi
if [ -z "${SERVICE}" ]; then
echo 'Must provide --service or be in a helmfile.d/services directory'
exit 1
fi
if [ -z "${CLUSTER}" ]; then
echo "Must provide a kubernetes --cluster"
exit 1
fi
if [ "${CLUSTER}" != "staging" -a "${CLUSTER}" != "eqiad" -a "${CLUSTER}" != "codfw" ]; then
echo "--cluster must be one of 'staging', 'codfw', or 'eqiad'"
exit 1
fi
if [ -z "${SINCE}" ]; then
SINCE="2h"
fi
if [ -z "${LOG_PARSE_CMD}" ]; then
LOG_PARSE_CMD=cat
fi
set -e
source /etc/profile.d/kube-env.sh
kube_env $SERVICE $CLUSTER
if [ -n "${POD}" ]; then
echo "Tailing logs for pod $POD in $SERVICE $CLUSTER"
set -x
kubectl logs -c $TILLER_NAMESPACE -f --since=${SINCE} $POD | $LOG_PARSE_CMD
else
echo "Tailing logs for all pods in $SERVICE $CLUSTER"
set -x
for pod in $(kubectl get pods -o wide | grep $SERVICE | awk '{print $1}'); do
kubectl logs -c $TILLER_NAMESPACE -f --since=${SINCE} $pod &
done | $LOG_PARSE_CMD
fi
set +x
set +e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment