apiVersion: v1
kind: Service
metadata:
  name: demo-session-db-service
  namespace: bakery-ia
  labels:
    app: demo-session-db
    component: database
spec:
  type: ClusterIP
  ports:
  - port: 5432
    targetPort: 5432
    protocol: TCP
    name: postgres
  selector:
    app: demo-session-db
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: demo-session-db
  namespace: bakery-ia
  labels:
    app: demo-session-db
    component: database
spec:
  serviceName: demo-session-db-service
  replicas: 1
  selector:
    matchLabels:
      app: demo-session-db
  template:
    metadata:
      labels:
        app: demo-session-db
        component: database
    spec:
      containers:
      - name: postgres
        image: postgres:17-alpine
        ports:
        - containerPort: 5432
          name: postgres
        env:
        - name: POSTGRES_DB
          value: "demo_session_db"
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: database-secrets
              key: DEMO_SESSION_DB_USER
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: database-secrets
              key: DEMO_SESSION_DB_PASSWORD
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        volumeMounts:
        - name: postgres-data
          mountPath: /var/lib/postgresql/data
        resources:
          requests:
            memory: "128Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
            cpu: "500m"
  volumeClaimTemplates:
  - metadata:
      name: postgres-data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 2Gi
