-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkm
executable file
·241 lines (196 loc) · 6.89 KB
/
km
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
#! /bin/bash
LOG_FILE="./install.log"
SCRIPT_VERSION="1.0.1"
COMMAND=""
KUBE_NETWORK_CIDR="10.244.0.0/16"
KUBE_NETWORK_TYPE="flannel"
KUBE_MASTER_MODE="ALONE"
KUBE_ADDRESS=""
KUBE_FIRST_MASTER_FLAG=0
KUBE_MASTER_TOKEN=""
KUBE_DISCOVERY_HASH=""
KUBE_PORT=6443
source "./common"
# =============================================================================
# NAME: help
# DESC: display help message
# =============================================================================
function help() {
cat <<-EOF
Kubernetes master
Usage: km --dry|--apply|--status|--list-networks|--kubeconfig|--version|--help [--mode <mode>] [--kube-version <version>] [--network-type <network-type>] [--kube-address <kube-address>] [--master-port <port>] [--first-master] [--master-token <master-token>] [--discovery-hash <discovery-hash>] [--km-token <km-token>]
Options (exclusives):
--dry
Run configuration without applying
--apply
Run configuration and apply it
--status
Display the current status of the kubernetes cluster
--list-networks
Display the available network list
--kubeconfig
Get kube config file
--generate-join-tokens
Generate and display tokens to join cluster
--version
Display version information and exit
--help
Display this help and exit
Parameters:
--mode
Define the master mode (alone, single, multi - default single)
--network-type
Define the kubernetes network type
--kube-address
Define the wanted DNS or IP of kubernetes API
--master-port
Define the kubernetes API port
--first-master
Define the current master as the first master of the cluster
--master-token
Define token to join cluster if multi and not first master
(example: l0yjqz.v980dvbljqzxva53)
--discovery-hash
Define discovery hash to join cluster if multi and not first master
(example: sha256:4f53c9bc7a89a2fd25d2bdcd2d7f371a93a6e80f60e9b6c71ef5678e977f0cb3)
--km-token
Define the km token containing the kube address, the master-token and the discovery-hash
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 master - $SCRIPT_VERSION
This is free software; see the source for copying conditions.
Written by J.F. Vincent
EOF
}
# =============================================================================
# NAME: network_list
# DESC: list supported kubernetes networks
# =============================================================================
function network_list() {
cat <<-EOF
Available network list:
- flannel (default)
EOF
}
# =============================================================================
# NAME: generate_join_tokens
# DESC: Generate and display the token to allow new note joining the cluster
# =============================================================================
function generate_join_tokens() {
if [ ! -f /usr/bin/kubeadm ];
then
display_message "Kubeadm not present, is it a master node ?" "KO"
exit 1
fi
command=$(kubeadm token create --print-join-command 2> /dev/null)
if [ -z "${command}" ];
then
display_message "Can't generate token" "KO"
exit 1
else
kube_api=$(echo "$command"|cut -d ' ' -f 3)
token=$(echo "$command"|cut -d ' ' -f 5)
discover_hash=$(echo "$command"|cut -d ' ' -f 7)
if [ -z "${kube_api}" ];
then
display_message "Can't get api url" "KO"
exit 1
fi
if [ -z "${token}" ];
then
display_message "Can't get token" "KO"
exit 1
fi
if [ -z "$discover_hash" ];
then
display_message "Can't get discover hash" "KO"
exit 1
fi
echo "${kube_api},${token},${discover_hash}"|base64 -w 0
fi
}
# =============================================================================
# NAME: check_master_mode
# DESC: check if mode is supported
# =============================================================================
# $1 : mode
# =============================================================================
function check_master_mode() {
case "$1" in
"alone") ;;
"single") ;;
"multi");;
*) display_message "Mode '$1' not supported" "ERROR";
exit 1
esac
echo "$1"
}
# =============================================================================
# NAME: main
# =============================================================================
opts=$(getopt \
--longoptions "help,version,dry,apply,status,list-networks,generate-join-tokens,kubeconfig,mode:,network-type:,kube-address:,first-master,master-token:,discovery-hash:,km-token:" \
--name "$(basename "$0")" \
--options "" \
-- "$@"
)
eval set "--$opts"
while [ $# -gt 0 ]
do
case "$1" in
--help) help; exit;;
--version) version; exit;;
--list-networks) network_list; exit;;
--generate-join-tokens) generate_join_tokens; exit;;
--dry) COMMAND="dry"; display_message "Not yet implemented" "KO"; exit 1;;
--apply) COMMAND="apply";;
--status) status_master; exit;;
--kubeconfig) COMMAND="config";;
--mode) KUBE_MASTER_MODE=$(check_master_mode "${2,,}"); shift;;
--network-type) KUBE_NETWORK_TYPE="$2";shift;;
--kube-address) KUBE_ADDRESS="$2";shift;;
--first-master) KUBE_FIRST_MASTER_FLAG=1;;
--master-token) KUBE_MASTER_TOKEN="$2";shift;;
--discovery-hash) KUBE_DISCOVERY_HASH="$2";shift;;
--km-token) decode_km_token "$2"; shift;;
esac
shift
done
if [ -z "${COMMAND}" ];
then
display_message "Usage: km --dry|--apply|--status|--list-networks|--kubeconfig|--version|--help [--mode <mode>] [--kube-version <version>] [--network-type <network-type>] [--kube-address <kube-address>] [--master-port <port>] [--first-master] [--master-token <master-token>] [--discovery-hash <discovery-hash>] [--km-token <km-token>]" "ERROR"
exit 1
fi
if [ "$EUID" -ne 0 ]
then
display_message "This script must be started as root" "ERROR"
exit
fi
if [ "${COMMAND}" == "config" ];
then
if [ ! -f /etc/kubernetes/admin.conf ];
then
display_message "Can't access kubeconfig file" "ERROR"
else
cat /etc/kubernetes/admin.conf
fi
fi
if [ "${COMMAND}" == "apply" ];
then
case "${KUBE_MASTER_MODE,,}" in
"alone") common_config; KUBE_FIRST_MASTER_FLAG=1; configure_master "alone";;
"single") common_config; KUBE_FIRST_MASTER_FLAG=1; configure_master "single";;
"multi") common_config; configure_master "multi";;
*) display_message "Unsupported mode '$KUBE_MASTER_MODE'" "ERROR"
esac
fi