{{- 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 }}
  backup:
    barmanObjectStore:
      destinationPath: {{ .Values.cluster.destinationPath | default ""}}
      serverName: {{ include "Atlantis.fullname" . }}-db
      endpointURL: http://10.255.241.30:30080
      s3Credentials:
        accessKeyId:
          key: access_key
          name: cnpg-s3
        secretAccessKey:
          key: access_secret
          name: cnpg-s3
      wal:
        compression: snappy
    retentionPolicy: {{ .Values.cluster.backup.backupRetention | default "60d" }}
    {{- 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 }}
{{- end }}
