@@ -199,79 +199,110 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
199199 {{- end }}
200200{{- end -}}
201201
202- ######################### PostgreSQL username #########################
202+ ######################### PostgreSQL connection configuration #########################
203203{{/*
204204 This template renders the username used for the PostgreSQL instance.
205205*/}}
206206{{- define "invenio.postgresql.username" -}}
207207 {{- if .Values.postgresql.enabled -}}
208- {{- required "Missing .Values.postgresql.auth.username" .Values.postgresql.auth.username -}}
209- {{/* 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. */}}
208+ {{- required "Missing .Values.postgresql.auth.username" (tpl .Values.postgresql.auth.username .) -}}
210209 {{- else -}}
211- {{- required "Missing .Values.postgresqlExternal.username" .Values.postgresqlExternal.username -}}
210+ {{- required "Missing .Values.postgresqlExternal.username" (tpl .Values.postgresqlExternal.username .) -}}
212211 {{- end -}}
213212{{- end -}}
214213
215- ######################### PostgreSQL password #########################
216214{{/*
217215 This template renders the password used for the PostgreSQL instance.
216+ In production environments we encourage you to use secrets instead.
218217*/}}
219218{{- define "invenio.postgresql.password" -}}
220219 {{- if .Values.postgresql.enabled -}}
221220 {{- required "Missing .Values.postgresql.auth.password" .Values.postgresql.auth.password -}}
222- {{/* 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. */}}
223221 {{- else -}}
224222 {{- required "Missing .Values.postgresqlExternal.password" .Values.postgresqlExternal.password -}}
225223 {{- end -}}
226224{{- end -}}
227225
228- ######################### PostgreSQL hostname #########################
226+ {{/*
227+ Get the database password secret name
228+ */}}
229+ {{- define "invenio.postgresql.secretName" -}}
230+ {{- if .Values.postgresql.enabled -}}
231+ {{- required "Missing .Values.postgresql.auth.existingSecret" (tpl .Values.postgresql.auth.existingSecret .) -}}
232+ {{- else -}}
233+ {{- required "Missing .Values.postgresqlExternal.existingSecret" (tpl .Values.postgresqlExternal.existingSecret .) -}}
234+ {{- end -}}
235+ {{- end -}}
236+
237+ {{/*
238+ Get the database password secret key
239+ */}}
240+ {{- define "invenio.postgresql.secretKey" -}}
241+ {{- if .Values.postgresql.enabled -}}
242+ {{- required "Missing .Values.postgresql.auth.secretKeys.userPasswordKey" .Values.postgresql.auth.secretKeys.userPasswordKey -}}
243+ {{- else -}}
244+ {{- required "Missing .Values.postgresqlExternal.existingSecretPasswordKey" .Values.postgresqlExternal.existingSecretPasswordKey -}}
245+ {{- end -}}
246+ {{- end -}}
247+
229248{{/*
230249 This template renders the hostname used for the PostgreSQL instance.
231250*/}}
232251{{- define "invenio.postgresql.hostname" -}}
233252 {{- if .Values.postgresql.enabled -}}
234253 {{- include "postgresql.v1.primary.fullname" .Subcharts.postgresql -}}
235254 {{- else -}}
236- {{- required "Missing .Values.postgresqlExternal.hostname" .Values.postgresqlExternal.hostname -}}
255+ {{- required "Missing .Values.postgresqlExternal.hostname" (tpl .Values.postgresqlExternal.hostname .) -}}
237256 {{- end -}}
238257{{- end -}}
239258
240- ########################### PostgreSQL port ###########################
241259{{/*
242260 This template renders the port number used for the PostgreSQL instance.
243261*/}}
244262{{- define "invenio.postgresql.port" -}}
245263 {{- if .Values.postgresql.enabled -}}
246- {{- required "Missing .Values.postgresql.primary.service.ports.postgresql" .Values.postgresql.primary.service.ports.postgresql -}}
264+ {{- required "Missing .Values.postgresql.primary.service.ports.postgresql" (tpl .Values.postgresql.primary.service.ports.postgresql .) -}}
247265 {{- else -}}
248- {{- required "Missing .Values.postgresqlExternal.port" .Values.postgresqlExternal.port -}}
266+ {{- required "Missing .Values.postgresqlExternal.port" (tpl .Values.postgresqlExternal.port .) -}}
249267 {{- end -}}
250268{{- end -}}
251269
252- ############################ Database name ############################
253270{{/*
254271 This template renders the name of the database in PostgreSQL.
255272*/}}
256- {{- define "invenio.postgresql.databaseName " -}}
273+ {{- define "invenio.postgresql.database " -}}
257274 {{- if .Values.postgresql.enabled -}}
258- {{- required "Missing .Values.postgresql.auth.database" .Values.postgresql.auth.database -}}
275+ {{- required "Missing .Values.postgresql.auth.database" (tpl .Values.postgresql.auth.database .) -}}
259276 {{- else -}}
260- {{- required "Missing .Values.postgresqlExternal.databaseName" .Values.postgresqlExternal.databaseName -}}
277+ {{- required "Missing .Values.postgresqlExternal.database" (tpl .Values.postgresqlExternal.database .) -}}
261278 {{- end -}}
262279{{- end -}}
263280
264- ####################### SQLAlchemy database URI #######################
265281{{/*
266- This template renders the SQLAlchemy database URI.
282+ Define database connection env section.
267283*/}}
268- {{- define "invenio.sqlAlchemyDbUri" -}}
269- {{- $username := include "invenio.postgresql.username" . -}}
270- {{- $password := include "invenio.postgresql.password" . -}}
271- {{- $hostname := include "invenio.postgresql.hostname" . -}}
272- {{- $port := include "invenio.postgresql.port" . -}}
273- {{- $databaseName := include "invenio.postgresql.databaseName" . -}}
274- {{- printf "postgresql+psycopg2://%s:%s@%s:%v/%s" $username $password $hostname $port $databaseName -}}
284+ {{- define "invenio.config.database" -}}
285+ - name: INVENIO_DB_USER
286+ value: {{ include "invenio.postgresql.username" . }}
287+ - name: INVENIO_DB_HOST
288+ value: {{ include "invenio.postgresql.hostname" . }}
289+ - name: INVENIO_DB_PORT
290+ value: {{ include "invenio.postgresql.port" . }}
291+ - name: INVENIO_DB_NAME
292+ value: {{ include "invenio.postgresql.database" . }}
293+ - name: INVENIO_DB_PROTOCOL
294+ value: "postgresql+psycopg2"
295+ - name: INVENIO_DB_PASSWORD
296+ {{- if or (and .Values.postgresql.enabled .Values.postgresql.auth.password) .Values.postgresqlExternal.password }}
297+ value: {{ include "invenio.postgresql.password" . | quote }}
298+ {{- else }}
299+ valueFrom:
300+ secretKeyRef:
301+ name: {{ include "invenio.postgresql.secretName" .}}
302+ key: {{ include "invenio.postgresql.secretKey" .}}
303+ {{- end }}
304+ - name: INVENIO_SQLALCHEMY_DATABASE_URI
305+ value: "$(INVENIO_DB_PROTOCOL)://$(INVENIO_DB_USER):$(INVENIO_DB_PASSWORD)@$(INVENIO_DB_HOST):$(INVENIO_DB_PORT)/$(INVENIO_DB_NAME)"
275306{{- end -}}
276307
277308{{/*
0 commit comments