---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
  namespace: core
spec:
  serviceName: postgres
  replicas: 1
  selector:
    matchLabels:
      name: postgres
  template:
    metadata:
      labels:
        name: postgres
    spec:
      restartPolicy: Always
      containers:
        - name: postgres
          image: postgres:17.4
          ports:
            - name: http
              containerPort: 5432
              protocol: TCP
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgres-data
              subPath: "data"
          env:
            - name: POSTGRES_USER
              value: postgres
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  key: ROOT_PASSWORD
                  name: postgres-secrets
          startupProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - exec pg_isready -U ${POSTGRES_USER}
          livenessProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - exec pg_isready -U ${POSTGRES_USER}
          readinessProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - exec pg_isready -U ${POSTGRES_USER}
  volumeClaimTemplates:
    - metadata:
        name: postgres-data
        annotations:
          name: postgres-data
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: longhorn-private
        resources:
          requests:
            storage: 2Gi
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: core
spec:
  type: ClusterIP
  selector:
    name: postgres
  ports:
    - port: 5432
      name: http
      targetPort: http
      protocol: TCP
---
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: TCPRoute
metadata:
  name: postgres
  namespace: core
spec:
  parentRefs:
    - name: homelab-gateway
      sectionName: postgres
      kind: Gateway
      namespace: homelab

  # hostnames:
  #   - postgres.leechpepin.local

  rules:
    # - matches:
    #     - path:
    #         type: PathPrefix
    #         value: /

    - backendRefs:
        - name: postgres
          namespace: core
          port: 5432