Skip to content

Instantly share code, notes, and snippets.

@ialidzhikov
Created April 15, 2022 11:54
Show Gist options
  • Select an option

  • Save ialidzhikov/312f744da93599b27188ac6fef2c540d to your computer and use it in GitHub Desktop.

Select an option

Save ialidzhikov/312f744da93599b27188ac6fef2c540d to your computer and use it in GitHub Desktop.
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
# 1. Fetch all gcp Shoots with K8s <= 1.20
# 2. Check if the Shoot has PVs with label topology.kubernetes.io/zone
TMP_DIR=$(mktemp -d -t run-XXXXXXXXXX)
echo "TMP_DIR=$TMP_DIR"
SHOOTS=$(kubectl get shoot -A --field-selector=spec.cloudProfileName=gcp -o json | jq -r '.items[] | .metadata.namespace + "," + .metadata.name')
for SHOOT in $SHOOTS
do
sleep 1s
echo "> Checking $SHOOT"
TOKENS=(${SHOOT//,/ })
NAMESPACE=${TOKENS[0]}
NAME=${TOKENS[1]}
SHOOT_JSON=$(kubectl -n $NAMESPACE get shoot $NAME -o json)
K8S_VERSION=$(echo $SHOOT_JSON | jq -r '.spec.kubernetes.version')
echo $K8S_VERSION
VERSION_COMPARISON=0
vercomp $K8S_VERSION '1.21.0' || VERSION_COMPARISON=$?
if [ $VERSION_COMPARISON != 2 ]
then
echo "K8S version $K8S_VERSION is greater than 1.21.0, will skip..."
continue
fi
IS_HIBERNATED=$(echo $SHOOT_JSON | jq -r '.status.hibernated')
if [ $IS_HIBERNATED = 'true' ]
then
echo "The Shoot is hibernated, will skip..."
continue
fi
LAST_OP_STATE=$(echo $SHOOT_JSON | jq -r '.status.lastOperation.state')
if [ $LAST_OP_STATE != 'Succeeded' ]
then
echo "The Shoot is not in Succeed state, will skip..."
continue
fi
kubectl -n $NAMESPACE get secret $NAME.kubeconfig -o jsonpath='{.data.kubeconfig}' | base64 -d > $TMP_DIR/$NAME.yaml
echo "Checking for PVs..."
OUTPUT=$(kubectl --kubeconfig=$TMP_DIR/$NAME.yaml get pv -l topology.kubernetes.io/zone -o json | jq '.items | length')
echo "OUTPUT=$OUTPUT"
if [ "$OUTPUT" = 0 ]
then
echo "No affected PVs found!"
else
echo "Found $OUTPUT affected PVs!"
fi
done
rm -rf $TMP_DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment