Skip to content

Commit

Permalink
fix: sentinel connection and add key prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
daddyy committed Oct 7, 2024
1 parent 516a37e commit 6315b7b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
8 changes: 7 additions & 1 deletion engine/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ class DatabaseTypes:
REDIS_PORT = os.getenv("REDIS_PORT", 6379)
REDIS_DATABASE = os.getenv("REDIS_DATABASE", 1)
REDIS_PROTOCOL = os.getenv("REDIS_PROTOCOL", "redis")
REDIS_KEY_PREFIX = os.getenv("REDIS_KEY_PREFIX", None)

REDIS_URI = os.getenv("REDIS_URI")
if not REDIS_URI:
Expand Down Expand Up @@ -278,6 +279,9 @@ class DatabaseTypes:
DJANGO_SENTINELS = [(host, int(port)) for host, port in DJANGO_SENTINELS]
CACHES["default"]["OPTIONS"]["SENTINELS"] = DJANGO_SENTINELS

if REDIS_KEY_PREFIX:
CACHES["default"]["KEY_PREFIX"] = REDIS_KEY_PREFIX

if REDIS_SENTINEL_MASTER_NAME:
CACHES["default"]["OPTIONS"]["LOCATION"] = "redis://%s/%s" % (REDIS_SENTINEL_MASTER_NAME, REDIS_DATABASE)

Expand Down Expand Up @@ -533,10 +537,12 @@ class BrokerTypes:
CELERY_BROKER_URL = RABBITMQ_URI
elif BROKER_TYPE == BrokerTypes.REDIS:
CELERY_BROKER_URL = REDIS_URI
if REDIS_KEY_PREFIX:
CELERY_BROKER_TRANSPORT_OPTIONS["global_keyprefix"] = REDIS_KEY_PREFIX
if REDIS_USE_SSL:
CELERY_BROKER_USE_SSL = REDIS_SSL_CONFIG
if REDIS_SENTINELS:
CELERY_BROKER_URL = ";".join([f"sentinel://{sentinel}" for sentinel in REDIS_SENTINELS.split(",")])
CELERY_BROKER_URL = ";".join([f"sentinel://{REDIS_USERNAME}:{REDIS_PASSWORD}@{sentinel}/{REDIS_DATABASE}" for sentinel in REDIS_SENTINELS.split(",")])
if REDIS_SENTINEL_MASTER_NAME:
CELERY_BROKER_TRANSPORT_OPTIONS["master_name"] = REDIS_SENTINEL_MASTER_NAME
if REDIS_SENTINEL_KWARGS:
Expand Down
8 changes: 7 additions & 1 deletion helm/oncall/templates/_env.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,10 @@
{{- end }}
{{- end }}

{{- define "snippet.redis.key_prefix" -}}
{{ default "" .Values.externalRedis.key_prefix | quote }}
{{- end }}

{{- define "snippet.redis.sentinel.password.secret.key" -}}
{{ if .Values.externalRedis.sentinel.existingSecret -}}
{{ required "externalRedis.sentinel.passwordKey is required if externalRedis.sentinel.existingSecret is non-empty" .Values.externalRedis.sentinel.passwordKey }}
Expand All @@ -565,7 +569,7 @@
{{- $hosts := .Values.externalRedis.sentinel.hosts -}}
{{- if $hosts -}}
{{- range $index, $host := $hosts -}}
{{- if $host.host -}}
{{- if $host.host -}}
{{ if $index -}},{{- end }}{{ $host.host }}:{{ default 26379 $host.port }}
{{- else -}}
{{ required (printf "Host at index %d is required and cannot be empty." $index) $host.host }}
Expand Down Expand Up @@ -602,6 +606,8 @@
- name: REDIS_PORT
value: {{ include "snippet.redis.port" . }}
{{- end }}
- name: REDIS_KEY_PREFIX
value: {{ include "snippet.redis.key_prefix" . }}
- name: REDIS_DATABASE
value: {{ include "snippet.redis.database" . }}
- name: REDIS_USERNAME
Expand Down
19 changes: 10 additions & 9 deletions helm/oncall/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -606,23 +606,24 @@ redis:
existingSecret:

externalRedis:
protocol:
host:
port:
database:
username:
password:
protocol:
host:
port:
database:
username:
password:
# Use an existing secret for the redis password
existingSecret:
# The key in the secret containing the redis password
passwordKey:
key_prefix:
sentinel:
hosts:
- host:
- host:
port:
master:
master:
username:
password:
password:
passwordKey:
existingSecret:

Expand Down

0 comments on commit 6315b7b

Please sign in to comment.