apiVersion: v1
kind: Service
metadata:
  name: ai-control-chat-postgres
  namespace: aegida-services
spec:
  type: ClusterIP
  ports:
    - name: postgres
      port: 5432
      targetPort: postgres
  selector:
    app: ai-control-chat-postgres
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: ai-control-chat-postgres
  namespace: aegida-services
spec:
  serviceName: ai-control-chat-postgres
  replicas: 1
  selector:
    matchLabels:
      app: ai-control-chat-postgres
  template:
    metadata:
      labels:
        app: ai-control-chat-postgres
    spec:
      containers:
        - name: postgres
          image: postgres:16-alpine
          ports:
            - name: postgres
              containerPort: 5432
          env:
            - name: POSTGRES_DB
              valueFrom:
                secretKeyRef:
                  name: ai-control-chat-secrets
                  key: POSTGRES_DB
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: ai-control-chat-secrets
                  key: POSTGRES_USER
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ai-control-chat-secrets
                  key: POSTGRES_PASSWORD
          readinessProbe:
            exec:
              command:
                - sh
                - -c
                - pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB"
            initialDelaySeconds: 5
            periodSeconds: 5
          livenessProbe:
            exec:
              command:
                - sh
                - -c
                - pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB"
            initialDelaySeconds: 15
            periodSeconds: 10
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi
