Files
ess-helm/charts/matrix-stack/templates/postgres/statefulset.yaml

228 lines
9.3 KiB
YAML

{{- /*
Copyright 2025 New Vector Ltd
Copyright 2025-2026 Element Creations Ltd
SPDX-License-Identifier: AGPL-3.0-only
*/ -}}
{{- with $.Values.postgres -}}
{{- if (include "element-io.postgres.enabled" (dict "root" $)) }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
{{- include "element-io.postgres.labels" (dict "root" $ "context" .) | nindent 4 }}
k8s.element.io/postgres-secret-hash: "{{ include "element-io.postgres.secret-data" (dict "root" $ "context" .) | sha1sum }}"
k8s.element.io/postgres-config-hash: "{{ include "element-io.postgres.configmap-data" (dict "root" $ "context" .) | sha1sum }}"
name: {{ $.Release.Name }}-postgres
namespace: {{ $.Release.Namespace }}
{{- with .annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
{{ include "element-io.ess-library.workloads.commonSpec" (dict "root" $ "context" (dict "nameSuffix" "postgres" "kind" "StatefulSet" "componentValues" .)) | nindent 2 }}
template:
metadata:
labels:
{{- include "element-io.postgres.labels" (dict "root" $ "context" (dict "image" .image "labels" .labels "withChartVersion" false)) | nindent 8 }}
k8s.element.io/postgres-secret-hash: "{{ include "element-io.postgres.secret-data" (dict "root" $ "context" .) | sha1sum }}"
k8s.element.io/postgres-config-hash: "{{ include "element-io.postgres.configmap-data" (dict "root" $ "context" .) | sha1sum }}"
{{- with .annotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
{{- include "element-io.ess-library.pods.commonSpec" (dict "root" $ "context" (dict "componentValues" . "instanceSuffix" "postgres" "kind" "StatefulSet")) | nindent 6 }}
{{- with .extraInitContainers }}
initContainers:
{{- toYaml . | nindent 6 }}
{{- end }}
containers:
- name: postgres
{{- include "element-io.ess-library.pods.image" (dict "root" $ "context" .image) | nindent 8 }}
{{- with .containersSecurityContext }}
securityContext:
{{- toYaml . | nindent 10 }}
{{- end }}
{{- include "element-io.ess-library.pods.env" (dict "root" $ "context" (dict "componentValues" . "componentName" "postgres")) | nindent 8 }}
args:
{{ include "element-io.postgres.args" (dict "root" $ "context" .) | nindent 8 }}
{{- with .resources }}
resources:
{{- toYaml . | nindent 10 }}
{{- end }}
lifecycle:
preStop:
exec:
command: ["pg_ctl", "stop", "-D", "/var/lib/postgres/data", "-w", "-t", "55", "-m", "fast"]
startupProbe: {{- include "element-io.ess-library.pods.probe" .startupProbe | nindent 10 }}
exec:
command: ["psql", "-w", "-U", "postgres", "-d", "postgres", "-h", "localhost", "-c", "SELECT 1"]
readinessProbe: {{- include "element-io.ess-library.pods.probe" .readinessProbe | nindent 10 }}
exec:
command: ["pg_isready", "-U", "postgres", "-d", "postgres", "-h", "localhost"]
livenessProbe: {{- include "element-io.ess-library.pods.probe" .livenessProbe | nindent 10 }}
exec:
command: ["psql", "-w", "-U", "postgres", "-d", "postgres", "-h", "localhost", "-c", "SELECT 1"]
volumeMounts:
{{- with (include "element-io.init-secrets.postgres-generated-secrets" (dict "root" $)) | fromYamlArray }}
{{- range . -}}
{{- $secretArg := . | splitList ":" }}
{{- if (index $secretArg 1) | contains "POSTGRES" }}
- mountPath: /secrets/{{ index $secretArg 0 }}/{{ index $secretArg 1 }}
name: "secret-generated"
subPath: "{{ index $secretArg 1 }}"
readOnly: true
{{- end }}
{{- end }}
{{- end }}
{{- range $secret := include "element-io.postgres.configSecrets" (dict "root" $ "context" .) | fromJsonArray }}
{{- with (tpl $secret $) }}
- mountPath: /secrets/{{ . }}
name: "secret-{{ . | sha256sum | trunc 12 }}"
readOnly: true
{{- end }}
{{- end }}
- name: config
mountPath: /docker-entrypoint-initdb.d/init-ess-dbs.sh
subPath: configure-dbs.sh
- name: database
mountPath: /var/lib/postgres/data
- name: temp
mountPath: /tmp
- name: var-run
mountPath: /var/run/postgresql
{{- range .extraVolumeMounts }}
- {{ (. | toYaml) | nindent 10 }}
{{- end }}
- name: postgres-ess-updater
{{- include "element-io.ess-library.pods.image" (dict "root" $ "context" .image) | nindent 8 }}
command:
- /bin/sh
- -c
- >
while ! pg_isready -d postgres -U postgres -h localhost; do echo "Postgres not yet ready"; sleep 1; done;
echo "Postgres now ready, so changing password";
/bin/sh /var/run/configure-ess-dbs.sh;
echo "Done";
trap : TERM INT; sleep infinity & wait
{{- with .containersSecurityContext }}
securityContext:
{{- toYaml . | nindent 10 }}
{{- end }}
{{- include "element-io.ess-library.pods.env" (dict "root" $ "context" (dict "componentValues" . "componentName" "postgres-password-updater")) | nindent 8 }}
args:
{{ include "element-io.postgres.args" (dict "root" $ "context" .) | nindent 8 }}
{{- with .resources }}
resources:
{{- toYaml . | nindent 10 }}
{{- end }}
lifecycle:
preStop:
exec:
command: ["pg_ctl", "stop", "-D", "/var/lib/postgres/data", "-w", "-t", "55", "-m", "fast"]
startupProbe: {{- include "element-io.ess-library.pods.probe" .startupProbe | nindent 10 }}
exec:
command: ["psql", "-w", "-U", "postgres", "-d", "postgres", "-h", "localhost", "-c", "SELECT 1"]
readinessProbe: {{- include "element-io.ess-library.pods.probe" .readinessProbe | nindent 10 }}
exec:
command: ["pg_isready", "-U", "postgres", "-d", "postgres", "-h", "localhost"]
livenessProbe: {{- include "element-io.ess-library.pods.probe" .livenessProbe | nindent 10 }}
exec:
command: ["psql", "-w", "-U", "postgres", "-d", "postgres", "-h", "localhost", "-c", "SELECT 1"]
ports:
- containerPort: 5432
name: postgres
volumeMounts:
{{- range .extraVolumeMounts }}
- {{ (. | toYaml) | nindent 10 }}
{{- end }}
{{- with (include "element-io.init-secrets.postgres-generated-secrets" (dict "root" $)) | fromYamlArray }}
{{- range . -}}
{{- $secretArg := . | splitList ":" }}
{{- if (index $secretArg 1) | contains "POSTGRES" }}
- mountPath: /secrets/{{ index $secretArg 0 }}/{{ index $secretArg 1 }}
name: "secret-generated"
subPath: "{{ index $secretArg 1 }}"
readOnly: true
{{- end }}
{{- end }}
{{- end }}
{{- range $secret := include "element-io.postgres.configSecrets" (dict "root" $ "context" .) | fromJsonArray }}
{{- with (tpl $secret $) }}
- mountPath: /secrets/{{ . }}
name: "secret-{{ . | sha256sum | trunc 12 }}"
readOnly: true
{{- end }}
{{- end }}
- name: config
mountPath: /var/run/configure-ess-dbs.sh
subPath: configure-dbs.sh
- name: database
mountPath: /var/lib/postgres/data
{{- with .postgresExporter }}
- name: postgres-exporter
{{- include "element-io.ess-library.pods.image" (dict "root" $ "context" .image) | nindent 8 }}
{{- with .containersSecurityContext }}
securityContext:
{{- toYaml . | nindent 10 }}
{{- end }}
{{- include "element-io.ess-library.pods.env" (dict "root" $ "context" (dict "componentValues" . "componentName" "postgres-exporter")) | nindent 8 }}
ports:
- name: metrics
containerPort: 9187
startupProbe: {{- include "element-io.ess-library.pods.probe" .startupProbe | nindent 10 }}
httpGet:
path: /metrics
port: metrics
livenessProbe: {{- include "element-io.ess-library.pods.probe" .livenessProbe | nindent 10 }}
httpGet:
path: /metrics
port: metrics
readinessProbe: {{- include "element-io.ess-library.pods.probe" .readinessProbe | nindent 10 }}
httpGet:
path: /metrics
port: metrics
{{- with .extraVolumeMounts }}
volumeMounts:
{{- range . }}
- {{ (. | toYaml) | nindent 10 }}
{{- end }}
{{- end }}
{{- with .resources }}
resources:
{{- toYaml . | nindent 10 }}
{{- end }}
{{- end }}
terminationGracePeriodSeconds: 60
volumes:
- emptyDir:
medium: Memory
name: temp
- emptyDir:
medium: Memory
name: var-run
{{- if and $.Values.initSecrets.enabled (include "element-io.init-secrets.postgres-generated-secrets" (dict "root" $)) }}
- secret:
secretName: {{ $.Release.Name }}-generated
name: secret-generated
{{- end }}
{{- range $secret := include "element-io.postgres.configSecrets" (dict "root" $ "context" .) | fromJsonArray }}
{{- with (tpl $secret $) }}
- secret:
secretName: {{ . }}
name: "secret-{{ . | sha256sum | trunc 12 }}"
{{- end }}
{{- end }}
{{- range .extraVolumes }}
- {{- (tpl (. | toYaml) $) | nindent 8 }}
{{- end }}
- name: config
configMap:
name: {{ $.Release.Name }}-postgres
- name: database
persistentVolumeClaim:
claimName: {{ $.Release.Name }}-postgres-data
{{- end }}
{{- end }}