apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: {{ include "cnpg-cluster.fullname" . }}
  labels:
    {{- include "cnpg-cluster.labels" . | nindent 4 }}
  annotations:
    {{- include "cnpg-cluster.annotations" . | nindent 4 }}
spec:
  logLevel: {{ .Values.logLevel }}
  instances: {{ .Values.instances }}
  {{- if .Values.image.repository }}
  imageName: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
  {{- end }}
  imagePullPolicy: {{ .Values.image.pullPolicy }}
  {{- if or .Values.imagePullSecrets .Values.registryCredentials }}
  imagePullSecrets:
  {{- with .Values.imagePullSecrets }}
    {{- toYaml . | nindent 8 }}
  {{- end }}
  {{- range $name, $settings := .Values.registryCredentials }}
    - name: "{{ include "cnpg-cluster.fullname" $ }}-{{ $name }}"
  {{- end }}
  {{- end }}

  {{- with .Values.resources }}
  resources:
    {{- toYaml . | nindent 4 }}
  {{- end }}

  {{- if or .Values.nodeSelector .Values.tolerations .Values.extraAffinity }}
  affinity:
    {{- with .Values.nodeSelector }}
    nodeSelector:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- with .Values.tolerations }}
    tolerations:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- with .Values.extraAffinity }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- end }}

  storage:
    size: {{ .Values.persistence.size | quote }}
    {{- with .Values.persistence.resizeInUseVolumes }}
    resizeInUseVolumes: {{ . | quote }}
    {{- end }}
    {{- if .Values.persistence.storageClass }}
    {{- if (eq "-" .Values.persistence.storageClass) }}
    storageClass: ""
    {{- else }}
    storageClass: "{{ .Values.persistence.storageClass }}"
    {{- end }}
    {{- end }}
    {{- with .Values.persistence.pvcTemplate }}
    pvcTemplate:
      {{- toYaml . | nindent 6 }}
    {{- end }}

{{- if .Values.backup.enabled }}
  backup:
    retentionPolicy: "{{ .Values.backup.retentionPolicy }}"
    barmanObjectStore:
      {{- toYaml .Values.backup.barmanObjectStore | nindent 6 }}
{{- end }}
  
  minSyncReplicas: {{ .Values.minSyncReplicas }}
  maxSyncReplicas: {{ .Values.maxSyncReplicas }}
  postgresql:
    pg_hba:
      {{- with .Values.pg_hba }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
    parameters:
      {{- with .Values.postgresqlParameters }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
    shared_preload_libraries:
      {{- with .Values.sharedPreloadLibraries }}
        {{- toYaml . | nindent 8 }}
      {{- end }}

  monitoring:
    enablePodMonitor: {{ .Values.monitoring.enablePodMonitor }}

  {{ if .Values.superuserSecretName }}
  superuserSecret:
    name: {{ .Values.superuserSecretName }}
  {{ end}}

  {{- if .Values.replica.enabled }}
  replica:
    enabled: true
    source: {{ .Values.replica.source }}
  {{- end }}

  {{- if .Values.priorityClassName }}
  priorityClassName: {{ .Values.priorityClassName }}
  {{- end }}

  bootstrap:
    {{- if .Values.recovery.enabled }}
    recovery:
      source: {{ .Values.recovery.externalClusterName | default "recovery-cluster" }}
      {{ if .Values.recovery.database }}
      database: {{ .Values.recovery.database }}
      {{- end }}
      {{ if .Values.recovery.owner }}
      owner: {{ .Values.recovery.owner }}
      {{- end }}
      {{ if .Values.recovery.secretName }}
      secret:
        name: {{ .Values.recovery.secretName }}
      {{ end }}
      {{- if .Values.recovery.targetTime }}
      recoveryTarget:
        targetTime: "{{ .Values.recovery.targetTime }}"
      {{- end }}
    {{- else if (and .Values.pg_basebackup.enabled .Values.pg_basebackup.source) }}
    pg_basebackup:
      source: {{ .Values.pg_basebackup.source }}
    {{- else }}
    initdb:
      database: {{ .Values.dbName }}
      owner: {{ .Values.dbOwner }}
      {{ if .Values.dbSecretName }}
      secret:
        name: {{ .Values.dbSecretName }}
      {{ end }}
      # postgis configuration plugins
      postInitTemplateSQL:
        {{- range $cmd := .Values.postgresqlInitCommandsBeforeExtensions }}
          - {{ $cmd | quote }}
        {{- end }}
        {{- range $name := .Values.extensions }}
          - CREATE EXTENSION IF NOT EXISTS "{{ $name }}";
        {{- end }}
        {{- range $cmd := .Values.postgresqlInitCommands }}
          - {{ $cmd | quote }}
        {{- end }}
      {{ if .Values.postInitApplicationSQL }}
      postInitApplicationSQL:
        {{- toYaml .Values.postInitApplicationSQL | nindent 8 }}
      {{ end }}
      {{ if .Values.postInitApplicationSQLRefs }}
      postInitApplicationSQLRefs:
        {{- toYaml .Values.postInitApplicationSQLRefs | nindent 8 }}
      {{ end }}
    {{- end }}

  externalClusters:
  {{- if .Values.recovery.enabled }}
    - name: {{ .Values.recovery.externalClusterName | default "recovery-cluster" }}
      barmanObjectStore:
        {{- toYaml .Values.recovery.barmanObjectStore | nindent 8 }}
  {{- end }}
  {{- if .Values.externalClusters }}
    {{- toYaml .Values.externalClusters | nindent 4 }}
  {{- end }}

{{- with .Values.clusterExtraSpec }}
  {{- toYaml . | nindent 2 }}
{{- end }}
