Skip to content

Instantly share code, notes, and snippets.

@checco
Forked from yuanying/kubectl-run-with-pvc.sh
Created July 15, 2024 07:08
Show Gist options
  • Save checco/c0e2b1ee29e2deba7b7c09c86a68ac04 to your computer and use it in GitHub Desktop.
Save checco/c0e2b1ee29e2deba7b7c09c86a68ac04 to your computer and use it in GitHub Desktop.
kubectl run with PVCs
#!/bin/bash
IMAGE="gcr.io/google-containers/ubuntu-slim:0.14"
COMMAND="/bin/bash"
SUFFIX=$(date +%s | shasum | base64 | fold -w 10 | head -1 | tr '[:upper:]' '[:lower:]')
usage_exit() {
echo "Usage: $0 [-c command] [-i image] PVC ..." 1>&2
exit 1
}
while getopts i:h OPT
do
case $OPT in
i) IMAGE=$OPTARG
;;
c) COMMAND=$OPTARG
;;
h) usage_exit
;;
\?) usage_exit
;;
esac
done
shift $(($OPTIND - 1))
VOL_MOUNTS=""
VOLS=""
COMMA=""
for i in $@
do
VOL_MOUNTS="${VOL_MOUNTS}${COMMA}{\"name\": \"${i}\",\"mountPath\": \"/pvcs/${i}\"}"
VOLS="${VOLS}${COMMA}{\"name\": \"${i}\",\"persistentVolumeClaim\": {\"claimName\": \"${i}\"}}"
COMMA=","
done
kubectl run -it --rm --restart=Never --image=${IMAGE} pvc-mounter-${SUFFIX} --overrides "
{
\"spec\": {
\"hostNetwork\": true,
\"containers\":[
{
\"args\": [\"${COMMAND}\"],
\"stdin\": true,
\"tty\": true,
\"name\": \"pvc\",
\"image\": \"${IMAGE}\",
\"volumeMounts\": [
${VOL_MOUNTS}
]
}
],
\"volumes\": [
${VOLS}
]
}
}
" -- ${COMMAND}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment