apiVersion: apps/v1
kind: Deployment
metadata:
  name: external-db
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: external-db
    app.kubernetes.io/component: database
    app.kubernetes.io/part-of: bakery-ia
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: external-db
      app.kubernetes.io/component: database
  template:
    metadata:
      labels:
        app.kubernetes.io/name: external-db
        app.kubernetes.io/component: database
    spec:
      imagePullSecrets:
      - name: dockerhub-creds
      securityContext:
        fsGroup: 70
      initContainers:
      - name: fix-tls-permissions
        image: busybox:latest
        securityContext:
          runAsUser: 0
        command: ['sh', '-c']
        args:
        - |
          cp /tls-source/* /tls/
          chmod 600 /tls/server-key.pem
          chmod 644 /tls/server-cert.pem /tls/ca-cert.pem
          chown 70:70 /tls/*
          ls -la /tls/
        volumeMounts:
        - name: tls-certs-source
          mountPath: /tls-source
          readOnly: true
        - name: tls-certs-writable
          mountPath: /tls
      containers:
      - name: postgres
        image: postgres:17-alpine
        command: ["docker-entrypoint.sh", "-c", "config_file=/etc/postgresql/postgresql.conf"]
        ports:
        - containerPort: 5432
          name: postgres
        env:
        - name: POSTGRES_DB
          valueFrom:
            configMapKeyRef:
              name: bakery-config
              key: EXTERNAL_DB_NAME
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: database-secrets
              key: EXTERNAL_DB_USER
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: database-secrets
              key: EXTERNAL_DB_PASSWORD
        - name: POSTGRES_INITDB_ARGS
          valueFrom:
            configMapKeyRef:
              name: bakery-config
              key: POSTGRES_INITDB_ARGS
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        - name: POSTGRES_HOST_SSL
          value: "on"
        - name: PGSSLCERT
          value: /tls/server-cert.pem
        - name: PGSSLKEY
          value: /tls/server-key.pem
        - name: PGSSLROOTCERT
          value: /tls/ca-cert.pem
        volumeMounts:
        - name: postgres-data
          mountPath: /var/lib/postgresql/data
        - name: init-scripts
          mountPath: /docker-entrypoint-initdb.d
        - name: tls-certs-writable
          mountPath: /tls
        - name: postgres-config
          mountPath: /etc/postgresql
          readOnly: true
        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
            cpu: "500m"
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - pg_isready -U $POSTGRES_USER -d $POSTGRES_DB
          initialDelaySeconds: 30
          timeoutSeconds: 5
          periodSeconds: 10
          failureThreshold: 3
        readinessProbe:
          exec:
            command:
            - sh
            - -c
            - pg_isready -U $POSTGRES_USER -d $POSTGRES_DB
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 5
          failureThreshold: 3
      volumes:
      - name: postgres-data
        persistentVolumeClaim:
          claimName: external-db-pvc
      - name: init-scripts
        configMap:
          name: postgres-init-config
      - name: tls-certs-source
        secret:
          secretName: postgres-tls
      - name: tls-certs-writable
        emptyDir: {}
      - name: postgres-config
        configMap:
          name: postgres-logging-config

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


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