Skip to content

Instantly share code, notes, and snippets.

@yalab
Last active September 6, 2021 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yalab/f5aea3a5f7bf8cad66ba4bb3ef22a587 to your computer and use it in GitHub Desktop.
Save yalab/f5aea3a5f7bf8cad66ba4bb3ef22a587 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
while getopts i:n: OPT
do
case "$OPT" in
"n") NAMESPACE="$OPTARG";
;;
"i") IMAGE="$OPTARG";
;;
esac
done
shift $(($OPTIND - 1))
PREFIX="$1"
COMMAND="$2"
POD=$(kubectl -n $NAMESPACE get pod | grep "$PREFIX" | tail -1 | awk '{print $1}')
JSON=$(kubectl -n $NAMESPACE get pod $POD -ojson | jq '.spec.containers[0]')
if [ -z "$COMMAND" ];then
COMMAND="/bin/sh"
fi
NAME=${PREFIX}-run-$(date +%Y%m%d%H%M%S)
if [ -z "$IMAGE" ];then
IMAGE=$(echo $JSON | jq -r '.image')
fi
ENV_FROM=$(echo $JSON | jq -r '.envFrom')
OVERRIDES=$(cat <<EOF | tr -d "\n"
{
"spec": {
"containers": [
{
"name": "${NAME}",
"image": "${IMAGE}",
"envFrom": ${ENV_FROM},
"command": ["sh", "-c", "${COMMAND}"],
"tty": true,
"stdin": true
}
]
}
}
EOF
)
echo $IMAGE
kubectl -n ${NAMESPACE} run --tty -i --rm --restart=Never ${NAME} --image ${IMAGE} --overrides="$OVERRIDES" --command -- "$COMMAND"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment