Skip to content

Commit 4c7f8e8

Browse files
authored
Merge pull request #10 from kvaps/debug-shell
Add support for kaniko --debug-shell and extra fixes
2 parents 11a4db7 + 9bd0dcd commit 4c7f8e8

File tree

2 files changed

+61
-25
lines changed

2 files changed

+61
-25
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ docker login docker.io
4646
# Short form
4747
kubectl build -c . -d docker.io/some/image:latest
4848

49+
# Run debug shell
50+
kubectl build -c . --no-push --debug
51+
4952
# Use cache building
5053
kubectl build --context . --destination docker.io/some/image:latest --cache --cache-repo docker.io/some/cache
5154

kubectl-build

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ set -e
33
export DOCKER_CONFIG=${KUBECTL_BUILD_DOCKER_CONFIG:-${DOCKER_CONFIG:-$HOME/.docker/config.json}}
44
export KUBECONFIG="${KUBECTL_BUILD_KUBECONFIG:-$KUBECONFIG}"
55
kubectl=kubectl
6+
version=1.6.0
7+
image="${KUBECTL_BUILD_IMAGE:-ghcr.io/kvaps/kaniko-executor:v1.6.0}"
8+
name="${KUBECTL_BUILD_NAME_OVERRIDE:-kaniko-$(env LC_ALL=C tr -dc a-z0-9 </dev/urandom | head -c 6)}"
69
context=""
10+
debug="false"
11+
tty="false"
12+
usetar="false"
713
generator=""
814
digestfile=""
915
imagenamewithdigestfile=""
@@ -14,6 +20,10 @@ while [ $# -gt 0 ]; do
1420
key="$1"
1521

1622
case $key in
23+
-v | --version)
24+
echo "kubectl-build $version"
25+
exit 0
26+
;;
1727
-c | --context)
1828
context="$2"
1929
shift
@@ -41,6 +51,14 @@ while [ $# -gt 0 ]; do
4151
imagenamewithdigestfile="${key##*=}"
4252
shift
4353
;;
54+
--debug)
55+
debug="true"
56+
shift
57+
;;
58+
--debug=*)
59+
debug="${key##*=}"
60+
shift
61+
;;
4462
--kubecontext)
4563
nodefaultctx=1
4664
kubectl="$kubectl --context $2"
@@ -89,16 +107,23 @@ else
89107
args="$args\"--image-name-with-digest-file=\""
90108
fi
91109

92-
if [ -z "$context" ]; then
93-
args="$args ]"
94-
elif [ -d "$context" ]; then
95-
args="$args, \"--context=tar://stdin\" ]"
110+
if [ -d "$context" ] || [ "$context" = "tar://stdin" ]; then
111+
args="$args, \"--context=tar://stdin\""
112+
usetar=true
113+
tty=false
96114
else
97-
args="$args, \"--context=$context\" ]"
115+
args="$args, \"--context=$context\""
116+
usetar=false
117+
if [ $debug = true ]; then
118+
tty=true
119+
fi
98120
fi
99121

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

103128
overrides="$(
104129
cat <<EOT
@@ -109,6 +134,7 @@ overrides="$(
109134
{
110135
"image": "$image",
111136
"name": "kaniko",
137+
"tty": $tty,
112138
"stdin": true,
113139
"stdinOnce": true,
114140
"terminationMessagePath": "/dev/termination-log",
@@ -129,39 +155,46 @@ if [ "$m" -lt 18 ]; then
129155
generator="--generator=run-pod/v1"
130156
fi
131157

132-
# Support bsdtar as well
133-
m=$(tar --version | awk '{print $1; exit}')
134-
if [ "$m" != "bsdtar" ]; then
135-
tarf() {
158+
tarf() {
159+
# Support bsdtar as well
160+
m=$(tar --version | awk '{print $1; exit}')
161+
if [ "$m" != "bsdtar" ]; then
136162
if [ -f "$DOCKER_CONFIG" ]; then
137163
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"
138164
else
139165
tar -P --record-size=100K --checkpoint=1 --checkpoint-action='ttyout=Sending build context to Kaniko %{r}T\r' --exclude-ignore=.dockerignore "$@"
140166
fi
141-
}
142-
else
143-
tarf() {
167+
else
144168
if [ -f "$DOCKER_CONFIG" ]; then
145169
tar -P -s "|^${DOCKER_CONFIG}|../.docker/config.json|" "$@" "$DOCKER_CONFIG"
146170
else
147171
tar -P "$@"
148172
fi
149-
}
150-
fi
151-
152-
if [ -n "$context" ] && ([ ! -d "$context" ] && [ "$context" != "tar://stdin" ]); then
153-
echo "Error: $context: Cannot open: Not a directory" >&2
154-
exit 1
155-
fi
173+
fi
174+
}
156175

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

161180
echo "spawning \"$name\""
162-
if [ -n "$context" ] && [ "$context" != "tar://stdin" ]; then
163-
tarf -C "$context" -czf - . |
164-
$kubectl run --image "$image" --restart=Never --overrides="$overrides" -i "$name" $generator
181+
if [ "$usetar" = "true" ]; then
182+
pidfile=$(mktemp -u)
183+
(
184+
tarf -C "$context" -czf - .
185+
if [ "$debug" = true ]; then
186+
sh -c 'echo $PPID' > "$pidfile"
187+
trap 'stty icanon echo' EXIT
188+
stty -icanon -echo
189+
cat
190+
fi
191+
) | (
192+
$kubectl run --image "$image" --restart=Never --overrides="$overrides" -i "$name" $generator || ec=$?
193+
if [ "$debug" = true ]; then
194+
kill $(cat "$pidfile")
195+
fi
196+
exit $ec
197+
)
165198
else
166199
$kubectl run --image "$image" --restart=Never --overrides="$overrides" -i "$name" $generator
167200
fi

0 commit comments

Comments
 (0)