apiVersion: v1 kind: ConfigMap metadata: name: fluent-bit-sidecar-wrapper namespace: sparkns labels: app.kubernetes.io/name: fluent-bit data: subprocess.sh: | MOUNT_PATH="/var/log/fluentd" FILE_TO_WATCH="$MOUNT_PATH/main-container-terminated" INITIAL_HEARTBEAT_TIMEOUT_THRESHOLD=120 HEARTBEAT_TIMEOUT_THRESHOLD=30 SLEEP_DURATION=5 WAIT_BEFORE_TERMINATION=15 echo "Waiting for file $FILE_TO_WATCH to appear..." start_wait=$(date +%s) echo "Start wait: $start_wait" #echo "Found file $FILE_TO_WATCH; watching for heartbeats..." # Waiting for the first heartbeat sent by Spark main container NOTFOUND_COUNT=0 while ! [[ -f "$FILE_TO_WATCH" ]]; do elapsed_wait=$(expr $(date +%s) - $start_wait) echo "Elapsed Wait: $elapsed_wait" if [ "$elapsed_wait" -gt "$INITIAL_HEARTBEAT_TIMEOUT_THRESHOLD" ]; then echo "File $FILE_TO_WATCH not found after $INITIAL_HEARTBEAT_TIMEOUT_THRESHOLD seconds; aborting" terminate_main_process exit 1 fi echo "Not found count: $NOTFOUND_COUNT" echo "Waiting..." sleep $SLEEP_DURATION; NOTFOUND_COUNT=$((NOTFOUND_COUNT+1)) done; function terminate_main_process() { echo "TERMINATED PROCESS" fpid=`pidof fluent-bit` echo "Fluent-Bit pid: $fpid" echo "Killing process after sleeping for $WAIT_BEFORE_TERMINATION seconds" ps -ef | grep fluent sleep $WAIT_BEFORE_TERMINATION echo "Killing process $fpid" kill $fpid } # Waiting for the first heartbeat sent by Spark main container echo "Waiting for file $FILE_TO_WATCH to appear..." start_wait=$(date +%s) #echo "Found file $FILE_TO_WATCH; watching for heartbeats..." FOUND_COUNT=0 while [[ -f "$FILE_TO_WATCH" ]]; do LAST_HEARTBEAT=$(stat -c %Y $FILE_TO_WATCH) ELAPSED_TIME_SINCE_AFTER_HEARTBEAT=$(expr $(date +%s) - $LAST_HEARTBEAT) echo "Last heartbeat: $LAST_HEARTBEAT" echo "Elapsed Time since after heartbeat: $ELAPSED_TIME_SINCE_AFTER_HEARTBEAT" if [ "$ELAPSED_TIME_SINCE_AFTER_HEARTBEAT" -gt "$HEARTBEAT_TIMEOUT_THRESHOLD" ]; then echo "Last heartbeat to file $FILE_TO_WATCH was more than $HEARTBEAT_TIMEOUT_THRESHOLD seconds ago at $LAST_HEARTBEAT; terminating" terminate_main_process exit 0 fi echo "Found count: $FOUND_COUNT" echo "list files:" ls -l $FILE_TO_WATCH sleep $SLEEP_DURATION; FOUND_COUNT=$((FOUND_COUNT+1)) done; echo "Outside of loop, main-container-terminated file no longer exists" ls -l $FILE_TO_WATCH # the file will be deleted once the fluentd container is terminated echo "The file $FILE_TO_WATCH doesn't exist anymore;" terminate_main_process exit 0 wrapper.sh: | echo -n "AWS for Fluent Bit Container Image Version " cat /AWS_FOR_FLUENT_BIT_VERSION /fluent-bit/bin/fluent-bit -e /fluent-bit/firehose.so -e /fluent-bit/cloudwatch.so -e /fluent-bit/kinesis.so -c /fluent-bit/etc/fluent-bit.conf