Add custom postgresql configuration in helm chart

This commit is contained in:
wbsong111
2024-11-13 10:57:38 +09:00
parent 84cef6d9bd
commit ca4f8416e4
4 changed files with 210 additions and 0 deletions
@@ -0,0 +1,88 @@
{{/*
Return Postgresql - memory
*/}}
{{- define "postgresql-ha.configuration.postgresql.memory" -}}
{{- $memory := required "postgresql.resources.limits.memory is required" .Values.postgresql.resources.limits.memory | trim -}}
{{- if hasSuffix "Gi" $memory -}}
{{ mul (int (trimSuffix "Gi" $memory)) 1024 }}
{{- else if hasSuffix "GB" $memory -}}
{{ mul (int (trimSuffix "GB" $memory)) 1000 }}
{{- else if hasSuffix "Mi" $memory -}}
{{ int (trimSuffix "Mi" $memory) }}
{{- else if hasSuffix "MB" $memory -}}
{{ int (trimSuffix "MB" $memory) }}
{{- else -}}
{{ fail "`postgresql.resources.limits.memory` 값이 올바른 형식으로 설정되지 않았습니다. (예: '10Gi', '512Mi')" }}
{{- end -}}
{{- end }}
{{/*
Return Postgresql - shared memory.
*/}}
{{- define "postgresql-ha.configuration.postgresql.sharedMemory" -}}
{{- $memory := include "postgresql-ha.configuration.postgresql.memory" . | int -}}
{{- if gt $memory 256 -}}
{{ div $memory 4 }}Mi
{{- end }}
{{- "" -}}
{{- end }}
{{/*
Return postgres.conf.
*/}}
{{- define "postgresql-ha.configuration.postgresConf" -}}
{{- $memory := include "postgresql-ha.configuration.postgresql.memory" . | int -}}
listen_addresses = '*'
port = {{ .Values.postgresql.containerPorts.postgresql | quote }}
password_encryption = md5
wal_level = 'replica'
archive_mode = 'on'
archive_command = '/bin/true'
max_wal_senders = {{ add .Values.postgresql.replicaCount 2 }}
max_replication_slots = '10'
hot_standby = 'on'
logging_collector = 'on'
log_directory = '/opt/bitnami/postgresql/logs'
log_filename = 'postgresql.log'
log_connections = 'false'
log_disconnections = 'false'
log_hostname = 'true'
client_min_messages = 'error'
pg_stat_statements.max = 10000
pg_stat_statements.track = all
include_dir = 'conf.d'
pgaudit.log_catalog = 'off'
wal_buffers = -1
max_wal_size = 10GB
min_wal_size = 1GB
shared_buffers = {{ div $memory 4 }}MB
effective_cache_size = {{ mul (div $memory 4) 3 }}MB
random_page_cost = 2.0
{{- if ge $memory 16384 }}
maintenance_work_mem = 2GB
work_mem = {{ div (subf $memory (div $memory 4) 2048) .Values.postgresql.maxConnections }}MB
{{- else if ge $memory 8192 }}
maintenance_work_mem = 1GB
work_mem = {{ div (subf $memory (div $memory 4) 1024) .Values.postgresql.maxConnections }}MB
{{- else }}
maintenance_work_mem = 512MB
work_mem = {{ div (subf $memory (div $memory 4) 512) .Values.postgresql.maxConnections }}MB
{{- end }}
log_min_duration_statement = 3000
{{- $tzEnv := "" }}
{{- range .Values.postgresql.extraEnvVars -}}
{{- if eq "TZ" .name -}}
{{- $tzEnv = .value -}}
{{- end -}}
{{- end -}}
{{- if $tzEnv }}
timezone={{ $tzEnv }}
{{- end -}}
{{- end -}}
@@ -27,4 +27,18 @@ data:
pg_hba.conf: |-
{{- include "common.tplvalues.render" ( dict "value" .Values.postgresql.pgHbaConfiguration "context" $ ) | nindent 4 }}
{{- end }}
{{- else }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ printf "%s-configuration" (include "postgresql-ha.postgresql" .) }}
namespace: {{ include "common.names.namespace" . | quote }}
labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
app.kubernetes.io/component: postgresql
{{- if .Values.commonAnnotations }}
annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
{{- end }}
data:
postgresql.conf: |-
{{- include "postgresql-ha.configuration.postgresConf" . | nindent 4 }}
{{- end }}
@@ -458,6 +458,10 @@ spec:
{{- if .Values.postgresql.extraVolumeMounts }}
{{- include "common.tplvalues.render" (dict "value" .Values.postgresql.extraVolumeMounts "context" $) | nindent 12 }}
{{- end }}
{{- if ( include "postgresql-ha.configuration.postgresql.sharedMemory" . ) }}
- name: dshm
mountPath: /dev/shm
{{- end }}
{{- if .Values.metrics.enabled }}
- name: metrics
image: {{ include "postgresql-ha.metrics.image" . }}
@@ -598,6 +602,12 @@ spec:
- name: postgresql-certificates
emptyDir: {}
{{- end }}
{{- if ( include "postgresql-ha.configuration.postgresql.sharedMemory" . ) }}
- name: dshm
emptyDir:
medium: Memory
sizeLimit: {{ include "postgresql-ha.configuration.postgresql.sharedMemory" . }}
{{- end }}
{{- if and .Values.persistence.enabled .Values.persistence.existingClaim }}
- name: data
persistentVolumeClaim: