-
Notifications
You must be signed in to change notification settings - Fork 4
/
kubewave-k8s-v1.9.sh
executable file
·1890 lines (1732 loc) · 61.5 KB
/
kubewave-k8s-v1.9.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
if [ $(uname) = Darwin ]; then
readlinkf(){ perl -MCwd -e 'print Cwd::abs_path shift' "$1";}
else
readlinkf(){ readlink -f "$1"; }
fi
DIND_ROOT="$(cd $(dirname "$(readlinkf "${BASH_SOURCE}")"); pwd)"
RUN_ON_BTRFS_ANYWAY="${RUN_ON_BTRFS_ANYWAY:-}"
if [[ ! ${RUN_ON_BTRFS_ANYWAY} ]] && docker info| grep -q '^Storage Driver: btrfs'; then
echo "ERROR: Docker is using btrfs storage driver which is unsupported by kubernetes-docker-in-docker-cluster" >&2
echo "Please refer to the documentation for more info." >&2
echo "Set RUN_ON_BTRFS_ANYWAY to non-empty string to continue anyway." >&2
exit 1
fi
# In case of linuxkit / moby linux, -v will not work so we can't
# mount /lib/modules and /boot. Also we'll be using localhost
# to access the apiserver.
using_linuxkit=
if ! docker info|grep -s '^Operating System: .*Docker for Windows' > /dev/null 2>&1 ; then
if docker info|grep -s '^Kernel Version: .*-moby$' >/dev/null 2>&1 ||
docker info|grep -s '^Kernel Version: .*-linuxkit-' > /dev/null 2>&1 ; then
using_linuxkit=1
fi
fi
# In case of linux as the OS and docker running locally we will be using
# localhost to access the apiserver nor do we need to add routes
using_linuxdocker=
if [[ $(uname) == Linux && -z ${DOCKER_HOST:-} ]]; then
using_linuxdocker=1
fi
EMBEDDED_CONFIG=y;DIND_IMAGE=richardsonlima/kubernetes-docker-in-docker-cluster:v1.9
function dind::find-free-ipv4-subnet() {
local maxIP anAddressInNewSubnet
subnetSize="$1"
maxIP=$( dind::ipv4::find-maximum-claimed-ip )
if [ $maxIP -eq 0 ]
then
echo '10.192.0.0'
return
fi
# maxIP is the highest IP we cannot use (because it already belongs to a subnet)
# One could argue that maxIP+1 could be the MinHost of the subnet we're about to allocate (a.k.a. "new subnet").
# But, consider:
# maxIP: 10.0.0.255
# maybeNextMinHost: 10.0.1.0
# In the above, a subnet size of /16, will start at 10.0.0.0 - which is invalid.
# So we need to start the new subnet at least 32-(subnetSize) IP spaces away.
anAddressInNewSubnet=$(( maxIP + (1<<(32-subnetSize)) ))
# apply mask to get min host
nextMinHost=$(( anAddressInNewSubnet & $(dind::ipv4::netmask "$subnetSize") ))
dind::ipv4::itoa "$nextMinHost"
}
function dind::ipv4::netmask() {
local netmask i
netmask=0
for i in $( seq $(( 32 - $1 )) 32 )
do
netmask=$(( netmask + 2**i ))
done
echo "$netmask"
}
function dind::ipv4::find-maximum-claimed-ip() {
local maxIP upperIPs b m i
maxIP=0
upperIPs="$(
docker network ls --format '{{ .Name }}' | while read -r nw
do
subnet="$( docker network inspect "$nw" --format "{{ range .IPAM.Config }}{{ .Subnet }}{{ end }}" )"
if [ -z "$subnet" ]
then
continue
fi
IFS='/' read -r b m <<<"$subnet"
echo $(( $(dind::ipv4::atoi "$b") + (1<<(32-m)) - 1 ))
done
)"
for i in $upperIPs
do
if [ "$(( i - maxIP ))" -gt 1 ]
then
maxIP="$i"
fi
done
echo "$maxIP"
}
function dind::ipv4::itoa() {
echo -n $(( ($1 / 256 / 256 / 256) % 256)).
echo -n $(( ($1 / 256 / 256) % 256 )).
echo -n $(( ($1 / 256) % 256 )).
echo $(( $1 % 256 ))
}
function dind::ipv4::atoi() {
local ip="$1"
local ret=0
for (( i=0 ; i<4 ; ++i ))
do
(( ret += ${ip%%.*} * ( 256**(3-i) ) ))
ip=${ip#*.}
done
echo $ret
}
function dind::localhost() {
if [[ ${IP_MODE} = "ipv6" ]]; then
echo '[::1]'
else
echo '127.0.0.1'
fi
}
IP_MODE="${IP_MODE:-ipv4}" # ipv4, ipv6, (future) dualstack
if [[ ! ${EMBEDDED_CONFIG:-} ]]; then
source "${DIND_ROOT}/config.sh"
fi
CNI_PLUGIN="${CNI_PLUGIN:-bridge}"
ETCD_HOST="${ETCD_HOST:-127.0.0.1}"
GCE_HOSTED="${GCE_HOSTED:-}"
DIND_ALLOW_AAAA_USE="${DIND_ALLOW_AAAA_USE:-}" # Default is to use DNS64 always for IPv6 mode
if [[ ${IP_MODE} = "ipv6" ]]; then
DIND_SUBNET="${DIND_SUBNET:-fd00:10::}"
dind_ip_base="${DIND_SUBNET}"
ETCD_HOST="::1"
KUBE_RSYNC_ADDR="${KUBE_RSYNC_ADDR:-::1}"
SERVICE_CIDR="${SERVICE_CIDR:-fd00:10:30::/110}"
DIND_SUBNET_SIZE="${DIND_SUBNET_SIZE:-64}"
REMOTE_DNS64_V4SERVER="${REMOTE_DNS64_V4SERVER:-8.8.8.8}"
LOCAL_NAT64_SERVER="${DIND_SUBNET}200"
NAT64_V4_SUBNET_PREFIX="${NAT64_V4_SUBNET_PREFIX:-172.18}"
DNS64_PREFIX="${DNS64_PREFIX:-fd00:10:64:ff9b::}"
DNS64_PREFIX_SIZE="${DNS64_PREFIX_SIZE:-96}"
DNS64_PREFIX_CIDR="${DNS64_PREFIX}/${DNS64_PREFIX_SIZE}"
dns_server="${dind_ip_base}100"
DEFAULT_POD_NETWORK_CIDR="fd00:10:20::/72"
USE_HAIRPIN="${USE_HAIRPIN:-true}" # Default is to use hairpin for IPv6
if [[ ${DIND_ALLOW_AAAA_USE} && ${GCE_HOSTED} ]]; then
echo "ERROR! GCE does not support use of IPv6 for external addresses - aborting."
exit 1
fi
else
KUBE_RSYNC_ADDR="${KUBE_RSYNC_ADDR:-127.0.0.1}"
SERVICE_CIDR="${SERVICE_CIDR:-10.96.0.0/12}"
dns_server="${REMOTE_DNS64_V4SERVER:-8.8.8.8}"
DEFAULT_POD_NETWORK_CIDR="10.244.0.0/16"
USE_HAIRPIN="${USE_HAIRPIN:-false}" # Disabled for IPv4, as issue with Virtlet networking
if [[ ${DIND_ALLOW_AAAA_USE} ]]; then
echo "WARNING! The DIND_ALLOW_AAAA_USE option is for IPv6 mode - ignoring setting."
DIND_ALLOW_AAAA_USE=
fi
if [[ ${CNI_PLUGIN} = "calico" || ${CNI_PLUGIN} = "calico-kdd" ]]; then
DEFAULT_POD_NETWORK_CIDR="192.168.0.0/16"
fi
fi
dns_prefix="$(echo ${SERVICE_CIDR} | sed 's,/.*,,')"
if [[ ${IP_MODE} != "ipv6" ]]; then
dns_prefix="$(echo ${dns_prefix} | sed 's/0$//')"
DNS_SVC_IP="${dns_prefix}10"
else
DNS_SVC_IP="${dns_prefix}a"
fi
POD_NETWORK_CIDR="${POD_NETWORK_CIDR:-${DEFAULT_POD_NETWORK_CIDR}}"
if [[ ${IP_MODE} = "ipv6" ]]; then
# For IPv6 will extract the network part and size from pod cluster CIDR.
# The size will be increased by eight, as the pod network will be split
# into subnets for each node. The network part will be converted into a
# prefix that will get the node ID appended, for each node. In some cases
# this means padding the prefix. In other cases, the prefix must be
# trimmed, so that when the node ID is added, it forms a correct prefix.
POD_NET_PREFIX="$(echo ${POD_NETWORK_CIDR} | sed 's,::/.*,:,')"
cluster_size="$(echo ${POD_NETWORK_CIDR} | sed 's,.*::/,,')"
POD_NET_SIZE=$((${cluster_size}+8))
num_colons="$(grep -o ":" <<< "${POD_NET_PREFIX}" | wc -l)"
need_zero_pads=$((${cluster_size}/16))
# Will be replacing lowest byte with node ID, so pull off last byte and colon
if [[ ${num_colons} -gt ${need_zero_pads} ]]; then
POD_NET_PREFIX=${POD_NET_PREFIX::-3}
fi
# Add in zeros to pad 16 bits at a time, up to the padding needed, which is
# need_zero_pads - num_colons.
while [ ${num_colons} -lt ${need_zero_pads} ]; do
POD_NET_PREFIX+="0:"
num_colons+=1
done
elif [[ ${CNI_PLUGIN} = "bridge" ]]; then # IPv4, bridge
# For IPv4, will assume user specifies /16 CIDR and will use a /24 subnet
# on each node.
POD_NET_PREFIX="$(echo ${POD_NETWORK_CIDR} | sed 's/^\([0-9]*\.[0-9]*\.\).*/\1/')"
POD_NET_SIZE=24
else
POD_NET_PREFIX=
POD_NET_SIZE=
fi
DIND_IMAGE="${DIND_IMAGE:-}"
BUILD_KUBEADM="${BUILD_KUBEADM:-}"
BUILD_HYPERKUBE="${BUILD_HYPERKUBE:-}"
KUBEADM_SOURCE="${KUBEADM_SOURCE-}"
HYPERKUBE_SOURCE="${HYPERKUBE_SOURCE-}"
NUM_NODES=${NUM_NODES:-2}
EXTRA_PORTS="${EXTRA_PORTS:-}"
LOCAL_KUBECTL_VERSION=${LOCAL_KUBECTL_VERSION:-}
KUBECTL_DIR="${KUBECTL_DIR:-${HOME}/.kubeadm-dind-cluster}"
DASHBOARD_URL="${DASHBOARD_URL:-https://rawgit.com/kubernetes/dashboard/bfab10151f012d1acc5dfb1979f3172e2400aa3c/src/deploy/kubernetes-dashboard.yaml}"
SKIP_SNAPSHOT="${SKIP_SNAPSHOT:-}"
E2E_REPORT_DIR="${E2E_REPORT_DIR:-}"
DIND_NO_PARALLEL_E2E="${DIND_NO_PARALLEL_E2E:-}"
DNS_SERVICE="${DNS_SERVICE:-kube-dns}"
DIND_CA_CERT_URL="${DIND_CA_CERT_URL:-}"
DIND_PROPAGATE_HTTP_PROXY="${DIND_PROPAGATE_HTTP_PROXY:-}"
DIND_HTTP_PROXY="${DIND_HTTP_PROXY:-}"
DIND_HTTPS_PROXY="${DIND_HTTPS_PROXY:-}"
DIND_NO_PROXY="${DIND_NO_PROXY:-}"
DIND_DAEMON_JSON_FILE="${DIND_DAEMON_JSON_FILE:-/etc/docker/daemon.json}" # can be set to /dev/null
DIND_REGISTRY_MIRROR="${DIND_REGISTRY_MIRROR:-}" # plain string format
DIND_INSECURE_REGISTRIES="${DIND_INSECURE_REGISTRIES:-}" # json list format
FEATURE_GATES="${FEATURE_GATES:-MountPropagation=true}"
# you can set special value 'none' not to set any kubelet's feature gates.
KUBELET_FEATURE_GATES="${KUBELET_FEATURE_GATES:-MountPropagation=true,DynamicKubeletConfig=true}"
if [[ ! ${LOCAL_KUBECTL_VERSION:-} && ${DIND_IMAGE:-} =~ :(v[0-9]+\.[0-9]+)$ ]]; then
LOCAL_KUBECTL_VERSION="${BASH_REMATCH[1]}"
fi
DEFAULT_DIND_LABEL='mirantis.kubeadm_dind_cluster_runtime'
: "${DIND_LABEL:=${DEFAULT_DIND_LABEL}}"
# not configurable for now, would need to setup context for kubectl _inside_ the cluster
readonly INTERNAL_APISERVER_PORT=8080
function dind::need-source {
if [[ ! -f cluster/kubectl.sh ]]; then
echo "$0 must be called from the Kubernetes repository root directory" 1>&2
exit 1
fi
}
build_tools_dir="build"
use_k8s_source=y
if [[ ! ${BUILD_KUBEADM} && ! ${BUILD_HYPERKUBE} ]]; then
use_k8s_source=
fi
if [[ ${use_k8s_source} ]]; then
dind::need-source
kubectl=cluster/kubectl.sh
if [[ ! -f ${build_tools_dir}/common.sh ]]; then
build_tools_dir="build-tools"
fi
else
if [[ ! ${LOCAL_KUBECTL_VERSION:-} ]] && ! hash kubectl 2>/dev/null; then
echo "You need kubectl binary in your PATH to use prebuilt DIND image" 1>&2
exit 1
fi
kubectl=kubectl
fi
function dind::retry {
# based on retry function in hack/jenkins/ scripts in k8s source
for i in {1..10}; do
"$@" && return 0 || sleep ${i}
done
"$@"
}
busybox_image="busybox:1.26.2"
e2e_base_image="golang:1.9.2"
sys_volume_args=()
build_volume_args=()
function dind::set-build-volume-args {
if [ ${#build_volume_args[@]} -gt 0 ]; then
return 0
fi
build_container_name=
if [ -n "${KUBEADM_DIND_LOCAL:-}" ]; then
build_volume_args=(-v "$PWD:/go/src/k8s.io/kubernetes")
else
build_container_name="$(KUBE_ROOT=${PWD} ETCD_HOST=${ETCD_HOST} &&
. ${build_tools_dir}/common.sh &&
kube::build::verify_prereqs >&2 &&
echo "${KUBE_DATA_CONTAINER_NAME:-${KUBE_BUILD_DATA_CONTAINER_NAME}}")"
build_volume_args=(--volumes-from "${build_container_name}")
fi
}
function dind::volume-exists {
local name="$1"
if docker volume inspect "${name}" >& /dev/null; then
return 0
fi
return 1
}
function dind::create-volume {
local name="$1"
docker volume create --label "${DIND_LABEL}" --name "${name}" >/dev/null
}
# We mount /boot and /lib/modules into the container
# below to in case some of the workloads need them.
# This includes virtlet, for instance. Also this may be
# useful in future if we want DIND nodes to pass
# preflight checks.
# Unfortunately we can't do this when using Mac Docker
# (unless a remote docker daemon on Linux is used)
# NB: there's no /boot on recent Mac dockers
function dind::prepare-sys-mounts {
if [[ ! ${using_linuxkit} ]]; then
sys_volume_args=()
if [[ -d /boot ]]; then
sys_volume_args+=(-v /boot:/boot)
fi
if [[ -d /lib/modules ]]; then
sys_volume_args+=(-v /lib/modules:/lib/modules)
fi
return 0
fi
local dind_sys_vol_name
dind_sys_vol_name="kubeadm-dind-sys$( dind::clusterSuffix )"
if ! dind::volume-exists "$dind_sys_vol_name"; then
dind::step "Saving a copy of docker host's /lib/modules"
dind::create-volume "$dind_sys_vol_name"
# Use a dirty nsenter trick to fool Docker on Mac and grab system
# /lib/modules into sys.tar file on kubeadm-dind-sys volume.
local nsenter="nsenter --mount=/proc/1/ns/mnt --"
docker run \
--rm \
--privileged \
-v "$dind_sys_vol_name":/dest \
--pid=host \
"${busybox_image}" \
/bin/sh -c \
"if ${nsenter} test -d /lib/modules; then ${nsenter} tar -C / -c lib/modules >/dest/sys.tar; fi"
fi
sys_volume_args=(-v "$dind_sys_vol_name":/dind-sys)
}
tmp_containers=()
function dind::cleanup {
if [ ${#tmp_containers[@]} -gt 0 ]; then
for name in "${tmp_containers[@]}"; do
docker rm -vf "${name}" 2>/dev/null
done
fi
}
trap dind::cleanup EXIT
function dind::check-image {
local name="$1"
if docker inspect --format 'x' "${name}" >&/dev/null; then
return 0
else
return 1
fi
}
function dind::filter-make-output {
# these messages make output too long and make Travis CI choke
egrep -v --line-buffered 'I[0-9][0-9][0-9][0-9] .*(parse|conversion|defaulter|deepcopy)\.go:[0-9]+\]'
}
function dind::run-build-command {
# this is like build/run.sh, but it doesn't rsync back the binaries,
# only the generated files.
local cmd=("$@")
(
# The following is taken from build/run.sh and build/common.sh
# of Kubernetes source tree. It differs in
# --filter='+ /_output/dockerized/bin/**'
# being removed from rsync
. ${build_tools_dir}/common.sh
kube::build::verify_prereqs
kube::build::build_image
kube::build::run_build_command "$@"
kube::log::status "Syncing out of container"
kube::build::start_rsyncd_container
local rsync_extra=""
if (( ${KUBE_VERBOSE} >= 6 )); then
rsync_extra="-iv"
fi
# The filter syntax for rsync is a little obscure. It filters on files and
# directories. If you don't go in to a directory you won't find any files
# there. Rules are evaluated in order. The last two rules are a little
# magic. '+ */' says to go in to every directory and '- /**' says to ignore
# any file or directory that isn't already specifically allowed.
#
# We are looking to copy out all of the built binaries along with various
# generated files.
kube::build::rsync \
--filter='- /vendor/' \
--filter='- /_temp/' \
--filter='+ zz_generated.*' \
--filter='+ generated.proto' \
--filter='+ *.pb.go' \
--filter='+ types.go' \
--filter='+ */' \
--filter='- /**' \
"rsync://k8s@${KUBE_RSYNC_ADDR}/k8s/" "${KUBE_ROOT}"
kube::build::stop_rsyncd_container
)
}
function dind::make-for-linux {
local copy="$1"
shift
dind::step "Building binaries:" "$*"
if [ -n "${KUBEADM_DIND_LOCAL:-}" ]; then
dind::step "+ make WHAT=\"$*\""
make WHAT="$*" 2>&1 | dind::filter-make-output
elif [ "${copy}" = "y" ]; then
dind::step "+ ${build_tools_dir}/run.sh make WHAT=\"$*\""
"${build_tools_dir}/run.sh" make WHAT="$*" 2>&1 | dind::filter-make-output
else
dind::step "+ [using the build container] make WHAT=\"$*\""
dind::run-build-command make WHAT="$*" 2>&1 | dind::filter-make-output
fi
}
function dind::check-binary {
local filename="$1"
local dockerized="_output/dockerized/bin/linux/amd64/${filename}"
local plain="_output/local/bin/linux/amd64/${filename}"
dind::set-build-volume-args
# FIXME: don't hardcode amd64 arch
if [ -n "${KUBEADM_DIND_LOCAL:-${force_local:-}}" ]; then
if [ -f "${dockerized}" -o -f "${plain}" ]; then
return 0
fi
elif docker run --rm "${build_volume_args[@]}" \
"${busybox_image}" \
test -f "/go/src/k8s.io/kubernetes/${dockerized}" >&/dev/null; then
return 0
fi
return 1
}
function dind::ensure-downloaded-kubectl {
local kubectl_url
local kubectl_sha1
local kubectl_sha1_linux
local kubectl_sha1_darwin
local kubectl_link
local kubectl_os
local full_kubectl_version
case "${LOCAL_KUBECTL_VERSION}" in
v1.8)
full_kubectl_version=v1.8.15
kubectl_sha1_linux=52a1ee321e1e8c0ecfd6e83c38bf972c2c60adf2
kubectl_sha1_darwin=ac3f823d7aa104237929a1e35ea400c6aa3cc356
;;
v1.9)
full_kubectl_version=v1.9.9
kubectl_sha1_linux=c8163a6360119c56d163fbd8cef8727e9841e712
kubectl_sha1_darwin=09585552eb7616954481789489ec382c633a0162
;;
v1.10)
full_kubectl_version=v1.10.5
kubectl_sha1_linux=dbe431b2684f8ff4188335b3b3cea185d5a9ec44
kubectl_sha1_darwin=08e58440949c71053b45bfadf80532ea3d752d12
;;
v1.11)
full_kubectl_version=v1.11.0
kubectl_sha1_linux=e23f251ca0cb848802f3cb0f69a4ba297d07bfc6
kubectl_sha1_darwin=6eff29a328c4bc00879fd6a0c8b33690c6f75908
;;
"")
return 0
;;
*)
echo "Invalid kubectl version" >&2
exit 1
esac
export PATH="${KUBECTL_DIR}:$PATH"
if [ $(uname) = Darwin ]; then
kubectl_sha1="${kubectl_sha1_darwin}"
kubectl_os=darwin
else
kubectl_sha1="${kubectl_sha1_linux}"
kubectl_os=linux
fi
local link_target="kubectl-${full_kubectl_version}"
local link_name="${KUBECTL_DIR}"/kubectl
if [[ -h "${link_name}" && "$(readlink "${link_name}")" = "${link_target}" ]]; then
return 0
fi
local path="${KUBECTL_DIR}/${link_target}"
if [[ ! -f "${path}" ]]; then
mkdir -p "${KUBECTL_DIR}"
curl -sSLo "${path}" "https://storage.googleapis.com/kubernetes-release/release/${full_kubectl_version}/bin/${kubectl_os}/amd64/kubectl"
echo "${kubectl_sha1} ${path}" | sha1sum -c
chmod +x "${path}"
fi
ln -fs "${link_target}" "${KUBECTL_DIR}/kubectl"
}
function dind::ensure-kubectl {
if [[ ! ${use_k8s_source} ]]; then
# already checked on startup
dind::ensure-downloaded-kubectl
return 0
fi
if [ $(uname) = Darwin ]; then
if [ ! -f _output/local/bin/darwin/amd64/kubectl ]; then
dind::step "Building kubectl"
dind::step "+ make WHAT=cmd/kubectl"
make WHAT=cmd/kubectl 2>&1 | dind::filter-make-output
fi
elif ! force_local=y dind::check-binary kubectl; then
dind::make-for-linux y cmd/kubectl
fi
}
function dind::prepare-kube-local-environment {
dind::step "Creating .kube local dir"
mkdir -p ~/.kube
dind::step "Making a copy of admin conf into .kube"
sudo docker cp `sudo docker ps -a |grep kube-master |awk '{print $1}'`:/etc/kubernetes/admin.conf ~/.kube/config
dind::step "Granting local user perms on ~/.kube/config"
sudo chown -R ${USER} ~/.kube
dind::step "Now you can use local kubectl like this: kubectl --kubeconfig ~/.kube/config create namespace test-docker-in-docker"
dind::step ""
}
function dind::ensure-binaries {
local -a to_build=()
for name in "$@"; do
if ! dind::check-binary "$(basename "${name}")"; then
to_build+=("${name}")
fi
done
if [ "${#to_build[@]}" -gt 0 ]; then
dind::make-for-linux n "${to_build[@]}"
fi
return 0
}
function dind::ensure-network {
local netName
netName="$(dind::net-name)"
read -r net mask exists <<<"$( dind::get-subnet )"
if [ "$exists" -ne 1 ]
then
local v6settings=""
if [[ ${IP_MODE} = "ipv6" ]]; then
# Need second network for NAT64
v6settings="--subnet=${NAT64_V4_SUBNET_PREFIX}.0.0/16 --ipv6"
fi
docker network create \
${v6settings} \
--subnet="${net}/${mask}" \
--gateway="$(dind::get-ip-from-range 1)" \
"${netName}" >/dev/null
fi
}
function dind::ensure-volume {
local reuse_volume=
if [[ $1 = -r ]]; then
reuse_volume=1
shift
fi
local name="$1"
if dind::volume-exists "${name}"; then
if [[ ! ${reuse_volume} ]]; then
docker volume rm "${name}" >/dev/null
fi
elif [[ ${reuse_volume} ]]; then
echo "*** Failed to locate volume: ${name}" 1>&2
return 1
fi
dind::create-volume "${name}"
}
function dind::ensure-dns {
if [[ ${IP_MODE} = "ipv6" ]]; then
if ! docker inspect bind9 >&/dev/null; then
local bind9_path=/tmp/bind9
rm -rf ${bind9_path}
mkdir -p ${bind9_path}/conf ${bind9_path}/cache
local force_dns64_for=""
if [[ ! ${DIND_ALLOW_AAAA_USE} ]]; then
# Normally, if have an AAAA record, it is used. This clause tells
# bind9 to do ignore AAAA records for the specified networks
# and/or addresses and lookup A records and synthesize new AAAA
# records. In this case, we select "any" networks that have AAAA
# records meaning we ALWAYS use A records and do NAT64.
force_dns64_for="exclude { any; };"
fi
cat >${bind9_path}/conf/named.conf <<BIND9_EOF
options {
directory "/var/bind";
allow-query { any; };
forwarders {
${DNS64_PREFIX}${REMOTE_DNS64_V4SERVER};
};
auth-nxdomain no; # conform to RFC1035
listen-on-v6 { any; };
dns64 ${DNS64_PREFIX_CIDR} {
${force_dns64_for}
};
};
BIND9_EOF
if [[ ${GCE_HOSTED} ]]; then
# TODO: Have bind9 create config file, and pass in variables via Env vars.
docker-machine scp ${bind9_path}/conf/named.conf k8s-dind:bind9-named.conf
docker-machine ssh k8s-dind sudo rm -rf ${bind-path}
docker-machine ssh k8s-dind sudo mkdir -p ${bind9_path}/conf ${bind9_path}/cache
docker-machine ssh k8s-dind sudo cp /home/docker-user/bind9-named.conf ${bind9_path}/conf/named.conf
fi
docker run -d --name bind9 --hostname bind9 --net "$(dind::net-name)" --label "${DIND_LABEL}" \
--sysctl net.ipv6.conf.all.disable_ipv6=0 --sysctl net.ipv6.conf.all.forwarding=1 \
--privileged=true --ip6 ${dns_server} --dns ${dns_server} \
-v ${bind9_path}/conf/named.conf:/etc/bind/named.conf \
resystit/bind9:latest >/dev/null
ipv4_addr="$(docker exec bind9 ip addr list eth0 | grep "inet" | awk '$1 == "inet" {print $2}')"
docker exec bind9 ip addr del ${ipv4_addr} dev eth0
docker exec bind9 ip -6 route add ${DNS64_PREFIX_CIDR} via ${LOCAL_NAT64_SERVER}
fi
fi
}
function dind::ensure-nat {
if [[ ${IP_MODE} = "ipv6" ]]; then
if ! docker ps | grep tayga >&/dev/null; then
docker run -d --name tayga --hostname tayga --net "$(dind::net-name)" --label "${DIND_LABEL}" \
--sysctl net.ipv6.conf.all.disable_ipv6=0 --sysctl net.ipv6.conf.all.forwarding=1 \
--privileged=true --ip ${NAT64_V4_SUBNET_PREFIX}.0.200 --ip6 ${LOCAL_NAT64_SERVER} --dns ${REMOTE_DNS64_V4SERVER} --dns ${dns_server} \
-e TAYGA_CONF_PREFIX=${DNS64_PREFIX_CIDR} -e TAYGA_CONF_IPV4_ADDR=${NAT64_V4_SUBNET_PREFIX}.0.200 \
-e TAYGA_CONF_DYNAMIC_POOL=${NAT64_V4_SUBNET_PREFIX}.0.128/25 danehans/tayga:latest >/dev/null
# Need to check/create, as "clean" may remove route
local route="$(ip route | egrep "^${NAT64_V4_SUBNET_PREFIX}.0.128/25")"
if [[ -z "${route}" ]]; then
if [[ ${GCE_HOSTED} ]]; then
docker-machine ssh k8s-dind sudo ip route add ${NAT64_V4_SUBNET_PREFIX}.0.128/25 via ${NAT64_V4_SUBNET_PREFIX}.0.200
elif [[ -z ${using_linuxdocker} ]]; then
:
else
docker run --net=host --rm --privileged ${busybox_image} ip route add ${NAT64_V4_SUBNET_PREFIX}.0.128/25 via ${NAT64_V4_SUBNET_PREFIX}.0.200
fi
fi
fi
fi
}
function dind::run {
local reuse_volume=
if [[ $1 = -r ]]; then
reuse_volume="-r"
shift
fi
local container_name="${1:-}"
local ip="${2:-}"
local node_id="${3:-}"
local portforward="${4:-}"
if [[ $# -gt 4 ]]; then
shift 4
else
shift $#
fi
local ip_arg="--ip"
if [[ ${IP_MODE} = "ipv6" ]]; then
ip_arg="--ip6"
fi
local -a opts=("${ip_arg}" "${ip}" "$@")
local -a args=("systemd.setenv=CNI_PLUGIN=${CNI_PLUGIN}")
args+=("systemd.setenv=IP_MODE=${IP_MODE}")
if [[ ${IP_MODE} = "ipv6" ]]; then
opts+=(--sysctl net.ipv6.conf.all.disable_ipv6=0)
opts+=(--sysctl net.ipv6.conf.all.forwarding=1)
opts+=(--dns ${dns_server})
args+=("systemd.setenv=DNS64_PREFIX_CIDR=${DNS64_PREFIX_CIDR}")
args+=("systemd.setenv=LOCAL_NAT64_SERVER=${LOCAL_NAT64_SERVER}")
# For prefix, if node ID will be in the upper byte, push it over
if [[ $((${POD_NET_SIZE} % 16)) -ne 0 ]]; then
node_id=$(printf "%02x00\n" "${node_id}")
else
if [[ "${POD_NET_PREFIX: -1}" = ":" ]]; then
node_id=$(printf "%x\n" "${node_id}")
else
node_id=$(printf "%02x\n" "${node_id}") # In lower byte, so ensure two chars
fi
fi
fi
if [[ ${POD_NET_PREFIX} ]]; then
args+=("systemd.setenv=POD_NET_PREFIX=${POD_NET_PREFIX}${node_id}")
fi
args+=("systemd.setenv=POD_NET_SIZE=${POD_NET_SIZE}")
args+=("systemd.setenv=USE_HAIRPIN=${USE_HAIRPIN}")
args+=("systemd.setenv=DNS_SVC_IP=${DNS_SVC_IP}")
args+=("systemd.setenv=DNS_SERVICE=${DNS_SERVICE}")
if [[ ! "${container_name}" ]]; then
echo >&2 "Must specify container name"
exit 1
fi
# remove any previously created containers with the same name
docker rm -vf "${container_name}" >&/dev/null || true
if [[ "${portforward}" ]]; then
IFS=';' read -ra array <<< "${portforward}"
for element in "${array[@]}"; do
opts+=(-p "${element}")
done
fi
opts+=(${sys_volume_args[@]+"${sys_volume_args[@]}"})
dind::step "Starting DIND container:" "${container_name}"
if [[ ! ${using_linuxkit} ]]; then
opts+=(-v /boot:/boot -v /lib/modules:/lib/modules)
fi
local volume_name="kubeadm-dind-${container_name}"
dind::ensure-network
dind::ensure-volume ${reuse_volume} "${volume_name}"
dind::ensure-nat
dind::ensure-dns
# TODO: create named volume for binaries and mount it to /k8s
# in case of the source build
# Start the new container.
docker run \
-e IP_MODE="${IP_MODE}" \
-e KUBEADM_SOURCE="${KUBEADM_SOURCE}" \
-e HYPERKUBE_SOURCE="${HYPERKUBE_SOURCE}" \
-d --privileged \
--net "$(dind::net-name)" \
--name "${container_name}" \
--hostname "${container_name}" \
-l "${DIND_LABEL}" \
-v "${volume_name}:/dind" \
${opts[@]+"${opts[@]}"} \
"${DIND_IMAGE}" \
${args[@]+"${args[@]}"}
}
function dind::kubeadm {
local container_id="$1"
shift
dind::step "Running kubeadm:" "$*"
status=0
# See image/bare/wrapkubeadm.
# Capturing output is necessary to grab flags for 'kubeadm join'
kubelet_feature_gates="-e KUBELET_FEATURE_GATES=${KUBELET_FEATURE_GATES}"
if ! docker exec ${kubelet_feature_gates} "${container_id}" /usr/local/bin/wrapkubeadm "$@" 2>&1 | tee /dev/fd/2; then
echo "*** kubeadm failed" >&2
return 1
fi
return ${status}
}
# function dind::bare {
# local container_name="${1:-}"
# if [[ ! "${container_name}" ]]; then
# echo >&2 "Must specify container name"
# exit 1
# fi
# shift
# run_opts=(${@+"$@"})
# dind::run "${container_name}"
# }
function dind::configure-kubectl {
dind::step "Setting cluster config"
local host
host="$(dind::master-ip)"
if [[ ${IP_MODE} = "ipv6" ]]; then
host="[${host}]"
fi
if [[ ${GCE_HOSTED} || ${DOCKER_HOST:-} =~ ^tcp: || ${using_linuxkit} || -n ${using_linuxdocker} ]]; then
if [[ "${IP_MODE}" = "ipv4" ]]; then
host="localhost"
else
host="[::1]"
fi
fi
local context_name cluster_name
context_name="$(dind::context-name)"
cluster_name="$(dind::context-name)"
"${kubectl}" config set-cluster "$cluster_name" \
--server="http://${host}:$(dind::apiserver-port)" \
--insecure-skip-tls-verify=true
"${kubectl}" config set-context "$context_name" --cluster="$cluster_name"
}
force_make_binaries=
function dind::set-master-opts {
master_opts=()
if [[ ${BUILD_KUBEADM} || ${BUILD_HYPERKUBE} ]]; then
# share binaries pulled from the build container between nodes
local dind_k8s_bin_vol_name
dind_k8s_bin_vol_name="dind-k8s-binaries$(dind::clusterSuffix)"
dind::ensure-volume "${dind_k8s_bin_vol_name}"
dind::set-build-volume-args
master_opts+=("${build_volume_args[@]}" -v "${dind_k8s_bin_vol_name}:/k8s")
local -a bins
if [[ ${BUILD_KUBEADM} ]]; then
master_opts+=(-e KUBEADM_SOURCE=build://)
bins+=(cmd/kubeadm)
else
master_opts+=(-e ${KUBEADM_SOURCE})
fi
if [[ ${BUILD_HYPERKUBE} ]]; then
master_opts+=(-e HYPERKUBE_SOURCE=build://)
bins+=(cmd/hyperkube)
fi
if [[ ${force_make_binaries} ]]; then
dind::make-for-linux n "${bins[@]}"
else
dind::ensure-binaries "${bins[@]}"
fi
fi
if [[ ${MASTER_EXTRA_OPTS:-} ]]; then
master_opts+=( ${MASTER_EXTRA_OPTS} )
fi
}
function dind::ensure-dashboard-clusterrolebinding {
local ctx
ctx="$(dind::context-name)"
# 'create' may cause etcd timeout, yet create the clusterrolebinding.
# So use 'apply' to actually create it
"${kubectl}" --context "$ctx" create clusterrolebinding add-on-cluster-admin \
--clusterrole=cluster-admin \
--serviceaccount=kube-system:default \
-o json --dry-run |
docker exec -i "$(dind::master-name)" jq '.apiVersion="rbac.authorization.k8s.io/v1beta1"|.kind|="ClusterRoleBinding"' |
"${kubectl}" --context "$ctx" apply -f -
}
function dind::deploy-dashboard {
dind::step "Deploying k8s dashboard"
dind::retry "${kubectl}" --context "$(dind::context-name)" apply -f "${DASHBOARD_URL}"
# https://kubernetes-io-vnext-staging.netlify.com/docs/admin/authorization/rbac/#service-account-permissions
# Thanks @liggitt for the hint
dind::retry dind::ensure-dashboard-clusterrolebinding
}
function dind::kubeadm-version {
if [[ ${use_k8s_source} ]]; then
(cluster/kubectl.sh version --short 2>/dev/null || true) |
grep Client |
sed 's/^.*: v\([0-9.]*\).*/\1/'
else
docker exec "$(dind::master-name)" \
/bin/bash -c 'kubeadm version -o json | jq -r .clientVersion.gitVersion' |
sed 's/^v\([0-9.]*\).*/\1/'
fi
}
function dind::init {
local -a opts
dind::set-master-opts
local local_host master_name container_id
master_name="$(dind::master-name)"
local_host="$( dind::localhost )"
container_id=$(dind::run "${master_name}" "$(dind::master-ip)" 1 "${local_host}:$(dind::apiserver-port):${INTERNAL_APISERVER_PORT}" ${master_opts[@]+"${master_opts[@]}"})
# FIXME: I tried using custom tokens with 'kubeadm ex token create' but join failed with:
# 'failed to parse response as JWS object [square/go-jose: compact JWS format must have three parts]'
# So we just pick the line from 'kubeadm init' output
# Using a template file in the image to build a kubeadm.conf file and to customize
# it based on CNI plugin, IP mode, and environment settings. User can add additional
# customizations to template and then rebuild the image used (build/build-local.sh).
local pod_subnet_disable="# "
# TODO: May want to specify each of the plugins that require --pod-network-cidr
if [[ ${CNI_PLUGIN} != "bridge" ]]; then
pod_subnet_disable=""
fi
local bind_address="0.0.0.0"
if [[ ${IP_MODE} = "ipv6" ]]; then
bind_address="::"
fi
dind::proxy "$master_name"
dind::custom-docker-opts "$master_name"
# HACK: Indicating mode, so that wrapkubeadm will not set a cluster CIDR for kube-proxy
# in IPv6 (only) mode.
if [[ ${IP_MODE} = "ipv6" ]]; then
docker exec --privileged -i "$master_name" touch /v6-mode
fi
feature_gates="{}"
if [[ ${DNS_SERVICE} == "coredns" ]]; then
# can't just use 'CoreDNS: false' because
# it'll break k8s 1.8. FIXME: simplify
# after 1.8 support is removed
feature_gates="{CoreDNS: true}"
elif docker exec "$master_name" kubeadm init --help 2>&1 | grep -q CoreDNS; then
# FIXME: CoreDNS should be the default in 1.11
feature_gates="{CoreDNS: false}"
fi
component_feature_gates=""
if [ "${FEATURE_GATES}" != "none" ]; then
component_feature_gates="feature-gates: \\\"${FEATURE_GATES}\\\""
fi
apiserver_extra_args=""
for e in $(set -o posix ; set | grep -E "^APISERVER_[a-z_]+=" | cut -d'=' -f 1); do
opt_name=$(echo ${e#APISERVER_} | sed 's/_/-/g')
apiserver_extra_args+=" ${opt_name}: \\\"$(eval echo \$$e)\\\"\\n"
done
controller_manager_extra_args=""
for e in $(set -o posix ; set | grep -E "^CONTROLLER_MANAGER_[a-z_]+=" | cut -d'=' -f 1); do
opt_name=$(echo ${e#CONTROLLER_MANAGER_} | sed 's/_/-/g')
controller_manager_extra_args+=" ${opt_name}: \\\"$(eval echo \$$e)\\\"\\n"
done
scheduler_extra_args=""
for e in $(set -o posix ; set | grep -E "^SCHEDULER_[a-z_]+=" | cut -d'=' -f 1); do
opt_name=$(echo ${e#SCHEDULER_} | sed 's/_/-/g')
scheduler_extra_args+=" ${opt_name}: \\\"$(eval echo \$$e)\\\"\\n"
done
kubeadm_version="$(dind::kubeadm-version)"
api_version="kubeadm.k8s.io/v1alpha2"
if [[ ${kubeadm_version} =~ 1\.(8|9|10)\. ]]; then
api_version="kubeadm.k8s.io/v1alpha1"
fi
docker exec -i "$master_name" bash <<EOF
sed -e "s|{{API_VERSION}}|${api_version}|" \
-e "s|{{ADV_ADDR}}|$(dind::master-ip)|" \
-e "s|{{POD_SUBNET_DISABLE}}|${pod_subnet_disable}|" \
-e "s|{{POD_NETWORK_CIDR}}|${POD_NETWORK_CIDR}|" \
-e "s|{{SVC_SUBNET}}|${SERVICE_CIDR}|" \
-e "s|{{BIND_ADDR}}|${bind_address}|" \
-e "s|{{BIND_PORT}}|${INTERNAL_APISERVER_PORT}|" \
-e "s|{{FEATURE_GATES}}|${feature_gates}|" \
-e "s|{{KUBEADM_VERSION}}|${kubeadm_version}|" \
-e "s|{{COMPONENT_FEATURE_GATES}}|${component_feature_gates}|" \
-e "s|{{APISERVER_EXTRA_ARGS}}|${apiserver_extra_args}|" \
-e "s|{{CONTROLLER_MANAGER_EXTRA_ARGS}}|${controller_manager_extra_args}|" \
-e "s|{{SCHEDULER_EXTRA_ARGS}}|${scheduler_extra_args}|" \
-e "s|{{KUBE_MASTER_NAME}}|${master_name}|" \
/etc/kubeadm.conf.tmpl > /etc/kubeadm.conf
EOF
# TODO: --skip-preflight-checks needs to be replaced with
# --ignore-preflight-errors=all for k8s 1.10+
# (need to check k8s 1.9)
init_args=(--config /etc/kubeadm.conf)
# required when building from source
if [[ ${BUILD_KUBEADM} || ${BUILD_HYPERKUBE} ]]; then
docker exec "$master_name" mount --make-shared /k8s
fi
kubeadm_join_flags="$(dind::kubeadm "${container_id}" init "${init_args[@]}" --skip-preflight-checks "$@" | grep '^ *kubeadm join' | sed 's/^ *kubeadm join //')"
dind::configure-kubectl
dind::start-port-forwarder
}
function dind::create-node-container {
local reuse_volume next_node_index node_ip node_name
reuse_volume=''