apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{SERVICE_NAME}}-db
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: {{SERVICE_NAME}}-db
    app.kubernetes.io/component: database
    app.kubernetes.io/part-of: bakery-ia
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: {{SERVICE_NAME}}-db
      app.kubernetes.io/component: database
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{SERVICE_NAME}}-db
        app.kubernetes.io/component: database
    spec:
      imagePullSecrets:
      - name: dockerhub-creds
      containers:
      - name: postgres
        image: postgres:17-alpine
        ports:
        - containerPort: 5432
          name: postgres
        env:
        - name: POSTGRES_DB
          valueFrom:
            configMapKeyRef:
              name: bakery-config
              key: {{SERVICE_NAME_UPPER}}_DB_NAME
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: database-secrets
              key: {{SERVICE_NAME_UPPER}}_DB_USER
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: database-secrets
              key: {{SERVICE_NAME_UPPER}}_DB_PASSWORD
        - name: POSTGRES_INITDB_ARGS
          valueFrom:
            configMapKeyRef:
              name: bakery-config
              key: POSTGRES_INITDB_ARGS
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        volumeMounts:
        - name: postgres-data
          mountPath: /var/lib/postgresql/data
        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
            cpu: "500m"
        livenessProbe:
          exec:
            command:
            - pg_isready
            - -U
            - $(POSTGRES_USER)
            - -d
            - $(POSTGRES_DB)
          initialDelaySeconds: 30
          timeoutSeconds: 5
          periodSeconds: 10
          failureThreshold: 3
        readinessProbe:
          exec:
            command:
            - pg_isready
            - -U
            - $(POSTGRES_USER)
            - -d
            - $(POSTGRES_DB)
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 5
          failureThreshold: 3
      volumes:
      - name: postgres-data
        persistentVolumeClaim:
          claimName: {{SERVICE_NAME}}-db-pvc

---
apiVersion: v1
kind: Service
metadata:
  name: {{SERVICE_NAME}}-db-service
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: {{SERVICE_NAME}}-db
    app.kubernetes.io/component: database
spec:
  type: ClusterIP
  ports:
  - port: 5432
    targetPort: 5432
    protocol: TCP
    name: postgres
  selector:
    app.kubernetes.io/name: {{SERVICE_NAME}}-db
    app.kubernetes.io/component: database

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: {{SERVICE_NAME}}-db-pvc
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: {{SERVICE_NAME}}-db
    app.kubernetes.io/component: database
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
