{{- if .Values.postgresql.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "certctl.fullname" . }}-postgres
  labels:
    {{- include "certctl.labels" . | nindent 4 }}
    app.kubernetes.io/component: postgres
spec:
  serviceName: {{ include "certctl.fullname" . }}-postgres
  replicas: 1
  selector:
    matchLabels:
      {{- include "certctl.postgresSelectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "certctl.postgresSelectorLabels" . | nindent 8 }}
    spec:
      securityContext:
        {{- toYaml .Values.postgresql.securityContext | nindent 8 }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: postgres
          image: {{ include "certctl.postgresImage" . }}
          imagePullPolicy: {{ .Values.postgresql.image.pullPolicy }}
          ports:
            - name: postgres
              containerPort: 5432
              protocol: TCP
          env:
            - name: POSTGRES_DB
              valueFrom:
                secretKeyRef:
                  name: {{ include "certctl.fullname" . }}-postgres
                  key: database
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: {{ include "certctl.fullname" . }}-postgres
                  key: username
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "certctl.fullname" . }}-postgres
                  key: password
            - name: POSTGRES_INITDB_ARGS
              value: "--encoding=UTF8"
          livenessProbe:
            {{- toYaml .Values.postgresql.livenessProbe | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.postgresql.readinessProbe | nindent 12 }}
          resources:
            {{- toYaml .Values.postgresql.resources | nindent 12 }}
          volumeMounts:
            - name: postgres-data
              mountPath: /var/lib/postgresql/data
              subPath: postgres
            - name: postgres-init
              mountPath: /docker-entrypoint-initdb.d
      volumes:
        - name: postgres-init
          emptyDir: {}
  volumeClaimTemplates:
    - metadata:
        name: postgres-data
      spec:
        accessModes:
          - ReadWriteOnce
        {{- if .Values.postgresql.storage.storageClass }}
        storageClassName: {{ .Values.postgresql.storage.storageClass }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.postgresql.storage.size }}
{{- end }}
