Skip to content

Instantly share code, notes, and snippets.

@basilfx
Forked from yuanying/kubectl-run-with-pvc.sh
Last active August 10, 2021 07:05
Show Gist options
  • Save basilfx/cc542dc277f886e54c02aaaddeea8386 to your computer and use it in GitHub Desktop.
Save basilfx/cc542dc277f886e54c02aaaddeea8386 to your computer and use it in GitHub Desktop.
kubectl run with PVCs
#!/bin/bash
# Modified from https://gist.github.com/yuanying/3aa7d59dcce65470804ab43def646ab6.
IMAGE="ubuntu:20.04"
COMMAND="/bin/bash"
SUFFIX=$(date +%s | shasum | base64 | fold -w 10 | head -1 | tr '[:upper:]' '[:lower:]')
usage_exit() {
echo "Usage: $0 [-n namespace] [-c command] [-i image] PVC ..." 1>&2
exit 1
}
while getopts i:c:n:h OPT
do
case $OPT in
i) IMAGE=$OPTARG
;;
c) COMMAND=$OPTARG
;;
n) NAMESPACE=$OPTARG
;;
h) usage_exit
;;
esac
done
shift $(($OPTIND - 1))
VOLUME_MOUNTS=""
VOLUMES=""
COMMA=""
for i in $@
do
VOLUME_MOUNTS="${VOLUME_MOUNTS}${COMMA}{\"name\": \"${i}\",\"mountPath\": \"/pvcs/${i}\"}"
VOLUMES="${VOLUMES}${COMMA}{\"name\": \"${i}\",\"persistentVolumeClaim\": {\"claimName\": \"${i}\"}}"
COMMA=","
done
kubectl --namespace="${NAMESPACE}" 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\": [
${VOLUME_MOUNTS}
]
}
],
\"volumes\": [
${VOLUMES}
],
\"tolerations\": [
{
\"effect\": \"NoSchedule\",
\"operator\": \"Exists\"
}
]
}
}
" -- ${COMMAND}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment