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

{{- range $i, $e := until $nodesCount }}
{{- $targetPod := printf "%s-%d" (printf "%s" $fullName) $i }}
{{- $_ := set $ "targetPod" $targetPod }}

---
apiVersion: v1
kind: Service
metadata:
  name: {{ template "common.names.fullname" $ }}-{{ $i }}-svc
  namespace: {{ $.Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" $ | nindent 4 }}
    pod: {{ $targetPod }}
    {{- if $root.Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" $root.Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  annotations:
  {{- if $root.Values.commonAnnotations }}
  {{- include "common.tplvalues.render" ( dict "value" $root.Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
  {{- if $root.Values.cluster.externalAccess.service.annotations }}
  {{ include "common.tplvalues.render" ( dict "value" $root.Values.cluster.externalAccess.service.annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ $root.Values.cluster.externalAccess.service.type }}
  {{- if $root.Values.cluster.externalAccess.service.loadBalancerIP }}
  loadBalancerIP: {{ index $root.Values.cluster.externalAccess.service.loadBalancerIP $i }}
  {{- end }}
  ports:
    - name: tcp-redis
      port: {{ $root.Values.cluster.externalAccess.service.port }}
      targetPort: tcp-redis
    - name: tcp-redis-bus
      targetPort: tcp-redis-bus
      port: {{ $root.Values.redis.containerPorts.bus }}
  selector: {{- include "common.labels.matchLabels" $ | nindent 4 }}
    statefulset.kubernetes.io/pod-name: {{ $targetPod }}
---
{{- end }}
{{- end }}
