Files
ma/provision-volume-claim.sh

116 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# This creates a persistent volume and claim.
# Params:
# -p|--pv <pv-name> -c|--pvc <pvc-name> -s|--nfs-server <nfs-server> -n|--nfs-path <nfs-path>
# pv-name - the name used for the persistent volume
# pvc-name - the name used for the claim
# nfs-server - IP or hostname of the NFS server
# nfs-path - Path on the NFS server
PV_NAME=
PVC_NAME=
NFS_SERVER=
NFS_PATH=
SIZE=
usage()
{
echo "Usage: provision-volume-claim.sh < -p | --pv-name name > < -c | --pvc-name name >
< -n | --nfs-server server >
< -m | --nfs-path path >
< -s | --size size >"
exit 2
}
PARSED_ARGUMENTS=$(getopt -a -n provision-volume-claim -o p:c:n:m:s: --long pv-name:,pvc-name:,nfs-server:,nfs-path:,size: -- "$@")
VALID_ARGUMENTS=$?
if [ "$VALID_ARGUMENTS" != "0" ]; then
usage
fi
eval set -- "$PARSED_ARGUMENTS"
while :
do
case "$1" in
-p | --pv-name) PV_NAME="$2" ; shift 2 ;;
-c | --pvc-name) PVC_NAME="$2" ; shift 2 ;;
-n | --nfs-server) NFS_SERVER="$2" ; shift 2 ;;
-m | --nfs-path) NFS_PATH="$2" ; shift 2 ;;
-s | --size) SIZE="$2" ; shift 2 ;;
# -- means the end of the arguments; drop this, and break out of the while loop
--) shift; break ;;
*) echo "Unexpected option: $1 - this should not happen."
usage ;;
esac
done
if [ -z $PV_NAME ] || [ -z $PVC_NAME ] || [ -z $NFS_SERVER ] || [ -z $NFS_PATH ] || [ -z $SIZE ];
then
echo "Missing required option(s)"
usage
fi
PV_MANIFEST=$(mktemp /tmp/pvc.XXXXXXX) || exit 1
PVC_MANIFEST=$(mktemp /tmp/pv.XXXXXXX) || exit 1
# Create the PV manifest
cat > $PV_MANIFEST << EOM
apiVersion: v1
kind: PersistentVolume
metadata:
name: $PV_NAME
labels:
is_nf_pv: "yes"
spec:
capacity:
storage: $SIZE
volumeMode: Filesystem
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Recycle
storageClassName: slow
mountOptions:
- hard
- nfsvers=4.1
nfs:
path: $NFS_PATH
server: $NFS_SERVER
EOM
echo "Created PV Manifest $PV_MANIFEST:"
cat $PV_MANIFEST
# kubectl apply -f $PV_MANIFEST || exit 1
# Create the pvc manifest
cat > $PVC_MANIFEST << EOM
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: $PVC_NAME
spec:
accessModes:
- ReadWriteMany
volumeMode: Filesystem
resources:
requests:
storage: $SIZE
storageClassName: slow
selector:
matchLabels:
is_nf_pv: "yes"
EOM
echo "Created PVC Manifest: $PVC_MANIFEST"
cat $PVC_MANIFEST
# Generate cleanup script
cat > cleanup-volume-claim.sh << EOM
#!/bin/bash
kubectl delete pvc $PVC_NAME
kubectl delete pv $PV_NAME
EOM
chmod +x cleanup-volume-claim.sh
kubectl apply -f $PV_MANIFEST || exit 1
kubectl apply -f $PVC_MANIFEST || exit 1