apiVersion: v1
kind: Secret
metadata:
  name: postgres-credentials
  namespace: apps
type: Opaque
stringData:
  POSTGRES_USER: app
  POSTGRES_PASSWORD: "e16cde7aeb535edc96b435d7a1523cd5"
  POSTGRES_DB: db_nextcloud
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-initdb
  namespace: apps
data:
  init.sql: |
    -- per-app database bootstrap (postgres)
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-data
  namespace: apps
spec:
  accessModes: ["ReadWriteOnce"]
  resources:
    requests:
      storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  namespace: apps
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:16-alpine
          ports:
            - containerPort: 5432
          envFrom:
            - secretRef:
                name: postgres-credentials
          resources:
            requests:
              cpu: 50m
              memory: 128Mi
            limits:
              cpu: 500m
              memory: 256Mi
          volumeMounts:
            - name: pgdata
              mountPath: /var/lib/postgresql/data
            - name: initdb
              mountPath: /docker-entrypoint-initdb.d
      volumes:
        - name: pgdata
          persistentVolumeClaim:
            claimName: postgres-data
        - name: initdb
          configMap:
            name: postgres-initdb
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: apps
spec:
  selector:
    app: postgres
  ports:
    - port: 5432
      targetPort: 5432
