Last active
May 14, 2018 19:12
-
-
Save janosroden/1cc18b54bbe9692560fa5f77dd74fd59 to your computer and use it in GitHub Desktop.
The script polls the list of persistent volumes and if finds an unused one then recreates it with the same size but different name on nfs.
This file contains 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
#!/usr/bin/bash | |
export PATH=/usr/local/bin:/usr/bin | |
[[ -f /var/run/pv.lock ]] && exit 0 | |
touch /var/run/pv.lock | |
createPv () | |
{ | |
local pvRoot="$1"; | |
local size="$2"; | |
local number=$(cat /etc/pvseq || echo 0); | |
let number++; | |
local pvName=pv$(printf "%06d" $number); | |
local pvDir=$pvRoot/$pvName; | |
mkdir -p $pvDir; | |
chown nfsnobody:nfsnobody $pvDir; | |
chmod 775 $pvDir; | |
echo "$pvDir *(rw,root_squash,no_wdelay)" >> /etc/exports; | |
/usr/sbin/exportfs -ra | |
oc create -f - > /dev/null <<EOF | |
apiVersion: v1 | |
kind: PersistentVolume | |
metadata: | |
name: $pvName | |
spec: | |
capacity: | |
storage: $size | |
accessModes: | |
- ReadOnlyMany | |
- ReadWriteOnce | |
- ReadWriteMany | |
nfs: | |
path: $pvDir | |
server: localhost | |
EOF | |
echo $number > /etc/pvseq | |
} | |
deletePv () | |
{ | |
local pvRoot="$1"; | |
local pvName="$2"; | |
local pvDir=$pvRoot/$pvName; | |
oc delete pv $pvName > /dev/null; | |
rm -rf $pvDir; | |
sed -i "/^${pvDir////\\/} /d" /etc/exports | |
} | |
oc get pv | grep '^pv' | grep -e Failed -e Released | awk '{print $1 " " $2;}' | while read pvName pvSize; do | |
deletePv /mnt/pvs $pvName | |
createPv /mnt/pvs $pvSize | |
done | |
rm /var/run/pv.lock |
This file contains 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
pvRoot=/mnt/pvs | |
for size in \ | |
$(yes 100Mi | head -10) \ | |
$(yes 500Mi | head -6) \ | |
$(yes 1Gi | head -20) \ | |
$(yes 5Gi | head -11) \ | |
$(yes 10Gi | head -2) | |
do | |
# createPv from pv_recycle.sh | |
createPv $pvRoot $size | |
done |
This file contains 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
*/1 * * * * /usr/bin/pv_recycle.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment