Skip to content

Instantly share code, notes, and snippets.

@gildean
Last active March 8, 2018 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gildean/34fe2d264a631b972cc686f7fa5142f3 to your computer and use it in GitHub Desktop.
Save gildean/34fe2d264a631b972cc686f7fa5142f3 to your computer and use it in GitHub Desktop.
Stream openshift events to the terminal (probably works with plain kubernetes as well)
#! /bin/bash
if ! oc whoami &> /dev/null; then echo "login to oc first" && exit 1; fi
set -euo pipefail
TOKEN=$(oc whoami -t)
API_SERVER_ADDRESS="https://master/api/v1/watch/events"
# setting LANG gives correct timestamps
LANG=fi_FI.UTF-8
# print timestamp in the middle of the row every 60s
while sleep 60; do
columns=$(tput cols)
timestamp="---> $(date +'%x %X') <---"
printf "%*s\n" $(((${#timestamp}+$columns)/2)) "$timestamp"
done &
# kill children at exit
trap 'kill $(jobs -p)' EXIT
while IFS=$'\t' read -r eventtype namespace service kind reason eventtime message; do
namespace="${namespace,,}"
: ${namespace:-infra}
eventtime=$(date -d $eventtime +'%x %X')
if [[ "$kind" == "Pod" ]]; then
kindcolor="\e[38;5;152m"
elif [[ "$kind" == "DeploymentConfig" ]]; then
kindcolor="\e[38;5;92m"
elif [[ "$kind" == "Node" ]]; then
kindcolor="\e[38;5;171m"
else
kindcolor="\e[38;5;222m"
fi
if [[ "$eventtype" == "Normal" ]]; then
eventcolor="\e[38;5;154m"
else
eventcolor="\e[38;5;198m"
fi
if [[ $reason == "Unhealthy" ]]; then
reasoncolor="\e[5m\e[38;5;172m"
else
reasoncolor="\e[38;5;121m"
fi
printf "\e[90m%-19s\e[0m - ${eventcolor}%-7s\e[0m | \e[38;5;121m%-15s\e[0m \e[97m%-35s\e[0m ${kindcolor}%-21s\e[0m ${reasoncolor}%-17s\e[0m \e[38;5;245m%-30s\e[0m\n" "${eventtime}" "${eventtype}" "${namespace}" "${service}" "${kind}" "${reason}" "${message}"
done < <(curl -s -k \
-H "Authorization: Bearer $TOKEN" \
-H 'Accept: application/json' "$API_SERVER_ADDRESS" \
| jq --unbuffered -r \
'[.object.type, .object.involvedObject.namespace // " ", .object.involvedObject.name, .object.involvedObject.kind, .object.reason, .object.metadata.creationTimestamp, (.object.message // "" | gsub("\\n"; " "))] | @tsv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment