Skip to content

Instantly share code, notes, and snippets.

@drakonstein
Last active May 19, 2023 02:59
Show Gist options
  • Save drakonstein/2d93b3c1ea37b2713dbc97b2ef7ae20c to your computer and use it in GitHub Desktop.
Save drakonstein/2d93b3c1ea37b2713dbc97b2ef7ae20c to your computer and use it in GitHub Desktop.
#!/bin/bash
API="https://registry.hub.docker.com/v2/repositories"
HASH_DIR="/home/david/k8s/scripts/hash"
# image tag namespace { space deliminated list of labels }
IMAGES="wordpress latest friends app=george
beamdog/nwserver latest games app=nwn
itzg/minecraft-server latest games app=minecraft instance=mc-persistent"
do_sleep=false
while read image tag namespace labels; do
newhash=$(wget -qO- $API/$image/tags/$tag | jq -r '.images[]|select(.architecture == "amd64")|.digest')
oldhash=$(cat $HASH_DIR/$image/$tag.dat 2>&1)
if [[ $newhash != $oldhash ]]; then
$do_sleep && {
echo "Waiting 5 minutes between restarting different pods..."
sleep 300
}
selector=
for label in $labels; do
selector="$selector --selector=$label"
done
pod=$(kubectl -n $namespace get pods $selector -o json | jq -r '.items | .[].metadata.name' | head -n1)
echo Restarting $namespace/$pod
kubectl -n $namespace delete pod $pod
mkdir -p $HASH_DIR/$image
echo $newhash > $HASH_DIR/$image/$tag.dat
do_sleep=true
fi
done <<< "$IMAGES"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment