Last active
September 6, 2021 00:03
-
-
Save yalab/f5aea3a5f7bf8cad66ba4bb3ef22a587 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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