{{- if eq .Values.postgresql.mode "integrated" }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "groombook.fullname" . }}-postgresql
  labels:
    {{- include "groombook.labels" . | nindent 4 }}
    app.kubernetes.io/component: postgresql
spec:
  serviceName: {{ include "groombook.fullname" . }}-postgresql
  replicas: 1
  selector:
    matchLabels:
      {{- include "groombook.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: postgresql
  template:
    metadata:
      labels:
        {{- include "groombook.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: postgresql
    spec:
      containers:
        - name: postgresql
          image: {{ .Values.postgresql.integrated.image }}
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 5432
              name: postgresql
              protocol: TCP
          env:
            - name: POSTGRES_DB
              value: {{ .Values.postgresql.integrated.auth.database | quote }}
            - name: POSTGRES_USER
              value: {{ .Values.postgresql.integrated.auth.username | quote }}
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "groombook.fullname" . }}-db-credentials
                  key: postgresql-password
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
          readinessProbe:
            exec:
              command:
                - pg_isready
                - -U
                - {{ .Values.postgresql.integrated.auth.username | quote }}
            initialDelaySeconds: 10
            periodSeconds: 10
          livenessProbe:
            exec:
              command:
                - pg_isready
                - -U
                - {{ .Values.postgresql.integrated.auth.username | quote }}
            initialDelaySeconds: 30
            periodSeconds: 30
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        {{- if .Values.postgresql.integrated.storage.storageClass }}
        storageClassName: {{ .Values.postgresql.integrated.storage.storageClass }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.postgresql.integrated.storage.size }}
{{- end }}
