apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "postgresql-helm.fullname" . }}
  labels:
    {{- include "postgresql-helm.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  serviceName: {{ include "postgresql-helm.fullname" . }}
  selector:
    matchLabels:
      {{- include "postgresql-helm.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "postgresql-helm.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      volumes:
        {{ if .Values.tls.enabled -}}
        - name: certs
          secret:
            secretName: {{ .Values.tls.certificatesSecret }}
        {{- end }}
        - configMap:
            defaultMode: 420
            name: {{ include "postgresql-helm.fullname" . }}
          name: config
        - name: data
          {{- .Values.volume | nindent 10 }}
        - emptyDir:
            medium: Memory
          name: shm
      containers:
        - name: postgresql
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  key: "postgres-password"
                  name: {{ include "postgresql-helm.fullname" . }}
          ports:
            - name: sql
              containerPort: 5432
              protocol: TCP
          startupProbe:
            exec:
              command:
                - sh
                - -c
                - exec pg_isready --host localhost
          livenessProbe:
            exec:
              command:
                - sh
                - -c
                - exec pg_isready --host localhost
          readinessProbe:
            exec:
              command:
                - sh
                - -c
                - exec pg_isready --host localhost
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - mountPath: "/var/lib/postgresql/data"
              name: data
            - mountPath: "/configs"
              name: config
            - mountPath: "/dev/shm"
              name: shm
            {{ if .Values.tls.enabled -}}
            - mountPath: "/certs"
              name: certs
            {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}