|
| 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 }} |
0 commit comments