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

{{- if and .Values.keeper.enabled (or .Values.keeper.configdFiles (gt (int .Values.keeper.replicaCount) 1)) (not .Values.keeper.existingConfigdConfigmap) }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "clickhouse.keeper.configd.configmapName" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" (dict "customLabels" .Values.commonLabels "context" .) | nindent 4 }}
    app.kubernetes.io/component: keeper
    app.kubernetes.io/part-of: clickhouse
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" (dict "value" .Values.commonAnnotations "context" .) | nindent 4 }}
  {{- end }}
data:
  {{- range $key, $value := .Values.keeper.configdFiles }}
  {{ $key }}: |
    {{- include "common.tplvalues.render" (dict "value" $value "context" $) | nindent 4 }}
  {{- end }}
  {{- if gt (int .Values.keeper.replicaCount) 1 }}
  01-raft.xml: |
    <yandex>
        <keeper_server>
            <raft_configuration>
                {{- $fullname := include "clickhouse.keeper.fullname" . }}
                {{- $headlessSvcName := include "clickhouse.keeper.headlessServiceName" . }}
                {{- $releaseNamespace := .Release.Namespace }}
                {{- $clusterDomain := .Values.clusterDomain }}
                {{- range $i := until (int .Values.keeper.replicaCount) }}
                <server>
                    <id>{{ $i }}</id>
                    <hostname>{{ printf "%s-%d.%s.%s.svc.%s" $fullname $i $headlessSvcName $releaseNamespace $clusterDomain }}</hostname>
                    <port from_env="CLICKHOUSE_KEEPER_RAFT_PORT"></port>
                </server>
                {{- end }}
                {{- if .Values.tls.enabled }}
                <secure>true</secure>
                {{- end }}
            </raft_configuration>
        </keeper_server>
    </yandex>
  {{- end }}
  {{- if .Values.tls.enabled }}
  02-tls.xml: |
    <yandex>
        <keeper_server>
            <tcp_port_secure from_env="CLICKHOUSE_KEEPER_TCP_SECURE_PORT"></tcp_port_secure>
        </keeper_server>
        <openSSL>
            <server>
                <certificateFile>/opt/bitnami/clickhouse-keeper/certs/keeper/tls.crt</certificateFile>
                <privateKeyFile>/opt/bitnami/clickhouse-keeper/certs/keeper/tls.key</privateKeyFile>
                <caConfig>/opt/bitnami/clickhouse-keeper/certs/ca/tls.crt</caConfig>
                <cacheSessions>true</cacheSessions>
                <disableProtocols>sslv2,sslv3</disableProtocols>
                <preferServerCiphers>true</preferServerCiphers>
                <verificationMode>relaxed</verificationMode>
            </server>
            <client>
                <loadDefaultCAFile>false</loadDefaultCAFile>
                <caConfig>/opt/bitnami/clickhouse-keeper/certs/ca/tls.crt</caConfig>
                <cacheSessions>true</cacheSessions>
                <disableProtocols>sslv2,sslv3</disableProtocols>
                <preferServerCiphers>true</preferServerCiphers>
                <verificationMode>relaxed</verificationMode>
                <invalidCertificateHandler>
                    <name>AcceptCertificateHandler</name>
                </invalidCertificateHandler>
            </client>
        </openSSL>
    </yandex>
  {{- end }}
{{- end }}
