{{- if .Values.externalAccess.enabled }}
{{- $fullName := include "common.names.fullname" . }}
{{- $replicaCount := .Values.replicaCount | int }}
{{- $root := . }}

{{- range $i, $e := until $replicaCount }}
{{- $targetPod := printf "%s-%d" (printf "%s" $fullName) $i }}
{{- $_ := set $ "targetPod" $targetPod }}
apiVersion: v1
kind: Service
metadata:
  name: {{ printf "%s-%d-external" (include "common.names.fullname" $) $i | trunc 63 | trimSuffix "-" }}
  namespace: {{ $root.Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" $ | nindent 4 }}
    app.kubernetes.io/component: kafka
    pod: {{ $targetPod }}
    {{- if $root.Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" $root.Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    {{- if $root.Values.externalAccess.service.labels }}
    {{- include "common.tplvalues.render" ( dict "value" $root.Values.externalAccess.service.labels "context" $) | nindent 4 }}
    {{- end }}
  {{- if or $root.Values.externalAccess.service.annotations $root.Values.commonAnnotations $root.Values.externalAccess.service.loadBalancerAnnotations }}
  annotations:
    {{- if and (not (empty $root.Values.externalAccess.service.loadBalancerAnnotations)) (eq (len $root.Values.externalAccess.service.loadBalancerAnnotations) $replicaCount) }}
    {{ include "common.tplvalues.render" ( dict "value" (index $root.Values.externalAccess.service.loadBalancerAnnotations $i) "context" $) | nindent 4 }}
    {{- end }}
    {{- if $root.Values.externalAccess.service.annotations }}
    {{- include "common.tplvalues.render" ( dict "value" $root.Values.externalAccess.service.annotations "context" $) | nindent 4 }}
    {{- end }}
    {{- if $root.Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" $root.Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  type: {{ $root.Values.externalAccess.service.type }}
  {{- if eq $root.Values.externalAccess.service.type "LoadBalancer" }}
  {{- if and (not (empty $root.Values.externalAccess.service.loadBalancerIPs)) (eq (len $root.Values.externalAccess.service.loadBalancerIPs) $replicaCount) }}
  loadBalancerIP: {{ index $root.Values.externalAccess.service.loadBalancerIPs $i }}
  {{- end }}
  {{- if $root.Values.externalAccess.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges: {{- toYaml $root.Values.externalAccess.service.loadBalancerSourceRanges | nindent 4 }}
  {{- end }}
  {{- end }}
  publishNotReadyAddresses: {{ $root.Values.externalAccess.service.publishNotReadyAddresses }}
  ports:
    - name: tcp-kafka
      port: {{ $root.Values.externalAccess.service.ports.external }}
      {{- if not (empty $root.Values.externalAccess.service.nodePorts) }}
      nodePort: {{ index $root.Values.externalAccess.service.nodePorts $i }}
      {{- else }}
      nodePort: null
      {{- end }}
      targetPort: kafka-external
    {{- if $root.Values.externalAccess.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" $root.Values.externalAccess.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  selector: {{- include "common.labels.matchLabels" $ | nindent 4 }}
    app.kubernetes.io/component: kafka
    statefulset.kubernetes.io/pod-name: {{ $targetPod }}
---
{{- end }}
{{- end }}
