Skip to content

Instantly share code, notes, and snippets.

@qrkourier
Last active April 15, 2023 21:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qrkourier/a4f72086530e43fd85f5d7b988a64c8b to your computer and use it in GitHub Desktop.
Save qrkourier/a4f72086530e43fd85f5d7b988a64c8b to your computer and use it in GitHub Desktop.
run any command in any container image in any Kubernetes namespace
# krun busybox whoami
krun(){
local NAMESPACE=default
local PRIVILEGED=false
while getopts 'n:pu:' OPT; do
case $OPT in
n) NAMESPACE=$OPTARG
;;
p) PRIVILEGED=true
;;
u) RUNAS=$OPTARG
;;
esac
done
shift $(( OPTIND -1 ))
local IMAGE=$1; shift;
eval kubectl -n "$NAMESPACE" run \
--privileged "${PRIVILEGED}" \
--image "$IMAGE" \
"${RUNAS:+ --as $RUNAS}" \
--tty --stdin --rm \
--restart=Never \
"krun-${IMAGE//[:\/.]/-}" \
--command -- \
"${@}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment