apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: workclub-postgres
  labels:
    app: workclub-postgres
    component: database
spec:
  serviceName: workclub-postgres-headless
  replicas: 1
  selector:
    matchLabels:
      app: workclub-postgres
  template:
    metadata:
      labels:
        app: workclub-postgres
        component: database
    spec:
      containers:
      - name: postgres
        image: postgres:16-alpine
        imagePullPolicy: IfNotPresent
        ports:
        - name: postgresql
          containerPort: 5432
          protocol: TCP
        livenessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - pg_isready -U app -d workclub
          initialDelaySeconds: 10
          periodSeconds: 15
          timeoutSeconds: 5
          failureThreshold: 3
        
        readinessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - pg_isready -U app -d workclub
          initialDelaySeconds: 5
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 2
        
        resources:
          requests:
            cpu: 100m
            memory: 256Mi
          limits:
            cpu: 500m
            memory: 512Mi
        
        env:
        - name: POSTGRES_DB
          value: workclub
        - name: POSTGRES_USER
          value: app
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: workclub-secrets
              key: postgres-password
        volumeMounts:
        - name: postgres-data
          mountPath: /var/lib/postgresql/data
          subPath: postgres
        - name: postgres-init
          mountPath: /docker-entrypoint-initdb.d
      
      volumes:
        - name: postgres-init
          configMap:
            name: postgres-init
            items:
            - key: init.sql
              path: init.sql
  volumeClaimTemplates:
  - metadata:
      name: postgres-data
    spec:
      accessModes:
      - ReadWriteOnce
      storageClassName: standard
      resources:
        requests:
          storage: 10Gi
