-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkubectl-build
executable file
·214 lines (203 loc) · 5.55 KB
/
kubectl-build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env sh
set -e
export DOCKER_CONFIG=${KUBECTL_BUILD_DOCKER_CONFIG:-${DOCKER_CONFIG:-$HOME/.docker/config.json}}
export KUBECONFIG="${KUBECTL_BUILD_KUBECONFIG:-$KUBECONFIG}"
kubectl=kubectl
version=1.7.2
image="${KUBECTL_BUILD_IMAGE:-ghcr.io/kvaps/kaniko-executor:v1.7.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"
generator=""
digestfile=""
imagenamewithdigestfile=""
nodefaultctx=0
nodefaultns=0
def="{}"
args="[ "
while [ $# -gt 0 ]; do
key="$1"
case $key in
-v | --version)
echo "kubectl-build $version"
exit 0
;;
-c | --context)
context="$2"
shift
shift
;;
--context=*)
context="${key##*=}"
shift
;;
--digest-file)
digestfile="$2"
shift
shift
;;
--digest-file=*)
digestfile="${key##*=}"
shift
;;
--image-name-with-digest-file)
imagenamewithdigestfile="$2"
shift
shift
;;
--image-name-with-digest-file=*)
imagenamewithdigestfile="${key##*=}"
shift
;;
--debug)
debug="true"
shift
;;
--debug=*)
debug="${key##*=}"
shift
;;
--kubecontext)
nodefaultctx=1
kubectl="$kubectl --context $2"
shift
shift
;;
--kubecontext=*)
nodefaultctx=1
kubectl="$kubectl --context=${key##*=}"
shift
;;
--kubeconfig)
kubectl="$kubectl --kubeconfig $2"
shift
shift
;;
--kubeconfig=*)
kubectl="$kubectl --kubeconfig=${key##*=}"
shift
;;
-n | --namespace)
nodefaultns=1
kubectl="$kubectl --namespace $2"
shift
shift
;;
--namespace=*)
nodefaultns=1
kubectl="$kubectl --namespace=${key##*=}"
shift
;;
*)
args="$args\"$1\", "
shift
;;
esac
done
# Set the default context and namespace to avoid situations where the user switch them during the build process
[ "$nodefaultctx" = 1 ] || kubectl="$kubectl --context=${KUBECTL_BUILD_CONTEXT:-$(kubectl config current-context)}"
[ "$nodefaultns" = 1 ] || kubectl="$kubectl --namespace=${KUBECTL_BUILD_NAMESPACE:-$(kubectl config view --minify --output 'jsonpath={.contexts..namespace}')}"
if [ -n "$digestfile" ] || [ -n "$imagenamewithdigestfile" ]; then
args="$args\"--image-name-with-digest-file=/dev/termination-log\""
else
args="$args\"--image-name-with-digest-file=\""
fi
if [ -d "$context" ] || [ "$context" = "tar://stdin" ]; then
args="$args, \"--context=tar://stdin\""
usetar=true
tty=false
else
args="$args, \"--context=$context\""
usetar=false
if [ $debug = true ]; then
tty=true
fi
fi
if [ "$debug" = true ]; then
args="$args, \"--debug\" ]"
else
args="$args ]"
fi
overrides="$(
cat <<EOT
{
"metadata": ${KUBECTL_BUILD_METADATA_OVERRIDES:-$def},
"spec": {
"containers": [
{
"image": "$image",
"name": "kaniko",
"tty": $tty,
"stdin": true,
"stdinOnce": true,
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"args": $args
}
],
"automountServiceAccountToken": false,
"enableServiceLinks": false
}
}
EOT
)"
# Support Kubectl <1.18
m=$(kubectl version --client -o yaml | awk -F'[ :"]+' '$2 == "minor" {print $3+0}')
if [ "$m" -lt 18 ]; then
generator="--generator=run-pod/v1"
fi
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
if [ -f "$DOCKER_CONFIG" ]; then
tar -P -s "|^${DOCKER_CONFIG}|../.docker/config.json|" "$@" "$DOCKER_CONFIG"
else
tar -P "$@"
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 [ "$usetar" = "true" ]; then
pidfile=$(mktemp -u)
(
tarf -C "$context" -czf - .
if [ "$debug" = true ]; then
sh -c 'echo $PPID' > "$pidfile"
trap "stty '$(stty -g)'" EXIT
stty -icanon -ctlecho -iexten -isig -echo
cat 2>/dev/null
fi
) | (
$kubectl run --image "$image" --restart=Never --overrides="$overrides" -i "$name" --leave-stdin-open=true $generator
if [ "$debug" = true ]; then
kill $(cat "$pidfile") 2>/dev/null
fi
$kubectl get pod "$name" -w -o 'go-template={{with .status.containerStatuses}}{{range .}}{{with .state.terminated}}{{index . "exitCode"}}{{"\n"}}{{.exitCode.err}}{{end}}{{end}}{{end}}' 2>/dev/null \
| { read ec; exit $ec; }
)
else
$kubectl run --image "$image" --restart=Never --overrides="$overrides" -i "$name" $generator
fi
# Retrieve digest file
if [ $? -eq 0 ] && ([ -n "$digestfile" ] || [ -n "$imagenamewithdigestfile" ]); then
imagenamewithdigest=$($kubectl get pod "$name" -o go-template='{{ range .status.containerStatuses }}{{ with .state.terminated.message }}{{ . }}{{ end }}{{ end }}')
if [ -z "$imagenamewithdigest" ]; then
echo "Error: termination message is missing" >&2
exit 2
fi
digest=${imagenamewithdigest##*@}
[ -z "$digestfile" ] || printf "%s" "$digest" >"$digestfile"
[ -z "$imagenamewithdigestfile" ] || printf "%s\n" "$imagenamewithdigest" >"$imagenamewithdigestfile"
fi