blob: 8ebaa25314b7574db527637d3a2f7269133ae653 [file] [log] [blame]
Tinoj Josephdd1fd9d2022-08-01 23:59:26 +05301apiVersion: v1
2kind: ConfigMap
3metadata:
4 name: {{ template "redis.fullname" . }}-scripts
5 namespace: {{ .Release.Namespace | quote }}
6 labels:
7 app: {{ template "redis.name" . }}
8 chart: {{ template "redis.chart" . }}
9 heritage: {{ .Release.Service }}
10 release: {{ .Release.Name }}
11data:
12{{- if and .Values.global.redis.cluster.enabled .Values.global.redis.sentinel.enabled }}
13 start-node.sh: |
14 #!/bin/bash
15 is_boolean_yes() {
16 local -r bool="${1:-}"
17 # comparison is performed without regard to the case of alphabetic characters
18 shopt -s nocasematch
19 if [[ "$bool" = 1 || "$bool" =~ ^(yes|true)$ ]]; then
20 true
21 else
22 false
23 fi
24 }
25
26 HEADLESS_SERVICE="{{ template "redis.fullname" . }}-headless.{{ .Release.Namespace }}.svc.{{ .Values.clusterDomain }}"
27 REDIS_SERVICE="{{ template "redis.fullname" . }}.{{ .Release.Namespace }}.svc.{{ .Values.clusterDomain }}"
28
29 export REDIS_REPLICATION_MODE="slave"
30 if [[ -z "$(getent ahosts "$HEADLESS_SERVICE" | grep -v "^$(hostname -i) ")" ]]; then
31 export REDIS_REPLICATION_MODE="master"
32 fi
33
34 {{- if and .Values.securityContext.runAsUser (eq (.Values.securityContext.runAsUser | int) 0) }}
35 useradd redis
36 chown -R redis {{ .Values.slave.persistence.path }}
37 {{- end }}
38
39 if [[ -n $REDIS_PASSWORD_FILE ]]; then
40 password_aux=`cat ${REDIS_PASSWORD_FILE}`
41 export REDIS_PASSWORD=$password_aux
42 fi
43
44 if [[ -n $REDIS_MASTER_PASSWORD_FILE ]]; then
45 password_aux=`cat ${REDIS_MASTER_PASSWORD_FILE}`
46 export REDIS_MASTER_PASSWORD=$password_aux
47 fi
48
49 if [[ "$REDIS_REPLICATION_MODE" == "master" ]]; then
50 echo "I am master"
51 if [[ ! -f /opt/bitnami/redis/etc/master.conf ]];then
52 cp /opt/bitnami/redis/mounted-etc/master.conf /opt/bitnami/redis/etc/master.conf
53 fi
54 else
55 if [[ ! -f /opt/bitnami/redis/etc/replica.conf ]];then
56 cp /opt/bitnami/redis/mounted-etc/replica.conf /opt/bitnami/redis/etc/replica.conf
57 fi
58
59 if is_boolean_yes "$REDIS_TLS_ENABLED"; then
60 sentinel_info_command="redis-cli {{- if .Values.usePassword }} -a $REDIS_PASSWORD {{- end }} -h $REDIS_SERVICE -p {{ .Values.sentinel.port }} --tls --cert ${REDIS_TLS_CERT_FILE} --key ${REDIS_TLS_KEY_FILE} --cacert ${REDIS_TLS_CA_FILE} sentinel get-master-addr-by-name {{ .Values.sentinel.masterSet }}"
61 else
62 sentinel_info_command="redis-cli {{- if .Values.usePassword }} -a $REDIS_PASSWORD {{- end }} -h $REDIS_SERVICE -p {{ .Values.sentinel.port }} sentinel get-master-addr-by-name {{ .Values.sentinel.masterSet }}"
63 fi
64 REDIS_SENTINEL_INFO=($($sentinel_info_command))
65 REDIS_MASTER_HOST=${REDIS_SENTINEL_INFO[0]}
66 REDIS_MASTER_PORT_NUMBER=${REDIS_SENTINEL_INFO[1]}
67
68
69 # Immediately attempt to connect to the reported master. If it doesn't exist the connection attempt will either hang
70 # or fail with "port unreachable" and give no data. The liveness check will then timeout waiting for the redis
71 # container to be ready and restart the it. By then the new master will likely have been elected
72 if is_boolean_yes "$REDIS_TLS_ENABLED"; then
73 sentinel_info_command="redis-cli {{- if .Values.usePassword }} -a $REDIS_PASSWORD {{- end }} -h $REDIS_MASTER_HOST -p {{ .Values.sentinel.port }} --tls --cert ${REDIS_TLS_CERT_FILE} --key ${REDIS_TLS_KEY_FILE} --cacert ${REDIS_TLS_CA_FILE} sentinel get-master-addr-by-name {{ .Values.sentinel.masterSet }}"
74 else
75 sentinel_info_command="redis-cli {{- if .Values.usePassword }} -a $REDIS_PASSWORD {{- end }} -h $REDIS_MASTER_HOST -p {{ .Values.sentinel.port }} sentinel get-master-addr-by-name {{ .Values.sentinel.masterSet }}"
76 fi
77
78 if [[ ! ($($sentinel_info_command)) ]]; then
79 # master doesn't actually exist, this probably means the remaining pods haven't elected a new one yet
80 # and are reporting the old one still. Once this happens the container will get stuck and never see the new
81 # master. We stop here to allow the container to not pass the liveness check and be restarted.
82 exit 1
83 fi
84 fi
85
86 if [[ ! -f /opt/bitnami/redis/etc/redis.conf ]];then
87 cp /opt/bitnami/redis/mounted-etc/redis.conf /opt/bitnami/redis/etc/redis.conf
88 fi
89 {{- if .Values.tls.enabled }}
90 ARGS=("--port" "0")
91 ARGS+=("--tls-port" "${REDIS_TLS_PORT}")
92 ARGS+=("--tls-cert-file" "${REDIS_TLS_CERT_FILE}")
93 ARGS+=("--tls-key-file" "${REDIS_TLS_KEY_FILE}")
94 ARGS+=("--tls-ca-cert-file" "${REDIS_TLS_CA_FILE}")
95 ARGS+=("--tls-auth-clients" "${REDIS_TLS_AUTH_CLIENTS}")
96 ARGS+=("--tls-replication" "yes")
97 {{- if .Values.tls.dhParamsFilename }}
98 ARGS+=("--tls-dh-params-file" "${REDIS_TLS_DH_PARAMS_FILE}")
99 {{- end }}
100 {{- else }}
101 ARGS=("--port" "${REDIS_PORT}")
102 {{- end }}
103
104 if [[ "$REDIS_REPLICATION_MODE" == "slave" ]]; then
105 ARGS+=("--slaveof" "${REDIS_MASTER_HOST}" "${REDIS_MASTER_PORT_NUMBER}")
106 fi
107
108 {{- if .Values.usePassword }}
109 ARGS+=("--requirepass" "${REDIS_PASSWORD}")
110 ARGS+=("--masterauth" "${REDIS_MASTER_PASSWORD}")
111 {{- else }}
112 ARGS+=("--protected-mode" "no")
113 {{- end }}
114
115 if [[ "$REDIS_REPLICATION_MODE" == "master" ]]; then
116 ARGS+=("--include" "/opt/bitnami/redis/etc/master.conf")
117 else
118 ARGS+=("--include" "/opt/bitnami/redis/etc/replica.conf")
119 fi
120
121 ARGS+=("--include" "/opt/bitnami/redis/etc/redis.conf")
122 {{- if .Values.slave.extraFlags }}
123 {{- range .Values.slave.extraFlags }}
124 ARGS+=({{ . | quote }})
125 {{- end }}
126 {{- end }}
127
128 {{- if .Values.slave.preExecCmds }}
129 {{ .Values.slave.preExecCmds | nindent 4}}
130 {{- end }}
131
132 {{- if .Values.slave.command }}
133 exec {{ .Values.slave.command }} "${ARGS[@]}"
134 {{- else }}
135 exec redis-server "${ARGS[@]}"
136 {{- end }}
137
138 start-sentinel.sh: |
139 #!/bin/bash
140 replace_in_file() {
141 local filename="${1:?filename is required}"
142 local match_regex="${2:?match regex is required}"
143 local substitute_regex="${3:?substitute regex is required}"
144 local posix_regex=${4:-true}
145
146 local result
147
148 # We should avoid using 'sed in-place' substitutions
149 # 1) They are not compatible with files mounted from ConfigMap(s)
150 # 2) We found incompatibility issues with Debian10 and "in-place" substitutions
151 del=$'\001' # Use a non-printable character as a 'sed' delimiter to avoid issues
152 if [[ $posix_regex = true ]]; then
153 result="$(sed -E "s${del}${match_regex}${del}${substitute_regex}${del}g" "$filename")"
154 else
155 result="$(sed "s${del}${match_regex}${del}${substitute_regex}${del}g" "$filename")"
156 fi
157 echo "$result" > "$filename"
158 }
159 sentinel_conf_set() {
160 local -r key="${1:?missing key}"
161 local value="${2:-}"
162
163 # Sanitize inputs
164 value="${value//\\/\\\\}"
165 value="${value//&/\\&}"
166 value="${value//\?/\\?}"
167 [[ "$value" = "" ]] && value="\"$value\""
168
169 replace_in_file "/opt/bitnami/redis-sentinel/etc/sentinel.conf" "^#*\s*${key} .*" "${key} ${value}" false
170 }
171 sentinel_conf_add() {
172 echo $'\n'"$@" >> "/opt/bitnami/redis-sentinel/etc/sentinel.conf"
173 }
174 is_boolean_yes() {
175 local -r bool="${1:-}"
176 # comparison is performed without regard to the case of alphabetic characters
177 shopt -s nocasematch
178 if [[ "$bool" = 1 || "$bool" =~ ^(yes|true)$ ]]; then
179 true
180 else
181 false
182 fi
183 }
184 host_id() {
185 echo "$1" | openssl sha1 | awk '{print $2}'
186 }
187
188 HEADLESS_SERVICE="{{ template "redis.fullname" . }}-headless.{{ .Release.Namespace }}.svc.{{ .Values.clusterDomain }}"
189 REDIS_SERVICE="{{ template "redis.fullname" . }}.{{ .Release.Namespace }}.svc.{{ .Values.clusterDomain }}"
190
191 if [[ -n $REDIS_PASSWORD_FILE ]]; then
192 password_aux=`cat ${REDIS_PASSWORD_FILE}`
193 export REDIS_PASSWORD=$password_aux
194 fi
195
196 if [[ ! -f /opt/bitnami/redis-sentinel/etc/sentinel.conf ]]; then
197 cp /opt/bitnami/redis-sentinel/mounted-etc/sentinel.conf /opt/bitnami/redis-sentinel/etc/sentinel.conf
198 {{- if .Values.usePassword }}
199 printf "\nsentinel auth-pass %s %s" "{{ .Values.sentinel.masterSet }}" "$REDIS_PASSWORD" >> /opt/bitnami/redis-sentinel/etc/sentinel.conf
200 {{- if .Values.sentinel.usePassword }}
201 printf "\nrequirepass %s" "$REDIS_PASSWORD" >> /opt/bitnami/redis-sentinel/etc/sentinel.conf
202 {{- end }}
203 {{- end }}
204 {{- if .Values.sentinel.staticID }}
205 printf "\nsentinel myid %s" "$(host_id "$HOSTNAME")" >> /opt/bitnami/redis-sentinel/etc/sentinel.conf
206 {{- end }}
207 fi
208
209 export REDIS_REPLICATION_MODE="slave"
210 if [[ -z "$(getent ahosts "$HEADLESS_SERVICE" | grep -v "^$(hostname -i) ")" ]]; then
211 export REDIS_REPLICATION_MODE="master"
212 fi
213
214 if [[ "$REDIS_REPLICATION_MODE" == "master" ]]; then
215 REDIS_MASTER_HOST="$(hostname -i)"
216 REDIS_MASTER_PORT_NUMBER="{{ .Values.redisPort }}"
217 else
218 if is_boolean_yes "$REDIS_SENTINEL_TLS_ENABLED"; then
219 sentinel_info_command="redis-cli {{- if .Values.usePassword }} -a $REDIS_PASSWORD {{- end }} -h $REDIS_SERVICE -p {{ .Values.sentinel.port }} --tls --cert ${REDIS_SENTINEL_TLS_CERT_FILE} --key ${REDIS_SENTINEL_TLS_KEY_FILE} --cacert ${REDIS_SENTINEL_TLS_CA_FILE} sentinel get-master-addr-by-name {{ .Values.sentinel.masterSet }}"
220 else
221 sentinel_info_command="redis-cli {{- if .Values.usePassword }} -a $REDIS_PASSWORD {{- end }} -h $REDIS_SERVICE -p {{ .Values.sentinel.port }} sentinel get-master-addr-by-name {{ .Values.sentinel.masterSet }}"
222 fi
223 REDIS_SENTINEL_INFO=($($sentinel_info_command))
224 REDIS_MASTER_HOST=${REDIS_SENTINEL_INFO[0]}
225 REDIS_MASTER_PORT_NUMBER=${REDIS_SENTINEL_INFO[1]}
226
227 # Immediately attempt to connect to the reported master. If it doesn't exist the connection attempt will either hang
228 # or fail with "port unreachable" and give no data. The liveness check will then timeout waiting for the sentinel
229 # container to be ready and restart the it. By then the new master will likely have been elected
230 if is_boolean_yes "$REDIS_SENTINEL_TLS_ENABLED"; then
231 sentinel_info_command="redis-cli {{- if .Values.usePassword }} -a $REDIS_PASSWORD {{- end }} -h $REDIS_MASTER_HOST -p {{ .Values.sentinel.port }} --tls --cert ${REDIS_SENTINEL_TLS_CERT_FILE} --key ${REDIS_SENTINEL_TLS_KEY_FILE} --cacert ${REDIS_SENTINEL_TLS_CA_FILE} sentinel get-master-addr-by-name {{ .Values.sentinel.masterSet }}"
232 else
233 sentinel_info_command="redis-cli {{- if .Values.usePassword }} -a $REDIS_PASSWORD {{- end }} -h $REDIS_MASTER_HOST -p {{ .Values.sentinel.port }} sentinel get-master-addr-by-name {{ .Values.sentinel.masterSet }}"
234 fi
235
236 if [[ ! ($($sentinel_info_command)) ]]; then
237 # master doesn't actually exist, this probably means the remaining pods haven't elected a new one yet
238 # and are reporting the old one still. Once this happens the container will get stuck and never see the new
239 # master. We stop here to allow the container to not pass the liveness check and be restarted.
240 exit 1
241 fi
242 fi
243 sentinel_conf_set "sentinel monitor" "{{ .Values.sentinel.masterSet }} "$REDIS_MASTER_HOST" "$REDIS_MASTER_PORT_NUMBER" {{ .Values.sentinel.quorum }}"
244
245 add_replica() {
246 if [[ "$1" != "$REDIS_MASTER_HOST" ]]; then
247 sentinel_conf_add "sentinel known-replica {{ .Values.sentinel.masterSet }} $1 {{ .Values.redisPort }}"
248 fi
249 }
250
251 {{- if .Values.sentinel.staticID }}
252 # remove generated known sentinels and replicas
253 tmp="$(sed -e '/^sentinel known-/d' -e '/^$/d' /opt/bitnami/redis-sentinel/etc/sentinel.conf)"
254 echo "$tmp" > /opt/bitnami/redis-sentinel/etc/sentinel.conf
255
256 for node in $(seq 0 {{ .Values.cluster.slaveCount }}); do
257 NAME="{{ template "redis.fullname" . }}-node-$node"
258 IP="$(getent hosts "$NAME.$HEADLESS_SERVICE" | awk ' {print $1 }')"
259 if [[ "$NAME" != "$HOSTNAME" && -n "$IP" ]]; then
260 sentinel_conf_add "sentinel known-sentinel {{ .Values.sentinel.masterSet }} $IP {{ .Values.sentinel.port }} $(host_id "$NAME")"
261 add_replica "$IP"
262 fi
263 done
264 add_replica "$(hostname -i)"
265 {{- end }}
266
267 {{- if .Values.tls.enabled }}
268 ARGS=("--port" "0")
269 ARGS+=("--tls-port" "${REDIS_SENTINEL_TLS_PORT_NUMBER}")
270 ARGS+=("--tls-cert-file" "${REDIS_SENTINEL_TLS_CERT_FILE}")
271 ARGS+=("--tls-key-file" "${REDIS_SENTINEL_TLS_KEY_FILE}")
272 ARGS+=("--tls-ca-cert-file" "${REDIS_SENTINEL_TLS_CA_FILE}")
273 ARGS+=("--tls-replication" "yes")
274 ARGS+=("--tls-auth-clients" "${REDIS_SENTINEL_TLS_AUTH_CLIENTS}")
275 {{- if .Values.tls.dhParamsFilename }}
276 ARGS+=("--tls-dh-params-file" "${REDIS_SENTINEL_TLS_DH_PARAMS_FILE}")
277 {{- end }}
278 {{- end }}
279 {{- if .Values.sentinel.preExecCmds }}
280 {{ .Values.sentinel.preExecCmds | nindent 4 }}
281 {{- end }}
282 exec redis-server /opt/bitnami/redis-sentinel/etc/sentinel.conf --sentinel {{- if .Values.tls.enabled }} "${ARGS[@]}" {{- end }}
283{{- else }}
284 start-master.sh: |
285 #!/bin/bash
286 echo "y" | /opt/bitnami/redis/bin/redis-check-aof --fix /data/appendonly.aof
287 {{- if and .Values.securityContext.runAsUser (eq (.Values.securityContext.runAsUser | int) 0) }}
288 useradd redis
289 chown -R redis {{ .Values.master.persistence.path }}
290 {{- end }}
291 if [[ -n $REDIS_PASSWORD_FILE ]]; then
292 password_aux=`cat ${REDIS_PASSWORD_FILE}`
293 export REDIS_PASSWORD=$password_aux
294 fi
295 if [[ ! -f /opt/bitnami/redis/etc/master.conf ]];then
296 cp /opt/bitnami/redis/mounted-etc/master.conf /opt/bitnami/redis/etc/master.conf
297 fi
298 if [[ ! -f /opt/bitnami/redis/etc/redis.conf ]];then
299 cp /opt/bitnami/redis/mounted-etc/redis.conf /opt/bitnami/redis/etc/redis.conf
300 fi
301 {{- if .Values.tls.enabled }}
302 ARGS=("--port" "0")
303 ARGS+=("--tls-port" "${REDIS_TLS_PORT}")
304 ARGS+=("--tls-cert-file" "${REDIS_TLS_CERT_FILE}")
305 ARGS+=("--tls-key-file" "${REDIS_TLS_KEY_FILE}")
306 ARGS+=("--tls-ca-cert-file" "${REDIS_TLS_CA_FILE}")
307 ARGS+=("--tls-auth-clients" "${REDIS_TLS_AUTH_CLIENTS}")
308 {{- if .Values.tls.dhParamsFilename }}
309 ARGS+=("--tls-dh-params-file" "${REDIS_TLS_DH_PARAMS_FILE}")
310 {{- end }}
311 {{- else }}
312 ARGS=("--port" "${REDIS_PORT}")
313 {{- end }}
314 {{- if .Values.usePassword }}
315 ARGS+=("--requirepass" "${REDIS_PASSWORD}")
316 ARGS+=("--masterauth" "${REDIS_PASSWORD}")
317 {{- else }}
318 ARGS+=("--protected-mode" "no")
319 {{- end }}
320 ARGS+=("--include" "/opt/bitnami/redis/etc/redis.conf")
321 ARGS+=("--include" "/opt/bitnami/redis/etc/master.conf")
322 {{- if .Values.master.extraFlags }}
323 {{- range .Values.master.extraFlags }}
324 ARGS+=({{ . | quote }})
325 {{- end }}
326 {{- end }}
327 {{- if .Values.master.preExecCmds }}
328 {{ .Values.master.preExecCmds | nindent 4}}
329 {{- end }}
330 {{- if .Values.master.command }}
331 exec {{ .Values.master.command }} "${ARGS[@]}"
332 {{- else }}
333 exec redis-server "${ARGS[@]}"
334 {{- end }}
335 {{- if .Values.global.redis.cluster.enabled }}
336 start-slave.sh: |
337 #!/bin/bash
338 {{- if and .Values.securityContext.runAsUser (eq (.Values.securityContext.runAsUser | int) 0) }}
339 useradd redis
340 chown -R redis {{ .Values.slave.persistence.path }}
341 {{- end }}
342 if [[ -n $REDIS_PASSWORD_FILE ]]; then
343 password_aux=`cat ${REDIS_PASSWORD_FILE}`
344 export REDIS_PASSWORD=$password_aux
345 fi
346 if [[ -n $REDIS_MASTER_PASSWORD_FILE ]]; then
347 password_aux=`cat ${REDIS_MASTER_PASSWORD_FILE}`
348 export REDIS_MASTER_PASSWORD=$password_aux
349 fi
350 if [[ ! -f /opt/bitnami/redis/etc/replica.conf ]];then
351 cp /opt/bitnami/redis/mounted-etc/replica.conf /opt/bitnami/redis/etc/replica.conf
352 fi
353 if [[ ! -f /opt/bitnami/redis/etc/redis.conf ]];then
354 cp /opt/bitnami/redis/mounted-etc/redis.conf /opt/bitnami/redis/etc/redis.conf
355 fi
356 {{- if .Values.tls.enabled }}
357 ARGS=("--port" "0")
358 ARGS+=("--tls-port" "${REDIS_TLS_PORT}")
359 ARGS+=("--tls-cert-file" "${REDIS_TLS_CERT_FILE}")
360 ARGS+=("--tls-key-file" "${REDIS_TLS_KEY_FILE}")
361 ARGS+=("--tls-ca-cert-file" "${REDIS_TLS_CA_FILE}")
362 ARGS+=("--tls-auth-clients" "${REDIS_TLS_AUTH_CLIENTS}")
363 ARGS+=("--tls-replication" "yes")
364 {{- if .Values.tls.dhParamsFilename }}
365 ARGS+=("--tls-dh-params-file" "${REDIS_TLS_DH_PARAMS_FILE}")
366 {{- end }}
367 {{- else }}
368 ARGS=("--port" "${REDIS_PORT}")
369 {{- end }}
370 ARGS+=("--slaveof" "${REDIS_MASTER_HOST}" "${REDIS_MASTER_PORT_NUMBER}")
371 {{- if .Values.usePassword }}
372 ARGS+=("--requirepass" "${REDIS_PASSWORD}")
373 ARGS+=("--masterauth" "${REDIS_MASTER_PASSWORD}")
374 {{- else }}
375 ARGS+=("--protected-mode" "no")
376 {{- end }}
377 ARGS+=("--include" "/opt/bitnami/redis/etc/redis.conf")
378 ARGS+=("--include" "/opt/bitnami/redis/etc/replica.conf")
379 {{- if .Values.slave.extraFlags }}
380 {{- range .Values.slave.extraFlags }}
381 ARGS+=({{ . | quote }})
382 {{- end }}
383 {{- end }}
384 {{- if .Values.slave.preExecCmds }}
385 {{ .Values.slave.preExecCmds | nindent 4}}
386 {{- end }}
387 {{- if .Values.slave.command }}
388 exec {{ .Values.slave.command }} "${ARGS[@]}"
389 {{- else }}
390 exec redis-server "${ARGS[@]}"
391 {{- end }}
392 {{- end }}
393
394{{- end -}}