Skip to content

Instantly share code, notes, and snippets.

@dhrp
Created August 19, 2024 15:26
Show Gist options
  • Save dhrp/6a754cc5a59899ff8f8cf2c63be51b86 to your computer and use it in GitHub Desktop.
Save dhrp/6a754cc5a59899ff8f8cf2c63be51b86 to your computer and use it in GitHub Desktop.
helper to start a kubernetes utility with options
#!/bin/bash
image="dhrp/tools:slim" # Default image
serviceaccount="default" # Default service account
pvcname=""
while [[ $# -gt 0 ]]; do
case $1 in
--full)
image="dhrp/tools:full"
shift
;;
--serviceaccount)
serviceaccount=$2
shift 2
;;
--pvc)
pvcname=$2
shift 2
;;
*)
shift
;;
esac
done
if [ -n "$pvcname" ]; then
volumeMounts=$(cat <<EOF
volumeMounts:
- mountPath: /mnt/data
name: mypvc
EOF
)
volumes=$(cat <<EOF
volumes:
- name: mypvc
persistentVolumeClaim:
claimName: $pvcname
EOF
)
else
volumeMounts=""
volumes=""
fi
overrides=$(cat <<EOF
apiVersion: v1
spec:
serviceAccountName: $serviceaccount
containers:
- name: tools
image: $image
command:
- /bin/bash
tty: true
stdin: true
$volumeMounts
$volumes
EOF
)
echo "$overrides"
JSONOVERRIDES=$(echo "$overrides" | yq -o=json)
echo "Will connect when running, and delete when done"
command="kubectl run tools --attach -it --rm --image $image --overrides '$JSONOVERRIDES' --command /bin/bash"
eval $command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment