This repository has been archived by the owner on Oct 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
entrypoint.sh
executable file
·574 lines (445 loc) · 14.8 KB
/
entrypoint.sh
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#!/usr/bin/env bash
# Inspired by concourse/docker-image-resource:
# https://github.com/concourse/docker-image-resource/blob/master/assets/common.sh
set -o errexit -o pipefail -o nounset
[ -z "${KINDONC_DEBUG:-}" ] || set -x
# Waits DOCKERD_TIMEOUT seconds for startup (default: 60)
DOCKERD_TIMEOUT="${DOCKERD_TIMEOUT:-60}"
# Accepts optional DOCKERD_OPTS (default: --data-root /scratch/docker --storage-driver overlay2)
DOCKERD_OPTS="${DOCKERD_OPTS:-}"
# Constants
DOCKERD_PID_FILE="/tmp/docker.pid"
DOCKERD_LOG_FILE="/tmp/docker.log"
cgroups::sanitize() {
local cgroup="/sys/fs/cgroup"
mkdir -p "${cgroup}"
if ! mountpoint -q "${cgroup}"; then
if ! mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup "${cgroup}"; then
log::error "Could not make a tmpfs mount. Did you use --privileged?"
exit 1
fi
fi
mount -o remount,rw "${cgroup}"
# Skip AppArmor
# See: https://github.com/moby/moby/commit/de191e86321f7d3136ff42ff75826b8107399497
export container=docker
# Mount /sys/kernel/security
if [[ -d /sys/kernel/security ]] && ! mountpoint -q /sys/kernel/security; then
if ! mount -t securityfs none /sys/kernel/security; then
log::error "Could not mount /sys/kernel/security."
log::error "AppArmor detection and --privileged mode might break."
fi
fi
sed -e 1d /proc/cgroups | while read sys hierarchy num enabled; do
if [[ "${enabled}" != "1" ]]; then
# subsystem disabled; skip
continue
fi
grouping="$(cat /proc/self/cgroup | cut -d: -f2 | grep "\\<${sys}\\>")"
if [[ -z "${grouping}" ]]; then
# subsystem not mounted anywhere; mount it on its own
grouping="${sys}"
fi
mountpoint="${cgroup}/${grouping}"
mkdir -p "${mountpoint}"
# clear out existing mount to make sure new one is read-write
if mountpoint -q "${mountpoint}"; then
umount "${mountpoint}"
fi
mount -n -t cgroup -o "${grouping}" cgroup "${mountpoint}"
if [[ "${grouping}" != "${sys}" ]]; then
if [[ -L "${cgroup}/${sys}" ]]; then
rm "${cgroup}/${sys}"
fi
ln -s "${mountpoint}" "${cgroup}/${sys}"
fi
done
# Initialize systemd cgroup if host isn't using systemd.
# Workaround for https://github.com/docker/for-linux/issues/219
if ! [[ -d /sys/fs/cgroup/systemd ]]; then
mkdir "${cgroup}/systemd"
mount -t cgroup -o none,name=systemd cgroup "${cgroup}/systemd"
fi
}
# Setup container environment and start docker daemon in the background.
docker::start() {
mkdir -p /var/log
mkdir -p /var/run
cgroups::sanitize
# check for /proc/sys being mounted readonly, as systemd does
if grep '/proc/sys\s\+\w\+\s\+ro,' /proc/mounts >/dev/null; then
mount -o remount,rw /proc/sys
fi
local docker_opts="${DOCKERD_OPTS:-}"
# Pass through `--garden-mtu` from gardian container
if [[ "${docker_opts}" != *'--mtu'* ]]; then
local mtu="$(cat /sys/class/net/$(ip route get 8.8.8.8|awk '{ print $5 }')/mtu)"
docker_opts+=" --mtu ${mtu}"
fi
# Use Concourse's scratch volume to bypass the graph filesystem by default
if [[ "${docker_opts}" != *'--data-root'* ]] && [[ "${docker_opts}" != *'--graph'* ]]; then
docker_opts+=' --data-root /scratch/docker'
fi
# Set storage driver to overlay2 by default
# https://kind.sigs.k8s.io/docs/user/known-issues/#docker-on-btrfs
if [[ "${docker_opts}" != *'--storage-driver'* ]]; then
docker_opts+=' --storage-driver overlay2'
fi
rm -f "${DOCKERD_PID_FILE}"
touch "${DOCKERD_LOG_FILE}"
dockerd ${docker_opts} &>"${DOCKERD_LOG_FILE}" &
echo "$!" > "${DOCKERD_PID_FILE}"
}
# Wait for docker daemon to be healthy
# Timeout after DOCKERD_TIMEOUT seconds
docker::await() {
local timeout="${DOCKERD_TIMEOUT}"
local start=${SECONDS}
timeout=$(( timeout + start ))
until docker info &>/dev/null; do
if (( SECONDS >= timeout )); then
log::error 'Timed out trying to connect to docker daemon.'
if [[ -f "${DOCKERD_LOG_FILE}" ]]; then
log::error '---DOCKERD LOGS---'
log::error "$(cat "${DOCKERD_LOG_FILE}")"
fi
exit 1
fi
if [[ -f "${DOCKERD_PID_FILE}" ]] && ! kill -0 $(cat "${DOCKERD_PID_FILE}"); then
log::error 'Docker daemon failed to start.'
if [[ -f "${DOCKERD_LOG_FILE}" ]]; then
log::error '---DOCKERD LOGS---'
log::error "$(cat "${DOCKERD_LOG_FILE}")"
fi
exit 1
fi
sleep 1
done
}
# Gracefully stop Docker daemon.
docker::stop() {
local rc docker_pid start
rc="${1:-}"
if [ "$rc" != "0" ]
then
log::error "Build failed (${rc}), not stopping docker."
return "$rc"
fi
if ! [[ -f "${DOCKERD_PID_FILE}" ]]; then
return 0
fi
docker_pid="$(cat ${DOCKERD_PID_FILE})"
if [[ -z "${docker_pid}" ]]; then
return 0
fi
kill -TERM "${docker_pid}"
start=${SECONDS}
wait "${docker_pid}"
}
export::node::image() {
local imageName="$1"
[ -n "$imageName" ] || {
log::warn "not exporting node image, we use the upstream's node image"
return
}
log::info "Exporting node image"
local destDir="${PWD}/exported-node-image"
local destName="${destDir}/image.tar"
docker save -o "$destName" "$imageName"
}
export::node::rootfs() {
local imageName="$1"
[ -n "$imageName" ] || {
log::warn "not exporting node rootfs, we use the upstream's node image"
return
}
log::info "Exporting node rootfs"
(
local cid
local destDir="${PWD}/exported-node-rootfs"
local destDirRootfs="${destDir}/rootfs"
mkdir -p "$destDirRootfs"
cid="$( docker create "$imageName" )"
trap 'docker rm "$cid"' EXIT
docker inspect "$cid" \
| jq '.[0] | { user: .Config.User, env: .Config.Env }' \
> "${destDir}/metadata.json"
docker export "$cid" \
| tar -C "$destDirRootfs" -xf -
)
}
export::node() {
local imageName="$1"
[ -z "${EXPORT_NODE_ROOTFS:-}" ] || export::node::rootfs "$imageName"
[ -z "${EXPORT_NODE_IMAGE:-}" ] || export::node::image "$imageName"
}
kind::hack::node_prepper::script() {
# Get the prepper script's content and remove all comments
grep -o '^[^#]*' "${PWD}/kind-on-c/node-prepper.sh"
}
kind::hack::node_prepper::runner() {
local nodes0 nodes1 nodeCmd clusterName \
nodes=() node pids=() pid
clusterName="$1"
nodeCmd="$( kind::hack::node_prepper::script )"
# get all the nodes currently known by kind
# remember those nodes for a bit
nodes0="$( kind get nodes --name "$clusterName" | sort )" || return 1
mapfile -t nodes <<< "$nodes0"
# we expect at least 1 node
[ "${#nodes[@]}" -ge 1 ] || return 2
# for each node, spawn a background process for doing the preperation for one
# node keep track of the PIDs
for node in "${nodes[@]}"
do
docker exec "$node" sh -c "$nodeCmd" &
pids+=( $! )
done
# wait for all preperation processes in the background
# bail out if anyone of those failed
for pid in "${pids[@]}"
do
wait "$pid" || return 3
done
# wait a bit, give kind a chance to bring up more nodes
sleep 5
# check again which nodes kind knows about
# if that is different then the list of nodes from the beginning, consider
# this run failed
nodes1="$( kind get nodes --name "$clusterName" | sort )" || return 4
[ "$nodes0" = "$nodes1" ] || return 5
return 0
}
kind::hack::node_prepper() {
log::info 'node-prepper starting in the background'
retry 300 1 kind::hack::node_prepper::runner "$1"
log::info 'node-prepper was successful on all nodes, shutting down'
}
# Generate a config file for kind
# Either from $KIND_CONFIG or fallback to kind-on-c's default
kind::gen_config() {
local defaultConfFile="$1"
local userConf="$2"
local kindConfFile
kindConfFile="$( mktemp )"
if [ -n "$userConf" ]
then
if [ -e "$userConf" ]
then
cat "$userConf" > "$kindConfFile"
else
echo "$userConf" > "$kindConfFile"
fi
else
cat "$defaultConfFile" > "$kindConfFile"
fi
echo "$kindConfFile"
}
kubectl::install() {
local binDir="$1"
local compiledKubectl="${PWD}/go/src/k8s.io/kubernetes/_output/dockerized/bin/linux/amd64/kubectl"
if [ -e "$compiledKubectl" ]
then
install -m 0750 "$compiledKubectl" "${binDir}/kubectl"
return 0
fi
local urlVer urlKubectl kubectlVer curler
curler="curl -sL"
urlVer='https://storage.googleapis.com/kubernetes-release/release/stable.txt'
kubectlVer="$( $curler "$urlVer" )"
urlKubectl="https://storage.googleapis.com/kubernetes-release/release/${kubectlVer}/bin/linux/amd64/kubectl"
install -m 0750 <($curler "$urlKubectl") "${binDir}/kubectl"
}
kind::install() {
local binDir="$1"
local kindBin="${PWD}/kind-release/kind-linux-amd64"
[ -f "$kindBin" ] || {
log::error "'kind-release' input not configured correctly"
log::error " expected the kind binary at '${kindBin}'"
return 1
}
# install kind itself
install -m 0750 "$kindBin" "${binDir}/kind"
}
metallb::install() {
[ -n "${INSTALL_METALLB:-}" ] || return 0
log::info 'Installing MetalLB'
local metallbConfigMap="${PWD}/kind-on-c/metallb-cm.yaml"
local metallbSystemConfs=(
'https://raw.githubusercontent.com/metallb/metallb/v0.10/manifests/namespace.yaml'
'https://raw.githubusercontent.com/metallb/metallb/v0.10/manifests/metallb.yaml'
)
{
for c in "${metallbSystemConfs[@]}" ; do
kubectl apply -f "$c"
done
kubectl apply -f <( kind::hack::genMetalConfig "$metallbConfigMap" )
kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"
} >/dev/null
}
kind::hack::genMetalConfig() {
local orgConfig="$1"
local kindNetName='kind' # will this change?
local dockerNet
local net cidr
local i1 i2 i3 i4 m1 m2 m3 m4
local ip0 ip1
local config
dockerNet="$( docker network inspect "$kindNetName" | jq -r '.[0].IPAM.Config[0].Subnet' )"
IFS=/ read -r net cidr <<< "$dockerNet"
IFS=. read -r i1 i2 i3 i4 <<< "$net"
IFS=. read -r m1 m2 m3 m4 <<< "$( kind::hack::cidrToNetmask "$cidr" )"
ip0="$((i1 & m1 | 255-m1)).$((i2 & m2 | 255-m2)).$((i3 & m3 | 255-m3)).$(((i4 & m4)+1))"
ip1="$((i1 & m1 | 255-m1)).$((i2 & m2 | 255-m2)).$((i3 & m3 | 255-m3)).$(((i4 & m4 | 255-m4)-1))"
# shellcheck disable=SC2016
config="$(
</dev/null oq -i yaml -o yaml -n --arg ip0 "$ip0" --arg ip1 "$ip1" '{
"address-pools": [{
"name": "default",
"protocol": "layer2",
"addresses": [
"\($ip0)-\($ip1)"
]
}]
}'
)"
# shellcheck disable=SC2016
oq -i yaml -o yaml -r --arg c "$config" '.data.config = $c' "${orgConfig}"
}
kind::hack::cidrToNetmask() {
local value="$(( 0xffffffff ^ ((1 << (32 - ${1})) - 1) ))"
echo "$(( (value >> 24) & 0xff )).$(( (value >> 16) & 0xff )).$(( (value >> 8) & 0xff )).$(( value & 0xff ))"
}
kind::hack::loglevel() {
local level="$1"
local kindVersion
local re='^[0-9]+$'
local flagName='verbosity'
[ -n "$level" ] || return 0
kindVersion=$( kind --version | sed 's/^kind version v*//g' )
# for versions 0.5.1 and older
if dpkg --compare-versions "$kindVersion" le '0.5.1'
then
re='^(panic|fatal|error|warning|info|debug)$'
flagName='loglevel'
fi
[[ "$level" =~ $re ]] || {
log::warn "${flagName} '${level}' not allowed, must match '${re}'"
return 1
}
echo "--${flagName}=${level}"
}
kind::start() {
local imageName="${1}"
local clusterName="${KIND_CLUSTER_NAME:-kind}"
local userKindConfig="${KIND_CONFIG:-}"
local defaultKindConfigFile="${PWD}/kind-on-c/kind-default-config.yaml"
# generate the config for kind and the logging option
local kindConfigFile kindLogOpt
kindConfigFile="$( kind::gen_config "$defaultKindConfigFile" "$userKindConfig" )"
kindLogOpt="$( kind::hack::loglevel "${KIND_LOGLEVEL:-}" )"
# prepare kind opts
local kindOpts
kindOpts=( --config "$kindConfigFile" --name "$clusterName" --retain )
[ -z "$kindLogOpt" ] || kindOpts+=( "$kindLogOpt" )
[ -z "$imageName" ] || kindOpts+=( --image "$imageName" )
# start the cluster
kind::hack::node_prepper "$clusterName" &
kind create cluster "${kindOpts[@]}"
# wait until the default service account is available, so pods can actually
# be scheduled
retry 60 1 kubectl -n default get serviceaccount default -o name
metallb::install
# tell 'em!
log::info "cluster available"
log::info "$( kubectl version | sed 's/^/ /g' )"
log::info " kind Version: $(kind version)"
}
kind::image::prepare() {
local nodeImagePath="${PWD}/node-image/image.tar"
local k8sSrcPath="${PWD}/go/src/k8s.io/kubernetes"
local imageName
# If we have a node image as an input, we will use that, this has precedence
if [ -e "$nodeImagePath" ]
then
log::info "found '${nodeImagePath}', will import and use that as a node image"
nodeImageName="$( docker image load -q -i "$nodeImagePath" | sed 's/^Loaded image: //' )"
echo "$nodeImageName"
return 0
fi
# Else, if we have a kubernetes source tree as an input, we will build a node
# image off of that
if [ -d "$k8sSrcPath" ]
then
log::info "found '${k8sSrcPath}', will use that source tree to build a node image off of"
local gitTag
gitTag="$( cd "${k8sSrcPath}" && git describe --dirty )"
imageName="kind/local-image:${gitTag}"
(
export GOPATH="${PWD}/go"
cd "$GOPATH"
kind build node-image --image "$imageName" >&2
)
echo "$imageName"
return 0
fi
# Fallback to not using a special node image, use the default from kind
# upstream
}
lib::load() {
local dir
dir="$( cd "$(dirname "${BASH_SOURCE[0]}")" && pwd )"
# shellcheck source=./lib.inc.sh
. "${dir}/lib.inc.sh"
}
log::version() {
local v
v="$(
cd kind-on-c \
&& git describe --dirty --exclude='*' --always 2>/dev/null \
|| echo '<unknown>'
)"
log::info "kind-on-c: ${v}"
}
main() {
lib::load
log::version
docker::start
trap 'docker::stop "$?"' EXIT
docker::await
# setup bin dir & path
local binPath="${PWD}/bin"
mkdir -p "$binPath"
export PATH="${PATH}:${binPath}"
# add some useful thingies to the bashrc to ease debugging
echo "export PATH=\"\${PATH}:${binPath}\"" >> ~/.bashrc
echo 'alias k=kubectl' >> ~/.bashrc
# install kind
kind::install "$binPath"
# prepare the node image, if configured
local imageName
imageName="$( kind::image::prepare )"
# run pre start stuff
if [ -n "${KIND_PRE_START:-}" ]
then
# shellcheck disable=SC2016
log::info 'Running pre start commands from "$KIND_PRE_START"'
bash -e -u -c "$KIND_PRE_START"
fi
# export the node image/rootfs, if configured
export::node "$imageName"
if [ -z "${KIND_TESTS:-}" ]
then
# shellcheck disable=SC2016
log::warn '"$KIND_TESTS" not sepcified. Not really running any tests.'
return 0
fi
# install kubectl
kubectl::install "$binPath"
# start the cluster
kind::start "$imageName"
# shellcheck disable=SC2016
log::info 'Running tests from "$KIND_TESTS"'
bash -e -u -c "$KIND_TESTS"
}
main "$@"