forked from Yelp/paasta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dind-cluster-v1.13.sh
executable file
·2427 lines (2244 loc) · 79.1 KB
/
dind-cluster-v1.13.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
# Copyright 2018 Mirantis
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
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 kubeadm-dind-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
# Determine when using Linux and docker daemon running locally
using_local_linuxdocker=
if [[ $(uname) == Linux && -z ${DOCKER_HOST:-} ]]; then
using_local_linuxdocker=1
fi
EMBEDDED_CONFIG=y;DOWNLOAD_KUBECTL=y;DIND_K8S_VERSION=v1.13;DIND_IMAGE_DIGEST=sha256:4c61aa22b080ee3c4e86461afdbdd772ee0d62b01b36784335a1a69096b4b6d9;DIND_COMMIT=76f7b8a5f3966aa80700a8c9f92d23f6936f949b
# dind::localhost provides the local host IP based on the address family used for service subnet.
function dind::localhost() {
if [[ ${SERVICE_NET_MODE} = "ipv6" ]]; then
echo '[::1]'
else
echo '127.0.0.1'
fi
}
# dind::family-for indicates whether the CIDR or IP is for an IPv6 or IPv4 family.
function dind::family-for {
local addr=$1
if [[ "$addr" = *":"* ]]; then
echo "ipv6"
else
echo "ipv4"
fi
}
# dind::cluster-suffix builds a suffix used for resources, based on the DIND_LABEL.
function dind::cluster-suffix {
if [ "$DIND_LABEL" != "$DEFAULT_DIND_LABEL" ]; then
echo "-${DIND_LABEL}"
else
echo ''
fi
}
function dind::net-name {
echo "kubeadm-dind-net$( dind::cluster-suffix )"
}
# dind::add-cluster will inject the cluster ID to the IP address. For IPv4, it is
# assumed that the IP is a /24 with the third part of the address available for cluster ID.
# For IPv6, it is assumed that there is enough space for the cluster to be added, and the
# cluster ID will be added to the 16 bits before the double colon. For example:
#
# 10.192.0.0/24 => 10.192.5.0/24
# fd00:77:20::/64 => fd00:77:20:5::/64
#
# This function is intended to be used for management networks.
#
# TODO: Validate that there is enough space for cluster ID.
# TODO: For IPv6 could get fancy and handle case where cluster ID is placed in upper 8 bits of hextet
# TODO: Consider if want to do /16 for IPv4 management subnet.
#
function dind::add-cluster {
local cidr=$1
local ip_mode=$2
if [[ ${ip_mode} = "ipv4" ]]; then
echo ${cidr} | sed "s/^\([0-9]*\.[0-9]*\.\).*\/24$/\1${CLUSTER_ID}.0\/24/"
else # IPv6
echo ${cidr} | sed "s/^\(.*\)\(\:\:\/[0-9]*\)$/\1:${CLUSTER_ID}\2/"
fi
}
# dind::get-and-validate-cidrs takes a list of CIDRs and validates them based on the ip
# mode, returning them. For IPv4 only and IPv6 only modes, only one CIDR is expected. For
# dual stack, two CIDRS are expected. It verifies that the CIDRs are the right family and
# will use the provided defaults, when CIDRs are missing. For dual-stack, the IPv4 address
# will be first.
#
# For the management network, the cluster ID will be injected into the CIDR. Also, if no
# MGMT_CIDRS value is specified, but the legacy DIND_SUBNET/DIND_SUBNET_SIZE is provided,
# that will be used for the (first) CIDR.
#
# NOTE: It is expected that the CIDR size is /24 for IPv4 management networks.
#
# For pod CIDRs, the size will be increased by 8, to leave room for the node ID to be
# injected into the address.
#
# NOTE: For IPv4, the pod size is expected to be /16 -> /24 in usage.
#
function dind::get-and-validate-cidrs {
IFS=', ' read -r -a cidrs <<< "$1"
IFS=', ' read -r -a defaults <<< "$2"
local is_mgmt=$3
case ${IP_MODE} in
ipv4)
case ${#cidrs[@]} in
0)
cidrs[0]="${defaults[0]}"
;;
1)
;;
*)
echo "ERROR! More than one CIDR provided '$1'"
exit 1
;;
esac
if [[ $( dind::family-for "${cidrs[0]}" ) != "ipv4" ]]; then
echo "ERROR! CIDR must be IPv4 value"
exit 1
fi
if [[ ${is_mgmt} = true ]]; then
cidrs[0]="$( dind::add-cluster "${cidrs[0]}" "${IP_MODE}" )"
fi
;;
ipv6)
case ${#cidrs[@]} in
0)
cidrs[0]="${defaults[0]}"
;;
1)
;;
*)
echo "ERROR! More than one CIDR provided '$1'"
exit 1
;;
esac
if [[ $( dind::family-for "${cidrs[0]}" ) != "ipv6" ]]; then
echo "ERROR! CIDR must be IPv6 value"
exit 1
fi
if [[ ${is_mgmt} = true ]]; then
cidrs[0]="$( dind::add-cluster "${cidrs[0]}" "${IP_MODE}" )"
fi
;;
dual-stack)
case ${#cidrs[@]} in
0)
cidrs[0]="${defaults[0]}"
cidrs[1]="${defaults[1]}"
;;
1)
if [[ $( dind::family-for "${cidrs[0]}" ) = "ipv6" ]]; then
cidrs[1]=${cidrs[0]}
cidrs[0]="${defaults[0]}" # Assuming first default is a V4 address
else
cidrs[1]="${defaults[1]}"
fi
;;
2)
# Force ordering to have V4 address first
if [[ $( dind::family-for "${cidrs[0]}" ) = "ipv6" ]]; then
local temp=${cidrs[0]}
cidrs[0]=${cidrs[1]}
cidrs[1]=${temp}
fi
;;
*)
echo "ERROR! More than two CIDRs provided '$1'"
exit 1
;;
esac
local have_v4=""
local have_v6=""
for cidr in ${cidrs[@]}; do
if [[ $( dind::family-for "${cidr}" ) = "ipv6" ]]; then
have_v6=1
else
have_v4=1
fi
done
if [[ -z ${have_v4} ]]; then
echo "ERROR! Missing IPv4 CIDR in '$1'"
exit 1
fi
if [[ -z ${have_v6} ]]; then
echo "ERROR! Missing IPv6 CIDR in '$1'"
exit 1
fi
if [[ ${is_mgmt} = true ]]; then
cidrs[0]="$( dind::add-cluster "${cidrs[0]}" "${IP_MODE}" )"
cidrs[1]="$( dind::add-cluster "${cidrs[1]}" "${IP_MODE}" )"
fi
;;
esac
echo "${cidrs[@]}"
}
# dind::make-ip-from-cidr strips off the slash and size, and appends the
# interface part to the prefix to form an IP. For IPv4, it strips off the
# fourth part of the prefix, so that it can be replaced. It assumes that the
# resulting prefix will be of sufficient size. It also will use hex for the
# appended part for IPv6, and decimal for IPv4.
#
# fd00:20::/64 -> fd00:20::a
# 10.96.0.0/12 -> 10.96.0.10
#
function dind::make-ip-from-cidr {
prefix="$(echo $1 | sed 's,/.*,,')"
if [[ $( dind::family-for ${prefix} ) == "ipv4" ]]; then
printf "%s%d" $( echo ${prefix} | sed 's/0$//' ) $2
else
printf "%s%x" ${prefix} $2
fi
}
# dind::add-cluster-id-and-validate-nat64-prefix will modify the IPv4 mapping
# subnet prefix, by adding the cluster ID (default 0) to the second octet.
# It will produce an error, if the prefix is not in the 10.0.0.0/8 or
# 172.16.0.0/12 private networks.
function dind::add-cluster-id-and-validate-nat64-prefix {
local parts
IFS="." read -a parts <<<${NAT64_V4_SUBNET_PREFIX}
if [[ ${#parts[@]} -ne 2 ]]; then
echo "ERROR! NAT64_V4_SUBNET_PREFIX must be two octets (have '${NAT64_V4_SUBNET_PREFIX}')"
exit 1
fi
(( parts[1]+=${CLUSTER_ID} ))
NAT64_V4_SUBNET_PREFIX="${parts[0]}.${parts[1]}"
echo "Added cluster ID offset (${CLUSTER_ID}) to NAT64_V4_SUBNET_PREFIX giving prefix '${NAT64_V4_SUBNET_PREFIX}'"
if [[ ${parts[0]} -eq 10 ]]; then
if [[ ${parts[1]} > 253 ]]; then
echo "ERROR! NAT64_V4_SUBNET_PREFIX is too large for 10.0.0.0/8 private net"
exit 1
fi
elif [[ ${parts[0]} -eq 172 ]]; then
if [[ ${parts[1]} -lt 16 || ${parts[1]} -gt 31 ]]; then
echo "ERROR! NAT64_V4_SUBNET_PREFIX is outside of range for 172.16.0.0/12 private net"
exit 1
fi
else
echo "ERROR! NAT64_V4_SUBNET_PREFIX is not in 10.0.0.0/8 or 172.16.0.0/12 private networks"
exit 1
fi
echo "Using NAT64 V4 mapping network prefix: ${NAT64_V4_SUBNET_PREFIX}"
}
# START OF PROCESSING...
IP_MODE="${IP_MODE:-ipv4}" # ipv4, ipv6, dual-stack
# FUTURE: Once dual-stack support is released, check K8s version, and reject for older versions.
if [[ ! ${EMBEDDED_CONFIG:-} ]]; then
source "${DIND_ROOT}/config.sh"
fi
# Multicluster support
# Users can specify a cluster ID number from 1..254, represented as a string.
# This will be used to form resource names "cluster-#", and will be used in the
# management subnet to give unique networks for each cluster. If the cluster ID
# is not specified, or zero, it will be considered a single cluster or the first
# in the multi-cluster. This is the recommended usage.
#
# For legacy support, the user can specify DIND_LABEL, which will be used in the
# resource names. If a cluster ID is specified (a hybrid case, where people are
# using the new method, but want custom names), the resourse name will have the
# suffix "-#" with the cluster ID. If no cluster ID is specified (for backward
# compatibility), then the resource name will be just the DIND_LABEL, and a pseudo-
# random number from 1..13 will be generated for the cluster ID to be used in
# management network. The range is limited, because, in IPv6 mode, the cluster ID
# is used in the NAT64 V4 subnet prefix, which must be in a private network.
# The default is 172.18, so the cluster ID cannot be larger than 13 to guarantee
# a valid value.
#
# To get around that limitation, you can set the cluster ID, in addition to the
# DIND_LABEL, and optionally, change the NAT64_V4_SUBNET_PREFIX value.
#
DEFAULT_DIND_LABEL='mirantis.kubeadm_dind_cluster_runtime'
if [[ -z ${DIND_LABEL+x} ]]; then # No legacy DIND_LABEL set
if [[ -z ${CLUSTER_ID+x} ]]; then # No cluster ID set
DIND_LABEL=${DEFAULT_DIND_LABEL} # Single cluster mode
CLUSTER_ID="0"
else # Have cluster ID
if [[ ${CLUSTER_ID} = "0" ]]; then
DIND_LABEL=${DEFAULT_DIND_LABEL} # Single cluster mode or first cluster of multi-cluster
else
DIND_LABEL="cluster-${CLUSTER_ID}" # Multi-cluster
fi
fi
else # Legacy DIND_LABEL set for multi-cluster
if [[ -z ${CLUSTER_ID+x} ]]; then # No cluster ID set, make one from 1..13, but don't use in resource names
CLUSTER_ID="$(( ($RANDOM % 12) + 1 ))"
else
if [[ ${CLUSTER_ID} = "0" ]]; then
CLUSTER_ID="$(( ($RANDOM % 12) + 1 ))" # Force a pseudo-random cluster for additional legacy cluster
else
DIND_LABEL="${DIND_LABEL}-${CLUSTER_ID}"
fi
fi
fi
CNI_PLUGIN="${CNI_PLUGIN:-bridge}"
GCE_HOSTED="${GCE_HOSTED:-}"
DIND_ALLOW_AAAA_USE="${DIND_ALLOW_AAAA_USE:-}" # Default is to use DNS64 always for IPv6 mode
KUBE_ROUTER_VERSION="${KUBE_ROUTER_VERSION:-v0.2.0}"
# Use legacy DIND_SUBNET/DIND_SUBNET_SIZE, only if MGMT_CIDRS is not set.
legacy_mgmt_cidr=""
if [[ ${DIND_SUBNET:-} && ${DIND_SUBNET_SIZE:-} ]]; then
legacy_mgmt_cidr="${DIND_SUBNET}/${DIND_SUBNET_SIZE}"
fi
if [[ ${IP_MODE} = "dual-stack" ]]; then
mgmt_net_defaults="10.192.0.0/24, fd00:20::/64"
KUBE_RSYNC_ADDR="${KUBE_RSYNC_ADDR:-::1}"
SERVICE_CIDR="${SERVICE_CIDR:-fd00:30::/110}" # Will default to IPv6 service net family
pod_net_defaults="10.244.0.0/16, fd00:40::/72"
USE_HAIRPIN="${USE_HAIRPIN:-true}" # Default is to use hairpin for dual-stack
DIND_ALLOW_AAAA_USE=true # Forced, so can access external hosts via 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
elif [[ ${IP_MODE} = "ipv6" ]]; then
mgmt_net_defaults="fd00:20::/64"
KUBE_RSYNC_ADDR="${KUBE_RSYNC_ADDR:-::1}"
SERVICE_CIDR="${SERVICE_CIDR:-fd00:30::/110}"
pod_net_defaults="fd00:40::/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 # IPv4 mode
mgmt_net_defaults="10.192.0.0/24"
KUBE_RSYNC_ADDR="${KUBE_RSYNC_ADDR:-127.0.0.1}"
SERVICE_CIDR="${SERVICE_CIDR:-10.96.0.0/12}"
pod_net_defaults="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
pod_net_defaults="192.168.0.0/16"
fi
fi
IFS=' ' read -r -a mgmt_net_cidrs <<<$( dind::get-and-validate-cidrs "${MGMT_CIDRS:-${legacy_mgmt_cidr}}" "${mgmt_net_defaults[@]}" true )
REMOTE_DNS64_V4SERVER="${REMOTE_DNS64_V4SERVER:-8.8.8.8}"
if [[ ${IP_MODE} == "ipv6" ]]; then
# Uses local DNS64 container
dns_server="$( dind::make-ip-from-cidr ${mgmt_net_cidrs[0]} 0x100 )"
DNS64_PREFIX="${DNS64_PREFIX:-fd00:10:64:ff9b::}"
DNS64_PREFIX_SIZE="${DNS64_PREFIX_SIZE:-96}"
DNS64_PREFIX_CIDR="${DNS64_PREFIX}/${DNS64_PREFIX_SIZE}"
LOCAL_NAT64_SERVER="$( dind::make-ip-from-cidr ${mgmt_net_cidrs[0]} 0x200 )"
NAT64_V4_SUBNET_PREFIX="${NAT64_V4_SUBNET_PREFIX:-172.18}"
dind::add-cluster-id-and-validate-nat64-prefix
else
dns_server="${REMOTE_DNS64_V4SERVER}"
fi
SERVICE_NET_MODE="$( dind::family-for ${SERVICE_CIDR} )"
DNS_SVC_IP="$( dind::make-ip-from-cidr ${SERVICE_CIDR} 10 )"
ETCD_HOST="${ETCD_HOST:-$( dind::localhost )}"
IFS=' ' read -r -a pod_net_cidrs <<<$( dind::get-and-validate-cidrs "${POD_NETWORK_CIDR:-}" "${pod_net_defaults[@]}" false )
declare -a pod_prefixes
declare -a pod_sizes
# Extract the prefix and size from the provided pod CIDR(s), based on the IP mode of each. The
# size will be increased by 8, to make room for the node ID to be added to the prefix later.
# Bridge and PTP plugins can process IPv4 and IPv6 pod CIDRs, other plugins must be IPv4 only.
for pod_cidr in "${pod_net_cidrs[@]}"; do
if [[ $( dind::family-for "${pod_cidr}" ) = "ipv4" ]]; then
actual_size=$( echo ${pod_cidr} | sed 's,.*/,,' )
if [[ ${actual_size} -ne 16 ]]; then
echo "ERROR! For IPv4 CIDRs, the size must be /16. Have '${pod_cidr}'"
exit 1
fi
pod_sizes+=( 24 )
pod_prefixes+=( "$(echo ${pod_cidr} | sed 's/^\([0-9]*\.[0-9]*\.\).*/\1/')" )
else # IPv6
if [[ ${CNI_PLUGIN} != "bridge" && ${CNI_PLUGIN} != "ptp" ]]; then
echo "ERROR! IPv6 pod networks are only supported by bridge and PTP CNI plugins"
exit 1
fi
# There are several cases to address. First, is normal split of prefix and size:
# fd00:10:20:30::/64 ---> fd00:10:20:30: /72
#
# Second, is when the prefix needs to be padded, so that node ID can be added later:
# fd00:10::/64 ---> fd00:10:0:0: /72
#
# Third, is when the low order part of the address, must be removed for the prefix,
# as the node ID will be placed in the lower byte:
# fd00:10:20:30:4000::/72 ---> fd00:10:20:30:40 /80
#
# We will attempt to check for three error cases. One is when the address part is
# way too big for the size specified:
# fd00:10:20:30:40::/48 ---> fd00:10:20: /56 desired, but conflict with 30:40:
#
# Another is when the address part, once trimmed for the size, would loose info:
# fd00:10:20:1234::/56 ---> fd00:10:20:12 /64, but lost 34:, which conflicts
#
# Lastly, again, trimming would leave high byte in hextet, conflicting with
# the node ID:
# fd00:10:20:30:1200::/64 ---> fd00:10:20:30:12 /72, but 12 conflicts
#
# Note: later, the node ID will be appended to the prefix generated.
#
cluster_size="$(echo ${pod_cidr} | sed 's,.*::/,,')"
pod_sizes+=( $((${cluster_size}+8)) )
pod_prefix="$(echo ${pod_cidr} | sed 's,::/.*,:,')"
num_colons="$(grep -o ":" <<< "${pod_prefix}" | wc -l)"
need_zero_pads=$((${cluster_size}/16))
if [[ ${num_colons} -gt $((need_zero_pads + 1)) ]]; then
echo "ERROR! Address part of CIDR (${pod_prefix}) is too large for /${cluster_size}"
exit 1
fi
if [[ ${num_colons} -gt ${need_zero_pads} ]]; then
# Will be replacing lowest byte with node ID, so pull off lower byte and colon
if [[ ${pod_prefix: -3} != "00:" ]]; then # last byte is not zero
echo "ERROR! Cannot trim address part of CIDR (${pod_prefix}) to fit in /${cluster_size}"
exit 1
fi
pod_prefix=${pod_prefix::-3}
if [[ $(( ${cluster_size} % 16 )) -eq 0 && $( ${pod_prefix: -1} ) != ":" ]]; then # should not be upper byte for this size CIDR
echo "ERROR! Trimmed address part of CIDR (${pod_prefix}) is still too large for /${cluster_size}"
exit 1
fi
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_prefix+="0:"
((num_colons++))
done
pod_prefixes+=( "${pod_prefix}" )
fi
done
DIND_IMAGE_BASE="${DIND_IMAGE_BASE:-mirantis/kubeadm-dind-cluster}"
if [[ ${DIND_COMMIT:-} ]]; then
if [[ ${DIND_COMMIT} = current ]]; then
DIND_COMMIT="$(cd "${DIND_ROOT}"; git rev-parse HEAD)"
fi
DIND_K8S_VERSION="${DIND_K8S_VERSION:-v1.13}"
DIND_IMAGE="${DIND_IMAGE_BASE}:${DIND_COMMIT}-${DIND_K8S_VERSION}"
else
DIND_IMAGE="${DIND_IMAGE:-${DIND_IMAGE_BASE}:local}"
fi
if [[ ${DIND_IMAGE_DIGEST:-} ]]; then
DIND_IMAGE="${DIND_IMAGE}@${DIND_IMAGE_DIGEST}"
fi
BUILD_KUBEADM="${BUILD_KUBEADM:-}"
BUILD_HYPERKUBE="${BUILD_HYPERKUBE:-}"
if [[ ! -z ${DIND_K8S_BIN_DIR:-} ]]; then
BUILD_KUBEADM=""
BUILD_HYPERKUBE=""
fi
KUBEADM_SOURCE="${KUBEADM_SOURCE-}"
HYPERKUBE_SOURCE="${HYPERKUBE_SOURCE-}"
NUM_NODES=${NUM_NODES:-2}
EXTRA_PORTS="${EXTRA_PORTS:-}"
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:-coredns}"
DIND_STORAGE_DRIVER="${DIND_STORAGE_DRIVER:-overlay2}"
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
# comma-separated custom network(s) for cluster nodes to join
DIND_CUSTOM_NETWORKS="${DIND_CUSTOM_NETWORKS:-}"
SKIP_DASHBOARD="${SKIP_DASHBOARD:-}"
# you can set special value 'none' not to set any FEATURE_GATES / KUBELET_FEATURE_GATES.
FEATURE_GATES="${FEATURE_GATES:-none}"
KUBELET_FEATURE_GATES="${KUBELET_FEATURE_GATES:-DynamicKubeletConfig=true}"
ENABLE_CEPH="${ENABLE_CEPH:-}"
DIND_CRI="${DIND_CRI:-docker}"
case "${DIND_CRI}" in
docker)
CRI_SOCKET=/var/run/dockershim.sock
;;
containerd)
CRI_SOCKET=/var/run/containerd/containerd.sock
;;
*)
echo >&2 "Bad DIND_CRI. Please specify 'docker' or 'containerd'"
;;
esac
# TODO: Test multi-cluster for IPv6, before enabling
if [[ "${DIND_LABEL}" != "${DEFAULT_DIND_LABEL}" && "${IP_MODE}" == 'dual-stack' ]]; then
echo "Multiple parallel clusters currently not supported for dual-stack mode" >&2
exit 1
fi
# 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 [[ ! ${DOWNLOAD_KUBECTL:-} ]] && ! 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.10.5"
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::cluster-suffix )"
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
if [[ ! ${DOWNLOAD_KUBECTL:-} ]]; then
return 0
fi
export PATH="${KUBECTL_DIR}:$PATH"
eval "$(docker run --entrypoint /bin/bash --rm "${DIND_IMAGE}" -c "cat /dind-env")"
if [ $(uname) = Darwin ]; then
kubectl_sha1="${KUBECTL_DARWIN_SHA1}"
kubectl_url="${KUBECTL_DARWIN_URL}"
else
kubectl_sha1="${KUBECTL_LINUX_SHA1}"
kubectl_url="${KUBECTL_LINUX_URL}"
fi
local link_target="kubectl-${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}" "${kubectl_url}"
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
dind::step "Building kubectl"
dind::step "+ make WHAT=cmd/kubectl"
make WHAT=cmd/kubectl 2>&1 | dind::filter-make-output
else
dind::make-for-linux y cmd/kubectl
fi
}
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
}
# dind::ensure-network creates the management network for the cluster. For IPv4
# only it will have the management network CIDR. For IPv6 only, it will have
# the IPv6 management network CIDR and the NAT64 V4 mapping network CIDR. For
# dual stack, it will have the IPv4 and IPv6 management CIDRs. Each of the
# management networks (not the NAT64 network) will have a gateway specified.
#
function dind::ensure-network {
if ! docker network inspect $(dind::net-name) >&/dev/null; then
local -a args
for cidr in "${mgmt_net_cidrs[@]}"; do
if [[ $( dind::family-for ${cidr} ) = "ipv6" ]]; then
args+=(--ipv6)
fi
args+=(--subnet="${cidr}")
local gw=$( dind::make-ip-from-cidr ${cidr} 1 )
args+=(--gateway="${gw}")
done
if [[ ${IP_MODE} = "ipv6" ]]; then
# Need second network for NAT64 V4 mapping network
args+=(--subnet=${NAT64_V4_SUBNET_PREFIX}.0.0/16)
fi
docker network create ${args[@]} $(dind::net-name) >/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
fi
dind::create-volume "${name}"
}
function dind::ensure-dns {
if [[ ${IP_MODE} = "ipv6" ]]; then
local dns64_name="bind9$( dind::cluster-suffix )"
if ! docker inspect ${dns64_name} >&/dev/null; then
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
read -r -d '' bind9_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
docker run -d --name ${dns64_name} --hostname ${dns64_name} --net "$(dind::net-name)" --label "dind-support$( dind::cluster-suffix )" \
--sysctl net.ipv6.conf.all.disable_ipv6=0 --sysctl net.ipv6.conf.all.forwarding=1 \
--privileged=true --ip6 ${dns_server} --dns ${dns_server} \
-e bind9_conf="${bind9_conf}" \
diverdane/bind9:latest /bin/sh -c 'echo "${bind9_conf}" >/named.conf && named -c /named.conf -g -u named' >/dev/null
ipv4_addr="$(docker exec ${dns64_name} ip addr list eth0 | grep "inet" | awk '$1 == "inet" {print $2}')"
docker exec ${dns64_name} ip addr del ${ipv4_addr} dev eth0
docker exec ${dns64_name} ip -6 route add ${DNS64_PREFIX_CIDR} via ${LOCAL_NAT64_SERVER}
fi
fi
}
function dind::ensure-nat {
if [[ ${IP_MODE} = "ipv6" ]]; then
local nat64_name="tayga$( dind::cluster-suffix )"
if ! docker ps | grep ${nat64_name} >&/dev/null; then
docker run -d --name ${nat64_name} --hostname ${nat64_name} --net "$(dind::net-name)" --label "dind-support$( dind::cluster-suffix )" \
--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
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
}
function dind::run {
local reuse_volume=
if [[ $1 = -r ]]; then
reuse_volume="-r"
shift
fi
local container_name="${1:-}"
local node_id=${2:-0}
local portforward="${3:-}"
if [[ $# -gt 3 ]]; then
shift 3
else
shift $#
fi
local -a opts=("$@")
local ip_mode="--ip"
for cidr in "${mgmt_net_cidrs[@]}"; do
if [[ $( dind::family-for ${cidr} ) = "ipv6" ]]; then
ip_mode="--ip6"
fi
opts+=("${ip_mode}" "$( dind::make-ip-from-cidr ${cidr} $((${node_id}+1)) )")
done
opts+=("$@")
local -a args=("systemd.setenv=CNI_PLUGIN=${CNI_PLUGIN}")
args+=("systemd.setenv=IP_MODE=${IP_MODE}")
args+=("systemd.setenv=DIND_STORAGE_DRIVER=${DIND_STORAGE_DRIVER}")
args+=("systemd.setenv=DIND_CRI=${DIND_CRI}")
if [[ ${IP_MODE} != "ipv4" ]]; then
opts+=(--sysctl net.ipv6.conf.all.disable_ipv6=0)
opts+=(--sysctl net.ipv6.conf.all.forwarding=1)
fi
if [[ ${IP_MODE} = "ipv6" ]]; then
opts+=(--dns ${dns_server})
args+=("systemd.setenv=DNS64_PREFIX_CIDR=${DNS64_PREFIX_CIDR}")
args+=("systemd.setenv=LOCAL_NAT64_SERVER=${LOCAL_NAT64_SERVER}")
fi
declare -a pod_nets
local i=0
if [[ ${IP_MODE} = "ipv4" || ${IP_MODE} = "dual-stack" ]]; then
pod_nets+=("${pod_prefixes[$i]}${node_id}")
i=$((i+1))
fi
if [[ ${IP_MODE} = "ipv6" || ${IP_MODE} = "dual-stack" ]]; then
# For prefix, if node ID will be in the upper byte, push it over
if [[ $((${pod_sizes[$i]} % 16)) -ne 0 ]]; then
n_id=$(printf "%02x00\n" "${node_id}")
else
if [[ "${pod_prefixes[$i]: -1}" = ":" ]]; then
n_id=$(printf "%x\n" "${node_id}")
else
n_id=$(printf "%02x\n" "${node_id}") # In lower byte, so ensure two chars
fi
fi
pod_nets+=("${pod_prefixes[$i]}${n_id}")
fi
args+=("systemd.setenv=POD_NET_PREFIX=\"${pod_nets[0]}\"")
args+=("systemd.setenv=POD_NET_SIZE=\"${pod_sizes[0]}\"")