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

{{/*
Return the Kafka broker configuration.
ref: https://kafka.apache.org/documentation/#configuration
*/}}
{{- define "kafka.broker.config" -}}
{{- if or .Values.config .Values.broker.config }}
{{- include "common.tplvalues.render" (dict "value" (coalesce .Values.broker.config .Values.config) "context" .) }}
{{- else }}
# Listeners configuration
listeners: {{ include "kafka.listeners" (dict "isController" false "context" .) }}
listener.security.protocol.map: {{ include "kafka.securityProtocolMap" (dict "isController" false "context" .) }}
advertised.listeners: {{ include "kafka.advertisedListeners" . }}
inter.broker.listener.name: {{ .Values.listeners.interbroker.name }}
# Kafka data logs directory
log.dir: {{ printf "%s/data" .Values.broker.persistence.mountPath }}
# Kafka application logs directory
logs.dir: {{ .Values.broker.logPersistence.mountPath }}
# KRaft node role
process.roles: broker
# Common Kafka Configuration
{{ include "kafka.commonConfig" . }}
{{- end -}}
{{- end -}}

{{- $replicaCount := int .Values.broker.replicaCount }}
{{- if and (include "kafka.broker.createConfigmap" .) (gt $replicaCount 0) }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ printf "%s-configuration" (include "kafka.broker.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: broker
    app.kubernetes.io/part-of: kafka
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
data:
  {{- $configuration := include "kafka.broker.config" . | fromYaml -}}
  {{- if or .Values.overrideConfiguration .Values.broker.overrideConfiguration }}
  {{- $overrideConfiguration := include "common.tplvalues.render" (dict "value" .Values.overrideConfiguration "context" .) | fromYaml }}
  {{- $brokerOverrideConfiguration := include "common.tplvalues.render" (dict "value" .Values.broker.overrideConfiguration "context" .) | fromYaml }}
  {{- $configuration = mustMergeOverwrite $configuration $overrideConfiguration $brokerOverrideConfiguration }}
  {{- end }}
  server.properties: |-
    {{- range $key, $value := $configuration }}
    {{ $key }}={{ include "common.tplvalues.render" (dict "value" $value "context" $) }}
    {{- end }}
{{- end }}
