Skip to content

Commit

Permalink
config: enhance RabbitMQ connection configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
egabancho committed Mar 3, 2025
1 parent 503729f commit cf9e856
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 57 deletions.
98 changes: 58 additions & 40 deletions charts/invenio/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
{{- end -}}

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

########################## RabbitMQ username ##########################
{{/*
This template renders the username for accessing RabbitMQ.
*/}}
{{- define "invenio.rabbitmq.username" -}}
{{- if .Values.rabbitmq.enabled }}
{{- required "Missing .Values.rabbitmq.auth.username" .Values.rabbitmq.auth.username -}}
{{- else }}
{{- required "Missing .Values.rabbitmqExternal.username" .Values.rabbitmqExternal.username -}}
{{- required "Missing .Values.rabbitmqExternal.username" (tpl .Values.rabbitmqExternal.username .) -}}
{{- end }}
{{- end -}}

########################## RabbitMQ password ##########################
{{/*
This template renders the password for accessing RabbitMQ.
*/}}
Expand All @@ -119,43 +117,61 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
{{- end -}}

########################## RabbitMQ AMQP port ##########################
{{/*
Get the database password secret name
*/}}
{{- define "invenio.rabbitmq.secretName" -}}
{{- if .Values.rabbitmq.enabled -}}
{{- required "Missing .Values.rabbitmq.auth.existingPasswordSecret" (tpl .Values.rabbitmq.auth.existingPasswordSecret .) -}}
{{- else -}}
{{- required "Missing .Values.rabbitmqExternal.existingSecret" (tpl .Values.rabbitmqExternal.existingSecret .) -}}
{{- end -}}
{{- end -}}

{{/*
Get the database password secret key
*/}}
{{- define "invenio.rabbitmq.secretKey" -}}
{{- if .Values.rabbitmq.enabled -}}
{{- required "Missing .Values.rabbitmq.auth.existingSecretPasswordKey" .Values.rabbitmq.auth.existingSecretPasswordKey -}}
{{- else -}}
{{- required "Missing .Values.rabbitmqExternal.existingSecretPasswordKey" .Values.rabbitmqExternal.existingSecretPasswordKey -}}
{{- end -}}
{{- end -}}

{{/*
This template renders the AMQP port number for RabbitMQ.
*/}}
{{- define "invenio.rabbitmq.amqpPort" -}}
{{- if .Values.rabbitmq.enabled }}
{{- required "Missing .Values.rabbitmq.service.ports.amqp" .Values.rabbitmq.service.ports.amqp -}}
{{- required "Missing .Values.rabbitmq.service.ports.amqp" .Values.rabbitmq.service.ports.amqp | quote -}}
{{- else }}
{{- required "Missing .Values.rabbitmqExternal.amqpPort" .Values.rabbitmqExternal.amqpPort -}}
{{- required "Missing .Values.rabbitmqExternal.amqpPort" (tpl .Values.rabbitmqExternal.amqpPort .) | quote -}}
{{- end }}
{{- end -}}

####################### RabbitMQ management port #######################
{{/*
This template renders the management port number for RabbitMQ.
*/}}
{{- define "invenio.rabbitmq.managementPort" -}}
{{- if .Values.rabbitmq.enabled }}
{{- required "Missing .Values.rabbitmq.service.ports.manager" .Values.rabbitmq.service.ports.manager -}}
{{- required "Missing .Values.rabbitmq.service.ports.manager" .Values.rabbitmq.service.ports.manager | quote -}}
{{- else }}
{{- required "Missing .Values.rabbitmqExternal.managementPort" .Values.rabbitmqExternal.managementPort -}}
{{- required "Missing .Values.rabbitmqExternal.managementPort" (tpl .Values.rabbitmqExternal.managementPort .) | quote -}}
{{- end }}
{{- end -}}

########################## RabbitMQ hostname ##########################
{{/*
This template renders the hostname for RabbitMQ.
*/}}
{{- define "invenio.rabbitmq.hostname" -}}
{{- if .Values.rabbitmq.enabled }}
{{- include "common.names.fullname" .Subcharts.rabbitmq -}}
{{- else }}
{{- required "Missing .Values.rabbitmqExternal.hostname" .Values.rabbitmqExternal.hostname }}
{{- required "Missing .Values.rabbitmqExternal.hostname" (tpl .Values.rabbitmqExternal.hostname .) }}
{{- end }}
{{- end -}}

########################## RabbitMQ protocol ##########################
{{/*
This template renders the protocol for RabbitMQ.
*/}}
Expand All @@ -167,44 +183,46 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
{{- end -}}

########################## RabbitMQ vhost ##########################
{{/*
This template renders the vhost for RabbitMQ.
*/}}
{{- define "invenio.rabbitmq.vhost" -}}
{{- if .Values.rabbitmq.enabled }}
{{- "" }}
{{- else }}
{{- required "Missing .Values.rabbitmqExternal.vhost" .Values.rabbitmqExternal.vhost }}
{{- required "Missing .Values.rabbitmqExternal.vhost" (tpl .Values.rabbitmqExternal.vhost .) }}
{{- end }}
{{- end -}}

########################## Celery broker URI ##########################
{{/*
This template renders the URI for connecting to RabbitMQ.
*/}}
{{- define "invenio.rabbitmq.uri" -}}
{{- $username := (include "invenio.rabbitmq.username" .) -}}
{{- $password := (include "invenio.rabbitmq.password" .) -}}
{{- $port := (include "invenio.rabbitmq.amqpPort" .) -}}
{{- $hostname := (include "invenio.rabbitmq.hostname" .) -}}
{{- $protocol := (include "invenio.rabbitmq.protocol" .) -}}
{{- $vhost := (include "invenio.rabbitmq.vhost" .) -}}
{{- printf "%s://%s:%s@%s:%v/%s" $protocol $username $password $hostname $port $vhost}}
{{- end -}}

########################### RabbitMQ API URI ###########################
{{/*
This template renders the URI for RabbitMQ's API endpoint.
*/}}
{{- define "invenio.rabbitmq.apiUri" -}}
{{- $username := (include "invenio.rabbitmq.username" .) -}}
{{- $password := (include "invenio.rabbitmq.password" .) -}}
{{- $port := (include "invenio.rabbitmq.managementPort" .) -}}
{{- $hostname := (include "invenio.rabbitmq.hostname" .) -}}
{{- printf "http://%s:%s@%s:%v/api/" $username $password $hostname $port }}
RabbitMQ connection env section.
*/}}
{{- define "invenio.config.queue" -}}
{{- $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)" -}}
- name: INVENIO_AMQP_BROKER_USER
value: {{ include "invenio.rabbitmq.username" . }}
- name: INVENIO_AMQP_BROKER_HOST
value: {{ include "invenio.rabbitmq.hostname" . }}
- name: INVENIO_AMQP_BROKER_PORT
value: {{ include "invenio.rabbitmq.amqpPort" . }}
- name: INVENIO_AMQP_BROKER_VHOST
value: {{ include "invenio.rabbitmq.vhost" . }}
- name: INVENIO_AMQP_BROKER_PROTOCOL
value: {{ include "invenio.rabbitmq.protocol" . }}
- name: INVENIO_AMQP_BROKER_PASSWORD
{{- if or (and .Values.rabbitmq.enabled .Values.rabbitmq.auth.password) .Values.rabbitmqExternal.password }}
value: {{ include "invenio.rabbitmq.password" . | quote }}
{{- else }}
valueFrom:
secretKeyRef:
name: {{ include "invenio.rabbitmq.secretName" .}}
key: {{ include "invenio.rabbitmq.secretKey" .}}
{{- end }}
- name: INVENIO_BROKER_URL
value: {{ $uri}}
- name: INVENIO_CELERY_BROKER_URL
value: {{ $uri}}
{{- end -}}
######################### OpenSearch hostname #########################
{{/*
This template renders the hostname of the OpenSearch instance.
Expand Down Expand Up @@ -297,7 +315,7 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end -}}

{{/*
Define database connection env section.
Define database connection env section.
*/}}
{{- define "invenio.config.database" -}}
- name: INVENIO_DB_USER
Expand Down
5 changes: 1 addition & 4 deletions charts/invenio/templates/install-init-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ spec:
env:
- name: TZ
value: {{ required "Missing .Values.global.timezone" .Values.global.timezone }}
- name: INVENIO_BROKER_URL
value: {{ include "invenio.rabbitmq.uri" . }}
- name: INVENIO_CELERY_BROKER_URL
value: {{ include "invenio.rabbitmq.uri" . }}
{{- include "invenio.config.queue" . | nindent 8 }}
{{- include "invenio.config.database" . | nindent 8 }}
volumeMounts:
{{- range $key, $value := .Values.invenio.vocabularies }}
Expand Down
5 changes: 1 addition & 4 deletions charts/invenio/templates/web-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ spec:
env:
- name: TZ
value: {{ required "Missing .Values.global.timezone" .Values.global.timezone }}
- name: INVENIO_BROKER_URL
value: {{ include "invenio.rabbitmq.uri" . }}
- name: INVENIO_CELERY_BROKER_URL
value: {{ include "invenio.rabbitmq.uri" . }}
{{- include "invenio.config.queue" . | nindent 8 }}
{{- include "invenio.config.database" . | nindent 8 }}
{{- with .Values.web.extraEnvVars }}
{{- toYaml . | nindent 8 }}
Expand Down
5 changes: 1 addition & 4 deletions charts/invenio/templates/worker-beat-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ spec:
env:
- name: TZ
value: {{ required "Missing .Values.global.timezone" .Values.global.timezone }}
- name: INVENIO_BROKER_URL
value: {{ include "invenio.rabbitmq.uri" . }}
- name: INVENIO_CELERY_BROKER_URL
value: {{ include "invenio.rabbitmq.uri" . }}
{{- include "invenio.config.queue" . | nindent 8 }}
{{- include "invenio.config.database" . | nindent 8}}
{{- with .Values.workerBeat.extraEnvVars }}
{{- toYaml . | nindent 8 }}
Expand Down
5 changes: 1 addition & 4 deletions charts/invenio/templates/worker-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ spec:
env:
- name: TZ
value: {{ required "Missing .Values.global.timezone" .Values.global.timezone }}
- name: INVENIO_BROKER_URL
value: {{ include "invenio.rabbitmq.uri" . }}
- name: INVENIO_CELERY_BROKER_URL
value: {{ include "invenio.rabbitmq.uri" . }}
{{- include "invenio.config.queue" . | nindent 10 }}
{{- include "invenio.config.database" . | nindent 10 }}
{{- with .Values.worker.extraEnvVars }}
{{- toYaml . | nindent 10 }}
Expand Down
25 changes: 24 additions & 1 deletion charts/invenio/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ redis:
cpu: 500m
memory: 500Mi

## RabbitMQ chart configuration
## ref: https://github.com/bitnami/charts/blob/main/bitnami/rabbitmq/values.yaml
rabbitmq:
enabled: true
auth:
Expand All @@ -345,7 +347,28 @@ rabbitmq:
cpu: "1"
memory: 2Gi

rabbitmqExternal: {}
## External RabbitMQ configuration
## All of these values are only used when rabbitmq.enabled is set to false
## @param rabbitmqExternal.username RabbitMQ user
## @param rabbitmqExternal.password Password
## @param rabbitmqExternal.amqpPort

Check failure on line 354 in charts/invenio/values.yaml

View workflow job for this annotation

GitHub Actions / lint-test

354:36 [trailing-spaces] trailing spaces
## @param rabbitmqExternal.managementPort
## @param rabbitmqExternal.hostname

Check failure on line 356 in charts/invenio/values.yaml

View workflow job for this annotation

GitHub Actions / lint-test

356:36 [trailing-spaces] trailing spaces
## @param rabbitmqExternal.protocol

Check failure on line 357 in charts/invenio/values.yaml

View workflow job for this annotation

GitHub Actions / lint-test

357:36 [trailing-spaces] trailing spaces
## @param rabbitmqExternal.vhost

Check failure on line 358 in charts/invenio/values.yaml

View workflow job for this annotation

GitHub Actions / lint-test

358:33 [trailing-spaces] trailing spaces
## @param rabbitmqExternal.existingSecret Name of an existing secret resource containing the credentials
## @param rabbitmqExternal.existingSecretPasswordKey Name of an existing secret key containing the credentials
##
rabbitmqExternal:
username: invenio
password: ""
amqpPort: 5671
managementPort: 15672
hostname: ""
protocol: ""
vhost: ""
existingSecret: ""
existingSecretPasswordKey: "amqp-password"

flower:
enabled: true
Expand Down

0 comments on commit cf9e856

Please sign in to comment.