Skip to content

Instantly share code, notes, and snippets.

@Stono
Created June 25, 2019 16:14
Show Gist options
  • Save Stono/7bf43a94ee9a132ecbea869f60ebb28d to your computer and use it in GitHub Desktop.
Save Stono/7bf43a94ee9a132ecbea869f60ebb28d to your computer and use it in GitHub Desktop.
A wrapper entrypoint for istio-proxy, which makes it slightly more graceful shutting down and also adds support for calling envoys quitquitquit endpoint
#!/bin/bash
echo "Starting custom autotrader pilot-agent wrapper..."
echo "Pilot-agent args: $@"
/usr/local/bin/pilot-agent "$@" &
AGENT_PID=$!
echo "Pilot agent started with pid: $AGENT_PID"
log() {
echo "[pilot-agent-agent] $*"
}
cleanup()
{
log "Stopping pilot-agent-agent..."
log "Waiting up to 20s for existing network connections to close..."
n=0
until [ $n -ge 20 ]; do
remaining="$(netstat -plunt | grep tcp | grep -v envoy | grep -v pilot | wc -l | xargs)"
if [ "$remaining" -eq 0 ]; then
log "All network connections have closed."
break
fi
if [ "$(($n%5))" -eq 0 ]; then
log "Still waiting for $remaining connections to close"
fi
sleep 1
n=$[$n+1]
done
log "Stopping pilot-agent..."
kill -s TERM $AGENT_PID
exit 0
}
trap cleanup SIGINT SIGTERM
n=0
until [ $n -ge 5 ]; do
if ! pidof envoy &>/dev/null; then
log "envoy is not running!"
n=$[$n+1]
else
n=0
envoy_pid=$(pidof -s envoy)
log "waiting for envoy (pid: $envoy_pid) to exit..."
tail --pid=$envoy_pid -f /dev/null
log "envoy exited, waiting up to 5 seconds for a new envoy to spawn before killing pilot-agent..."
fi
sleep 1
done
log "envoy not detected after 5 seconds, terminating pilot-agent"
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment