{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if and (eq .Values.architecture "replication") (not .Values.replica.existingConfigmap) }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "keydb.replica.configmapName" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: keydb
    app.kubernetes.io/component: replica
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
data:
  keydb.conf: |-
  {{- if .Values.commonConfiguration }}
    {{- include "common.tplvalues.render" (dict "value" .Values.commonConfiguration "context" .) | nindent 4 }}
  {{- else }}
    # Enable AOF
    # ref: https://docs.keydb.dev/docs/persistence/#append-only-file
    appendonly yes
    # Disable RDB persistence, AOF persistence already enabled
    # ref: https://docs.keydb.dev/docs/persistence/#rdb-disadvantages
    save ""
    loglevel notice
  {{- end }}
  {{- if .Values.replica.configuration }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.replica.configuration "context" $ ) | nindent 4 }}
  {{- else }}
    bind 0.0.0.0 ::
    dir {{ .Values.replica.persistence.mountPath }}
  {{- if not .Values.auth.enabled }}
    protected-mode no
  {{- end }}
  {{- if .Values.tls.enabled }}
    port 0
    tls-port {{ .Values.replica.containerPorts.keydb }}
    tls-cert-file /opt/bitnami/keydb/certs/tls.crt
    tls-key-file /opt/bitnami/keydb/certs/tls.key
    tls-ca-cert-file /opt/bitnami/keydb/certs/ca.crt
    tls-replication yes
    tls-auth-clients optional
  {{- else }}
    port {{ .Values.replica.containerPorts.keydb }}
  {{- end }}
    replica-announce-port {{ .Values.replica.service.ports.keydb }}
  {{- if .Values.replica.activeReplica }}
    active-replica yes
  {{- $masterReplicaCount := int .Values.master.replicaCount }}
  {{- if gt $masterReplicaCount 1 }}
    multi-master yes
  {{- end }}
  {{- end }}
  {{- range .Values.replica.disableCommands }}
    rename-command {{ . }} ""
  {{- end }}
  {{- end }}

{{- end }}
