apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:16
          imagePullPolicy: IfNotPresent

          ports:
            - containerPort: 5432

          # 🔑 關鍵：避免 lost+found 問題
          env:
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata

          envFrom:
            - secretRef:
                name: postgres-secret

          volumeMounts:
            - name: postgres-data
              mountPath: /var/lib/postgresql/data

          # 官方建議的健康檢查方式
          livenessProbe:
            exec:
              command:
                - sh
                - -c
                - pg_isready -d "$POSTGRES_DB" -U "$POSTGRES_USER"
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3

          readinessProbe:
            exec:
              command:
                - sh
                - -c
                - pg_isready -d "$POSTGRES_DB" -U "$POSTGRES_USER"
            initialDelaySeconds: 5
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3

      volumes:
        - name: postgres-data
          persistentVolumeClaim:
            claimName: postgres-pvc

