-
Notifications
You must be signed in to change notification settings - Fork 4
/
kind-setup.sh
executable file
·198 lines (128 loc) · 4.69 KB
/
kind-setup.sh
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
#!/usr/bin/env bash
#set -x
################################################################################
#### Variables, Arrays, and Hashes #############################################
################################################################################
CLUSTER_CONFIG="1m1w.yaml"
CONFIG_PATH="../config"
################################################################################
#### Functions #################################################################
################################################################################
# **********************************************
# Set command name
# **********************************************
cmd() {
echo `basename $0`
}
# **********************************************
# Print script usage message
# **********************************************
usage() {
echo "\
`cmd` [OPTION...]
-c, --create; Create a KIND Cluster
-d, --delete; Delete a KIND Cluster
-n, --name NAME; The name to use for the KIND Cluster
-f, --config-file [FILE]; The config file to use for the KIND Cluster (default = \"1m1w.yaml\")
" | column -t -s ";"
}
# **********************************************
# Set command arguments
# **********************************************
set_args() {
options=$(getopt -o :cdvn:f: --long create,delete,verbose,name:,config-file: -- "${@}")
[ $? -eq 0 ] || {
usage
exit 1
}
eval set -- "${options}"
while true; do
case ${1} in
-c|--create )
echo "Option \"create\" specified"
RUN_TYPE="create"
;;
-d|--delete )
echo "Option \"delete\" specified"
RUN_TYPE="delete"
;;
-v|--verbose )
echo "Option \"verbose\" specified"
DEBUG="echo"
;;
-n|--name )
echo "Option \"name\" specified"
shift;
CLUSTER_NAME="${1}"
;;
-f|--config-file )
echo "Option \"config-file\" specified"
shift;
if [ "${1}" != "" ]; then
if [ -f ${CONFIG_PATH}/${1}.yaml ]; then
CLUSTER_CONFIG="${1}.yaml"
else
echo "Config File \"${CLUSTER_CONFIG}\" was not found in \"${CONFIG_PATH}\""
echo
usage
exit 1
fi
else
echo "Config file was not specified"
fi
;;
--)
shift
break
;;
esac
shift
done
}
# **********************************************
# Create KIND Cluster
# **********************************************
cluster_create() {
echo "Creating KIND Cluster \"${CLUSTER_NAME}\" using config file \"${CONFIG_PATH}/${CLUSTER_CONFIG}\""
echo
# Create the KIND Cluster
kind create cluster --config ${CONFIG_PATH}/${CLUSTER_CONFIG} --name ${CLUSTER_NAME}
# Set KUBECONFIG
export KUBECONFIG="$(kind get kubeconfig-path --name="${CLUSTER_NAME}")"
# Label Worker Nodes
kubectl label nodes -l node-role.kubernetes.io/master!= node-role.kubernetes.io/worker=
if [ "${CLUSTER_CONFIG}" == "3m3w.yaml" ]; then
# Label Node AZ's
for node in `kubectl get nodes -o custom-columns=NODE:.metadata.name --no-headers`; do
# Get AZ based off node instance number
local AZ_NUM=`kubectl get node ${node} -o custom-columns=NODE:.metadata.name --no-headers | sed 's/.*\([0-9]\)$/\1/'`
# Label node for AZ
kubectl label node ${node} failure-domain.beta.kubernetes.io/zone="AZ${AZ_NUM}"
done
fi
}
# **********************************************
# Delete KIND Cluster
# **********************************************
cluster_delete() {
echo "Deleting KIND Cluster \"${CLUSTER_NAME}\""
echo
# Delete the KIND Cluster
kind delete cluster --name ${CLUSTER_NAME}
}
################################################################################
#### Main ######################################################################
################################################################################
set_args "${@}"
case ${RUN_TYPE} in
create)
cluster_create
;;
delete)
cluster_delete
;;
*)
usage
exit 1
;;
esac