Skip to content

Commit 3b43465

Browse files
committed
feat(passport): add replica database support
1 parent 32e4e84 commit 3b43465

5 files changed

Lines changed: 44 additions & 5 deletions

File tree

charts/passport/templates/_helpers.tpl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ env:
7373
secretKeyRef:
7474
name: passport-creds
7575
key: database-url
76+
{{- if (.Values.databaseUrl) }}
77+
- name: DRYCC_DATABASE_REPLICA_URL
78+
valueFrom:
79+
secretKeyRef:
80+
name: passport-creds
81+
key: database-replica-url
82+
{{- end }}
7683
{{- else if eq .Values.global.databaseLocation "on-cluster" }}
7784
- name: DRYCC_DATABASE_USER
7885
valueFrom:
@@ -85,7 +92,9 @@ env:
8592
name: database-creds
8693
key: password
8794
- name: DRYCC_DATABASE_URL
88-
value: "postgres://$(DRYCC_DATABASE_USER):$(DRYCC_DATABASE_PASSWORD)@$(DRYCC_DATABASE_SERVICE_HOST):$(DRYCC_DATABASE_SERVICE_PORT)/passport"
95+
value: "postgres://$(DRYCC_DATABASE_USER):$(DRYCC_DATABASE_PASSWORD)@drycc-database.{{.Release.Namespace}}.svc.{{.Values.global.clusterDomain}}:5432/passport"
96+
- name: DRYCC_DATABASE_REPLICA_URL
97+
value: "postgres://$(DRYCC_DATABASE_USER):$(DRYCC_DATABASE_PASSWORD)@drycc-database-replica.{{.Release.Namespace}}.svc.{{.Values.global.clusterDomain}}:5432/passport"
8998
{{- end }}
9099
- name: DRYCC_REDIS_ADDRS
91100
valueFrom:

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ data:
1111
{{- if (.Values.databaseUrl) }}
1212
database-url: {{ .Values.databaseUrl | b64enc }}
1313
{{- end }}
14+
{{- if (.Values.databaseReplicaUrl) }}
15+
database-replica-url: {{ .Values.databaseReplicaUrl | b64enc }}
16+
{{- end }}
1417
django-secret-key: {{ randAscii 64 | b64enc }}
1518
drycc-passport-controller-key: {{ randAlphaNum 40 | b64enc }}
1619
drycc-passport-controller-secret: {{ randAlphaNum 64 | b64enc }}

charts/passport/values.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ podAntiAffinityPreset:
2525
replicas: 1
2626
# limitsCpu: "100m"
2727
# limitsMemory: "50Mi"
28-
## Configuring this will no longer use the built-in database component
28+
## databaseUrl and databaseReplicaUrl are will no longer use the built-in database component
2929
databaseUrl: ""
30+
databaseReplicaUrl: ""
3031
# Any custom controller environment variables
3132
# can be specified as key-value pairs under environment
3233
# this is usually a non required setting.

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: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,20 @@
265265
random_secret = ')u_jckp95wule8#wxd8sm!0tj2j&aveozu!nnpgl)2x&&16gfj'
266266
SECRET_KEY = os.environ.get('DRYCC_SECRET_KEY', random_secret)
267267

268-
# database setting
268+
# database default setting
269269
DRYCC_DATABASE_URL = os.environ.get('DRYCC_DATABASE_URL', 'postgres://postgres:123456@49.232.207.93:5432/drycc_passport') # noqa
270270
DATABASES = {
271-
'default': dj_database_url.config(default=DRYCC_DATABASE_URL,
272-
conn_max_age=600)
271+
'default': dj_database_url.config(
272+
default=DRYCC_DATABASE_URL, conn_max_age=600)
273273
}
274+
# database replica setting
275+
DRYCC_DATABASE_REPLICA_URL = os.environ.get('DRYCC_DATABASE_REPLICA_URL', None)
276+
if DRYCC_DATABASE_REPLICA_URL is not None:
277+
DATABASES["replica"] = dj_database_url.config(
278+
default=DRYCC_DATABASE_REPLICA_URL, conn_max_age=600)
279+
280+
# database routers
281+
DATABASE_ROUTERS = ['api.routers.DefaultReplicaRouter', ]
274282

275283
# Static files (CSS, JavaScript, Images)
276284
# https://docs.djangoproject.com/en/2.2/howto/static-files/

0 commit comments

Comments
 (0)