-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdeploy-grafana.sh
executable file
Β·355 lines (279 loc) Β· 10.1 KB
/
deploy-grafana.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
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
#!/usr/bin/env bash
set -eu -o pipefail
PROJECT_ROOT="$(git rev-parse --show-toplevel)"
declare -r PROJECT_ROOT
declare -r BACKUP_DIR=${BACKUP_DIR:-"$PROJECT_ROOT/tmp/grafana-deployment/$(date +%Y-%m-%d-%H-%M-%S)"}
declare -r CMO_ENABLE_UWM_CFG=hack/dashboard/openshift/uwm/00-openshift-monitoring-user-projects.yaml
declare -r MON_NS=openshift-monitoring
declare -r UWM_NS=openshift-user-workload-monitoring
declare -r CMO_CM=cluster-monitoring-config
declare -r BACKUP_CMO_CFG="$BACKUP_DIR/cmo-cm.yaml"
declare -r UWM_URL="https://docs.openshift.com/container-platform/latest/observability/monitoring/enabling-monitoring-for-user-defined-projects.html"
declare -r UWM_CONFIG_URL="https://docs.openshift.com/container-platform/latest/observability/monitoring/configuring-the-monitoring-stack.html#configuring-the-monitoring-stack_configuring-the-monitoring-stack"
declare -r GRAFANA_NS=kepler-grafana
declare -r GRAFANA_SA=grafana
source "$PROJECT_ROOT/hack/utils.bash"
oc_apply_grafana_ns() {
run oc apply -n "$GRAFANA_NS" "$@"
}
oc_replace_grafana_ns() {
oc replace --force -n "$GRAFANA_NS" "$@"
}
oc_get_grafana_ns() {
oc get -n "$GRAFANA_NS" "$@"
}
oc_delete_grafana_ns() {
run oc -n "$GRAFANA_NS" delete --ignore-not-found=true "$@"
}
oc_create_grafana_ns() {
run oc -n "$GRAFANA_NS" create "$@"
}
validate_cluster() {
header "Validating cluster"
command -v oc >/dev/null 2>&1 || {
fail "No oc command found in PATH"
info "Please install oc"
cat <<-EOF
curl -sNL https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/4.16.0/openshift-client-linux.tar.gz |
tar -xzf - -C <install/path>
EOF
# NOTE: do not proceed without oc installed
return 1
}
local -i ret=0
local oc_version=""
oc_version=$(oc version --client -oyaml | grep releaseClientVersion: | cut -f2 -d:)
[[ -z "$oc_version" ]] && {
oc_version=$(oc version --client -oyaml | grep gitVersion | cut -f2 -d:)
}
local -i oc_major_version oc_minor_version
oc_major_version=$(echo "$oc_version" | cut -f1 -d.)
oc_minor_version=$(echo "$oc_version" | cut -f2 -d.)
info "Found oc version: $oc_version -> ($oc_major_version.$oc_minor_version.z)"
[[ $oc_major_version -lt 4 ]] || [[ $oc_minor_version -lt 12 ]] && {
fail "oc version '$oc_version' should be at least 4.12.0"
info "install a newer version of oc"
cat <<-EOF
curl -sNL https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/4.13.0/openshift-client-linux.tar.gz |
tar -xzf - -C <install/path>
EOF
ret=1
}
oc get ns $MON_NS -o name >/dev/null 2>&1 || {
fail "$MON_NS namespace missing. Is this an OpenShift cluster?"
info "cluster: $(oc whoami --show-server)\n"
ret=1
}
oc get crds keplers.kepler.system.sustainable.computing.io || {
fail "Missing Kepler CRD. Is Kepler Operator Installed?\n"
ret=1
}
oc get kepler kepler >/dev/null 2>&1 || {
fail "Missing kepler instance. Did you forget to create kepler?"
info_run "oc apply -f ./config/samples/kepler.system_v1alpha1_kepler.yaml"
ret=1
}
return $ret
}
wait_for_kepler_to_be_available() {
header "waiting for kepler to be available"
wait_until 10 10 "kepler to be available" condition_check "True" oc get kepler kepler \
-o jsonpath="{.status.exporter.conditions[?(@.type=='Available')].status}" && {
ok "kepler is available"
return 0
}
fail "kepler not ready"
run oc get kepler
line 50
oc get kepler kepler -o jsonpath="$(
cat <<-EOF
{range .status.exporter.conditions[?(@.status!="True")]}
{" * "}{.type}{":"} {.status}
{.reason}
{.message}
{end}
EOF
)"
line 50
echo
info "Please check the operator logs for more details."
info_run "oc logs -n openshift-operators deployment/kepler-operator-controller"
return 1
}
enable_userworkload_monitoring() {
header "Enabling Openshift User Project Monitoring"
info "checking if user project monitoring is already enabled"
[[ $(oc get prometheus -n $UWM_NS -o name | wc -l) -gt 0 ]] && {
ok "User project monitoring already enabled; no further action is required"
return 0
}
backup_monitoring_config
if [[ -f "$BACKUP_CMO_CFG" ]]; then
patch_enable_uwm
else
setup_new_uwm
fi
show_restore_info
wait_until 10 10 "$UWM_NS to be created" oc get ns "$UWM_NS"
wait_until 10 10 "User Workload Prometheus to be created" \
oc wait --for condition=Available -n "$UWM_NS" prometheus user-workload
}
patch_enable_uwm() {
info "Patching existing config to enable User Project Monitoring"
line 60
if grep enableUserWorkload "$BACKUP_CMO_CFG"; then
info "toggling enableUserWorkload to true"
sed -e 's|enableUserWorkload:.*|enableUserWorkload: true π |g' <"$BACKUP_CMO_CFG"
sed -e 's|enableUserWorkload:.*|enableUserWorkload: true|g' <"$BACKUP_CMO_CFG" |
oc apply --server-side --force-conflicts -f-
else
info "adding enableUserWorkload to true"
sed \
-e 's|\(config.yaml.*\)|\1\n enableUserWorkload: true π\r|g' \
"$BACKUP_CMO_CFG"
sed \
-e 's|\(config.yaml.*\)|\1\n enableUserWorkload: true\r|g' \
"$BACKUP_CMO_CFG" |
oc apply --server-side --force-conflicts -f-
fi
}
setup_new_uwm() {
info "Creating new config to enable User Project Monitoring"
line 60
cat $CMO_ENABLE_UWM_CFG
line 60
run oc apply -n "$MON_NS" -f "$CMO_ENABLE_UWM_CFG"
}
show_restore_info() {
line 60 heavy
warn "π± In the event cluster-monitoring-operator becomes π degraded π±"
cat <<-EOF
* Restore the configuration $BACKUP_CMO_CFG
* Manually enable User Project Monitoring
π‘ see: $UWM_URL
* Rerun this script
EOF
line 60 heavy
sleep 10
}
show_uwm_info() {
info "Kepler use prometheus deployed in $UWM_NS to store metrics. To configure Prometheus to cater to needs of the cluster such as:"
cat <<-EOF
* Increase data retention for in-depth analysis
* Allocate more resources based on requirements
π‘ see: $UWM_CONFIG_URL
EOF
line 55 heavy
}
backup_monitoring_config() {
info "Backing up current monitoring configuration"
oc get -n "$MON_NS" configmap "$CMO_CM" || {
info "No monitoring config exists; will create a new one"
return
}
# backup but remove last-applied-configuration
oc get -n "$MON_NS" configmap "$CMO_CM" -o yaml |
grep -Ev 'kubectl.kubernetes.io/last-applied-configuration|{"apiVersion":|creationTimestamp:|resourceVersion:|uid:' >"$BACKUP_CMO_CFG" || {
fail "failed to backup $CMO_CM in $MON_NS"
return 1
}
ok "Backed up $CMO_CM config to $BACKUP_DIR"
}
setup_grafana() {
header "Setting up Grafana"
info "Creating Grafana resources"
# NOTE: create | apply will create only if needed
info "Creating grafana namespace - $GRAFANA_NS"
oc create ns $GRAFANA_NS --dry-run=client -o yaml | oc apply -f -
oc_apply_grafana_ns -k hack/dashboard/openshift/grafana-deploy
wait_until 20 10 "Grafana to be deployed" \
oc get grafana,grafanadatasource
wait_until 10 10 "Grafana CRDs to be Established" \
oc wait --for=condition=Established crd grafanas.grafana.integreatly.org
wait_until 10 10 "Grafana Datasource CRDs to be Established" \
oc wait --for=condition=Established crd grafanadatasources.grafana.integreatly.org
ok "grafana crds created\n"
info "Creating a grafana instance"
oc_apply_grafana_ns -f hack/dashboard/openshift/grafana-config/01-grafana-instance.yaml
ok "Grafana created"
}
config_grafana_sa() {
header "Grafana User Project Monitoring Setup"
oc_apply_grafana_ns -f hack/dashboard/openshift/grafana-config/02-grafana-sa.yaml
wait_until 20 2 "Grafana Service Account" \
oc_get_grafana_ns serviceaccount $GRAFANA_SA
run oc adm policy add-cluster-role-to-user cluster-monitoring-view \
-z $GRAFANA_SA -n "$GRAFANA_NS"
ok "grafana $GRAFANA_SA added to cluster-monitoring-view"
}
setup_grafana_dashboard() {
header "Setting up Grafana dashboard"
info "Creating datasource"
local sa_token=""
# sa_token="$(oc -n "$GRAFANA_NS" create token "$GRAFANA_SA")"
oc_create_grafana_ns token --duration=8760h "$GRAFANA_SA" >tmp/grafana-token
sa_token=$(cat tmp/grafana-token)
# Deploy from updated manifest
BEARER_TOKEN="$sa_token" \
envsubst <hack/dashboard/openshift/grafana-config/03-grafana-datasource-UPDATETHIS.yaml |
oc apply -n "$GRAFANA_NS" -f -
ok "created grafana datasource \n"
info "Creating dashboard config map"
oc_delete_grafana_ns configmap kepler-dashboard-cm
oc_create_grafana_ns configmap kepler-dashboard-cm --from-file=hack/dashboard/assets/kepler/dashboard.json
oc_delete_grafana_ns configmap prometheus-dashboard-cm
oc_create_grafana_ns configmap prometheus-dashboard-cm --from-file=hack/dashboard/assets/prometheus/dashboard.json
info "Creating Dashboard"
oc_delete_grafana_ns -f hack/dashboard/openshift/grafana-config/04-kepler-dashboard.yaml
oc_create_grafana_ns -f hack/dashboard/openshift/grafana-config/04-kepler-dashboard.yaml
oc_delete_grafana_ns -f hack/dashboard/openshift/grafana-config/05-prometheus-dashboard.yaml
oc_create_grafana_ns -f hack/dashboard/openshift/grafana-config/05-prometheus-dashboard.yaml
# NOTE: route name is dependent on the grafana instance
wait_until 20 2 "Grafana dashboard" \
oc_get_grafana_ns route kepler-grafana-route
ok "created grafana dashboard\n"
}
grafana_login_url() {
local grafana_url=""
echo "https://$(oc_get_grafana_ns route kepler-grafana-route -o jsonpath='{.spec.host}')/login"
}
show_key_info() {
header "Grafana Dashboard Setup Complete"
local grafana_url=""
grafana_url=$(grafana_login_url)
# disable use find instead of ls
# shellcheck disable=SC2012
[[ -d "$BACKUP_DIR" ]] && cat <<-EOF
π¦ Cluster Monitoring Configuration
Backup Directory: $BACKUP_DIR
$(ls "$BACKUP_DIR" | sed -e "s|^| β’ |g")
EOF
cat <<-EOF
π Grafana Configuration:
Dashboard URL: $grafana_url
Admin: kepler
Password: kepler
EOF
line 55 heavy
}
main() {
cd "$PROJECT_ROOT"
mkdir -p "$BACKUP_DIR"
validate_cluster || {
line 60 heavy
fail "Cluster validation failed!"
info "Fix issues reported above and rerun the script\n\n"
return 1
}
ok "cluster validated"
wait_for_kepler_to_be_available
enable_userworkload_monitoring
setup_grafana
config_grafana_sa
setup_grafana_dashboard
# remove backup dir if no backups were made
[[ -z "$(ls -A "$BACKUP_DIR")" ]] && rm -rf "$BACKUP_DIR"
show_key_info
show_uwm_info
}
main "$@"