-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkc
executable file
·633 lines (511 loc) · 17.9 KB
/
kc
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
#! /bin/bash
LOG_FILE="./install.log"
SCRIPT_VERSION="1.0.0"
COMMAND=""
CONFIG=""
KUBE_PORT="6443"
SSH_USER=""
SSH_PASS=""
SSH_KEY=""
source "./common"
# =============================================================================
# NAME: help
# DESC: display help message
# =============================================================================
function help() {
cat <<-EOF
Kubernetes controller tool
Usage: kc --dry|--apply|--status|--kubeconfig|--version|--help [--user <user>] [--password <password>] [--ssh-key ssh-key-file] --config Configuration-file
Options (exclusives):
--dry
Run configuration without applying
--apply
Run configuration and apply it
--status
Display the current status of the kubernetes cluster
--kubeconfig
Download kube config file
--version
Display version information and exit
--help
Display this help and exit
Parameters:
--user
User account to connect hosts using ssh if needed
--password
User account password to connect hosts using ssh if needed
--ssh-key
Ssh private key file to hosts using ssh if not default
Examples:
kc --dry --user admin ./config/cluster.json
Run all operations without applying anything on any hosts using "admin" account on targets
kc --apply --kubeconfig --user admin -- ssh-key ./.ssh/my_key --config ./config/cluster.json
Run all operations using "admin" account on targets with the ssh private key "mykey"
kc --kubeconfig --config ./config/cluster.json
Get kubeconfig of the cluster
kc --status --config ./config/cluster.json
Get status of the cluster
Configuration file:
kube:
- network-type: flannel
- load-balancers:
- type: nginx
- dns: kubernetes.local
- servers:
- server: 1.2.3.4
- server: 1.2.3.5
- workers:
- server: 2.3.4.5
- server: 3.3.4.5
- masters:
- server: 2.3.4.6
- server: 3.3.4.6
network-type [OPTIONNAL]: the internal kubernetes network type (example: flannel).
The current version only support flannel.
load-balancer > type [OPTIONNAL].
The current version only support nginx and external.
load-balancer > servers > server [OPTIONNAL] : IP or DNS name of an host to be used as load balancer.
masters > servers > server [MANDATORY] : IP or DNS name of an host to be used as master node.
workers > servers > server [OPTIONNAL] : IP or DNS name of an host to be used as worker node.
Exit status:
0 if OK,
1 if any problem
Report bugs to <[email protected]>.
EOF
}
# =============================================================================
# NAME: version
# DESC: display version of this tool
# =============================================================================
function version() {
cat <<-EOF
Kubernetes controler tool - $SCRIPT_VERSION
This is free software; see the source for copying conditions.
Written by J.F. Vincent
EOF
}
# =============================================================================
# NAME: download_master_config
# DESC: download an archive from the first master with the configuration
# files like certificates
# =============================================================================
# $1 : remote hostname or ip
# $2 : archive name
# $3 : remote user
# $4 : remote user password or ""
# $5 : private key if needed or ""
# =============================================================================
function download_master_config() {
target="$1"
cert_param=""
if [ -z "$1" ];
then
display_message "No remote hostname or ip" "ERROR"
exit 1
fi
if [ -z "$2" ];
then
display_message "No archive filename to download from remote" "ERROR"
exit 1
fi
if [ -n "$3" ];
then
target=$( echo "$3@$target")
fi
if [ -n "$4" ];
then
sshpass -p "$4" /usr/bin/scp "$target:~/$2 ./cache/" "${cert_param}" > /dev/null 2>&1
else
if [ -n "$5" ];
then
if [ ! -f "$5" ];
then
display_message "SSH keyfile '$5' not available" "ERROR"
exit 1
else
cert_param="-i $5"
fi
fi
if [ -n "${cert_param}" ];
then
/usr/bin/scp "$target":~/"$2" ./cache/ "${cert_param}" > /dev/null 2>&1
else
/usr/bin/scp "$target":~/"$2" ./cache/ > /dev/null 2>&1
fi
fi
}
# =============================================================================
# NAME: upload_tools
# DESC: upload tools files
# =============================================================================
# $1 : remote hostname or ip
# $2 : remote user
# $3 : remote user password or ""
# $4 : private key if needed or ""
# =============================================================================
function upload_tools() {
target="$1"
cert_param=""
if [ -z "$1" ];
then
display_message "No remote hostname or ip" "ERROR"
exit 1
fi
FILES="./klb ./km ./kw ./common"
if [ ! -f ./km ];
then
# Container mode
FILES="/klb /km /kw"
fi
if [ -n "$2" ];
then
target="$2@$target"
fi
if [ -n "$3" ];
then
sshpass -p "$3" /usr/bin/scp ${FILES} "$target:~/" "${cert_param}" > /dev/null 2>&1
else
if [ -n "$4" ];
then
if [ ! -f "$4" ];
then
display_message "SSH keyfile '$4' not available" "ERROR"
exit 1
else
cert_param="-i $4"
fi
fi
if [ -n "${cert_param}" ];
then
/usr/bin/scp ${FILES} "$target:~/" "${cert_param}" > /dev/null 2>&1
else
/usr/bin/scp ${FILES} "$target:~/" > /dev/null 2>&1
fi
fi
}
# =============================================================================
# NAME: upload_master_config
# DESC: upload an archive from the first master with the configuration
# files like certificates
# =============================================================================
# $1 : remote hostname or ip
# $2 : archive name
# $3 : remote user
# $4 : remote user password or ""
# $5 : private key if needed or ""
# =============================================================================
function upload_master_config() {
target="$1"
cert_param=""
if [ -z "$1" ];
then
display_message "No remote hostname or ip" "ERROR"
exit 1
fi
if [ -z "$2" ];
then
display_message "No archive filename to download from remote" "ERROR"
exit 1
fi
if [ -n "$3" ];
then
target="$3@$target"
fi
if [ -n "$4" ];
then
sshpass -p "$4" /usr/bin/scp "./cache/$2" "$target:~/" "${cert_param}" > /dev/null 2>&1
else
if [ -n "$5" ];
then
if [ ! -f "$5" ];
then
display_message "SSH keyfile '$5' not available" "ERROR"
exit 1
else
cert_param="-i $5"
fi
fi
if [ -n "${cert_param}" ];
then
/usr/bin/scp "./cache/$2" "$target:~/" "${cert_param}" > /dev/null 2>&1
else
/usr/bin/scp "./cache/$2" "$target:~/" > /dev/null 2>&1
fi
fi
}
# =============================================================================
# NAME: ssh_command
# DESC: execute command throws ssh
# =============================================================================
# $1 : remote hostname or ip
# $2 : command to execute
# $3 : remote user
# $4 : remote user password or ""
# $5 : private key if needed or ""
# =============================================================================
function ssh_command() {
target="$1"
cert_param=""
if [ -z "$1" ];
then
display_message "No remote hostname or ip" "ERROR"
exit 1
fi
if [ -z "$2" ];
then
display_message "No command to execute on remote" "ERROR"
exit 1
fi
if [ -n "$3" ];
then
target="$3@$target"
fi
if [ -n "$4" ];
then
sshpass -p "$4" /usr/bin/ssh "$target" -C "$2" "${cert_param}"
else
if [ -n "$5" ];
then
if [ ! -f "$5" ];
then
display_message "SSH keyfile '$5' not available" "ERROR"
exit 1
else
cert_param="-i $5"
fi
fi
/usr/bin/ssh -n "$target" -C "$2" "${cert_param}"
fi
}
# =============================================================================
# NAME: apply_config
# DESC: apply a confirguration to cluster to create or update it
# =============================================================================
# $1 : configuration to apply
# =============================================================================
# @todo
# cas de la suppression d'un node à gérer plus tard
# cas de la bascule de alone à un autre état
function apply_config() {
rm -Rf cache
mkdir -p cache
display_section "Apply config"
KUBE_COMPUTED_TYPE=""
KUBE_NET=$(jq -r '.kube."network-type"' "$1"|grep -v null|head -n 1)
case "${KUBE_NET}" in
"flannel") ;;
*) display_message "Default or not supported network type: switching to default" "INFO";
KUBE_NET="flannel";;
esac
display_message "Targetting kubernetes network '${KUBE_NET}'" "INFO"
KUBE_NB_MASTERS=$(jq -r '.kube.masters| length' "$1")
KUBE_NB_WORKERS=$(jq -r '.kube.workers| length' "$1")
if [ "${KUBE_NB_MASTERS}" -eq "0" ] && [ "${KUBE_NB_WORKERS}" -eq "0" ];
then
display_message "Config does not reference neither masters nor workers" "ERROR"
exit 1
fi
if [ "${KUBE_NB_MASTERS}" -eq "0" ];
then
display_message "Config does not reference any masters" "ERROR"
exit 1
fi
if [ "${KUBE_NB_MASTERS}" -eq "1" ] && [ "${KUBE_NB_WORKERS}" -eq "0" ];
then
KUBE_COMPUTED_TYPE="ALONE"
CURRENT_MASTER=$(jq -r '.kube.masters[].server' "$1")
display_section "Deploying master >> ${CURRENT_MASTER}"
upload_tools "${CURRENT_MASTER}" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Pushing commands"
ssh_command "${CURRENT_MASTER}" "sudo ./km --apply --mode alone" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Deploying master"
fi
if [ "${KUBE_NB_MASTERS}" -eq "1" ] && [ "${KUBE_NB_WORKERS}" -gt "0" ];
then
KUBE_COMPUTED_TYPE="SINGLE"
CURRENT_MASTER=$(jq -r '.kube.masters[].server' "$1")
display_section "Deploying master >> ${CURRENT_MASTER}"
upload_tools "${CURRENT_MASTER}" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Pushing master"
ssh_command "${CURRENT_MASTER}" "sudo ./km --apply --mode single" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Deploying master"
fi
if [ "${KUBE_NB_MASTERS}" -gt "1" ] && [ "${KUBE_NB_WORKERS}" -eq "0" ];
then
display_message "Config references many masters but not workers" "ERROR"
exit 1
fi
if [ "${KUBE_NB_MASTERS}" -gt "1" ] && [ "${KUBE_NB_WORKERS}" -gt "0" ];
then
KUBE_COMPUTED_TYPE="MULTI"
fi
if [ "${KUBE_COMPUTED_TYPE}" == "MULTI" ];
then
KUBE_MASTERS=$(jq -r '.kube.masters[]' "$1"|jq -r ".server | select (. != null)")
KUBE_WORKERS=$(jq -r '.kube.workers[]' "$1"| jq -r ".server | select (. != null)")
LB_TYPE=$(jq -r '.kube."load-balancers"[].type| select( . != null )' "$1"|head -n 1)
LB_DNS=$(jq -r '.kube."load-balancers"[].dns| select( . != null )' "$1"|grep -v 'null'|head -n 1)
LB_SERVERS=$(jq -r '.kube."load-balancers"[].servers| select( . != null )' "$1" |jq -r ".[].server")
if [ -z "${LB_DNS}" ];
then
display_message "Config does not references a DNS for a multi master configuration" "ERROR"
exit 1
fi
if [ -z "${LB_TYPE}" ];
then
display_message "Config does not references a load-balancer type for a multi master configuration" "ERROR"
exit 1
fi
case "${LB_TYPE}" in
"nginx") install_nginx_servers "${LB_SERVERS}" "${KUBE_MASTERS}" "${KUBE_WORKERS}";;
"external") ;;
*) display_message "Config does not references a supported load-balancer type for a multi master configuration" "ERROR"; exit 1;;
esac
display_section "Deploying masters"
# Check all master candidates and test each one
KUBE_INSTALLED_MASTER=()
KUBE_UNINSTALLED_MASTER=()
while IFS= read -r KUBE_CURRENT_MASTER; do
upload_tools "${CURRENT_MASTER}" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Pushing master"
MASTER_STATUS=$(ssh_command "${KUBE_CURRENT_MASTER}" "sudo ./km --status" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}")
display_message "${KUBE_CURRENT_MASTER} is $MASTER_STATUS" "INFO"
if [ "${MASTER_STATUS}" == "installed" ];
then
KUBE_INSTALLED_MASTER+=("${KUBE_CURRENT_MASTER}")
else
KUBE_UNINSTALLED_MASTER+=("${KUBE_CURRENT_MASTER}")
fi
done <<< "${KUBE_MASTERS}"
CURRENT_MASTER=""
if [ ${#KUBE_INSTALLED_MASTER[@]} -eq 0 ];
then
display_message "No master(s) need to install first one" "INFO"
CURRENT_MASTER=${KUBE_UNINSTALLED_MASTER[0]}
display_message "Selecting ${CURRENT_MASTER} as first master" "INFO"
upload_tools "${CURRENT_MASTER}" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Pushing master"
ssh_command "${CURRENT_MASTER}" "sudo ./km --apply --mode multi --network-type ${KUBE_NET} --kube-address ${LB_DNS} --first-master" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
unset KUBE_UNINSTALLED_MASTER[0]
else
CURRENT_MASTER=${KUBE_UNINSTALLED_MASTER[0]}
display_message "One or more master(s) present" "INFO"
CURRENT_MASTER=${KUBE_INSTALLED_MASTER[0]}
display_message "Selecting ${CURRENT_MASTER} as first master" "INFO"
fi
fi
# At this step, we know one master is installed (maybe more)
# We can ask it if the worker candidates are already known
# by the cluster install it or not
CURRENT_MASTER=$(jq -r '.kube.masters[].server' "$1"|head -n 1)
upload_tools "${CURRENT_MASTER}" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Pushing master"
KUBE_CONFIG=$(ssh_command "${CURRENT_MASTER}" "sudo ./km --kubeconfig" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}")
display_message_result "Downloading kubeconfig"
echo "${KUBE_CONFIG}" > cache/config
download_master_config "${CURRENT_MASTER}" "kube-config.tgz" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Downloading archive config"
KUBE_TOKEN=$(ssh_command "${CURRENT_MASTER}" "sudo ./km --generate-join-tokens" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}")
if [ -z "${KUBE_TOKEN}" ];
then
display_message "Can't get join token" "ERROR"
exit 1
fi
if [ "${KUBE_COMPUTED_TYPE}" == "MULTI" ];
then
for CURRENT_MASTER in "${KUBE_UNINSTALLED_MASTER[@]}"
do
upload_tools "${CURRENT_MASTER}" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Pushing master"
upload_master_config "${CURRENT_MASTER}" "kube-config.tgz" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Uploading archive config to '${CURRENT_MASTER}'"
ssh_command "${CURRENT_MASTER}" "sudo ./km --apply --mode multi --km-token ${KUBE_TOKEN}" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
done
fi
display_section "Deploying workers >>"
export KUBE_WORKERS
if [ -n "${KUBE_WORKERS}" ];
then
while IFS= read -r KUBE_CURRENT_WORKER; do
WORKER_STATUS=$(kubectl --kubeconfig cache/config get node -o wide|grep "${KUBE_CURRENT_WORKER}"|wc -l)
display_message_result "Checking worker '${KUBE_CURRENT_WORKER}' in cluster"
if [ "${WORKER_STATUS}" -eq 0 ];
then
upload_tools "${CURRENT_MASTER}" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Pushing master"
display_message "Worker '${KUBE_CURRENT_WORKER}' to be installed" "INFO"
display_section "Deploying worker >>>> ${KUBE_CURRENT_WORKER}"
ssh_command "${KUBE_CURRENT_WORKER}" "sudo ./kw --apply --km-token ${KUBE_TOKEN}" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Worker '${KUBE_CURRENT_WORKER}' join cluster"
else
display_message "Worker '${KUBE_CURRENT_WORKER}' already known - skipping" "OK"
fi
done <<< "${KUBE_WORKERS}"
else
display_message "No Worker to deploy" "INFO"
fi
}
# =============================================================================
# NAME: main
# =============================================================================
opts=$(getopt \
--longoptions "help,version,dry,apply,status,kubeconfig,user:,password:,ssh-key,config:" \
--name "$(basename "$0")" \
--options "" \
-- "$@"
)
eval set "--$opts"
while [ $# -gt 0 ]
do
case "$1" in
--help) help; exit;;
--version) version; exit;;
--dry) COMMAND="dry";display_message "Not yet implemented" "KO"; exit 1;;
--apply) COMMAND="apply";;
--status) COMMAND="status";display_message "Not yet implemented" "KO"; exit 1;;
--kubeconfig) COMMAND="config";;
--user) SSH_USER="$2"; shift;;
--password) SSH_PASS="$2"; shift;;
--ssh-key) SSH_KEY="$2"; shift;;
--config) CONFIG="$2"; shift;;
esac
shift
done
# we need to read the command line to get all parameters before running commands
# then a second round is needed. Before we validate needs are present.
if ! command -v ssh &> /dev/null;
then
display_message "Command 'ssh' is needed but not installed" "ERROR"
exit 1
fi
if ! command -v jq &> /dev/null;
then
display_message "Command 'jq' is needed but not installed" "ERROR"
exit 1
fi
# check if configuration file is present and well formed.
if [ -z "${CONFIG}" ];
then
display_message "File ${CONFIG} can't has no value" "ERROR"
exit 1
fi
if [ ! -f "${CONFIG}" ];
then
display_message "File ${CONFIG} can't be found" "ERROR"
exit 1
fi
jq "." "${CONFIG}" > /dev/null 2>&1
if [ "$?" -eq 1 ];
then
display_message "File ${CONFIG} not well formed" "ERROR"
fi
# run commands
case "$COMMAND" in
"dry") echo "Under working"; exit;;
"apply") apply_config "${CONFIG}";;
"status") echo "Under working"; exit;;
"kubeconfig") echo "Under working"; exit;;
*) display_message "Missing command\n" "ERROR";
display_message "Usage: kc --dry|--apply|--status|--kubeconfig|--version|--help [--user <user>] [--password <password>] [--ssh-key ssh-key-file] --config Configuration-file" "ERROR";
exit 1;;
esac