Last active
June 2, 2023 21:30
-
-
Save rayjanoka/e39c1c9152be1fa605eff88ff6a04459 to your computer and use it in GitHub Desktop.
tail/pipe kubectl logs to a slack channel with slackcat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
ARCH=amd64 | |
KUBECTL_VERSION=1.27.2 | |
TETRAGON_VERSION=0.9.0 | |
SLACKCAT_VERSION=1.7.3 | |
SLACK_CHANNEL=test | |
apk -q add curl grep sed coreutils | |
curl -sLO https://dl.k8s.io/release/v$${KUBECTL_VERSION}/bin/linux/$${ARCH}/kubectl | |
chmod +x ./kubectl | |
curl -sLo slackcat https://github.com/bcicen/slackcat/releases/download/$${SLACKCAT_VERSION}/slackcat-$${SLACKCAT_VERSION}-linux-$${ARCH} | |
chmod +x ./slackcat | |
echo "$SLACK_TOKEN" > ~/.slackcat | |
LOOP_MINS=1 | |
LAST_SCAN=$(date --iso-8601=seconds --date "-$LOOP_MINS min") | |
MAX_LINES_PER_LOOP=100 | |
MAX_LOG_REQUESTS=200 | |
echo "starting tetragon log to slack..." | |
while true; do | |
WAIT_UNTIL=$(date --iso-8601=seconds --date="$LAST_SCAN +$LOOP_MINS min") | |
if [ "$(date '+%s')" -gt "$(date --date "$WAIT_UNTIL" '+%s')" ]; then | |
echo "requesting logs since $WAIT_UNTIL..." | |
START_SCAN=$(date --iso-8601=seconds) | |
./kubectl logs --since-time "$LAST_SCAN" \ | |
--namespace ${var.namespace} \ | |
--selector app.kubernetes.io/name=tetragon \ | |
--container export-stdout \ | |
--max-log-requests $MAX_LOG_REQUESTS \ | |
--tail $MAX_LINES_PER_LOOP | \ | |
./slackcat --stream --channel $SLACK_CHANNEL | |
LAST_SCAN="$START_SCAN" | |
fi | |
sleep 5 | |
done |
if you are going to put other modifications in the chain make sure they are set to not buffer very much.
sed --unbuffered -r 's|^(\w+) (.*)$|*\1* `\2`|' | \
grep --line-buffered -v -E "(stuff|we|dont|want|to|see)" | \
these scopes are required for the slackcat bot app, their auto-setup is broken atm.
["chat:write", "im:read", "mpim:read", "channels:read", "groups:read", "files:write", "users:read"]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This issue with kubelet forced me to write this as a loop. kubernetes/kubernetes#115701