Repository navigation
Expand file tree
/
Copy pathdind-cluster.sh
More file actions
executable file
·859 lines (719 loc) · 24.9 KB
/
Copy pathdind-cluster.sh
File metadata and controls
executable file
·859 lines (719 loc) · 24.9 KB
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
10BC8
div>
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
#!/bin/bash
# WARNING: The script modifies the host that docker is running on. It
# attempts to load the overlay and openvswitch modules. If this modification
# is undesirable consider running docker in a VM.
#
# Overview
# ========
#
# This script manages the lifecycle of an openshift dev cluster
# deployed to docker-in-docker containers. Once 'start' has been used
# to successfully create a dind cluster, 'docker exec' can be used to
# access the created containers (named
# openshift-{master,node-1,node-2}) as if they were VMs.
#
# Dependencies
# ------------
#
# This script has been tested on Fedora 24, but should work on any
# release. Docker is assumed to be installed. At this time,
# boot2docker is not supported.
#
# SELinux
# -------
#
# Docker-in-docker's use of volumes is not compatible with selinux set
# to enforcing mode. Set selinux to permissive or disable it
# entirely.
#
# OpenShift Configuration
# -----------------------
#
# By default, a dind openshift cluster stores its configuration
# (openshift.local.*) in /tmp/openshift-dind-cluster/openshift. It's
# possible to run multiple dind clusters simultaneously by overriding
# the instance prefix. The following command would ensure
# configuration was stored at /tmp/openshift-dind/cluster/my-cluster:
#
# OPENSHIFT_CLUSTER_ID=my-cluster hack/dind-cluster.sh [command]
#
# Suggested Workflow
# ------------------
#
# When making changes to the deployment of a dind cluster or making
# breaking golang changes, the -r argument to the start command will
# ensure that an existing cluster is cleaned up before deploying a new
# cluster.
#
# Running Tests
# -------------
#
# The extended tests can be run against a dind cluster as follows:
#
# OPENSHIFT_CONFIG_ROOT=dind test/extended/networking.sh
set -o errexit
set -o nounset
set -o pipefail
source "$(dirname "${BASH_SOURCE}")/lib/init.sh"
source "${OS_ROOT}/images/dind/node/openshift-dind-lib.sh"
function start() {
local origin_root=$1
local ovn_root=$2
local config_root=$3
local deployed_config_root=$4
local cluster_id=$5
local network_plugin=$6
local container_runtime=$7
local wait_for_cluster=$8
local node_count=$9
local additional_args=${10}
# docker-in-docker's use of volumes is not compatible with SELinux
check-selinux
runtime_endpoint=
if [[ "${container_runtime}" = "dockershim" ]]; then
# dockershim is default and doesn't need an endpoint path
runtime_endpoint=
elif [[ "${container_runtime}" = "crio" ]]; then
runtime_endpoint="/var/run/crio.sock"
else
>&2 echo "Invalid container runtime: ${container_runtime}"
exit 1
fi
echo "Starting dind cluster '${cluster_id}' with plugin '${network_plugin}' and runtime '${container_runtime}'"
# Error if a cluster is already configured
check-no-containers "start"
# Ensuring compatible host configuration
#
# Running in a container ensures that the docker host will be affected even
# if docker is running remotely. The openshift/dind-node image was chosen
# due to its having sysctl installed.
${DOCKER_CMD} run --privileged --net=host --rm -v /lib/modules:/lib/modules \
openshift/dind-node bash -e -c \
'/usr/sbin/modprobe openvswitch;
/usr/sbin/modprobe overlay 2> /dev/null || true;'
# Initialize the cluster config path
mkdir -p "${config_root}"
echo "OPENSHIFT_NETWORK_PLUGIN=${network_plugin}" > "${config_root}/dind-env"
echo "OPENSHIFT_ADDITIONAL_ARGS='${additional_args}'" >> "${config_root}/dind-env"
copy-runtime "${origin_root}" "${config_root}/"
echo "OPENSHIFT_CONTAINER_RUNTIME=${container_runtime}" >> "${config_root}/dind-env"
echo "OPENSHIFT_REMOTE_RUNTIME_ENDPOINT=${runtime_endpoint}" >> "${config_root}/dind-env"
ovn_kubernetes=
if [[ -d "${ovn_root}" ]]; then
copy-ovn-runtime "${ovn_root}" "${config_root}/"
ovn_kubernetes=1
fi
echo "OPENSHIFT_OVN_KUBERNETES=${ovn_kubernetes}" >> "${config_root}/dind-env"
# Create containers
start-container "${config_root}" "${deployed_config_root}" "${MASTER_IMAGE}" "${MASTER_NAME}"
for name in "${NODE_NAMES[@]}"; do
start-container "${config_root}" "${deployed_config_root}" "${NODE_IMAGE}" "${name}"
done
local rc_file="dind-${cluster_id}.rc"
local admin_config
admin_config="$(get-admin-config "${CONFIG_ROOT}")"
local bin_path
bin_path="$(os::build::get-bin-output-path "${OS_ROOT}")"
cat >"${rc_file}" <<EOF
export KUBECONFIG="${admin_config}"
export PATH="\$PATH:${bin_path}"
export OPENSHIFT_CLUSTER_ID="${cluster_id}"
export OPENSHIFT_CONFIG_ROOT="${config_root}"
for file in "${origin_root}/contrib/completions/bash"/* ; do
source "\${file}"
done
EOF
if [[ -n "${wait_for_cluster}" ]]; then
wait-for-cluster "${config_root}" "${node_count}"
fi
if [[ "${KUBECONFIG:-}" != "${admin_config}" ||
":${PATH}:" != *":${bin_path}:"* ]]; then
echo ""
echo "Before invoking the openshift cli, make sure to source the
cluster's rc file to configure the bash environment:
$ . ${rc_file}
$ oc get nodes
"
fi
}
function add-node () {
local config_root=$1
local deployed_config_root=$2
local cluster_id=$3
local wait_for_cluster=$4
echo "Adding node to dind cluster '${cluster_id}'"
# Error if a cluster is not already configured
check-containers "add-node"
# Find the first free number
local first_free=1
for cid in $( ${DOCKER_CMD} ps -a --filter "name=${NODE_PREFIX}" --format "{{.Names}}" | sort -V ); do
if [[ "${cid}" != "${NODE_PREFIX}${first_free}" ]]; then
break
fi
(( first_free += 1 ))
done
local node_name="${NODE_PREFIX}${first_free}"
start-container "${config_root}" "${deployed_config_root}" "${NODE_IMAGE}" "${node_name}"
echo
echo "Added node '${node_name}'"
if [[ -n "${wait_for_cluster}" ]]; then
wait-for-cluster "${config_root}" "$(count-nodes)"
fi
}
function start-container() {
local config_root=$1
local deployed_config_root=$2
local image=$3
local name=$4
local volumes run_cmd
volumes=""
${DOCKER_CMD} run -dt ${volumes} -v "${config_root}:${deployed_config_root}" --privileged \
--name="${name}" --hostname="${name}" "${image}" > /dev/null
}
function delete-node () {
local config_root=$1
local cluster_id=$2
local node_name=$3
echo "Removing node '${node_name}' from dind cluster '${cluster_id}'"
sudo echo -n
# Error if a cluster is not already configured
check-containers "delete-node"
check-containers "delete-node" "-f name=${node_name}" "No node named '${node_name}'"
# Remove it from docker
${DOCKER_CMD} rm -f "${node_name}"
clean-orphaned-storage
# Remove the stale config
sudo rm -rf "${config_root}"/openshift.local.config/node-"${node_name}"
# Delete it from openshift
local kubeconfig oc
kubeconfig="$(get-admin-config "${config_root}")"
oc="$(os::util::find::built_binary oc) --config=${kubeconfig}"
${oc} delete node "${node_name}"
}
function stop() {
local config_root=$1
local cluster_id=$2
echo "Stopping dind cluster '${cluster_id}'"
sudo echo -n
# Delete the containers
for cid in $( ${DOCKER_CMD} ps -qa --filter "name=${MASTER_NAME}|${NODE_PREFIX}" ); do
${DOCKER_CMD} rm -f "${cid}" > /dev/null
done
# Cleaning up configuration to avoid conflict with a future cluster
# The container will have created configuration as root
sudo rm -rf "${config_root}"/openshift.local.etcd
sudo rm -rf "${config_root}"/openshift.local.config
clean-orphaned-storage
}
function copy-image() {
local cluster_id=$1
local image=$2
echo "Installing image '${image}' on all nodes of dind cluster '${cluster_id}'"
# Error if a cluster is not configured
check-containers "copy-image"
check-no-containers "copy-image" "-f status=exited" "Paused parts"
# Make a temp file with a descriptor attached
tmpfile=$(mktemp /tmp/docker-image.XXXXXX)
exec 3>"$tmpfile"
rm "$tmpfile"
${DOCKER_CMD} save "${image}" >&3
for cid in $( ${DOCKER_CMD} ps -qa --filter "name=${NODE_PREFIX}" ); do
cat /dev/fd/3 | ${DOCKER_CMD} exec -i "${cid}" docker load &
done
wait
}
function clean-orphaned-storage() {
# Cleanup orphaned volumes
#
# See: https://github.com/jpetazzo/dind#important-warning-about-disk-usage
#
for volume in $( ${DOCKER_CMD} volume ls -qf dangling=true ); do
${DOCKER_CMD} volume rm "${volume}" > /dev/null
done
}
function pause() {
local cluster_id=$1
echo "Pausing dind cluster '${cluster_id}'"
# Error if a cluster is not configured
check-containers "pause"
for cid in $( ${DOCKER_CMD} ps -qa --filter "name=${MASTER_NAME}|${NODE_PREFIX}" ); do
${DOCKER_CMD} stop "${cid}" > /dev/null &
done
wait
}
function resume() {
local config_root=$1
local cluster_id=$2
local wait_for_cluster=$3
echo "Resuming dind cluster '${cluster_id}'"
sudo echo -n
# Error if there are no containers configured or if something isn't paused
check-containers "resume"
check-containers "resume" "-f status=exited" "No paused parts"
# Remove the old config and the serving certificates in case the IP changed... they will get regenerated
local local_config
local_config="${config_root}/openshift.local.config"
sudo rm -f "${local_config}"/*/master.server.*
sudo rm -f "${local_config}"/*/etcd.server.*
sudo rm -f "${local_config}"/*/*.kubeconfig
sudo rm -f "${local_config}"/*/master-config.yaml
# Remove the node configs so that the nodes re-make their configuration
sudo rm -f "${config_root}"/openshift.local.config/node-openshift-*/node-config.yaml
# Do restarts in case they were already running (because we just nuked the config)
echo " Starting master"
for cid in $( ${DOCKER_CMD} ps -qa --filter "name=${MASTER_NAME}" ); do
${DOCKER_CMD} restart "${cid}" > /dev/null
done
echo " Starting nodes"
for cid in $( ${DOCKER_CMD} ps -qa --filter "name=${NODE_PREFIX}" ); do
${DOCKER_CMD} restart "${cid}" > /dev/null
done
if [[ -n "${wait_for_cluster}" ]]; then
wait-for-cluster "${config_root}" "$(count-nodes)"
fi
}
function run-on-nodes() {
local filter=$1
shift
local command=("$@")
for cid in $( ${DOCKER_CMD} ps -qa --filter "name=${filter}" ); do
${DOCKER_CMD} exec "${cid}" "${command[@]}"
done
}
function cluster-ps() {
${DOCKER_CMD} ps -a --filter "name=${MASTER_NAME}|${NODE_PREFIX}"
}
function count-masters() {
local filter=${1:-}
count-containers "${MASTER_NAME}" "${filter}"
}
function count-nodes() {
local filter=${1:-}
count-containers "${NODE_PREFIX}" "${filter}"
}
function count-containers() {
local name=$1
local filter=${2:-}
local count=0
for cid in $( ${DOCKER_CMD} ps -qa --filter "name=${name}" $filter); do
(( count += 1 ))
done
echo "$count"
}
function check-no-containers {
local operation=$1
local filter=${2:-}
local message="${3:-Existing cluster parts}"
local existing_nodes existing_master
existing_nodes=$(count-nodes "${filter}")
existing_master=$(count-masters "${filter}")
if (( existing_nodes > 0 || existing_master > 0 )); then
echo
echo "ERROR: Can't ${operation}. ${message} (${existing_master} existing master or ${existing_nodes} existing nodes)"
exit 1
fi
}
function check-containers {
local operation=$1
local filter=${2:-}
local message="${3:-No existing cluster parts}"
local existing_nodes existing_master
existing_nodes=$(count-nodes "${filter}")
existing_master=$(count-masters "${filter}")
if (( existing_nodes == 0 && existing_master == 0 )); then
echo
echo "ERROR: Can't ${operation}. ${message} (${existing_master} existing master or ${existing_nodes} existing nodes)"
exit 1
fi
}
function refresh() {
local origin_root=$1
local config_root=$2
local cluster_id=$3
local wait_for_cluster=$4
echo "Refreshing dind cluster '${cluster_id}'"
# Error if a cluster is not configured or if there are paused parts
check-containers "refresh"
check-no-containers "refresh" "-f status=exited" "Paused parts"
# Stop the master and node openshift processes
echo " Stopping master and node processes in cluster '${cluster_id}'..."
run-on-nodes "${MASTER_NAME}" systemctl stop openshift-master.service
run-on-nodes "${MASTER_NAME}|${NODE_PREFIX}" systemctl stop openshift-node.service
# Wipe the state
run-on-nodes "${NODE_PREFIX}" ovs-ofctl del-flows -O OpenFlow13 br0
# Copy over the new openshift binaries
echo " Copying new runtime to cluster '${cluster_id}'..."
copy-runtime "${origin_root}" "${config_root}/"
# Restart the master and node openshift processes
echo " Restarting master and node processes in cluster '${cluster_id}'..."
run-on-nodes "${MASTER_NAME}" systemctl start openshift-master.service
run-on-nodes "${MASTER_NAME}|${NODE_PREFIX}" systemctl start openshift-node.service
if [[ -n "${wait_for_cluster}" ]]; then
wait-for-cluster "${config_root}" "$(count-nodes)"
fi
}
function check-selinux() {
if [[ "$(getenforce)" = "Enforcing" ]]; then
>&2 echo "Error: This script is not compatible with SELinux enforcing mode."
exit 1
fi
}
function get-network-plugin() {
local plugin=$1
local subnet_plugin="redhat/openshift-ovs-subnet"
local multitenant_plugin="redhat/openshift-ovs-multitenant"
local networkpolicy_plugin="redhat/openshift-ovs-networkpolicy"
local ovn_plugin="ovn"
local default_plugin="${multitenant_plugin}"
if [[ "${plugin}" = "subnet" || "${plugin}" = "${subnet_plugin}" ]]; then
echo "${subnet_plugin}"
elif [[ "${plugin}" = "multitenant" || "${plugin}" = "${multitenant_plugin}" ]]; then
echo "${multitenant_plugin}"
elif [[ "${plugin}" = "networkpolicy" || "${plugin}" = "${networkpolicy_plugin}" ]]; then
echo "${networkpolicy_plugin}"
elif [[ "${plugin}" = "ovn" ]]; then
echo "${ovn_plugin}"
elif [[ "${plugin}" = "cni" ]]; then
echo "cni"
elif [[ -n "${plugin}" ]]; then
>&2 echo "Invalid network plugin: ${plugin}"
exit 1
else
echo "${default_plugin}"
fi
}
function get-docker-ip() {
local cid=$1
${DOCKER_CMD} inspect --format '{{ .NetworkSettings.IPAddress }}' "${cid}"
}
function get-admin-config() {
local config_root=$1
echo "${config_root}/openshift.local.config/master/admin.kubeconfig"
}
function copy-runtime() {
local origin_root=$1
local target=$2
cp "$(os::util::find::built_binary openshift)" "${target}"
cp "$(os::util::find::built_binary host-local)" "${target}"
cp "$(os::util::find::built_binary loopback)" "${target}"
cp "$(os::util::find::built_binary sdn-cni-plugin)" "${target}/openshift-sdn"
}
function copy-ovn-runtime() {
local ovn_root=$1
local target=$2
local ovn_go_controller_built_binaries_path="${ovn_root}/go-controller/_output/go/bin"
cp "${ovn_go_controller_built_binaries_path}/ovnkube" "${target}"
cp "${ovn_go_controller_built_binaries_path}/ovn-kube-util" "${target}"
local ovn_k8s_binaries_path="${ovn_root}/bin"
cp "${ovn_k8s_binaries_path}/ovn-k8s-cni-overlay" "${target}"
cp "${ovn_k8s_binaries_path}/ovn-k8s-gateway-helper" "${target}"
cp "${ovn_k8s_binaries_path}/ovn-k8s-overlay" "${target}"
cp "${ovn_k8s_binaries_path}/ovn-k8s-util" "${target}"
cp "${ovn_k8s_binaries_path}/ovn-k8s-watcher" "${target}"
local ovn_k8s_python_module_path="${ovn_root}/ovn_k8s"
cp -R "${ovn_k8s_python_module_path}" "${target}/"
}
function wait-for-cluster() {
local config_root=$1
local expected_node_count=$2
# Make sure there is a cluster configured
check-containers "wait-for-cluster"
# Increment the node count to ensure that the sdn node on the master also reports readiness
(( expected_node_count += 1 ))
local kubeconfig oc
kubeconfig="$(get-admin-config "${config_root}")"
oc="$(os::util::find::built_binary oc)"
# wait for healthz to report ok before trying to get nodes
os::util::wait-for-condition "ok" "${oc} get --config=${kubeconfig} --raw=/healthz" "120"
local msg condition timeout
msg="${expected_node_count} nodes to report readiness"
condition="nodes-are-ready ${kubeconfig} ${oc} ${expected_node_count}"
timeout=120
os::util::wait-for-condition "${msg}" "${condition}" "${timeout}"
}
function nodes-are-ready() {
local kubeconfig=$1
local oc=$2
local expected_node_count=$3
# TODO - do not count any node whose name matches the master node e.g. 'node-master'
read -d '' template <<'EOF'
{{range $item := .items}}
{{range .status.conditions}}
{{if eq .type "Ready"}}
{{if eq .status "True"}}
{{printf "%s\\n" $item.metadata.name}}
{{end}}
{{end}}
{{end}}
{{end}}
EOF
# Remove formatting before use
template="$(echo "${template}" | tr -d '\n' | sed -e 's/} \+/}/g')"
local count
count="$("${oc}" --config="${kubeconfig}" get nodes \
--template "${template}" 2> /dev/null | \
wc -l)"
test "${count}" -ge "${expected_node_count}"
}
function build-images() {
local origin_root=$1
echo "Building container images"
build-image "${origin_root}/images/dind/" "${BASE_IMAGE}"
build-image "${origin_root}/images/dind/node" "${NODE_IMAGE}"
build-image "${origin_root}/images/dind/master" "${MASTER_IMAGE}"
}
function build-image() {
local build_root=$1
local image_name=$2
os::build::image "${image_name}" "${build_root}"
}
function os::build::get-bin-output-path() {
local os_root="${1:-}"
if [[ -n "${os_root}" ]]; then
os_root="${os_root}/"
fi
echo ${os_root}_output/local/bin/$(os::build::host_platform)
}
## Start of the main program
DEFAULT_DOCKER_CMD="sudo docker"
if [[ -w "/var/run/docker.sock" ]]; then
DEFAULT_DOCKER_CMD="docker"
# Since docker is a shell script we do not want to pass our restrictions to it
# This would be stripped by sudo, but we have to do it manually otherwise
export -n SHELLOPTS
else
# Make sure that they don't do half the work if sudo fails later by getting it primed now
sudo echo -n
fi
DOCKER_CMD="${DOCKER_CMD:-$DEFAULT_DOCKER_CMD}"
CLUSTER_ID="${OPENSHIFT_CLUSTER_ID:-openshift}"
TMPDIR="${TMPDIR:-"/tmp"}"
CONFIG_BASE="${OPENSHIFT_CONFIG_BASE:-${TMPDIR}/openshift-dind-cluster}"
CONFIG_ROOT="${OPENSHIFT_CONFIG_ROOT:-${CONFIG_BASE}/${CLUSTER_ID}}"
DEPLOYED_CONFIG_ROOT="/data"
MASTER_NAME="${CLUSTER_ID}-master"
NODE_PREFIX="${CLUSTER_ID}-node-"
NODE_COUNT=2
NODE_NAMES=()
BASE_IMAGE="openshift/dind"
NODE_IMAGE="openshift/dind-node"
MASTER_IMAGE="openshift/dind-master"
ADDITIONAL_ARGS=""
OVN_ROOT="${OVN_ROOT:-}"
CONTAINER_RUNTIME="${CONTAINER_RUNTIME:-dockershim}"
case "${1:-""}" in
start)
BUILD=
BUILD_IMAGES=
WAIT_FOR_CLUSTER=1
NETWORK_PLUGIN=
REMOVE_EXISTING_CLUSTER=
OPTIND=2
while getopts ":bc:in:rsN:" opt; do
case $opt in
b)
BUILD=1
;;
i)
BUILD_IMAGES=1
;;
n)
NETWORK_PLUGIN="${OPTARG}"
;;
N)
NODE_COUNT="${OPTARG}"
;;
r)
REMOVE_EXISTING_CLUSTER=1
;;
s)
WAIT_FOR_CLUSTER=
;;
c)
CONTAINER_RUNTIME="${OPTARG}"
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
exit 1
;;
:)
echo "Option -${OPTARG} requires an argument." >&2
exit 1
;;
esac
done
if [[ ${@:OPTIND-1:1} = "--" ]]; then
ADDITIONAL_ARGS=${@:OPTIND}
fi
for (( i=1; i<=NODE_COUNT; i++ )); do
NODE_NAMES+=( "${NODE_PREFIX}${i}" )
done
if [[ -n "${REMOVE_EXISTING_CLUSTER}" ]]; then
stop "${CONFIG_ROOT}" "${CLUSTER_ID}"
fi
# Build origin if requested or required
if [[ -n "${BUILD}" ]] || ! os::util::find::built_binary 'oc' >/dev/null 2>&1; then
"${OS_ROOT}/hack/build-go.sh"
fi
# Build images if requested or required
if [[ -n "${BUILD_IMAGES}" ||
-z "$(${DOCKER_CMD} images -q ${MASTER_IMAGE})" ]]; then
build-images "${OS_ROOT}"
fi
NETWORK_PLUGIN="$(get-network-plugin "${NETWORK_PLUGIN}")"
# OVN requires CNI network plugin and OVN_ROOT to be set
if [[ "${NETWORK_PLUGIN}" = "ovn" ]]; then
NETWORK_PLUGIN="cni"
if [[ -z "${OVN_ROOT}" ]]; then
echo "OVN network plugin requires OVN_ROOT set to ovn-kubernetes checkout"
exit 1
fi
elif [[ -n "${OVN_ROOT}" ]]; then
OVN_ROOT=
fi
start "${OS_ROOT}" "${OVN_ROOT}" "${CONFIG_ROOT}" "${DEPLOYED_CONFIG_ROOT}" \
"${CLUSTER_ID}" "${NETWORK_PLUGIN}" "${CONTAINER_RUNTIME}" \
"${WAIT_FOR_CLUSTER}" "${NODE_COUNT}" "${ADDITIONAL_ARGS}"
;;
add-node)
WAIT_FOR_CLUSTER=1
OPTIND=2
while getopts ":bis" opt; do
case $opt in
s)
WAIT_FOR_CLUSTER=
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
exit 1
;;
:)
echo "Option -${OPTARG} requires an argument." >&2
exit 1
;;
esac
done
add-node "${CONFIG_ROOT}" "${DEPLOYED_CONFIG_ROOT}" "${CLUSTER_ID}" "${WAIT_FOR_CLUSTER}"
;;
delete-node)
NODE_NUM=$2
NODE_NAME="${NODE_PREFIX}${NODE_NUM}"
delete-node "${CONFIG_ROOT}" "${CLUSTER_ID}" "${NODE_NAME}"
;;
stop)
stop "${CONFIG_ROOT}" "${CLUSTER_ID}"
;;
refresh)
BUILD=
BUILD_IMAGES=
WAIT_FOR_CLUSTER=1
OPTIND=2
while getopts ":bis" opt; do
case $opt in
b)
BUILD=1
;;
i)
BUILD_IMAGES=1
;;
s)
WAIT_FOR_CLUSTER=
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
exit 1
;;
:)
echo "Option -${OPTARG} requires an argument." >&2
exit 1
;;
esac
done
# Build origin if requested or required
if [[ -n "${BUILD}" ]] || ! os::util::find::built_binary 'oc' >/dev/null 2>&1; then
"${OS_ROOT}/hack/build-go.sh"
fi
# Build images if requested or required
if [[ -n "${BUILD_IMAGES}" ||
-z "$(${DOCKER_CMD} images -q ${MASTER_IMAGE})" ]]; then
build-images "${OS_ROOT}"
fi
refresh "${OS_ROOT}" "${CONFIG_ROOT}" "${CLUSTER_ID}" "${WAIT_FOR_CLUSTER}"
;;
pause)
pause "${CLUSTER_ID}"
;;
resume)
WAIT_FOR_CLUSTER=1
OPTIND=2
while getopts ":s" opt; do
case $opt in
s)
WAIT_FOR_CLUSTER=
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
exit 1
;;
:)
echo "Option -${OPTARG} requires an argument." >&2
exit 1
;;
esac
done
resume "${CONFIG_ROOT}" "${CLUSTER_ID}" "${WAIT_FOR_CLUSTER}"
;;
copy-image)
IMAGE_NAME=$2
copy-image "${CLUSTER_ID}" "${IMAGE_NAME}"
;;
wait-for-cluster)
wait-for-cluster "${CONFIG_ROOT}" "${NODE_COUNT}"
;;
build-images)
build-images "${OS_ROOT}"
;;
ps)
cluster-ps
;;
*)
>&2 echo "Usage: $0 {start|stop|refresh|add-node|delete-node|ps|pause|resume|copy-image|wait-for-cluster|build-images} [options]
Commands:
- start: Starts the containers in an openshift docker-in-docker environment
- stop: Destroys the docker containers for the docker-in-docker environment
- add-node: Adds a node to the cluster
- delete-node <node-num>: Deletes the given node from the cluster
- refresh: Refreshes the openshift binaries in the containers and reloads the processes
- ps: List all of the docker containers that make up the cluster
- pause: Stops running containers, but leaves the state around
- resume: Restarts paused containers
- copy-image: Copies an image from the outer docker into all node dockers
- wait-for-cluster: Waits for a cluster to come online
- build-images: Builds the docker-in-docker images themselves
start accepts the following options:
-n [net plugin] the name of the network plugin to deploy
-N number of nodes in the cluster
-b build origin before starting the cluster
-c [runtime name] use the specified container runtime instead of dockershim (eg, "crio")
-i build container images before starting the cluster
-r remove an existing cluster
-s skip waiting for nodes to become ready
Any of the arguments that would be used in creating openshift master can be passed
as is to the script after '--' ex: setting host subnet to 3
./dind-cluster.sh start -- --host-subnet-length=3
refresh accepts the following options:
-b build origin before starting the cluster
-i build container images before starting the cluster
-s skip waiting for nodes to become ready
add-node and resume accept the following option:
-s skip waiting for nodes to become ready
The following environment variables are honored:
- DOCKER_CMD: The docker command used. Default: 'sudo docker'
- OPENSHIFT_CLUSTER_ID: The name of the cluster (so multiple can be run). Default: 'openshift'
- OPENSHIFT_CONFIG_BASE: Where the cluster configs are written, move somewhere persistent if you
want to pause and resume across reboots. Default: '${TMPDIR}/openshift-dind-cluster'
- OPENSHIFT_CONFIG_ROOT: Where this specific cluster config is written.
Default: '\${OPENSHIFT_CONFIG_BASE}/\${OPENSHIFT_CLUSTER_ID}'
"
exit 2
esac