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

{{- if not .Values.existingChiUsersdConfigmap }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ printf "%s-chi-usersd" (include "common.names.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: clickhouse-operator
    app.kubernetes.io/component: operator
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
data:
  {{- if .Values.chiUsersd }}
  {{- range $key, $value := .Values.chiUsersd }}
  {{ $key }}: |
    {{- include "common.tplvalues.render" (dict "value" $value "context" $) | nindent 4 }}
  {{- end }}
  {{- else }}
  # Ref: https://github.com/Altinity/clickhouse-operator/tree/master/deploy/builder/templates-config/chi/users.d
  01-clickhouse-operator-profile.xml: |
    <yandex>
        <profiles>
            <clickhouse_operator>
                <log_queries>0</log_queries>
                <skip_unavailable_shards>1</skip_unavailable_shards>
                <http_connection_timeout>10</http_connection_timeout>
                <max_concurrent_queries_for_all_users>0</max_concurrent_queries_for_all_users>
                <os_thread_priority>0</os_thread_priority>
            </clickhouse_operator>
        </profiles>
    </yandex>
  02-clickhouse-default-profile.xml: |-
    <yandex>
        <profiles>
            <default>
                <os_thread_priority>2</os_thread_priority>
                <log_queries>1</log_queries>
                <connect_timeout_with_failover_ms>1000</connect_timeout_with_failover_ms>
                <distributed_aggregation_memory_efficient>1</distributed_aggregation_memory_efficient>
                <parallel_view_processing>1</parallel_view_processing>
                <do_not_merge_across_partitions_select_final>1</do_not_merge_across_partitions_select_final>
                <load_balancing>nearest_hostname</load_balancing>
                <prefer_localhost_replica>0</prefer_localhost_replica>
            </default>
        </profiles>
    </yandex>
  {{- end }}
{{- end }}
