{{- if .Values.postgresql.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "astron-agent.fullname" . }}-postgres
  labels:
    {{- include "astron-agent.labels" . | nindent 4 }}
    app.kubernetes.io/component: postgres
spec:
  serviceName: {{ include "astron-agent.fullname" . }}-postgres
  replicas: {{ .Values.postgresql.replicaCount }}
  selector:
    matchLabels:
      {{- include "astron-agent.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: postgres
  template:
    metadata:
      labels:
        {{- include "astron-agent.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: postgres
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - name: postgres
        image: "{{ .Values.postgresql.image }}"
        imagePullPolicy: {{ .Values.global.imagePullPolicy }}
        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 "astron-agent.fullname" . }}-postgres-secret
              key: postgres-password
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        ports:
        - name: postgres
          containerPort: 5432
          protocol: TCP
        livenessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - pg_isready -U {{ .Values.postgresql.auth.username }} -d {{ .Values.postgresql.auth.database }}
          initialDelaySeconds: 30
          periodSeconds: {{ .Values.postgresql.healthCheck.interval }}
          timeoutSeconds: {{ .Values.postgresql.healthCheck.timeout }}
          failureThreshold: {{ .Values.postgresql.healthCheck.retries }}
        readinessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - pg_isready -U {{ .Values.postgresql.auth.username }} -d {{ .Values.postgresql.auth.database }}
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 6
        resources:
          {{- toYaml .Values.postgresql.resources | nindent 10 }}
        volumeMounts:
        - name: postgres-data
          mountPath: /var/lib/postgresql/data
        - name: init-scripts
          mountPath: /docker-entrypoint-initdb.d
      volumes:
      - name: init-scripts
        configMap:
          name: {{ include "astron-agent.fullname" . }}-postgres-init
  {{- if .Values.postgresql.persistence.enabled }}
  volumeClaimTemplates:
  - metadata:
      name: postgres-data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      {{- if .Values.global.storageClass }}
      storageClassName: {{ .Values.global.storageClass | quote }}
      {{- end }}
      resources:
        requests:
          storage: {{ .Values.postgresql.persistence.size }}
  {{- else }}
      - name: postgres-data
        emptyDir: {}
  {{- end }}
{{- end }}
