apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: metabase-postgres-data
  namespace: metabase
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: azure-disk-std-ssd-lrs
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: metabase-postgres-config
  namespace: metabase
data:
  POSTGRES_DB: "metabase"
  POSTGRES_USER: "metabase"
---
apiVersion: v1
kind: Secret
metadata:
  name: metabase-postgres-secret
  namespace: metabase
type: Opaque
data:
  POSTGRES_PASSWORD: bWV0YWJhc2VwYXNzd29yZA==  # metabasepassword in base64
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: metabase-postgres
  namespace: metabase
  labels:
    app: metabase-postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: metabase-postgres
  template:
    metadata:
      labels:
        app: metabase-postgres
    spec:
      containers:
      - name: postgres
        image: postgres:15
        ports:
        - containerPort: 5432
          name: postgres
        env:
        - name: POSTGRES_DB
          valueFrom:
            configMapKeyRef:
              name: metabase-postgres-config
              key: POSTGRES_DB
        - name: POSTGRES_USER
          valueFrom:
            configMapKeyRef:
              name: metabase-postgres-config
              key: POSTGRES_USER
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: metabase-postgres-secret
              key: POSTGRES_PASSWORD
        - name: PGDATA
          value: "/var/lib/postgresql/data/pgdata"
        volumeMounts:
        - name: postgres-data
          mountPath: /var/lib/postgresql/data
        resources:
          requests:
            memory: "256Mi"
            cpu: "250m"
          limits:
            memory: "512Mi"
            cpu: "500m"
        livenessProbe:
          exec:
            command:
            - pg_isready
            - -U
            - metabase
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          exec:
            command:
            - pg_isready
            - -U
            - metabase
          initialDelaySeconds: 5
          periodSeconds: 5
      volumes:
      - name: postgres-data
        persistentVolumeClaim:
          claimName: metabase-postgres-data
---
apiVersion: v1
kind: Service
metadata:
  name: metabase-postgres
  namespace: metabase
  labels:
    app: metabase-postgres
spec:
  type: ClusterIP
  ports:
  - port: 5432
    targetPort: 5432
    protocol: TCP
    name: postgres
  selector:
    app: metabase-postgres
