Skip to content

Commit 395c15d

Browse files
masonCheinTian
authored andcommitted
refactor: refactor postgre and redis config template
1 parent 8d56084 commit 395c15d

File tree

13 files changed

+199
-168
lines changed

13 files changed

+199
-168
lines changed

charts/csghub/charts/csgship/charts/agentic/templates/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ spec:
6666
{{- if .Values.global.redis.enabled }}
6767
- name: REDIS_URL
6868
value: {{ printf ":@%s" $redisConfig.host }}
69-
{{/* value: {{ printf ":$(REDIS_PASSWD)@%s" (include "csghub.redis.host" .) }}*/}}
69+
{{/* value: {{ printf ":$(REDIS_PASSWD)@%s" $redisConfig.host }}*/}}
7070
{{- end }}
7171
{{- with .Values.environments }}
7272
{{- range $key, $value := . }}

charts/csghub/charts/csgship/charts/billing/templates/deployment.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ SPDX-License-Identifier: APACHE-2.0
44
*/}}
55

66
{{- if .Values.enabled }}
7+
{{- $redisConfig := include "csghub.redis.config" (dict "service" .Values "global" .) | fromYaml }}
78
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
89
kind: Deployment
910
metadata:
@@ -71,8 +72,8 @@ spec:
7172
value: nats://$(NATS_USERNAME):$(NATS_PASSWORD)@csghub-nats:4222
7273
{{- if .Values.global.redis.enabled }}
7374
- name: REDIS_URL
74-
value: {{ printf ":@%s" (include "csghub.redis.host" .) }}
75-
{{/* value: {{ printf ":$(REDIS_PASSWD)@%s" (include "csghub.redis.host" .) }}*/}}
75+
value: {{ printf ":@%s" $redisConfig.host }}
76+
{{/* value: {{ printf ":$(REDIS_PASSWD)@%s" $redisConfig.host }}*/}}
7677
{{- end }}
7778
{{- with .Values.environments }}
7879
{{- range $key, $value := . }}

charts/csghub/charts/csgship/charts/web/templates/statefulset.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ spec:
7474
{{- if .Values.global.redis.enabled }}
7575
- name: REDIS_URL
7676
value: {{ printf ":@%s" $redisConfig.host }}
77-
{{/* value: {{ printf ":$(REDIS_PASSWD)@%s" (include "csghub.redis.host" .) }}*/}}
77+
{{/* value: {{ printf ":$(REDIS_PASSWD)@%s" $redisConfig.host }}*/}}
7878
{{- end }}
7979
- name: NATS_URL
8080
value: "nats://$(NATS_USERNAME):$(NATS_PASSWORD)@{{ include "nats.internal.domain" . }}:{{ include "nats.internal.ports.api" . }}"

charts/csghub/charts/csgship/charts/worker/templates/deployment.yaml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Copyright OpenCSG, Inc. All Rights Reserved.
33
SPDX-License-Identifier: APACHE-2.0
44
*/}}
55

6+
{{- $redisConfig := include "csghub.redis.config" (dict "service" .Values "global" .) | fromYaml }}
67
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
78
kind: Deployment
89
metadata:
@@ -60,8 +61,8 @@ spec:
6061
env:
6162
{{- if .Values.global.redis.enabled }}
6263
- name: REDIS_URL
63-
{{/* value: {{ printf ":$(REDIS_PASSWD)@%s" (include "csghub.redis.host" .) }}*/}}
64-
value: {{ printf ":@%s" (include "csghub.redis.host" .) }}
64+
value: {{ printf ":@%s" $redisConfig.host }}
65+
{{/* value: {{ printf ":$(REDIS_PASSWD)@%s" $redisConfig.host }}*/}}
6566
{{- end }}
6667
{{- with .Values.environments }}
6768
{{- range $key, $value := . }}
@@ -94,8 +95,8 @@ spec:
9495
env:
9596
{{- if .Values.global.redis.enabled }}
9697
- name: REDIS_URL
97-
value: {{ printf ":@%s" (include "csghub.redis.host" .) }}
98-
{{/* value: {{ printf ":$(REDIS_PASSWD)@%s" (include "csghub.redis.host" .) }}*/}}
98+
value: {{ printf ":@%s" $redisConfig.host }}
99+
{{/* value: {{ printf ":$(REDIS_PASSWD)@%s" $redisConfig.host }}*/}}
99100
{{- end }}
100101
{{- with .Values.environments }}
101102
{{- range $key, $value := . }}
@@ -128,8 +129,8 @@ spec:
128129
env:
129130
{{- if .Values.global.redis.enabled }}
130131
- name: REDIS_URL
131-
value: {{ printf ":@%s" (include "csghub.redis.host" .) }}
132-
{{/* value: {{ printf ":$(REDIS_PASSWD)@%s" (include "csghub.redis.host" .) }}*/}}
132+
value: {{ printf ":@%s" $redisConfig.host }}
133+
{{/* value: {{ printf ":$(REDIS_PASSWD)@%s" $redisConfig.host }}*/}}
133134
{{- end }}
134135
{{- with .Values.environments }}
135136
{{- range $key, $value := . }}

charts/csghub/charts/redis/templates/secret.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ metadata:
1414
helm.sh/resource-policy: keep
1515
type: Opaque
1616
data:
17-
{{- $password := include "csghub.redis.password" . | b64enc }}
17+
{{- $password := include "common.randomPassword" "redis" | b64enc }}
1818
{{- $secretData := (lookup "v1" "Secret" .Release.Namespace (include "common.names.custom" .)).data }}
1919
{{- if $secretData }}
2020
{{- $secretPass := index $secretData "REDIS_PASSWD" }}

charts/csghub/templates/helpers/_database.tpl

Lines changed: 0 additions & 96 deletions
This file was deleted.

charts/csghub/templates/helpers/_ports.tpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ Get the port number by service name
2222
"portal" 8090
2323
"loki" 3100
2424
"mirror-lfs" 8092
25+
"redis" 6379
26+
"postgresql" 5432
27+
"mongo" 27017
28+
"label-studio" 8002
29+
"dataflow" 8000
2530
-}}
2631

2732
{{- if not (hasKey $portMap $serviceType) -}}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{{- /*
2+
Copyright OpenCSG, Inc. All Rights Reserved.
3+
SPDX-License-Identifier: APACHE-2.0
4+
*/ -}}
5+
6+
{{/*
7+
Generate PostgreSQL Connection Configuration
8+
*/}}
9+
{{- define "csghub.postgresql.config" -}}
10+
{{- $service := .service -}}
11+
{{- $global := .global -}}
12+
13+
{{- /* Configuration priority: internal PostgreSQL (if enabled) > service-level external > global external */ -}}
14+
15+
{{- /* Default configuration for internal PostgreSQL */ -}}
16+
{{- $postgresqlName := include "common.names.custom" (list $global "postgresql") -}}
17+
{{- $postgresqlConfig := dict
18+
"host" $postgresqlName
19+
"port" (include "csghub.svc.port" "postgresql")
20+
"user" ($service.postgresql.user | default "postgres")
21+
"password" (include "common.randomPassword" "postgresql")
22+
"database" ($service.postgresql.database | default "postgres")
23+
"timezone" "Etc/UTC"
24+
"sslmode" "disable"
25+
-}}
26+
27+
{{- /* If internal PostgreSQL is enabled and secret exists, use existing password */ -}}
28+
{{- if $global.Values.global.postgresql.enabled -}}
29+
{{- $secret := (lookup "v1" "Secret" $global.Release.Namespace $postgresqlName) -}}
30+
{{- if and $secret (index $secret.data $postgresqlConfig.user) -}}
31+
{{- $_ := set $postgresqlConfig "password" (index $secret.data $postgresqlConfig.user | b64dec) -}}
32+
{{- end -}}
33+
{{- end -}}
34+
35+
{{- /* Override with external PostgreSQL configuration if internal is disabled */ -}}
36+
{{- if not $global.Values.global.postgresql.enabled -}}
37+
{{- /* Global external PostgreSQL configuration */ -}}
38+
{{- with $global.Values.global.postgresql.external -}}
39+
{{- $postgresqlConfig = merge (dict
40+
"host" (.host | default $postgresqlConfig.host)
41+
"port" (.port | default $postgresqlConfig.port)
42+
"user" (.user | default $postgresqlConfig.user)
43+
"password" (.password | default $postgresqlConfig.password)
44+
"database" (.database | default $postgresqlConfig.database)
45+
"timezone" (.timezone | default $postgresqlConfig.timezone)
46+
"sslmode" (.sslmode | default $postgresqlConfig.sslmode)
47+
) $postgresqlConfig -}}
48+
{{- end -}}
49+
50+
{{- /* Service-level external PostgreSQL configuration (higher priority) */ -}}
51+
{{- with $service.postgresql -}}
52+
{{- $postgresqlConfig = merge (dict
53+
"host" (.host | default $postgresqlConfig.host)
54+
"port" (.port | default $postgresqlConfig.port)
55+
"user" (.user | default $postgresqlConfig.user)
56+
"password" (.password | default $postgresqlConfig.password)
57+
"database" (.database | default $postgresqlConfig.database)
58+
"timezone" (.timezone | default $postgresqlConfig.timezone)
59+
"sslmode" (.sslmode | default $postgresqlConfig.sslmode)
60+
) $postgresqlConfig -}}
61+
{{- end -}}
62+
{{- end -}}
63+
64+
{{- /* Validate required configurations */ -}}
65+
{{- if not $postgresqlConfig.password -}}
66+
{{- fail "PostgreSQL password must be set" -}}
67+
{{- end -}}
68+
69+
{{- if not $postgresqlConfig.host -}}
70+
{{- fail "PostgreSQL host must be set" -}}
71+
{{- end -}}
72+
73+
{{- if not $postgresqlConfig.database -}}
74+
{{- fail "PostgreSQL database name must be set" -}}
75+
{{- end -}}
76+
77+
{{- $postgresqlConfig | toYaml -}}
78+
{{- end -}}
79+
80+
{{/*
81+
Generate PostgreSQL Database DSN
82+
*/}}
83+
{{- define "csghub.postgresql.dsn" -}}
84+
{{- $service := .service -}}
85+
{{- $global := .global -}}
86+
{{- $config := include "csghub.postgresql.config" (dict "service" $service "global" $global) | fromYaml -}}
87+
{{- printf "host=%s port=%s user=%s password=%s dbname=%s sslmode=%s" $config.host $config.port $config.user $config.password $config.database $config.sslmode -}}
88+
{{- end }}
89+
90+
{{/*
91+
Generate PostgreSQL Database URL
92+
*/}}
93+
{{- define "csghub.postgresql.url" -}}
94+
{{- $service := .service -}}
95+
{{- $global := .global -}}
96+
{{- $config := include "csghub.postgresql.config" (dict "service" $service "global" $global) | fromYaml -}}
97+
{{- printf "postgresql://%s:%s@%s:%s/%s?sslmode=%s&timezone=%s" $config.user $config.password $config.host $config.port $config.database $config.sslmode $config.timezone -}}
98+
{{- end }}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{{- /*
2+
Copyright OpenCSG, Inc. All Rights Reserved.
3+
SPDX-License-Identifier: APACHE-2.0
4+
*/ -}}
5+
6+
{{/*
7+
Generate a 32-bit random password
8+
*/}}
9+
{{- define "common.randomPassword" -}}
10+
{{- $input := . -}}
11+
{{- $minuteSeed := now | date "200601021504" -}}
12+
{{- $base1 := printf "%s-%s-a" $input $minuteSeed | sha256sum | b64enc | replace "=" "" -}}
13+
{{- $base2 := printf "%s-%s-b" $input $minuteSeed | sha256sum | b64enc | replace "=" "" -}}
14+
{{- $base3 := printf "%s-%s-c" $input $minuteSeed | sha256sum | b64enc | replace "=" "" -}}
15+
16+
{{- $combined := print $base1 $base2 $base3 -}}
17+
{{- $chars := splitList "" $combined -}}
18+
19+
{{- $result := "" -}}
20+
{{- range $i := until 32 -}}
21+
{{- $index := mod (add (mul $i 13) (len $minuteSeed)) (len $chars) -}}
22+
{{- $result = printf "%s%s" $result (index $chars $index) -}}
23+
{{- end -}}
24+
25+
{{- $result -}}
26+
{{- end -}}

0 commit comments

Comments
 (0)