{{ if .Values.postgres_storage.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: postgres-storage
spec:
  type: ClusterIP
  selector:
    app: postgres-storage
  ports:
    - name: http
      protocol: TCP
      port: 5432
      targetPort: 5432
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres-storage
spec:
  replicas: 1
  serviceName: postgres-storage
  selector:
    matchLabels:
      app: postgres-storage
  template:
    metadata:
      name: postgres-storage
      labels:
        app: postgres-storage
    spec:      
      containers:
        - name: postgres-storage
          image: postgres:{{.Values.postgres_storage.postgres_version}}
          ports:
            - containerPort: 5432
              name: postgres
          env:
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: postgres-storage-credentials
                  key: username
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-storage-credentials
                  key: password
            - name: POSTGRES_DB
              value: storage
            - name: PGDATA
              value: /var/lib/postgresql/data
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgres-storage

      volumes:
        - name: postgres-storage
          hostPath:
            path: /data/{{.Values.customer}}/data/postgres-storage
            type: Directory
{{ end }}