{{- if .Values.postgres.enabled }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "label-studio.fullname" . }}-postgres
  labels:
    app.kubernetes.io/name: {{ include "label-studio.name" . }}-postgres
spec:
  ports:
    - port: 5432
      targetPort: 5432
  selector:
    app.kubernetes.io/name: {{ include "label-studio.name" . }}-postgres
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "label-studio.fullname" . }}-postgres
  labels:
    app.kubernetes.io/name: {{ include "label-studio.name" . }}-postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "label-studio.name" . }}-postgres
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "label-studio.name" . }}-postgres
    spec:
      containers:
        - name: postgres
          {{- if .Values.global.imageRegistry }}
          image: "{{ .Values.global.imageRegistry | trimSuffix "/" }}/{{ .Values.postgres.image.repository }}:{{ .Values.postgres.image.tag }}"
          {{- else }}
          image: "{{ .Values.postgres.image.repository }}:{{ .Values.postgres.image.tag }}"
          {{- end }}
          imagePullPolicy: {{ .Values.postgres.image.pullPolicy }}
          env:
            - name: POSTGRES_HOST_AUTH_METHOD
              value: {{ .Values.postgres.authMethod | quote }}
            - name: POSTGRES_USER
              value: {{ .Values.postgres.username | quote }}
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-data
              mountPath: /var/lib/postgresql/data
      volumes:
        - name: postgres-data
          persistentVolumeClaim:
            claimName: {{ include "label-studio.fullname" . }}-postgres
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: {{ include "label-studio.fullname" . }}-postgres
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: {{ .Values.postgres.persistence.size }}
  {{- if .Values.persistence.storageClass }}
  storageClassName: {{ .Values.postgres.persistence.storageClass }}
  {{- end }}
{{- end }}

