{{- if .Values.postgresql.enabled -}}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "supersync.postgresql.fullname" . }}
  labels:
    {{- include "supersync.labels" . | nindent 4 }}
    app.kubernetes.io/component: postgresql
spec:
  serviceName: {{ include "supersync.postgresql.fullname" . }}
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "supersync.name" . }}-postgresql
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "supersync.name" . }}-postgresql
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/component: postgresql
    spec:
      securityContext:
        runAsUser: 999
        fsGroup: 999
      containers:
        - name: postgresql
          image: "{{ .Values.postgresql.image.repository }}:{{ .Values.postgresql.image.tag }}"
          imagePullPolicy: {{ .Values.postgresql.image.pullPolicy }}
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop:
                - ALL
          ports:
            - name: postgresql
              containerPort: 5432
              protocol: TCP
          env:
            - name: POSTGRES_USER
              value: {{ .Values.postgresql.username | quote }}
            - name: POSTGRES_DB
              value: {{ .Values.postgresql.database | quote }}
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.postgresql.existingSecret | default (include "supersync.postgresql.fullname" .) }}
                  key: POSTGRES_PASSWORD
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          readinessProbe:
            exec:
              command:
                - pg_isready
                - -U
                - {{ .Values.postgresql.username }}
                - -d
                - {{ .Values.postgresql.database }}
            periodSeconds: 10
            timeoutSeconds: 5
          livenessProbe:
            exec:
              command:
                - pg_isready
                - -U
                - {{ .Values.postgresql.username }}
                - -d
                - {{ .Values.postgresql.database }}
            periodSeconds: 30
            timeoutSeconds: 5
          resources:
            {{- toYaml .Values.postgresql.resources | nindent 12 }}
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
    {{- if not .Values.postgresql.persistence.enabled }}
      volumes:
        - name: data
          emptyDir: {}
    {{- end }}
  {{- if .Values.postgresql.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          {{- range .Values.postgresql.persistence.accessModes }}
          - {{ . }}
          {{- end }}
        {{- if .Values.postgresql.persistence.storageClass }}
        storageClassName: {{ .Values.postgresql.persistence.storageClass | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.postgresql.persistence.size }}
  {{- end }}
{{- end }}
