-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers
executable file
·389 lines (328 loc) · 12.3 KB
/
helpers
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
#!/bin/bash
# ------------------------- [START] set shell options ------------------------ #
# exit script immediately if anything errors
set -e
# allow tracing to be toggled on
if [ "$DEBUG_GKE_GLUSTERFS_HEKETI" = 'true' ]
then
set -x
fi
# -------------------------- [END] set shell options ------------------------- #
# ------------------------- [START] get project path ------------------------- #
# get full path to this project
project_dir="$(cd -P -- "$(dirname -- "$0")" 2>&1 > /dev/null && pwd -P)"
# check that the project directory didn't turn out empty
if [ -z "$project_dir" ]
then
echo "Some error occured getting the full path to this project's directory."
# this is intended to be sourced instead of executed so we return
# instead of exiting so we don't exit the shell session entirely
return 1
fi
# -------------------------- [END] get project path -------------------------- #
# ------------------------ [START] source config file ------------------------ #
. "$project_dir/config"
# ------------------------- [END] source config file ------------------------- #
# ---------------------- [START] set required variables ---------------------- #
export project_name="$CLOUDSDK_PROJECT_NAME"
export cluster_name="$CLOUDSDK_CLUSTER_NAME"
export zone="$CLOUDSDK_COMPUTE_ZONE"
# ----------------------- [END] set required variables ----------------------- #
# ---------------------- [START] set optional variables ---------------------- #
# cluster version will default to whatever GKE's default is at the time
export cluster_version="$CLUSTER_VERSION"
export node_count=${NODE_COUNT:-'3'}
export machine_type=${MACHINE_TYPE:-'n1-standard-1'}
export disk_size=${DISK_SIZE:-'20'}
export docker_registry=${DOCKER_REGISTRY:-"gcr.io/$project_name"}
export docker_image_name=${DOCKER_IMAGE_NAME:-'glusterfs-heketi-bootstrap'}
export docker_image_version=${DOCKER_IMAGE_VERSION:-'0.0.1'}
if [ -z "$DOCKER_IMAGE_NAME" ]
then
# my public dockerhub
export job_image='stevenaldinger/gke-glusterfs-heketi:latest'
else
# my private gcr repo
export job_image="gcr.io/$project_name/$docker_image_name:$docker_image_version"
fi
# ----------------------- [END] set optional variables ----------------------- #
# ---------------- [START] ensure script requirements are met ---------------- #
script_requirements_met='true'
if [ -z "$(which gcloud)" ]
then
echo "You need gcloud installed."
echo "Visit https://cloud.google.com/sdk/downloads to download the latest version."
script_requirements_met='false'
fi
if [ -z "$(which kubectl)" ]
then
echo "You need kubectl installed."
echo "If you already have gcloud installed, run 'gcloud components install kubectl'."
script_requirements_met='false'
fi
if [ -z "$project_name" ] || [ -z "$cluster_name" ] || [ -z "$zone" ]
then
echo "You're missing required config variables."
echo "Make sure there are values for everything under \"required variables\" in $project_dir/config"
script_requirements_met='false'
fi
if [ "$node_count" -lt 3 ]
then
echo "You need at least 3 nodes in the cluster for Heketi to run properly"
script_requirements_met='false'
fi
if [ "$script_requirements_met" = 'false' ]
then
# this is intended to be sourced instead of executed so we return
# instead of exiting so we don't exit the shell session entirely
return 1
fi
# ----------------- [END] ensure script requirements are met ----------------- #
# ------------------------- [START] helper functions ------------------------- #
trim_whitespace () {
local var=$@
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
echo -n "$var"
}
gke_glusterfs_heketi_build_image () {
if [ -z "$(which docker)" ]
then
echo "You need docker installed to build this image."
else
docker build -t "$docker_registry/$docker_image_name:$docker_image_version" "$project_dir"
fi
}
gke_glusterfs_heketi_push_image () {
docker push "$docker_registry/$docker_image_name:$docker_image_version"
}
gke_glusterfs_heketi_generate_k8s () {
rm -rf "$project_dir/k8s"
mkdir -p "$project_dir/k8s"
cat > "$project_dir/k8s/00-namespace.yaml" <<EOF
---
apiVersion: v1
kind: Namespace
metadata:
name: glusterfs-heketi-bootstrap
EOF
cat > "$project_dir/k8s/01-configmap.yaml" <<EOF
---
apiVersion: v1
kind: ConfigMap
metadata:
name: glusterfs-heketi-bootstrap
namespace: glusterfs-heketi-bootstrap
data:
project-id: $project_name
cluster-name: $cluster_name
cluster-zone: $zone
cluster-node-count: "$node_count"
disk-size: "$disk_size"
EOF
cat > "$project_dir/k8s/01-heketi-clusterrolebinding.yaml" <<EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: glusterfs-heketi-bootstrap
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: glusterfs-heketi-bootstrap
namespace: glusterfs-heketi-bootstrap
EOF
cat > "$project_dir/k8s/02-job.yaml" <<EOF
---
apiVersion: batch/v1
kind: Job
metadata:
name: glusterfs-heketi-bootstrap
namespace: glusterfs-heketi-bootstrap
spec:
template:
metadata:
name: glusterfs-heketi-bootstrap
spec:
restartPolicy: Never
containers:
- name: glusterfs-heketi-bootstrap
image: $job_image
securityContext:
capabilities: {}
privileged: true
env:
- name: PROJECT_ID
valueFrom:
configMapKeyRef:
name: glusterfs-heketi-bootstrap
key: project-id
- name: CLUSTER_NAME
valueFrom:
configMapKeyRef:
name: glusterfs-heketi-bootstrap
key: cluster-name
- name: ZONE
valueFrom:
configMapKeyRef:
name: glusterfs-heketi-bootstrap
key: cluster-zone
- name: NODE_COUNT
valueFrom:
configMapKeyRef:
name: glusterfs-heketi-bootstrap
key: cluster-node-count
- name: DISK_SIZE
valueFrom:
configMapKeyRef:
name: glusterfs-heketi-bootstrap
key: disk-size
EOF
}
gke_glusterfs_heketi_delete_gluster_disks () {
disk_count=0
disk_list=""
echo "Deleting..."
for disk in $(gcloud compute disks list --format="table(name)" --filter="description=${cluster_name}-gfs-k8s-brick")
do
if [ "$disk_count" -eq 0 ]
then
# do nothing, the first returned is just a table header
echo ""
else
disk_list="$disk_list $disk"
fi
disk_count=$((disk_count + 1))
done
gcloud compute disks delete --quiet $disk_list
}
gke_glusterfs_heketi_deploy_glusterfs_heketi () {
kubectl apply -f "$project_dir/k8s"
}
gke_glusterfs_heketi_create_cluster () {
"$project_dir/scripts/create-cluster.sh"
}
_gke_glusterfs_heketi_create_service_account () {
kubectl create serviceaccount \
glusterfs-heketi-bootstrap \
--namespace glusterfs-heketi-bootstrap
}
_gke_glusterfs_heketi_add_admin_clusterrolebinding () {
kubectl apply -f "$project_dir/k8s/01-heketi-clusterrolebinding.yaml"
}
_gke_glusterfs_heketi_allow_own_user_rbac_admin () {
# outputs something like this:
# account = [email protected]
email_unclean=$(gcloud config list | grep account)
# remove the 'account = '
email_clean_untrimmed=${email_unclean#*=}
email_clean="$(trim_whitespace $email_clean_untrimmed)"
kubectl create clusterrolebinding \
"$email_clean-cluster-admin-binding" \
"--clusterrole=cluster-admin" \
"--user=$email_clean"
}
_gke_glusterfs_heketi_allow_default_compute_rbac_admin () {
# outputs something like this:
# Compute Engine default service account [email protected]
email_unclean=$(gcloud iam service-accounts list | grep "Compute Engine default service account")
# remove the 'Compute Engine default service account '
email_clean_untrimmed=${email_unclean#*"service account"}
email_clean="$(trim_whitespace $email_clean_untrimmed)"
kubectl create clusterrolebinding \
"$email_clean-cluster-admin-binding" \
"--clusterrole=cluster-admin" \
"--user=$email_clean"
}
gke_glusterfs_heketi_configure_rbac () {
# requires namespace already be created
kubectl apply -f k8s/00-namespace.yaml
_gke_glusterfs_heketi_allow_own_user_rbac_admin
# default_compute necessary to avoid error during job:
# Error from server (Forbidden): clusterrolebindings.rbac.authorization.k8s.io is forbidden: User "[email protected]" cannot create clusterrolebindings.rbac.authorization.k8s.io at the cluster scope: Required "container.clusterRoleBindings.create" permission.
# Error from server (NotFound): clusterrolebindings.rbac.authorization.k8s.io "heketi-sa-view" not found
_gke_glusterfs_heketi_allow_default_compute_rbac_admin
_gke_glusterfs_heketi_create_service_account
_gke_glusterfs_heketi_add_admin_clusterrolebinding
}
gke_glusterfs_heketi_delete_cluster () {
gcloud container clusters delete "$cluster_name" --quiet
}
gke_glusterfs_heketi_delete_cluster_and_disks () {
gke_glusterfs_heketi_delete_cluster && \
gke_glusterfs_heketi_delete_gluster_disks && \
gcloud compute firewall-rules delete allow-glusterfs --quiet
}
gke_glusterfs_heketi_tail_job_logs () {
kubectl -n glusterfs-heketi-bootstrap logs -f "$(kubectl get po -n glusterfs-heketi-bootstrap -l job-name=glusterfs-heketi-bootstrap -o jsonpath='{.items[0].metadata.name}')"
}
gke_glusterfs_heketi_if_storage_class_not_found_during_k8s_example_run_me () {
external_ip_first_node=$(kubectl get nodes -o jsonpath='{.items[0].status.addresses[?(@.type=="ExternalIP")].address}')
heketi_nodeport=$(kubectl get svc/heketi -n default -o jsonpath='{.spec.ports[0].nodePort}')
storage_class_yaml_path="$project_dir/k8s/heketi-glusterfs-storage-class.yaml"
# ---------------- [START] Generate storage class descriptor ----------------- #
cat > "$storage_class_yaml_path" <<EOF
---
apiVersion: storage.k8s.io/v1beta1
kind: StorageClass
metadata:
name: glusterfs-storage
provisioner: kubernetes.io/glusterfs
parameters:
resturl: "http://${external_ip_first_node}:${heketi_nodeport}"
EOF
# ----------------- [END] Generate storage class descriptor ------------------ #
# ----------------------- [START] Apply storage class ------------------------ #
kubectl apply -f "$storage_class_yaml_path"
# ------------------------ [END] Apply storage class ------------------------- #
}
# -------------------------- [END] helper functions -------------------------- #
# --------------------------- [START] print config --------------------------- #
echo "---------------------------------------------"
echo "Functions available:"
echo "- gke_glusterfs_heketi_build_image"
echo "- gke_glusterfs_heketi_push_image"
echo "- gke_glusterfs_heketi_generate_k8s"
echo "- gke_glusterfs_heketi_configure_rbac"
echo "- gke_glusterfs_heketi_delete_gluster_disks"
echo "- gke_glusterfs_heketi_deploy_glusterfs_heketi"
echo "- gke_glusterfs_heketi_create_cluster"
echo "- gke_glusterfs_heketi_delete_cluster"
echo "- gke_glusterfs_heketi_delete_cluster_and_disks"
echo ""
echo "---------------------------------------------"
echo "Current config:"
echo "- CLOUDSDK_PROJECT_NAME = $project_name"
echo "- CLOUDSDK_CLUSTER_NAME = $cluster_name"
echo "- CLOUDSDK_COMPUTE_ZONE = $zone"
echo "- CLUSTER_VERSION = $cluster_version"
echo "- NODE_COUNT = $node_count"
echo "- MACHINE_TYPE = $machine_type"
echo "- DOCKER_REGISTRY = $docker_registry"
echo "- DOCKER_IMAGE_NAME = $docker_image_name"
echo "- DOCKER_IMAGE_VERSION = $docker_image_version"
echo "---------------------------------------------"
# ---------------------------- [END] print config ---------------------------- #
# --------------- [START] execute command if argument provided --------------- #
if [ ! -z "$1" ]
then
$1
else
echo ""
echo "This script can be run with arguments or by sourcing and calling the functions directly."
echo "Try './helpers gke_glusterfs_heketi_build_image'"
fi
# ---------------- [END] execute command if argument provided ---------------- #
# ------------------------ [START] unset shell options ----------------------- #
# exit script immediately if anything errors
set +e
# allow tracing to be toggled on
if [ "$DEBUG_GKE_GLUSTERFS_HEKETI" = 'true' ]
then
set +x
fi
# ------------------------- [END] unset shell options ------------------------ #