{{- if .Values.postgresql.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "consentos.fullname" . }}-postgresql
  labels:
    {{- include "consentos.labels" . | nindent 4 }}
    app.kubernetes.io/component: postgresql
spec:
  serviceName: {{ include "consentos.fullname" . }}-postgresql
  replicas: 1
  selector:
    matchLabels:
      {{- include "consentos.selectorLabels" (dict "Chart" .Chart "Release" .Release "component" "postgresql") | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "consentos.selectorLabels" (dict "Chart" .Chart "Release" .Release "component" "postgresql") | nindent 8 }}
    spec:
      containers:
        - name: postgresql
          image: "{{ .Values.postgresql.image.repository }}:{{ .Values.postgresql.image.tag }}"
          ports:
            - name: postgresql
              containerPort: 5432
              protocol: TCP
          env:
            - name: POSTGRES_DB
              value: {{ .Values.postgresql.auth.database | quote }}
            - name: POSTGRES_USER
              value: {{ .Values.postgresql.auth.username | quote }}
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "consentos.secretName" . }}
                  key: postgresql-password
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          livenessProbe:
            exec:
              command:
                - pg_isready
                - -U
                - {{ .Values.postgresql.auth.username }}
            initialDelaySeconds: 15
            periodSeconds: 10
          readinessProbe:
            exec:
              command:
                - pg_isready
                - -U
                - {{ .Values.postgresql.auth.username }}
            initialDelaySeconds: 5
            periodSeconds: 5
          resources:
            {{- toYaml .Values.postgresql.resources | nindent 12 }}
          {{- if .Values.postgresql.persistence.enabled }}
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
          {{- end }}
  {{- if .Values.postgresql.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        {{- if .Values.postgresql.persistence.storageClass }}
        storageClassName: {{ .Values.postgresql.persistence.storageClass | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.postgresql.persistence.size }}
  {{- end }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "consentos.fullname" . }}-postgresql
  labels:
    {{- include "consentos.labels" . | nindent 4 }}
    app.kubernetes.io/component: postgresql
spec:
  type: ClusterIP
  ports:
    - port: 5432
      targetPort: postgresql
      protocol: TCP
      name: postgresql
  selector:
    {{- include "consentos.selectorLabels" (dict "Chart" .Chart "Release" .Release "component" "postgresql") | nindent 4 }}
{{- end }}
