{{- /*
CNPG-managed Postgres cluster for Gitea.

Gitea requires PostgreSQL for its database (repositories, users, issues,
pull requests, etc.). bp-gitea ships this Cluster CR so the database is
provisioned by CNPG alongside the HelmRelease — no separate Helm release or
manual setup required. Mirrors the same pattern used by bp-harbor
(platform/harbor/chart/templates/cnpg-cluster.yaml).

The Cluster name defaults to `gitea-pg`; CNPG synthesises a `gitea-pg-app`
Secret that `templates/database-secret.yaml` mirrors via reflector into
`gitea-database-secret`, which the Gitea deployment reads for the DB password.

Capabilities gate: bp-cnpg ships the `postgresql.cnpg.io/v1` CRD. On a cold
install before bp-cnpg is reconciling, the apiserver rejects this Cluster.
The Capabilities check skips this template until the CRD is registered.
The Sovereign's bootstrap order MUST land bp-cnpg before bp-gitea.
*/}}
{{- if .Capabilities.APIVersions.Has "postgresql.cnpg.io/v1" }}
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: {{ .Values.postgres.cluster.name | default "gitea-pg" }}
  namespace: {{ .Values.postgres.cluster.namespace | default .Release.Namespace }}
  labels:
    catalyst.openova.io/blueprint: bp-gitea
    catalyst.openova.io/component: gitea
  annotations:
    # Allow the k8s-reflector (bp-reflector) to reflect the CNPG-generated
    # `gitea-pg-app` Secret into the `gitea` namespace so that
    # `gitea-database-secret` (templates/database-secret.yaml) gets the
    # real password populated at runtime rather than empty on first install.
    reflector.v1.k8s.emberstack.com/reflection-allowed: "true"
    reflector.v1.k8s.emberstack.com/reflection-allowed-namespaces: {{ .Values.postgres.cluster.namespace | default .Release.Namespace | quote }}
spec:
  instances: {{ .Values.postgres.cluster.instances | default 1 }}
  imageName: ghcr.io/cloudnative-pg/postgresql:{{ .Values.postgres.cluster.pgVersion | default "16" }}

  bootstrap:
    initdb:
      database: {{ .Values.postgres.cluster.database | default "gitea" | quote }}
      owner: {{ .Values.postgres.cluster.owner | default "gitea" | quote }}

  postgresql:
    parameters:
      max_connections: "100"
      shared_buffers: "64MB"
      effective_cache_size: "192MB"
      work_mem: "4MB"
      maintenance_work_mem: "64MB"
      log_statement: "ddl"
      log_min_duration_statement: "1000"

  resources:
    {{- toYaml .Values.postgres.cluster.resources | nindent 4 }}

  storage:
    size: {{ .Values.postgres.cluster.storageSize | default "5Gi" }}
    storageClass: {{ .Values.postgres.cluster.storageClass | default "local-path" | quote }}

  enableSuperuserAccess: true

  monitoring:
    enablePodMonitor: false
{{- end }}
