{{- if .Values.cluster.enabled -}}
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: {{ include "Atlantis.fullname" . }}-db
  namespace: {{ .Release.Namespace }}
  annotations:
    linkerd.io/inject: disabled
  labels:
    {{- include "Atlantis.labels" . | nindent 4 }}
spec:
  instances: {{ .Values.cluster.instances | default "1" }}
  imageName: ghcr.io/cloudnative-pg/postgis:15-3.3
  # Example of rolling update strategy:
  # - unsupervised: automated update of the primary once all
  #                 replicas have been upgraded (default)
  # - supervised: requires manual supervision to perform
  #               the switchover of the primary
  primaryUpdateStrategy: unsupervised
  {{- if .Values.cluster.backup.enabled }}
  plugins:
  - name: barman-cloud.cloudnative-pg.io
    isWALArchiver: true
    parameters:
      barmanObjectName: tos-store
  {{- end}}
  storage:
    size: {{ .Values.cluster.size | default "5Gi" }}
{{- with .Values.cluster.bootstrap }}
  bootstrap:
{{- if .enabled }}
    pg_basebackup:
      source: archmaester
  externalClusters:
  - name: archmaester
    connectionParameters:
      host: {{ .source.db }}-rw.{{ .source.namespace }}
      user: streaming_replica
      sslmode: verify-full
    sslKey:
      name: {{ .source.db }}-replication
      key: tls.key
    sslCert:
      name: {{ .source.db }}-replication
      key: tls.crt
    sslRootCert:
      name: {{ .source.db }}-ca
      key: ca.crt
{{- else }}
    initdb:
      postInitTemplateSQL:
        - CREATE EXTENSION postgis;
        - CREATE EXTENSION postgis_topology;
        - CREATE EXTENSION fuzzystrmatch;
        - CREATE EXTENSION postgis_tiger_geocoder;
        - ALTER USER app WITH SUPERUSER;
{{- end }}
{{- end }}
---
{{- if and .Values.cluster.backup.enabled .Values.cluster.enabled -}}
apiVersion: barmancloud.cnpg.io/v1
kind: ObjectStore
metadata:
  name: tos-store
  namespace: {{ .Release.Namespace }}
spec:
  retentionPolicy: {{ .Values.cluster.backup.backupRetention | default "60d" }}
  configuration:
    destinationPath: {{ .Values.cluster.destinationPath | default ""}}
    endpointURL: http://10.255.241.30:30080
    s3Credentials:
      accessKeyId:
        name: cnpg-s3
        key: access_key
      secretAccessKey:
        name: cnpg-s3
        key: access_secret
    wal:
      compression: snappy
---
apiVersion: postgresql.cnpg.io/v1
kind: ScheduledBackup
metadata:
  name: {{ include "Atlantis.fullname" . }}-db
  namespace: {{ .Release.Namespace }}
spec:
  schedule: "0 0 1 * * *"
  backupOwnerReference: self
  cluster:
    name: '{{ include "Atlantis.fullname" . }}-db'
  method: plugin
  pluginConfiguration:
    name: barman-cloud.cloudnative-pg.io
{{- end }}
---
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: {{ include "Atlantis.fullname" . }}-db-monitor
  namespace: {{ .Release.Namespace }}
spec:
  selector:
    matchLabels:
      cnpg.io/cluster: {{ include "Atlantis.fullname" . }}-db
  podMetricsEndpoints:
  - port: metrics
{{- end }}
