apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
  namespace: foxhunt
  labels:
    app.kubernetes.io/name: postgres
    app.kubernetes.io/part-of: foxhunt
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  namespace: foxhunt
  labels:
    app.kubernetes.io/name: postgres
    app.kubernetes.io/part-of: foxhunt
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: postgres
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app.kubernetes.io/name: postgres
        app.kubernetes.io/part-of: foxhunt
    spec:
      nodeSelector:
        k8s.scaleway.com/pool-name: large
      containers:
        - name: postgres
          image: timescale/timescaledb:2.25.1-pg16   # PINNED to match the foxhunt source exactly (dump/restore needs equal tsdb versions; latest gave 2.28.3 -> catalog-restore failure)
          ports:
            - containerPort: 5432
              name: postgres
          args:
            - -c
            - max_connections=100
          env:
            - name: POSTGRES_DB
              value: foxhunt
            - name: POSTGRES_USER
              value: foxhunt
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: password
          volumeMounts:
            - name: postgres-data
              mountPath: /var/lib/postgresql/data
              subPath: pgdata
          readinessProbe:
            exec:
              command:
                - pg_isready
                - -U
                - foxhunt
            initialDelaySeconds: 10
            periodSeconds: 10
          resources:
            requests:
              cpu: 200m
              memory: 512Mi
            limits:
              cpu: 500m
              memory: 1Gi
      volumes:
        - name: postgres-data
          persistentVolumeClaim:
            claimName: postgres-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: foxhunt
  labels:
    app.kubernetes.io/name: postgres
    app.kubernetes.io/part-of: foxhunt
spec:
  selector:
    app.kubernetes.io/name: postgres
  ports:
    - port: 5432
      targetPort: 5432
      name: postgres
