# Location: ./mcpgateway/tools/builder/templates/kubernetes/postgres.yaml.j2
# Copyright 2025
# SPDX-License-Identifier: Apache-2.0
# Authors: Teryl Taylor
# PostgreSQL Database for MCP Gateway
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
  namespace: {{ namespace }}
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: {{ storage_size }}
  {% if storage_class %}
  storageClassName: {{ storage_class }}
  {% endif %}
---
apiVersion: v1
kind: Secret
metadata:
  name: postgres-secret
  namespace: {{ namespace }}
type: Opaque
stringData:
  # Official PostgreSQL image variables
  POSTGRES_USER: {{ user }}
  POSTGRES_PASSWORD: {{ password }}
  POSTGRES_DB: {{ database }}
  # Red Hat/SCL PostgreSQL image variables (OpenShift-compatible)
  POSTGRESQL_USER: {{ user }}
  POSTGRESQL_PASSWORD: {{ password }}
  POSTGRESQL_DATABASE: {{ database }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  namespace: {{ namespace }}
  labels:
    app: postgres
    component: database
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
        component: database
    spec:
      containers:
      - name: postgres
        image: {{ image }}
        imagePullPolicy: IfNotPresent

        ports:
        - name: postgres
          containerPort: 5432
          protocol: TCP

        envFrom:
        - secretRef:
            name: postgres-secret

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

        livenessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - pg_isready -U {{ user }}
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 3

        readinessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - pg_isready -U {{ user }}
          initialDelaySeconds: 10
          periodSeconds: 5
          timeoutSeconds: 3
          failureThreshold: 3

        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
            cpu: "500m"

      volumes:
      - name: postgres-data
        persistentVolumeClaim:
          claimName: postgres-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: {{ namespace }}
  labels:
    app: postgres
spec:
  type: ClusterIP
  ports:
  - name: postgres
    port: 5432
    targetPort: postgres
    protocol: TCP
  selector:
    app: postgres
