Skip to content

Commit 8db9362

Browse files
committed
feat(controller): add database replica support
1 parent cf5578e commit 8db9362

5 files changed

Lines changed: 44 additions & 4 deletions

File tree

charts/controller/templates/_helpers.tpl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@ env:
4949
- name: DRYCC_DATABASE_URL
5050
valueFrom:
5151
secretKeyRef:
52-
name: controller-creds
53-
key: databaseUrl
52+
name: passport-creds
53+
key: database-url
54+
{{- if (.Values.databaseReplicaUrl) }}
55+
- name: DRYCC_DATABASE_REPLICA_URL
56+
valueFrom:
57+
secretKeyRef:
58+
name: passport-creds
59+
key: database-replica-url
60+
{{- end }}
5461
{{- else if eq .Values.global.databaseLocation "on-cluster" }}
5562
- name: DRYCC_DATABASE_USER
5663
valueFrom:
@@ -63,7 +70,9 @@ env:
6370
name: database-creds
6471
key: password
6572
- name: DRYCC_DATABASE_URL
66-
value: "postgres://$(DRYCC_DATABASE_USER):$(DRYCC_DATABASE_PASSWORD)@$(DRYCC_DATABASE_SERVICE_HOST):$(DRYCC_DATABASE_SERVICE_PORT)/controller"
73+
value: "postgres://$(DRYCC_DATABASE_USER):$(DRYCC_DATABASE_PASSWORD)@drycc-database.{{.Release.Namespace}}.svc.{{.Values.global.clusterDomain}}:5432/controller"
74+
- name: DRYCC_DATABASE_REPLICA_URL
75+
value: "postgres://$(DRYCC_DATABASE_USER):$(DRYCC_DATABASE_PASSWORD)@drycc-database-replica.{{.Release.Namespace}}.svc.{{.Values.global.clusterDomain}}:5432/controller"
6776
{{- end }}
6877
- name: WORKFLOW_NAMESPACE
6978
valueFrom:

charts/controller/templates/controller-secret-creds.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ data:
1010
{{- if (.Values.databaseUrl) }}
1111
database-url: {{ .Values.databaseUrl | b64enc }}
1212
{{- end }}
13+
{{- if (.Values.databaseReplicaUrl) }}
14+
database-replica-url: {{ .Values.databaseReplicaUrl | b64enc }}
15+
{{- end }}
1316
django-secret-key: {{ randAscii 64 | b64enc }}
1417
deploy-hook-secret-key: {{ randAscii 64 | b64enc }}

charts/controller/values.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ webhookReplicas: 1
2929
# Forbid: The cron job does not allow concurrent runs; if it is time for a new job run and the previous job run hasn't finished yet, the cron job skips the new job run
3030
# Replace: If it is time for a new job run and the previous job run hasn't finished yet, the cron job replaces the currently running job run with a new job run
3131
concurrencyPolicy: "Replace"
32-
# Configuring this will no longer use the built-in database component
32+
# databaseUrl and databaseReplicaUrl are will no longer use the built-in database component
3333
databaseUrl: ""
34+
databaseReplicaUrl: ""
3435
# Configuring this will no longer use the built-in rabbitmq component
3536
rabbitmqUrl: ""
3637
# Service

rootfs/api/routers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from django.conf import settings
2+
3+
4+
class DefaultReplicaRouter(object):
5+
6+
def db_for_read(self, model, **hints):
7+
if 'replica' in settings.DATABASES:
8+
return 'replica'
9+
return 'default'
10+
11+
def db_for_write(self, model, **hints):
12+
return 'default'
13+
14+
def allow_relation(self, obj1, obj2, **hints):
15+
return True
16+
17+
def allow_migrate(self, db, app_label, model_name=None, **hints):
18+
return True

rootfs/api/settings/production.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,15 @@
405405
'default': dj_database_url.config(default=DRYCC_DATABASE_URL, conn_max_age=600)
406406
}
407407

408+
# database replica setting
409+
DRYCC_DATABASE_REPLICA_URL = os.environ.get('DRYCC_DATABASE_REPLICA_URL', None)
410+
if DRYCC_DATABASE_REPLICA_URL is not None:
411+
DATABASES["replica"] = dj_database_url.config(
412+
default=DRYCC_DATABASE_REPLICA_URL, conn_max_age=600)
413+
414+
# database routers
415+
DATABASE_ROUTERS = ['api.routers.DefaultReplicaRouter', ]
416+
408417
APP_URL_REGEX = '[a-z0-9-]+'
409418

410419
# Oauth settings

0 commit comments

Comments
 (0)