-
Notifications
You must be signed in to change notification settings - Fork 468
/
Copy pathgenerate_helm_chart.sh
executable file
·371 lines (305 loc) · 16.8 KB
/
generate_helm_chart.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/env bash
function usage() {
cat << EOT
Script splits clickhouse-operator-install-bundle.yaml to separate files and adjusts them to conform the helm standards
NOTE script requires some pre-installed tools:
- yq ( https://mikefarah.gitbook.io/yq/ ) > v4.14.x
- jq ( https://github.com/stedolan/jq )
- helm-docs ( https://github.com/norwoodj/helm-docs )
- perl ( https://learn.perl.org/installing/ )
Usage: ./generate_helm_chart.sh
EOT
}
function check_required_tools() {
for cmd in yq jq helm-docs perl; do
if ! command -v "${cmd}" &> /dev/null; then
echo "======================================"
usage
echo "======================================"
echo "The following tool is missing: ${cmd}"
echo "Please install it."
echo "Abort."
exit 1
fi
done
}
set -o errexit
set -o nounset
set -o pipefail
readonly script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
readonly manifest_file_path="${script_dir}/../deploy/operator/clickhouse-operator-install-bundle.yaml"
readonly release_file_path="${script_dir}/../release"
readonly dashboards_path="${script_dir}/../grafana-dashboard"
readonly chart_path="${script_dir}/../deploy/helm/clickhouse-operator"
readonly values_yaml="${chart_path}/values.yaml"
function main() {
echo "Generate HELM chart"
# process manifests from install bundle
local tmpdir
tmpdir=$(mktemp -d)
# shellcheck disable=SC2016
readonly manifest_file_realpath="$(get_abs_filename "${manifest_file_path}")"
(cd "${tmpdir}" && yq e --no-doc -s '$index' "${manifest_file_realpath}")
for f in "${tmpdir}"/*.yml; do
process "${f}"
done
# update version in Chart.yaml
readonly detected_version=$(cat "${release_file_path}")
readonly chart_yaml="${chart_path}/Chart.yaml"
yq e '.version = "'"${detected_version}"'"' -i "${chart_yaml}"
yq e '.appVersion = "'"${detected_version}"'"' -i "${chart_yaml}"
# process grafana dashboards
readonly prom_ds='{"current":{"selected":false,"text":"","value":""},"hide":0,"includeAll":false,"multi":false,"name":"ds_prometheus","options":[],"query":"prometheus","queryValue":"","refresh":1,"regex":"","skipUrlSync":false,"type":"datasource"}'
readonly files_dir="${chart_path}/files"
rm -f "${files_dir}/*"
for dashboard in "${dashboards_path}"/*.json; do
local dashboard_name
dashboard_name=$(basename "${dashboard}")
#echo "${dashboard_name}"
jq '(.templating.list) |= ['"${prom_ds}"'] + .' "${dashboard}" >"${files_dir}/${dashboard_name}"
perl -pi -e 's/"datasource": "\${DS_PROMETHEUS}"/"datasource": {"type":"prometheus","uid":"\${ds_prometheus}"}/g' "${files_dir}/${dashboard_name}"
perl -pi -e 's/"datasource": "\$db"/"datasource": {"type":"vertamedia-clickhouse-datasource","uid":"\${db}"}/g' "${files_dir}/${dashboard_name}"
done
if [[ $(command -v helm-docs) ]]; then
helm-docs --chart-search-root="${chart_path}" --log-level=warning
else
echo "WARNING"
echo "helm-docs is not available, skip docs generation"
fi
}
function process() {
local file="${1}"
local kind=$(yq e '.kind' "${file}")
local name=$(yq e '.metadata.name' "${file}")
local processed_file="${kind}-${name}.yaml"
local copied_file="${processed_file}"
# crds folder use for `helm install` or `helm upgrade --install` to install CRD before deployment
if [[ "${kind}" == "CustomResourceDefinition" ]]; then
# additional copy to templates for CRD, to fix `helm upgrade` wrong behavior
# local templates_dir="${chart_path}/templates/generated"
# copied_file="${templates_dir}/${processed_file}"
# mkdir -p "$(dirname "${copied_file}")"
# cp -f "${file}" "${copied_file}"
local crds_dir="${chart_path}/crds"
processed_file="${crds_dir}/${processed_file}"
else
local templates_dir="${chart_path}/templates/generated"
processed_file="${templates_dir}/${processed_file}"
fi
#echo $(basename "${processed_file}")
mkdir -p "$(dirname "${processed_file}")"
mv -f "${file}" "${processed_file}"
case ${kind} in
# CustomResourceDefinition)
# update_crd_resource "${copied_file}"
# generate_crd_resource "${processed_file}"
# ;;
Service)
update_service_resource "${processed_file}"
;;
Deployment)
update_deployment_resource "${processed_file}"
;;
ConfigMap)
update_configmap_resource "${processed_file}"
;;
ClusterRoleBinding)
update_clusterrolebinding_resource "${processed_file}"
;;
ClusterRole)
update_clusterrole_resource "${processed_file}"
;;
ServiceAccount)
update_serviceaccount_resource "${processed_file}"
;;
Secret)
update_secret_resource "${processed_file}"
;;
CustomResourceDefinition) ;;
*)
echo "Do not know how to process resource kind: ${kind}"
echo "Abort."
exit 1
;;
esac
}
function generate_crd_resource() {
local file="${1}"
version=$(yq e '.version' "${chart_path}/Chart.yaml") chart_name=$(yq e '.name' "${chart_path}/Chart.yaml") yq e -i '.metadata.labels."app.kubernetes.io/managed-by" = "Helm" | .metadata.labels."helm.sh/chart" = env(chart_name) + "-" + env(version) | .metadata.labels."app.kubernetes.io/name" = env(chart_name) | .metadata.labels."app.kubernetes.io/version"= env(version)' "${file}"
}
function update_crd_resource() {
local file="${1}"
yq e -i '.metadata.labels |= "{{ include \"altinity-clickhouse-operator.labels\" . | nindent 4 }}"' "${file}"
perl -pi -e "s/'{/{/g" "${file}"
perl -pi -e "s/}'/}/g" "${file}"
}
function update_service_resource() {
readonly file="${1}"
readonly name=$(yq e '.metadata.name' "${file}")
if [ "${name}" != 'clickhouse-operator-metrics' ]; then
echo "do not know how to process ${name} service"
exit 1
fi
yq e -i '.metadata.name |= "{{ printf \"%s-metrics\" (include \"altinity-clickhouse-operator.fullname\" .) }}"' "${file}"
yq e -i '.metadata.namespace |= "{{ .Release.Namespace }}"' "${file}"
yq e -i '.metadata.labels |= "{{ include \"altinity-clickhouse-operator.labels\" . | nindent 4 }}"' "${file}"
yq e -i '.spec.selector |= "{{ include \"altinity-clickhouse-operator.selectorLabels\" . | nindent 4 }}"' "${file}"
perl -pi -e "s/'//g" "${file}"
}
function update_deployment_resource() {
readonly file="${1}"
readonly name=$(yq e '.metadata.name' "${file}")
if [ "${name}" != 'clickhouse-operator' ]; then
echo "do not know how to process ${name} deployment"
exit 1
fi
yq e -i '.metadata.name |= "{{ include \"altinity-clickhouse-operator.fullname\" . }}"' "${file}"
yq e -i '.metadata.namespace |= "{{ .Release.Namespace }}"' "${file}"
yq e -i '.metadata.labels |= "{{ include \"altinity-clickhouse-operator.labels\" . | nindent 4 }}"' "${file}"
yq e -i '.spec.selector.matchLabels |= "{{ include \"altinity-clickhouse-operator.selectorLabels\" . | nindent 6 }}"' "${file}"
readonly annotations=$(yq e '.spec.template.metadata.annotations' "${file}")
a_data="${annotations}" yq e -i '.podAnnotations |= env(a_data)' "${values_yaml}"
yq e -i '.spec.template.metadata.annotations = {}' "${file}"
yq e -i '.spec.template.metadata.labels |= "{{ include \"altinity-clickhouse-operator.labels\" . | nindent 8 }}"' "${file}"
yq e -i '.spec.template.metadata.annotations += {"{{ toYaml .Values.podAnnotations | nindent 8 }}": null}' "${file}"
yq e -i '.spec.template.spec.imagePullSecrets |= "{{ toYaml .Values.imagePullSecrets | nindent 8 }}"' "${file}"
yq e -i '.spec.template.spec.serviceAccountName |= "{{ include \"altinity-clickhouse-operator.serviceAccountName\" . }}"' "${file}"
yq e -i '.spec.template.spec.nodeSelector |= "{{ toYaml .Values.nodeSelector | nindent 8 }}"' "${file}"
yq e -i '.spec.template.spec.affinity |= "{{ toYaml .Values.affinity | nindent 8 }}"' "${file}"
yq e -i '.spec.template.spec.tolerations |= "{{ toYaml .Values.tolerations | nindent 8 }}"' "${file}"
yq e -i '.spec.template.spec.securityContext |= "{{ toYaml .Values.podSecurityContext | nindent 8 }}"' "${file}"
yq e -i '.spec.template.spec.topologySpreadConstraints |= "{{ toYaml .Values.topologySpreadConstraints | nindent 8 }}"' "${file}"
for cm in $(yq e '.spec.template.spec.volumes[].configMap.name' "${file}"); do
local prefix='{{ include \"altinity-clickhouse-operator.fullname\" . }}'
local newCm="${cm/etc-clickhouse-operator/$prefix}"
newCm="${newCm/etc-keeper-operator/${prefix}-keeper}"
yq e -i '(.spec.template.spec.volumes[].configMap.name | select(. == "'"${cm}"'") | .) |= "'"${newCm}"'"' "${file}"
local cmName="${cm/etc-clickhouse-operator-/}"
cmName="${cmName/etc-keeper-operator-/keeper-}"
yq e -i '.spec.template.metadata.annotations += {"checksum/'"${cmName}"'": "{{ include (print $.Template.BasePath \"/generated/ConfigMap-'"${cm}"'.yaml\") . | sha256sum }}"}' "${file}"
done
yq e -i '.spec.template.spec.containers[0].name |= "{{ .Chart.Name }}"' "${file}"
yq e -i '.spec.template.spec.containers[0].image |= "{{ .Values.operator.image.repository }}:{{ include \"altinity-clickhouse-operator.operator.tag\" . }}"' "${file}"
yq e -i '.spec.template.spec.containers[0].imagePullPolicy |= "{{ .Values.operator.image.pullPolicy }}"' "${file}"
yq e -i '.spec.template.spec.containers[0].resources |= "{{ toYaml .Values.operator.resources | nindent 12 }}"' "${file}"
yq e -i '.spec.template.spec.containers[0].securityContext |= "{{ toYaml .Values.operator.containerSecurityContext | nindent 12 }}"' "${file}"
yq e -i '(.spec.template.spec.containers[0].env[] | select(.valueFrom.resourceFieldRef.containerName == "clickhouse-operator") | .valueFrom.resourceFieldRef.containerName) = "{{ .Chart.Name }}"' "${file}"
yq e -i '.spec.template.spec.containers[0].env += ["{{ with .Values.operator.env }}{{ toYaml . | nindent 12 }}{{ end }}"]' "${file}"
yq e -i '.spec.template.spec.containers[1].image |= "{{ .Values.metrics.image.repository }}:{{ include \"altinity-clickhouse-operator.metrics.tag\" . }}"' "${file}"
yq e -i '.spec.template.spec.containers[1].imagePullPolicy |= "{{ .Values.metrics.image.pullPolicy }}"' "${file}"
yq e -i '.spec.template.spec.containers[1].resources |= "{{ toYaml .Values.metrics.resources | nindent 12 }}"' "${file}"
yq e -i '.spec.template.spec.containers[1].securityContext |= "{{ toYaml .Values.metrics.containerSecurityContext | nindent 12 }}"' "${file}"
yq e -i '(.spec.template.spec.containers[1].env[] | select(.valueFrom.resourceFieldRef.containerName == "clickhouse-operator") | .valueFrom.resourceFieldRef.containerName) = "{{ .Chart.Name }}"' "${file}"
yq e -i '.spec.template.spec.containers[1].env += ["{{ with .Values.metrics.env }}{{ toYaml . | nindent 12 }}{{ end }}"]' "${file}"
perl -pi -e "s/'{{ toYaml .Values.podAnnotations \| nindent 8 }}': null/{{ toYaml .Values.podAnnotations \| nindent 8 }}/g" "${file}"
perl -pi -e "s/- '{{ with .Values.operator.env }}{{ toYaml . \| nindent 12 }}{{ end }}'/{{ with .Values.operator.env }}{{ toYaml . \| nindent 12 }}{{ end }}/g" "${file}"
perl -pi -e "s/- '{{ with .Values.metrics.env }}{{ toYaml . \| nindent 12 }}{{ end }}'/{{ with .Values.metrics.env }}{{ toYaml . \| nindent 12 }}{{ end }}/g" "${file}"
perl -pi -e 's/(\s+\- name: metrics-exporter)/{{ if .Values.metrics.enabled }}\n$1/g' "${file}"
perl -pi -e "s/(\s+imagePullSecrets: '\{\{ toYaml \.Values\.imagePullSecrets \| nindent 8 \}\}')/{{ end }}\n\$1/g" "${file}"
perl -pi -e "s/'//g" "${file}"
}
function update_configmap_resource() {
readonly file="${1}"
readonly name=$(yq e '.metadata.name' "${file}")
local data
data=$(yq e '.data' "${file}")
if [ "${name}" = "etc-clickhouse-operator-files" ]; then
local search='name: "clickhouse-operator"'
local replace="name: '{{ include \"altinity-clickhouse-operator.fullname\" . }}'"
data=${data/"${search}"/"${replace}"}
search='config.yaml: |'
replace='config.yaml:'
data=${data/"${search}"/"${replace}"}
fi
local name_suffix="${name/etc-clickhouse-operator-/}"
local name_suffix="${name_suffix/etc-keeper-operator-/keeper-}"
local camel_cased_name
camel_cased_name=$(to_camel_case "${name_suffix}")
yq e -i '.metadata.name |= "{{ printf \"%s-'"${name_suffix}"'\" (include \"altinity-clickhouse-operator.fullname\" .) }}"' "${file}"
yq e -i '.metadata.namespace |= "{{ .Release.Namespace }}"' "${file}"
yq e -i '.metadata.labels |= "{{ include \"altinity-clickhouse-operator.labels\" . | nindent 4 }}"' "${file}"
yq e -i '.data |= "{{ include \"altinity-clickhouse-operator.configmap-data\" (list . .Values.configs.'"${camel_cased_name}"') | nindent 2 }}"' "${file}"
if [ -z "${data}" ]; then
yq e -i '.configs.'"${camel_cased_name}"' |= null' "${values_yaml}"
else
data_arg="${data}" yq e -i '.configs.'"${camel_cased_name}"' |= env(data_arg)' "${values_yaml}"
fi
perl -pi -e "s/'//g" "${file}"
}
function update_clusterrolebinding_resource() {
readonly file="${1}"
readonly name=$(yq e '.metadata.name' "${file}")
if [ "${name}" != 'clickhouse-operator-kube-system' ]; then
echo "do not know how to process ${name} cluster role binding"
exit 1
fi
yq e -i '.metadata.name |= "{{ include \"altinity-clickhouse-operator.fullname\" . }}"' "${file}"
yq e -i '.metadata.namespace |= "{{ .Release.Namespace }}"' "${file}"
yq e -i '.metadata.labels |= "{{ include \"altinity-clickhouse-operator.labels\" . | nindent 4 }}"' "${file}"
yq e -i '.roleRef.name |= "{{ include \"altinity-clickhouse-operator.fullname\" . }}"' "${file}"
yq e -i '(.subjects[] | select(.kind == "ServiceAccount")) |= with(. ; .name = "{{ include \"altinity-clickhouse-operator.serviceAccountName\" . }}" | .namespace = "{{ .Release.Namespace }}")' "${file}"
printf '%s\n%s\n' '{{- if .Values.rbac.create -}}' "$(cat "${file}")" >"${file}"
printf '%s\n%s\n' "$(cat "${file}")" '{{- end }}' >"${file}"
perl -pi -e "s/'//g" "${file}"
}
function update_clusterrole_resource() {
readonly file="${1}"
readonly name=$(yq e '.metadata.name' "${file}")
if [ "${name}" != 'clickhouse-operator-kube-system' ]; then
echo "do not know how to process ${name} cluster role"
exit 1
fi
yq e -i '.metadata.name |= "{{ include \"altinity-clickhouse-operator.fullname\" . }}"' "${file}"
yq e -i '.metadata.namespace |= "{{ .Release.Namespace }}"' "${file}"
yq e -i '.metadata.labels |= "{{ include \"altinity-clickhouse-operator.labels\" . | nindent 4 }}"' "${file}"
yq e -i '(.rules[] | select(.resourceNames | contains(["clickhouse-operator"])) | .resourceNames) = ["{{ include \"altinity-clickhouse-operator.fullname\" . }}"]' "${file}"
printf '%s\n%s\n' '{{- if .Values.rbac.create -}}' "$(cat "${file}")" >"${file}"
printf '%s\n%s\n' "$(cat "${file}")" '{{- end }}' >"${file}"
perl -pi -e "s/'//g" "${file}"
}
function update_serviceaccount_resource() {
readonly file="${1}"
readonly name=$(yq e '.metadata.name' "${file}")
if [ "${name}" != 'clickhouse-operator' ]; then
echo "do not know how to process ${name} service account"
exit 1
fi
yq e -i '.metadata.name |= "{{ include \"altinity-clickhouse-operator.serviceAccountName\" . }}"' "${file}"
yq e -i '.metadata.namespace |= "{{ .Release.Namespace }}"' "${file}"
yq e -i '.metadata.labels |= "{{ include \"altinity-clickhouse-operator.labels\" . | nindent 4 }}"' "${file}"
yq e -i '.metadata.annotations |= "{{ toYaml .Values.serviceAccount.annotations | nindent 4 }}"' "${file}"
printf '%s\n%s\n' '{{- if .Values.serviceAccount.create -}}' "$(cat "${file}")" >"${file}"
printf '%s\n%s\n' "$(cat "${file}")" '{{- end -}}' >"${file}"
perl -pi -e "s/'//g" "${file}"
}
function update_secret_resource() {
readonly file="${1}"
readonly name=$(yq e '.metadata.name' "${file}")
if [ "${name}" != 'clickhouse-operator' ]; then
echo "do not know how to process ${name} secret"
exit 1
fi
yq e -i '.metadata.name |= "{{ include \"altinity-clickhouse-operator.fullname\" . }}"' "${file}"
yq e -i '.metadata.namespace |= "{{ .Release.Namespace }}"' "${file}"
yq e -i '.metadata.labels |= "{{ include \"altinity-clickhouse-operator.labels\" . | nindent 4 }}"' "${file}"
yq e -i '.data.username |= "{{ .Values.secret.username | b64enc }}"' "${file}"
yq e -i '.data.password |= "{{ .Values.secret.password | b64enc }}"' "${file}"
readonly username=$(yq e '.stringData.username' "${file}")
yq e -i '.secret.username |= "'"${username}"'"' "${values_yaml}"
readonly password=$(yq e '.stringData.password' "${file}")
yq e -i '.secret.password |= "'"${password}"'"' "${values_yaml}"
yq e -i 'del(.stringData)' "${file}"
printf '%s\n%s\n' '{{- if .Values.secret.create -}}' "$(cat "${file}")" >"${file}"
printf '%s\n%s\n' "$(cat "${file}")" '{{- end -}}' >"${file}"
perl -pi -e "s/'//g" "${file}"
}
function to_camel_case() {
readonly str="${1}"
echo "${str}" | awk -F - '{printf "%s", $1; for(i=2; i<=NF; i++) printf "%s", toupper(substr($i,1,1)) substr($i,2); print"";}'
}
function get_abs_filename() {
readonly filename="${1}"
echo "$(cd "$(dirname "${filename}")" && pwd)/$(basename "${filename}")"
}
check_required_tools
main