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

{{- range $i, $e := until $replicaCount }}
{{- $targetPod := printf "%s-%d" (printf "%s" $fullName) $i }}
{{- $_ := set $ "targetPod" $targetPod }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "kafka.fullname" $root }}-{{ $i }}-external
  labels:
    {{- include "kafka.labels" $root | nindent 4 }}
    statefulset.kubernetes.io/pod-name: {{ $targetPod }}
spec:
  type: NodePort
  ports:
  - name: tcp-kafka-ext
    protocol: TCP
    targetPort: tcp-kafka-ext
    port: 9094
    nodePort: {{ add $initNodePort $i }}
  selector:
    {{- include "kafka.selectorLabels" $root | nindent 4 }}
    statefulset.kubernetes.io/pod-name: {{ $targetPod }}
{{- end }}
{{- end }}
