{{- if not .Values.database }}
{{- /* Only deploy in-cluster PostgreSQL if external database is not configured */ -}}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ .Release.Name }}-{{ .Values.postgres.name }}
  namespace: {{ .Values.global.namespace }}
  labels:
    app: {{ .Release.Name }}-{{ .Values.postgres.name }}
    component: database
spec:
  serviceName: {{ .Release.Name }}-{{ .Values.postgres.name }}-headless
  replicas: 1
  selector:
    matchLabels:
      app: {{ .Release.Name }}-{{ .Values.postgres.name }}
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}-{{ .Values.postgres.name }}
        component: database
    spec:
      containers:
        - name: postgres
          image: "{{ .Values.postgres.image.repository }}:{{ .Values.postgres.image.tag }}"
          imagePullPolicy: {{ .Values.postgres.image.pullPolicy }}
          ports:
            - containerPort: {{ .Values.postgres.port }}
              name: postgres
              protocol: TCP
          env:
            - name: POSTGRES_USER
              value: {{ .Values.postgres.user | quote }}
            - name: POSTGRES_PASSWORD
              value: {{ .Values.postgres.password | quote }}
            - name: POSTGRES_DB
              value: {{ .Values.postgres.database | quote }}
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
            - name: init-sql
              mountPath: /docker-entrypoint-initdb.d
          livenessProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - pg_isready -U {{ .Values.postgres.user }}
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          readinessProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - pg_isready -U {{ .Values.postgres.user }}
            initialDelaySeconds: 10
            periodSeconds: 5
            timeoutSeconds: 3
            failureThreshold: 2
          resources:
            requests:
              cpu: {{ .Values.postgres.resources.requests.cpu }}
              memory: {{ .Values.postgres.resources.requests.memory }}
            limits:
              cpu: {{ .Values.postgres.resources.limits.cpu }}
              memory: {{ .Values.postgres.resources.limits.memory }}
      volumes:
        - name: init-sql
          configMap:
            name: {{ .Release.Name }}-db-schema
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - {{ .Values.postgres.persistence.accessMode }}
        resources:
          requests:
            storage: {{ .Values.postgres.persistence.size }}
        {{- if .Values.postgres.persistence.storageClass }}
        storageClassName: {{ .Values.postgres.persistence.storageClass | quote }}
        {{- end }}
{{- end }}

