Skip to content

Commit

Permalink
Add support for kaniko --debug-shell
Browse files Browse the repository at this point in the history
  • Loading branch information
kvaps committed Aug 22, 2021
1 parent 11a4db7 commit fe01321
Showing 1 changed file with 48 additions and 23 deletions.
71 changes: 48 additions & 23 deletions kubectl-build
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ set -e
export DOCKER_CONFIG=${KUBECTL_BUILD_DOCKER_CONFIG:-${DOCKER_CONFIG:-$HOME/.docker/config.json}}
export KUBECONFIG="${KUBECTL_BUILD_KUBECONFIG:-$KUBECONFIG}"
kubectl=kubectl
image="${KUBECTL_BUILD_IMAGE:-ghcr.io/kvaps/kaniko-executor:v1.6.0}"
name="${KUBECTL_BUILD_NAME_OVERRIDE:-kaniko-$(env LC_ALL=C tr -dc a-z0-9 </dev/urandom | head -c 6)}"
context=""
debug="false"
tty="false"
usetar="false"
debugcmd=""
generator=""
digestfile=""
imagenamewithdigestfile=""
Expand Down Expand Up @@ -41,6 +47,23 @@ while [ $# -gt 0 ]; do
imagenamewithdigestfile="${key##*=}"
shift
;;
--debug)
debug="true"
shift
;;
--debug=*)
debug="${key##*=}"
shift
;;
--debug-cmd)
debugcmd="$2"
shift
shift
;;
--debug-cmd=*)
debugcmd="${key##*=}"
shift
;;
--kubecontext)
nodefaultctx=1
kubectl="$kubectl --context $2"
Expand Down Expand Up @@ -89,16 +112,24 @@ else
args="$args\"--image-name-with-digest-file=\""
fi

if [ -z "$context" ]; then
args="$args ]"
elif [ -d "$context" ]; then
args="$args, \"--context=tar://stdin\" ]"
if [ -d "$context" ] || [ "$context" = "tar://stdin" ]; then
args="$args, \"--context=tar://stdin\""
debugcmd="${debugcmd:-exec /bin/sh -i}"
usetar=true
tty=false
else
args="$args, \"--context=$context\" ]"
args="$args, \"--context=$context\""
usetar=false
if [ $debug = true ]; then
tty=true
fi
fi

image="${KUBECTL_BUILD_IMAGE:-gcr.io/kaniko-project/executor:v1.6.0}"
name="${KUBECTL_BUILD_NAME_OVERRIDE:-kaniko-$(env LC_ALL=C tr -dc a-z0-9 </dev/urandom | head -c 6)}"
if [ "$debug" = true ]; then
args="$args, \"--debug\", \"--debug-cmd=$debugcmd\" ]"
else
args="$args ]"
fi

overrides="$(
cat <<EOT
Expand All @@ -109,6 +140,7 @@ overrides="$(
{
"image": "$image",
"name": "kaniko",
"tty": $tty,
"stdin": true,
"stdinOnce": true,
"terminationMessagePath": "/dev/termination-log",
Expand All @@ -129,38 +161,31 @@ if [ "$m" -lt 18 ]; then
generator="--generator=run-pod/v1"
fi

# Support bsdtar as well
m=$(tar --version | awk '{print $1; exit}')
if [ "$m" != "bsdtar" ]; then
tarf() {
tarf() {
# Support bsdtar as well
m=$(tar --version | awk '{print $1; exit}')
if [ "$m" != "bsdtar" ]; then
if [ -f "$DOCKER_CONFIG" ]; then
tar -P --transform "s|^${DOCKER_CONFIG}|../.docker/config.json|" --record-size=100K --checkpoint=1 --checkpoint-action='ttyout=Sending build context to Kaniko %{r}T\r' --exclude-ignore=.dockerignore "$@" "$DOCKER_CONFIG"
else
tar -P --record-size=100K --checkpoint=1 --checkpoint-action='ttyout=Sending build context to Kaniko %{r}T\r' --exclude-ignore=.dockerignore "$@"
fi
}
else
tarf() {
else
if [ -f "$DOCKER_CONFIG" ]; then
tar -P -s "|^${DOCKER_CONFIG}|../.docker/config.json|" "$@" "$DOCKER_CONFIG"
else
tar -P "$@"
fi
}
fi

if [ -n "$context" ] && ([ ! -d "$context" ] && [ "$context" != "tar://stdin" ]); then
echo "Error: $context: Cannot open: Not a directory" >&2
exit 1
fi
fi
}

if [ "$KUBECTL_BUILD_KEEP_POD" != "true" ]; then
trap "EC=\$?; $kubectl delete pod "$name" --wait=false 2>/dev/null || true; exit \$EC" EXIT INT TERM
fi

echo "spawning \"$name\""
if [ -n "$context" ] && [ "$context" != "tar://stdin" ]; then
tarf -C "$context" -czf - . |
if [ "$usetar" = "true" ]; then
(tarf -C "$context" -czf - . && [ "$debug" != true ] || cat) |
$kubectl run --image "$image" --restart=Never --overrides="$overrides" -i "$name" $generator
else
$kubectl run --image "$image" --restart=Never --overrides="$overrides" -i "$name" $generator
Expand Down

0 comments on commit fe01321

Please sign in to comment.