Skip to content

Commit 48995df

Browse files
committed
postgres updates
1 parent c7d2547 commit 48995df

File tree

6 files changed

+73
-36
lines changed

6 files changed

+73
-36
lines changed

Diff for: charts/invenio/templates/_helpers.tpl

+55-24
Original file line numberDiff line numberDiff line change
@@ -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
{{/*

Diff for: charts/invenio/templates/install-init-job.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ spec:
4242
value: {{ include "invenio.rabbitmq.uri" . }}
4343
- name: INVENIO_CELERY_BROKER_URL
4444
value: {{ include "invenio.rabbitmq.uri" . }}
45-
- name: INVENIO_SQLALCHEMY_DATABASE_URI
46-
value: {{ include "invenio.sqlAlchemyDbUri" . }}
45+
{{- include "invenio.config.database" . | nindent 8 }}
4746
- name: INVENIO_SECRET_KEY
4847
valueFrom:
4948
secretKeyRef:

Diff for: charts/invenio/templates/web-deployment.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ spec:
3939
value: {{ include "invenio.rabbitmq.uri" . }}
4040
- name: INVENIO_CELERY_BROKER_URL
4141
value: {{ include "invenio.rabbitmq.uri" . }}
42-
- name: INVENIO_SQLALCHEMY_DATABASE_URI
43-
value: {{ include "invenio.sqlAlchemyDbUri" . }}
42+
{{- include "invenio.config.database" . | nindent 8 }}
4443
{{- with .Values.web.extraEnvVars }}
4544
{{- toYaml . | nindent 8 }}
4645
{{- end }}

Diff for: charts/invenio/templates/worker-beat-deployment.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ spec:
5252
value: {{ include "invenio.rabbitmq.uri" . }}
5353
- name: INVENIO_CELERY_BROKER_URL
5454
value: {{ include "invenio.rabbitmq.uri" . }}
55-
- name: INVENIO_SQLALCHEMY_DATABASE_URI
56-
value: {{ include "invenio.sqlAlchemyDbUri" . }}
55+
{{- include "invenio.config.database" . | nindent 8}}
5756
{{- with .Values.workerBeat.extraEnvVars }}
5857
{{- toYaml . | nindent 8 }}
5958
{{- end }}

Diff for: charts/invenio/templates/worker-deployment.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ spec:
4343
value: {{ include "invenio.rabbitmq.uri" . }}
4444
- name: INVENIO_CELERY_BROKER_URL
4545
value: {{ include "invenio.rabbitmq.uri" . }}
46-
- name: INVENIO_SQLALCHEMY_DATABASE_URI
47-
value: {{ include "invenio.sqlAlchemyDbUri" . }}
46+
{{- include "invenio.config.database" . | nindent 10 }}
4847
{{- with .Values.worker.extraEnvVars }}
4948
{{- toYaml . | nindent 10 }}
5049
{{- end }}

Diff for: charts/invenio/values.yaml

+14-4
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,21 @@ flower:
316316
memory: 250Mi
317317
cpu: 0.1
318318

319+
## PostgreSQL chart configuration
320+
## ref: https://github.com/bitnami/charts/blob/main/bitnami/postgresql/values.yaml
321+
## @param postgresql.enabled Switch to enable or disable the PostgreSQL helm chart
322+
## @param postgresql.auth.enablePostgresUser Assign a password to the "postgres" admin user. Otherwise, remote access will be blocked for this user
323+
## @param postgresql.auth.username Name for a custom user to create
324+
## @param postgresql.auth.password Password for the custom user to create
325+
## @param postgresql.auth.database Name for a custom database to create
326+
##
319327
postgresql:
320328
enabled: true
321329
auth:
322330
username: invenio
331+
password: ""
323332
database: invenio
333+
existingSecret: ""
324334

325335
## External PostgreSQL configuration
326336
## All of these values are only used when postgresql.enabled is set to false
@@ -333,13 +343,13 @@ postgresql:
333343
## @param postgresqlExternal.existingSecretPasswordKey Name of an existing secret key containing the database credentials
334344
##
335345
postgresqlExternal:
336-
host: ""
346+
hostname: ""
337347
port: 5432
338-
user: postgres
339-
database: mastodon
348+
username: invenio
340349
password: ""
350+
database: invenio
341351
existingSecret: ""
342-
existingSecretPasswordKey: "db-password"
352+
existingSecretPasswordKey: "password"
343353

344354
opensearch:
345355
enabled: true

0 commit comments

Comments
 (0)