-
Notifications
You must be signed in to change notification settings - Fork 1
/
kw
executable file
·214 lines (174 loc) · 5.69 KB
/
kw
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
#! /bin/bash
LOG_FILE="./install.log"
SCRIPT_VERSION="1.0.1"
COMMAND=""
KUBE_ADDRESS=""
KUBE_MASTER_TOKEN=""
KUBE_DISCOVERY_HASH=""
KUBE_MASTER_PORT=6443
KUBE_API_URL=""
source "./common"
# =============================================================================
# NAME: help
# DESC: display help message
# =============================================================================
function help() {
cat <<-EOF
Kubernetes worker
Usage: kw --dry|--apply|--status|--version|--help [--kube-address <kube-address>] [--master-port <port>] [--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
--version
Display version information and exit
--help
Display this help and exit
Parameters:
--kube-address
Define the wanted DNS or IP of kubernetes API
--master-port
Define the kubernetes API port
--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 <jfv.opensource@gmail.com>.
EOF
}
# =============================================================================
# NAME: version
# DESC: display version of this tool
# =============================================================================
function version() {
cat <<-EOF
Kubernetes worker - $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
token=$(echo "$command"|cut -d ' ' -f 5)
discover_hash=$(echo "$command"|cut -d ' ' -f 7)
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 "token ${token} hash ${discover_hash}"
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
}
# =============================================================================
# NAME: main
# =============================================================================
opts=$(getopt \
--longoptions "help,version,dry,apply,status,kube-address:,master-token:,master-port:,discovery-hash:,km-token:" \
--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;;
--kube-address) KUBE_ADDRESS="$2";shift;;
--master-token) KUBE_MASTER_TOKEN="$2";shift;;
--discovery-hash) KUBE_DISCOVERY_HASH="$2";shift;;
--master-port) KUBE_MASTER_PORT="$2";shift;;
--km-token) decode_km_token "$2"; shift;;
esac
shift
done
if [ -z "${COMMAND}" ];
then
display_message "Usage: kw --dry|--apply|--status|--version|--help [--kube-address <kube-address>] [--master-port <port>] [--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 [ -z "${KUBE_ADDRESS}" ];
then
echo "--master-address is missing"
exit 1
fi
if [ -z "${KUBE_MASTER_TOKEN}" ];
then
echo "--master-token is missing"
exit 1
fi
if [ -z "${KUBE_DISCOVERY_HASH}" ];
then
echo "--discovery-hash is missing"
exit 1
fi
# if [ -n "${KUBE_MASTER_PORT}" ];
# then
# KUBE_API_URL="${KUBE_ADDRESS}:${KUBE_MASTER_PORT}"
# fi
common_config
display_section "Configure kubernetes"
kubeadm join "${KUBE_API_URL}" --token "${KUBE_MASTER_TOKEN}" --discovery-token-ca-cert-hash "${KUBE_DISCOVERY_HASH}" >> "$LOG_FILE" 2>&1
display_message_result "Joining cluster"