-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathklb
executable file
·164 lines (130 loc) · 4.13 KB
/
klb
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
#! /bin/bash
LOG_FILE="./install.log"
SCRIPT_VERSION="1.0.1"
COMMAND=""
TARGET_MASTERS=""
TARGET_WORKERS=""
TARGET_TYPE="nginx"
source "./common"
# =============================================================================
# NAME: help
# DESC: display help message
# =============================================================================
function help() {
cat <<-EOF
Kubernetes load-balancer
Usage: klb --dry|--apply|--status|--version|--help|--list-types --type <type> --master <master1> [--master <master2>] ... --worker <worker1> [--worker <worker2>] ...
Options (exclusives):
--dry
Run configuration without applying
--apply
Run configuration and apply it
--status
Display the current status of the kubernetes cluster
--list-types
Display supported load-balancing type
--version
Display version information and exit
--help
Display this help and exit
Parameters:
--type
Define the load balancer type (should not be changed after installation)
--master
Define a server name or ip to include into load balancing as master for API
--worker
Define a server name or ip to include into load balancing as worker
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 load-balancer - $SCRIPT_VERSION
This is free software; see the source for copying conditions.
Written by J.F. Vincent
EOF
}
# =============================================================================
# NAME: type_list
# DESC: list supported kubernetes networks
# =============================================================================
function type_list() {
cat <<-EOF
Available load-balancer list:
- nginx (default)
EOF
}
# =============================================================================
# NAME: check_type
# DESC: check if mode is supported
# =============================================================================
# $1 : mode
# =============================================================================
function check_type() {
case "$1" in
"nginx") ;;
*) display_message "Type '$1' not supported" "ERROR";
exit 1
esac
echo "$1"
}
# =============================================================================
# NAME: main
# =============================================================================
opts=$(getopt \
--longoptions "help,version,dry,apply,status,list-types,type:,master:,worker:" \
--name "$(basename "$0")" \
--options "" \
-- "$@"
)
eval set "--$opts"
while [ $# -gt 0 ]
do
case "$1" in
--help) help; exit;;
--version) version; exit;;
--list-type) type_list; 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;;
--type) TARGET_TYPE=$(check_type "${2,,}"); shift;;
--master) TARGET_MASTERS="${TARGET_MASTERS}\tserver $2;\n"; shift;;
--worker) TARGET_WORKERS="${TARGET_WORKERS}\tserver $2;\n"; shift;;
esac
shift
done
if [ -z "${COMMAND}" ];
then
display_message "Usage: km --dry|--apply|--status|--version|--help --master <master1> [--master <master2>] --master <master3> ... --worker <worker1> --worker <worker2> ..." "ERROR"
exit 1
fi
if [ "$EUID" -ne 0 ]
then
display_message "This script must be started as root" "ERROR"
exit 1
fi
if [ "${TARGET_MASTERS}" == "" ];
then
display_message "No master to add" "ERROR"
exit 1
fi
if [ "${TARGET_WORKERS}" == "" ];
then
display_message "No worker to add" "ERROR"
exit 1
fi
if [ "${COMMAND}" == "apply" ];
then
case "${TARGET_TYPE}" in
"nginx") install_nginx_instance "${TARGET_MASTERS}" "${TARGET_WORKERS}";;
*) display_message "Type '$1' not supported" "ERROR";
exit 1;;
esac
fi