-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcluster_manager.sh
More file actions
executable file
·487 lines (412 loc) · 14.7 KB
/
cluster_manager.sh
File metadata and controls
executable file
·487 lines (412 loc) · 14.7 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
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
#!/bin/bash
# MicroShift Cluster Manager
# Basic functions for managing MicroShift clusters
set -euo pipefail
# Configuration - These variables can be overridden by the environment
# They are used throughout the script for single-node and multi-node cluster management
USHIFT_MULTINODE_CLUSTER="${USHIFT_MULTINODE_CLUSTER:-microshift-okd-multinode}"
NODE_BASE_NAME="${NODE_BASE_NAME:-microshift-okd-}"
USHIFT_IMAGE="${USHIFT_IMAGE:-microshift-okd}"
LVM_DISK="${LVM_DISK:-/var/lib/microshift-okd/lvmdisk.image}"
EXTRA_CONFIG="${EXTRA_CONFIG:-/var/lib/microshift-okd/custom_config.yaml}"
LVM_VOLSIZE="${LVM_VOLSIZE:-1G}"
API_SERVER_PORT="${API_SERVER_PORT:-6443}"
VG_NAME="${VG_NAME:-myvg1}"
ISOLATED_NETWORK="${ISOLATED_NETWORK:-0}"
EXPOSE_KUBEAPI_PORT="${EXPOSE_KUBEAPI_PORT:-0}"
_is_cluster_created() {
if sudo podman container exists "${NODE_BASE_NAME}1"; then
return 0
fi
return 1
}
_is_container_created() {
local -r name="${1}"
if sudo podman container exists "${name}"; then
return 0
fi
return 1
}
create_topolvm_backend() {
if [ -f "${LVM_DISK}" ]; then
echo "INFO: '${LVM_DISK}' exists, reusing"
return 0
fi
sudo mkdir -p "$(dirname "${LVM_DISK}")"
sudo truncate --size="${LVM_VOLSIZE}" "${LVM_DISK}"
local -r device_name="$(sudo losetup --find --show --nooverlap "${LVM_DISK}")"
sudo vgcreate -f -y "${VG_NAME}" "${device_name}"
}
# Delete TopoLVM backend
delete_topolvm_backend() {
if [ -f "${LVM_DISK}" ]; then
echo "Deleting TopoLVM backend: ${LVM_DISK}"
sudo lvremove -y "${VG_NAME}" || true
sudo vgremove -y "${VG_NAME}" || true
local -r device_name="$(sudo losetup -j "${LVM_DISK}" | cut -d: -f1)"
[ -n "${device_name}" ] && sudo losetup -d "${device_name}" || true
sudo rm -rf "$(dirname "${LVM_DISK}")"
fi
}
_create_podman_network() {
local -r name="${1}"
if ! sudo podman network exists "${name}"; then
echo "Creating podman network: ${name}"
sudo podman network create "${name}"
else
echo "Podman network '${name}' already exists"
fi
}
_get_subnet() {
local -r network_name="${1}"
local -r subnet_with_mask=$(sudo podman network inspect "${network_name}" --format '{{range .}}{{range .Subnets}}{{.Subnet}}{{end}}{{end}}')
if [ -z "$subnet_with_mask" ]; then
echo "ERROR: Could not determine subnet for network '${network_name}'." >&2
exit 1
fi
local -r subnet="${subnet_with_mask%%/*}"
echo "$subnet"
}
_get_ip_address() {
local -r subnet="${1}"
local -r node_id="${2}"
echo "$subnet" | awk -F. -v new="$node_id" 'NF==4{$4=new+10; printf "%s.%s.%s.%s", $1,$2,$3,$4} NF!=4{print $0}'
}
_get_hostname() {
local -r hostname=$(hostname -f 2>/dev/null)
if [ -z "$hostname" ]; then
echo "ERROR: Could not determine local FQDN hostname" >&2
exit 1
fi
echo "$hostname"
}
# Notes:
# - The container joins the cluster network and gets the cluster network IP
# address when the ISOLATED_NETWORK environment variable is set to 0.
# - The /dev directory is shared with the container to enable TopoLVM CSI driver,
# masking the devices that may conflict with the host
# - The containers storage is mounted on a tmpfs to avoid usage of fuse-overlayfs,
# which is less efficient than the default driver
_add_node() {
local -r name="${1}"
local -r network_name="${2}"
local -r ip_address="${3}"
local vol_opts="--tty --volume /dev:/dev"
for device in input snd dri; do
[ -d "/dev/${device}" ] && vol_opts="${vol_opts} --tmpfs /dev/${device}"
done
local network_opts="--network ${network_name}"
if [ "${ISOLATED_NETWORK}" = "0" ]; then
network_opts="${network_opts} --ip ${ip_address}"
fi
local port_opts=""
local mount_opts=""
if [ "${EXPOSE_KUBEAPI_PORT}" = "1" ]; then
port_opts="-p ${API_SERVER_PORT}:${API_SERVER_PORT}"
echo -e "apiServer:\n subjectAltNames:\n - $(_get_hostname)" | sudo tee "${EXTRA_CONFIG}" >/dev/null
mount_opts="--volume ${EXTRA_CONFIG}:/etc/microshift/config.d/api_server.yaml:ro"
fi
local rc=0
# shellcheck disable=SC2086
sudo podman run --privileged -d \
--ulimit nofile=524288:524288 \
${vol_opts} \
${network_opts} \
${port_opts} \
${mount_opts} \
--tmpfs /var/lib/containers \
--name "${name}" \
--hostname "${name}" \
"${USHIFT_IMAGE}"
rc=$?
if [ $rc -ne 0 ]; then
return $rc
fi
# Wait up to 60 seconds for the container to activate the dbus service.
# It is necessary to prevent subsequent systemctl commands to fail with dbus errors.
local is_active=false
for _ in {1..60}; do
if sudo podman exec -i "${name}" systemctl is-active -q dbus.service ; then
is_active=true
break
fi
sleep 1
done
if [ "${is_active}" = "false" ]; then
echo "ERROR: The container did not activate the dbus service within 60 seconds"
return 1
fi
return 0
}
_join_node() {
local -r name="${1}"
local -r primary_name="${NODE_BASE_NAME}1"
local -r src_kubeconfig="/var/lib/microshift/resources/kubeadmin/${primary_name}/kubeconfig"
local -r tmp_kubeconfig="/tmp/kubeconfig.${primary_name}"
sudo podman cp "${primary_name}:${src_kubeconfig}" "${tmp_kubeconfig}"
local -r dest_kubeconfig="kubeconfig"
sudo podman cp "${tmp_kubeconfig}" "${name}:${dest_kubeconfig}"
sudo rm -f "${tmp_kubeconfig}"
sudo podman exec -i "${name}" bash -c "\
systemctl stop microshift kubepods.slice crio && \
microshift add-node --kubeconfig=${dest_kubeconfig} --learner=false > add-node.log 2>&1"
return $?
}
_get_cluster_containers() {
sudo podman ps -a --format '{{.Names}}' | grep -E "^${NODE_BASE_NAME}[0-9]+$" || true
}
_get_running_containers() {
sudo podman ps --format '{{.Names}}' | grep -E "^${NODE_BASE_NAME}[0-9]+$" || true
}
cluster_create() {
local -r container_name="${NODE_BASE_NAME}1"
echo "Creating cluster: ${container_name}"
if _is_container_created "${container_name}"; then
echo "ERROR: Container '${container_name}' already exists" >&2
exit 1
fi
sudo modprobe openvswitch || true
create_topolvm_backend
_create_podman_network "${USHIFT_MULTINODE_CLUSTER}"
local -r subnet=$(_get_subnet "${USHIFT_MULTINODE_CLUSTER}")
local network_name="${USHIFT_MULTINODE_CLUSTER}"
if [ "${ISOLATED_NETWORK}" = "1" ]; then
network_name="none"
fi
local -r node_name="${NODE_BASE_NAME}1"
local -r ip_address=$(_get_ip_address "$subnet" "1")
if ! _add_node "${node_name}" "${network_name}" "${ip_address}"; then
echo "ERROR: failed to create node: $node_name" >&2
exit 1
fi
if [ "${ISOLATED_NETWORK}" = "1" ] ; then
echo "Configuring isolated network for node: ${node_name}"
sudo podman cp ./src/config_isolated_net.sh "${node_name}:/tmp/config_isolated_net.sh"
sudo podman exec -i "${node_name}" /tmp/config_isolated_net.sh
sudo podman exec -i "${node_name}" rm -vf /tmp/config_isolated_net.sh
fi
echo "Cluster created successfully. To access the node container, run:"
echo " sudo podman exec -it ${node_name} /bin/bash -l"
}
cluster_add_node() {
if ! _is_cluster_created; then
echo "ERROR: Cluster is not created" >&2
exit 1
fi
if [ "${ISOLATED_NETWORK}" = "1" ]; then
echo "ERROR: Network type is isolated" >&2
exit 1
fi
local -r last_id=$(_get_cluster_containers | wc -l)
local -r subnet=$(_get_subnet "${USHIFT_MULTINODE_CLUSTER}")
local -r node_id=$((last_id + 1))
local -r node_name="${NODE_BASE_NAME}${node_id}"
local -r ip_address=$(_get_ip_address "$subnet" "$node_id")
cluster_healthy
echo "Creating node: ${node_name}"
if ! _add_node "${node_name}" "${USHIFT_MULTINODE_CLUSTER}" "${ip_address}"; then
echo "ERROR: failed to create node: ${node_name}" >&2
exit 1
fi
echo "Joining node to the cluster: ${node_name}"
if ! _join_node "${node_name}"; then
echo "ERROR: failed to join node to the cluster: ${node_name}" >&2
echo "=== Add-node log content ===" >&2
if sudo podman exec -i "${node_name}" test -f add-node.log; then
sudo podman exec -i "${node_name}" cat add-node.log >&2
else
echo "WARNING: add-node.log not found in ${node_name}" >&2
fi
exit 1
fi
echo "Node added successfully. To access the new node container, run:"
echo " sudo podman exec -it ${node_name} /bin/bash -l"
return 0
}
cluster_start() {
local -r containers=$(_get_cluster_containers)
if [ -z "${containers}" ]; then
echo "ERROR: No cluster containers found" >&2
exit 1
fi
echo "Starting cluster"
for container in ${containers}; do
echo "Starting container: ${container}"
sudo podman start "${container}" || true
done
}
cluster_stop() {
local -r containers=$(_get_running_containers)
if [ -z "${containers}" ]; then
echo "No running cluster containers"
return 0
fi
echo "Stopping cluster"
for container in ${containers}; do
echo "Stopping container: ${container}"
sudo podman stop --time 0 "${container}" || true
done
}
cluster_destroy() {
local containers
containers=$(_get_cluster_containers)
for container in ${containers}; do
echo "Stopping container: ${container}"
sudo podman stop --time 0 "${container}" || true
echo "Removing container: ${container}"
# Remove the container and its anonymous volumes
sudo podman rm -f --volumes "${container}" || true
done
if sudo podman network exists "${USHIFT_MULTINODE_CLUSTER}"; then
echo "Removing podman network: ${USHIFT_MULTINODE_CLUSTER}"
sudo podman network rm "${USHIFT_MULTINODE_CLUSTER}" || true
fi
sudo rmmod openvswitch || true
delete_topolvm_backend
echo "Cluster destroyed successfully"
}
cluster_ready() {
local -r containers=$(_get_running_containers)
if [ -z "${containers}" ]; then
echo "No running nodes found"
exit 1
fi
for container in ${containers}; do
echo "Checking readiness of node: ${container}"
state=$(sudo podman exec -i "${container}" systemctl show --property=SubState --value microshift.service 2>/dev/null || echo "unknown")
if [ "${state}" != "running" ]; then
echo "Node ${container} is not ready."
exit 1
fi
done
echo "All nodes running."
}
cluster_healthy() {
if ! _is_cluster_created ; then
echo "Cluster is not initialized"
exit 1
fi
local -r containers=$(_get_running_containers)
if [ -z "${containers}" ]; then
echo "Cluster is down. No cluster nodes are running."
exit 1
fi
for container in ${containers}; do
echo "Checking health of node: ${container}"
state=$(sudo podman exec -i "${container}" systemctl show --property=SubState --value greenboot-healthcheck 2>/dev/null || echo "unknown")
if [ "${state}" != "exited" ]; then
echo "Node ${container} is not healthy."
exit 1
fi
done
echo "All nodes healthy."
}
cluster_status() {
if ! _is_cluster_created ; then
echo "Cluster is not initialized"
exit 1
fi
local -r running_containers=$(_get_running_containers)
if [ -z "${running_containers}" ]; then
echo "Cluster is down. No cluster nodes are running."
return 0
fi
local -r created_containers=$(_get_cluster_containers)
for container in ${created_containers}; do
if ! echo "${running_containers}" | grep -q "${container}"; then
echo "Node ${container} is not running."
fi
done
local -r first_container=$(echo "${running_containers}" | head -n1)
echo "Cluster is running."
sudo podman exec -i "${first_container}" kubectl get nodes,pods -A -o wide 2>/dev/null || echo "Unable to retrieve cluster status"
return 0
}
cluster_env() {
local command="${1:-}"
# Set first_container from the first value of containers array
local -r first_container=$(_get_running_containers | head -n1)
if [ -z "${first_container}" ]; then
echo "ERROR: No running cluster containers found." >&2
exit 1
fi
# Verify that ${API_SERVER_PORT} is open for connections on the host
if ! sudo ss -ltn "( sport = :${API_SERVER_PORT} )" | grep -q ":${API_SERVER_PORT}"; then
echo "ERROR: API server port ${API_SERVER_PORT} is closed, make sure EXPOSE_KUBEAPI_PORT is set to 1." >&2
exit 1
fi
local -r workdir=$(mktemp -d /tmp/kubeconfig-XXXXXX)
# shellcheck disable=SC2064
trap "rm -rf '${workdir}'" RETURN
echo "Copying kubeconfig from ${first_container}..."
sudo podman cp "${first_container}:/var/lib/microshift/resources/kubeadmin/$(_get_hostname)/kubeconfig" "${workdir}/kubeconfig"
sudo chown "$(whoami):$(whoami)" "${workdir}/kubeconfig"
export KUBECONFIG="${workdir}/kubeconfig"
if [ -n "${command}" ]; then
# Execute the command and exit
echo "Executing command in environment with kubeconfig..."
sh -c "${command}"
else
# Start interactive shell
echo "Starting shell environment with kubeconfig..."
bash -li
fi
}
main() {
case "${1:-}" in
create)
shift
cluster_create
;;
add-node)
shift
cluster_add_node
;;
start)
shift
cluster_start
;;
stop)
shift
cluster_stop
;;
delete)
shift
cluster_destroy
;;
ready)
shift
cluster_ready
;;
healthy)
shift
cluster_healthy
;;
status)
shift
cluster_status
;;
env)
shift
cluster_env "$@"
;;
topolvm-create)
shift
create_topolvm_backend
;;
topolvm-delete)
shift
delete_topolvm_backend
;;
*)
echo "Usage: $0 {create|add-node|start|stop|delete|ready|healthy|status|env|topolvm-create|topolvm-delete}"
exit 1
;;
esac
}
# Ensure script is running from project root directory (where Makefile exists)
if [ ! -f "./Makefile" ]; then
echo "ERROR: Please run this script from the project root directory (where Makefile is located)" >&2
exit 1
fi
main "$@"