
{{- $port := print .Values.service.ports.client }}
{{- $host := list }}
{{- $bootstrapServers := list }}
{{- range $i, $e := until (int .Values.replicaCount) }}
  {{- $broker := printf "%s-%s.%s-headless.%s.svc.%s" (include "common.names.fullname" $) (print $i) (include "common.names.fullname" $) $.Release.Namespace $.Values.clusterDomain }}
  {{- $host = append $host $broker }}
  {{- $bootstrapServers = append $bootstrapServers (printf "%s:%s" $broker $port) }}
{{- end }}
{{- $clientUsers := .Values.auth.sasl.jaas.clientUsers }}
{{- $clientPasswords := .Values.auth.sasl.jaas.clientPasswords }}
{{- if not $clientPasswords }}
  {{- $clientPasswords = list }}
{{- range $clientUsers }}
  {{- $clientPasswords = append $clientPasswords (randAlphaNum 10) }}
{{- end }}
{{- end }}
{{- if (include "kafka.createJaasSecret" .) }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-jaas" (include "common.names.fullname" .) }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: Opaque
data:
  {{- if (include "kafka.client.saslAuthentication" .) }}
  client-passwords: {{ join "," $clientPasswords | b64enc | quote }}
  system-user-password: {{ index $clientPasswords 0 | b64enc | quote }}
  {{- end }}
  {{- $zookeeperUser := .Values.auth.sasl.jaas.zookeeperUser }}
  {{- if and .Values.zookeeper.auth.client.enabled $zookeeperUser }}
  {{- $zookeeperPassword := .Values.auth.sasl.jaas.zookeeperPassword }}
  zookeeper-password: {{ default (randAlphaNum 10) $zookeeperPassword | b64enc | quote }}
  {{- end }}
  {{- if (include "kafka.interBroker.saslAuthentication" .) }}
  {{- $interBrokerPassword := .Values.auth.sasl.jaas.interBrokerPassword }}
  inter-broker-password: {{ default (randAlphaNum 10) $interBrokerPassword | b64enc | quote }}
  {{- end }}
{{- end }}
{{- if .Values.serviceBindings.enabled }}
{{- if (include "kafka.client.saslAuthentication" .) }}
{{- range $i, $e := until (len $clientUsers) }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "common.names.fullname" $ }}-svcbind-user-{{ $i }}
  namespace: {{ $.Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" $ | nindent 4 }}
    {{- if $.Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if $.Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: servicebinding.io/kafka
data:
  provider: {{ print "bitnami" | b64enc | quote }}
  type: {{ print "kafka" | b64enc | quote }}
  username: {{ index $clientUsers $i | b64enc | quote }}
  password: {{ index $clientPasswords $i | b64enc | quote }}
  host: {{ join "," $host | b64enc | quote }}
  port: {{ print $port | b64enc | quote }}
  bootstrap-servers: {{ join "," $bootstrapServers | b64enc | quote }}
{{- end }}
{{- else }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "common.names.fullname" . }}-svcbind
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: servicebinding.io/kafka
data:
  provider: {{ print "bitnami" | b64enc | quote }}
  type: {{ print "kafka" | b64enc | quote }}
  host: {{ join "," $host | b64enc | quote }}
  port: {{ print $port | b64enc | quote }}
  bootstrap-servers: {{ join "," $bootstrapServers | b64enc | quote }}
{{- end }}
{{- end }}
