Skip to content

Commit 7aea719

Browse files
egabancholindhe
andauthored
config: enhance database/RabbitMQ connection configuration
Co-authored-by: Andreas Lindhé <[email protected]>
1 parent c49d980 commit 7aea719

File tree

7 files changed

+181
-94
lines changed

7 files changed

+181
-94
lines changed

charts/invenio/templates/_helpers.tpl

+120-68
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
8383
{{- end }}
8484
{{- end -}}
8585

86-
####################### RabbitMQ password secret #######################
86+
####################### RabbitMQ connection configuration #######################
8787
{{/*
8888
This template renders the name of the secret that stores the password for RabbitMQ.
8989
*/}}
@@ -95,19 +95,17 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
9595
{{- end }}
9696
{{- end -}}
9797

98-
########################## RabbitMQ username ##########################
9998
{{/*
10099
This template renders the username for accessing RabbitMQ.
101100
*/}}
102101
{{- define "invenio.rabbitmq.username" -}}
103102
{{- if .Values.rabbitmq.enabled }}
104103
{{- required "Missing .Values.rabbitmq.auth.username" .Values.rabbitmq.auth.username -}}
105104
{{- else }}
106-
{{- required "Missing .Values.rabbitmqExternal.username" .Values.rabbitmqExternal.username -}}
105+
{{- required "Missing .Values.rabbitmqExternal.username" (tpl .Values.rabbitmqExternal.username .) -}}
107106
{{- end }}
108107
{{- end -}}
109108

110-
########################## RabbitMQ password ##########################
111109
{{/*
112110
This template renders the password for accessing RabbitMQ.
113111
*/}}
@@ -119,43 +117,61 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
119117
{{- end }}
120118
{{- end -}}
121119

122-
########################## RabbitMQ AMQP port ##########################
120+
{{/*
121+
Get the database password secret name
122+
*/}}
123+
{{- define "invenio.rabbitmq.secretName" -}}
124+
{{- if .Values.rabbitmq.enabled -}}
125+
{{- required "Missing .Values.rabbitmq.auth.existingPasswordSecret" (tpl .Values.rabbitmq.auth.existingPasswordSecret .) -}}
126+
{{- else -}}
127+
{{- required "Missing .Values.rabbitmqExternal.existingSecret" (tpl .Values.rabbitmqExternal.existingSecret .) -}}
128+
{{- end -}}
129+
{{- end -}}
130+
131+
{{/*
132+
Get the database password secret key
133+
*/}}
134+
{{- define "invenio.rabbitmq.secretKey" -}}
135+
{{- if .Values.rabbitmq.enabled -}}
136+
{{- required "Missing .Values.rabbitmq.auth.existingSecretPasswordKey" .Values.rabbitmq.auth.existingSecretPasswordKey -}}
137+
{{- else -}}
138+
{{- required "Missing .Values.rabbitmqExternal.existingSecretPasswordKey" .Values.rabbitmqExternal.existingSecretPasswordKey -}}
139+
{{- end -}}
140+
{{- end -}}
141+
123142
{{/*
124143
This template renders the AMQP port number for RabbitMQ.
125144
*/}}
126-
{{- define "invenio.rabbitmq.amqpPort" -}}
145+
{{- define "invenio.rabbitmq.amqpPortString" -}}
127146
{{- if .Values.rabbitmq.enabled }}
128-
{{- required "Missing .Values.rabbitmq.service.ports.amqp" .Values.rabbitmq.service.ports.amqp -}}
147+
{{- required "Missing .Values.rabbitmq.service.ports.amqp" .Values.rabbitmq.service.ports.amqp | quote -}}
129148
{{- else }}
130-
{{- required "Missing .Values.rabbitmqExternal.amqpPort" .Values.rabbitmqExternal.amqpPort -}}
149+
{{- required "Missing .Values.rabbitmqExternal.amqpPort" (tpl (toString .Values.rabbitmqExternal.amqpPort) .) | quote -}}
131150
{{- end }}
132151
{{- end -}}
133152

134-
####################### RabbitMQ management port #######################
135153
{{/*
136154
This template renders the management port number for RabbitMQ.
137155
*/}}
138-
{{- define "invenio.rabbitmq.managementPort" -}}
156+
{{- define "invenio.rabbitmq.managementPortString" -}}
139157
{{- if .Values.rabbitmq.enabled }}
140-
{{- required "Missing .Values.rabbitmq.service.ports.manager" .Values.rabbitmq.service.ports.manager -}}
158+
{{- required "Missing .Values.rabbitmq.service.ports.manager" .Values.rabbitmq.service.ports.manager | quote -}}
141159
{{- else }}
142-
{{- required "Missing .Values.rabbitmqExternal.managementPort" .Values.rabbitmqExternal.managementPort -}}
160+
{{- required "Missing .Values.rabbitmqExternal.managementPort" (tpl (toString .Values.rabbitmqExternal.managementPort) .) | quote -}}
143161
{{- end }}
144162
{{- end -}}
145163

146-
########################## RabbitMQ hostname ##########################
147164
{{/*
148165
This template renders the hostname for RabbitMQ.
149166
*/}}
150167
{{- define "invenio.rabbitmq.hostname" -}}
151168
{{- if .Values.rabbitmq.enabled }}
152169
{{- include "common.names.fullname" .Subcharts.rabbitmq -}}
153170
{{- else }}
154-
{{- required "Missing .Values.rabbitmqExternal.hostname" .Values.rabbitmqExternal.hostname }}
171+
{{- required "Missing .Values.rabbitmqExternal.hostname" (tpl .Values.rabbitmqExternal.hostname .) }}
155172
{{- end }}
156173
{{- end -}}
157174

158-
########################## RabbitMQ protocol ##########################
159175
{{/*
160176
This template renders the protocol for RabbitMQ.
161177
*/}}
@@ -167,42 +183,47 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
167183
{{- end }}
168184
{{- end -}}
169185

170-
########################## RabbitMQ vhost ##########################
171186
{{/*
172187
This template renders the vhost for RabbitMQ.
173188
*/}}
174189
{{- define "invenio.rabbitmq.vhost" -}}
175190
{{- if .Values.rabbitmq.enabled }}
176191
{{- "" }}
177192
{{- else }}
178-
{{- required "Missing .Values.rabbitmqExternal.vhost" .Values.rabbitmqExternal.vhost }}
193+
{{- required "Missing .Values.rabbitmqExternal.vhost" (tpl .Values.rabbitmqExternal.vhost .) }}
179194
{{- end }}
180195
{{- end -}}
181196

182-
########################## Celery broker URI ##########################
183-
{{/*
184-
This template renders the URI for connecting to RabbitMQ.
185-
*/}}
186-
{{- define "invenio.rabbitmq.uri" -}}
187-
{{- $username := (include "invenio.rabbitmq.username" .) -}}
188-
{{- $password := (include "invenio.rabbitmq.password" .) -}}
189-
{{- $port := (include "invenio.rabbitmq.amqpPort" .) -}}
190-
{{- $hostname := (include "invenio.rabbitmq.hostname" .) -}}
191-
{{- $protocol := (include "invenio.rabbitmq.protocol" .) -}}
192-
{{- $vhost := (include "invenio.rabbitmq.vhost" .) -}}
193-
{{- printf "%s://%s:%s@%s:%v/%s" $protocol $username $password $hostname $port $vhost}}
194-
{{- end -}}
195-
196-
########################### RabbitMQ API URI ###########################
197197
{{/*
198-
This template renders the URI for RabbitMQ's API endpoint.
199-
*/}}
200-
{{- define "invenio.rabbitmq.apiUri" -}}
201-
{{- $username := (include "invenio.rabbitmq.username" .) -}}
202-
{{- $password := (include "invenio.rabbitmq.password" .) -}}
203-
{{- $port := (include "invenio.rabbitmq.managementPort" .) -}}
204-
{{- $hostname := (include "invenio.rabbitmq.hostname" .) -}}
205-
{{- printf "http://%s:%s@%s:%v/api/" $username $password $hostname $port }}
198+
RabbitMQ connection env section.
199+
*/}}
200+
{{- define "invenio.config.queue" -}}
201+
{{- $uri := "$(INVENIO_AMQP_BROKER_PROTOCOL)://$(INVENIO_AMQP_BROKER_USER):$(INVENIO_AMQP_BROKER_PASSWORD)@$(INVENIO_AMQP_BROKER_HOST):$(INVENIO_AMQP_BROKER_PORT)/$(INVENIO_AMQP_BROKER_VHOST)" -}}
202+
- name: INVENIO_AMQP_BROKER_USER
203+
value: {{ include "invenio.rabbitmq.username" . }}
204+
- name: INVENIO_AMQP_BROKER_HOST
205+
value: {{ include "invenio.rabbitmq.hostname" . }}
206+
- name: INVENIO_AMQP_BROKER_PORT
207+
value: {{ include "invenio.rabbitmq.amqpPortString" . }}
208+
- name: INVENIO_AMQP_BROKER_VHOST
209+
value: {{ include "invenio.rabbitmq.vhost" . }}
210+
- name: INVENIO_AMQP_BROKER_PROTOCOL
211+
value: {{ include "invenio.rabbitmq.protocol" . }}
212+
- name: INVENIO_AMQP_BROKER_PASSWORD
213+
{{- if or (and .Values.rabbitmq.enabled .Values.rabbitmq.auth.password) .Values.rabbitmqExternal.password }}
214+
value: {{ include "invenio.rabbitmq.password" . | quote }}
215+
{{- else }}
216+
valueFrom:
217+
secretKeyRef:
218+
name: {{ include "invenio.rabbitmq.secretName" .}}
219+
key: {{ include "invenio.rabbitmq.secretKey" .}}
220+
{{- end }}
221+
- name: INVENIO_BROKER_URL
222+
value: {{ $uri }}
223+
- name: INVENIO_CELERY_BROKER_URL
224+
value: $(INVENIO_BROKER_URL)
225+
- name: RABBITMQ_API_URI
226+
value: "http://$(INVENIO_AMQP_BROKER_USER):$(INVENIO_AMQP_BROKER_PASSWORD)@$(INVENIO_AMQP_BROKER_HOST):$(INVENIO_AMQP_BROKER_PORT)/api/"
206227
{{- end -}}
207228

208229
######################### OpenSearch hostname #########################
@@ -217,79 +238,110 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
217238
{{- end }}
218239
{{- end -}}
219240

220-
######################### PostgreSQL username #########################
241+
######################### PostgreSQL connection configuration #########################
221242
{{/*
222243
This template renders the username used for the PostgreSQL instance.
223244
*/}}
224245
{{- define "invenio.postgresql.username" -}}
225246
{{- if .Values.postgresql.enabled -}}
226-
{{- required "Missing .Values.postgresql.auth.username" .Values.postgresql.auth.username -}}
227-
{{/* NOTE: Specifying username explicitly like this is suboptmal. Would be desirable to refactor Invenio so it can take the postgres username as a spearate environment variable which we can populate dynamically from the secret. */}}
247+
{{- required "Missing .Values.postgresql.auth.username" (tpl .Values.postgresql.auth.username .) -}}
228248
{{- else -}}
229-
{{- required "Missing .Values.postgresqlExternal.username" .Values.postgresqlExternal.username -}}
249+
{{- required "Missing .Values.postgresqlExternal.username" (tpl .Values.postgresqlExternal.username .) -}}
230250
{{- end -}}
231251
{{- end -}}
232252

233-
######################### PostgreSQL password #########################
234253
{{/*
235254
This template renders the password used for the PostgreSQL instance.
255+
In production environments we encourage you to use secrets instead.
236256
*/}}
237257
{{- define "invenio.postgresql.password" -}}
238258
{{- if .Values.postgresql.enabled -}}
239259
{{- required "Missing .Values.postgresql.auth.password" .Values.postgresql.auth.password -}}
240-
{{/* NOTE: Specifying password explicitly like this is suboptmal. Would be desirable to refactor Invenio so it can take the postgres password as a spearate environment variable which we can populate dynamically from the secret. */}}
241260
{{- else -}}
242261
{{- required "Missing .Values.postgresqlExternal.password" .Values.postgresqlExternal.password -}}
243262
{{- end -}}
244263
{{- end -}}
245264

246-
######################### PostgreSQL hostname #########################
265+
{{/*
266+
Get the database password secret name
267+
*/}}
268+
{{- define "invenio.postgresql.secretName" -}}
269+
{{- if .Values.postgresql.enabled -}}
270+
{{- required "Missing .Values.postgresql.auth.existingSecret" (tpl .Values.postgresql.auth.existingSecret .) -}}
271+
{{- else -}}
272+
{{- required "Missing .Values.postgresqlExternal.existingSecret" (tpl .Values.postgresqlExternal.existingSecret .) -}}
273+
{{- end -}}
274+
{{- end -}}
275+
276+
{{/*
277+
Get the database password secret key
278+
*/}}
279+
{{- define "invenio.postgresql.secretKey" -}}
280+
{{- if .Values.postgresql.enabled -}}
281+
{{- required "Missing .Values.postgresql.auth.secretKeys.userPasswordKey" .Values.postgresql.auth.secretKeys.userPasswordKey -}}
282+
{{- else -}}
283+
{{- required "Missing .Values.postgresqlExternal.existingSecretPasswordKey" .Values.postgresqlExternal.existingSecretPasswordKey -}}
284+
{{- end -}}
285+
{{- end -}}
286+
247287
{{/*
248288
This template renders the hostname used for the PostgreSQL instance.
249289
*/}}
250290
{{- define "invenio.postgresql.hostname" -}}
251291
{{- if .Values.postgresql.enabled -}}
252292
{{- include "postgresql.v1.primary.fullname" .Subcharts.postgresql -}}
253293
{{- else -}}
254-
{{- required "Missing .Values.postgresqlExternal.hostname" .Values.postgresqlExternal.hostname -}}
294+
{{- required "Missing .Values.postgresqlExternal.hostname" (tpl .Values.postgresqlExternal.hostname .) -}}
255295
{{- end -}}
256296
{{- end -}}
257297

258-
########################### PostgreSQL port ###########################
259298
{{/*
260-
This template renders the port number used for the PostgreSQL instance.
299+
This template renders the port number used for the PostgreSQL instance (as a string).
261300
*/}}
262-
{{- define "invenio.postgresql.port" -}}
301+
{{- define "invenio.postgresql.portString" -}}
263302
{{- if .Values.postgresql.enabled -}}
264-
{{- required "Missing .Values.postgresql.primary.service.ports.postgresql" .Values.postgresql.primary.service.ports.postgresql -}}
303+
{{- required "Missing .Values.postgresql.primary.service.ports.postgresql" (tpl (toString .Values.postgresql.primary.service.ports.postgresql) .) | quote -}}
265304
{{- else -}}
266-
{{- required "Missing .Values.postgresqlExternal.port" .Values.postgresqlExternal.port -}}
305+
{{- required "Missing .Values.postgresqlExternal.port" (tpl (toString .Values.postgresqlExternal.port) .) | quote -}}
267306
{{- end -}}
268307
{{- end -}}
269308

270-
############################ Database name ############################
271309
{{/*
272310
This template renders the name of the database in PostgreSQL.
273311
*/}}
274-
{{- define "invenio.postgresql.databaseName" -}}
312+
{{- define "invenio.postgresql.database" -}}
275313
{{- if .Values.postgresql.enabled -}}
276-
{{- required "Missing .Values.postgresql.auth.database" .Values.postgresql.auth.database -}}
314+
{{- required "Missing .Values.postgresql.auth.database" (tpl .Values.postgresql.auth.database .) -}}
277315
{{- else -}}
278-
{{- required "Missing .Values.postgresqlExternal.databaseName" .Values.postgresqlExternal.databaseName -}}
316+
{{- required "Missing .Values.postgresqlExternal.database" (tpl .Values.postgresqlExternal.database .) -}}
279317
{{- end -}}
280318
{{- end -}}
281319

282-
####################### SQLAlchemy database URI #######################
283320
{{/*
284-
This template renders the SQLAlchemy database URI.
285-
*/}}
286-
{{- define "invenio.sqlAlchemyDbUri" -}}
287-
{{- $username := include "invenio.postgresql.username" . -}}
288-
{{- $password := include "invenio.postgresql.password" . -}}
289-
{{- $hostname := include "invenio.postgresql.hostname" . -}}
290-
{{- $port := include "invenio.postgresql.port" . -}}
291-
{{- $databaseName := include "invenio.postgresql.databaseName" . -}}
292-
{{- printf "postgresql+psycopg2://%s:%s@%s:%v/%s" $username $password $hostname $port $databaseName -}}
321+
Define database connection env section.
322+
*/}}
323+
{{- define "invenio.config.database" -}}
324+
- name: INVENIO_DB_USER
325+
value: {{ include "invenio.postgresql.username" . }}
326+
- name: INVENIO_DB_HOST
327+
value: {{ include "invenio.postgresql.hostname" . }}
328+
- name: INVENIO_DB_PORT
329+
value: {{ include "invenio.postgresql.portString" . }}
330+
- name: INVENIO_DB_NAME
331+
value: {{ include "invenio.postgresql.database" . }}
332+
- name: INVENIO_DB_PROTOCOL
333+
value: "postgresql+psycopg2"
334+
- name: INVENIO_DB_PASSWORD
335+
{{- if or (and .Values.postgresql.enabled .Values.postgresql.auth.password) .Values.postgresqlExternal.password }}
336+
value: {{ include "invenio.postgresql.password" . | quote }}
337+
{{- else }}
338+
valueFrom:
339+
secretKeyRef:
340+
name: {{ include "invenio.postgresql.secretName" .}}
341+
key: {{ include "invenio.postgresql.secretKey" .}}
342+
{{- end }}
343+
- name: INVENIO_SQLALCHEMY_DATABASE_URI
344+
value: "$(INVENIO_DB_PROTOCOL)://$(INVENIO_DB_USER):$(INVENIO_DB_PASSWORD)@$(INVENIO_DB_HOST):$(INVENIO_DB_PORT)/$(INVENIO_DB_NAME)"
293345
{{- end -}}
294346

295347
{{/*
@@ -299,7 +351,7 @@ Get the sentry secret name
299351
{{- if .Values.invenio.sentry.existingSecret -}}
300352
{{- print (tpl .Values.invenio.sentry.existingSecret .) -}}
301353
{{- else if .Values.invenio.sentry.secret_name -}}
302-
{{- print .Values.invenio.sentry.secret_name -}}
354+
{{- print .Values.invenio.sentry.secret_name -}}
303355
{{- else -}}
304356
{{- printf "%s-%s" (include "invenio.fullname" .) "sentry" -}}
305357
{{- end -}}

charts/invenio/templates/flower/deployment.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ spec:
2323
[
2424
"celery",
2525
"--config=/var/celery/celeryconfig.py",
26-
{{ printf "--broker=%s" (include "invenio.rabbitmq.uri" .) }},
26+
"--broker=$(INVENIO_CELERY_BROKER_URL)",
2727
"flower",
28-
{{ printf "--broker_api=%s" (include "invenio.rabbitmq.apiUri" .) }},
28+
"--broker_api=$(RABBITMQ_API_URI)",
2929
"--basic_auth=$(FLOWER_BASIC_AUTH_CREDENTIALS)",
3030
"--conf=/var/flower/flowerconfig.py"
3131
]
@@ -44,6 +44,7 @@ spec:
4444
secretKeyRef:
4545
name: {{ .Values.flower.secret_name }}
4646
key: FLOWER_BASIC_AUTH_CREDENTIALS
47+
{{- include "invenio.config.queue" . | nindent 12 }}
4748
{{- if (or .Values.invenio.extraEnvFrom .Values.flower.extraEnvFrom) }}
4849
envFrom:
4950
{{- with .Values.invenio.extraEnvFrom }}

charts/invenio/templates/install-init-job.yaml

+2-6
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,8 @@ spec:
3838
env:
3939
- name: TZ
4040
value: {{ required "Missing .Values.global.timezone" .Values.global.timezone }}
41-
- name: INVENIO_BROKER_URL
42-
value: {{ include "invenio.rabbitmq.uri" . }}
43-
- name: INVENIO_CELERY_BROKER_URL
44-
value: {{ include "invenio.rabbitmq.uri" . }}
45-
- name: INVENIO_SQLALCHEMY_DATABASE_URI
46-
value: {{ include "invenio.sqlAlchemyDbUri" . }}
41+
{{- include "invenio.config.queue" . | nindent 8 }}
42+
{{- include "invenio.config.database" . | nindent 8 }}
4743
volumeMounts:
4844
{{- range $key, $value := .Values.invenio.vocabularies }}
4945
- name: vocabularies

charts/invenio/templates/web-deployment.yaml

+2-6
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,8 @@ spec:
5050
env:
5151
- name: TZ
5252
value: {{ required "Missing .Values.global.timezone" .Values.global.timezone }}
53-
- name: INVENIO_BROKER_URL
54-
value: {{ include "invenio.rabbitmq.uri" . }}
55-
- name: INVENIO_CELERY_BROKER_URL
56-
value: {{ include "invenio.rabbitmq.uri" . }}
57-
- name: INVENIO_SQLALCHEMY_DATABASE_URI
58-
value: {{ include "invenio.sqlAlchemyDbUri" . }}
53+
{{- include "invenio.config.queue" . | nindent 8 }}
54+
{{- include "invenio.config.database" . | nindent 8 }}
5955
{{- with .Values.web.extraEnvVars }}
6056
{{- toYaml . | nindent 8 }}
6157
{{- end }}

charts/invenio/templates/worker-beat-deployment.yaml

+2-6
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@ spec:
6262
env:
6363
- name: TZ
6464
value: {{ required "Missing .Values.global.timezone" .Values.global.timezone }}
65-
- name: INVENIO_BROKER_URL
66-
value: {{ include "invenio.rabbitmq.uri" . }}
67-
- name: INVENIO_CELERY_BROKER_URL
68-
value: {{ include "invenio.rabbitmq.uri" . }}
69-
- name: INVENIO_SQLALCHEMY_DATABASE_URI
70-
value: {{ include "invenio.sqlAlchemyDbUri" . }}
65+
{{- include "invenio.config.queue" . | nindent 8 }}
66+
{{- include "invenio.config.database" . | nindent 8}}
7167
{{- with .Values.workerBeat.extraEnvVars }}
7268
{{- toYaml . | nindent 8 }}
7369
{{- end }}

0 commit comments

Comments
 (0)