{{- if $.Values.externalAccess.enabled }}
{{- $shards := $.Values.shards | int }}
{{- $replicas := $.Values.replicaCount | int }}
{{- $totalNodes := mul $shards $replicas }}
{{- range $shard, $e := until $shards }}
{{- range $i, $_e := until $replicas }}
{{- $targetPod := printf "%s-shard%d-%d" (include "common.names.fullname" $) $shard $i }}
apiVersion: v1
kind: Service
metadata:
  name: {{ printf "%s-external" $targetPod | trunc 63 | trimSuffix "-" }}
  namespace: {{ $.Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" $ | nindent 4 }}
    app.kubernetes.io/component: clickhouse
    pod: {{ $targetPod }}
    {{- if $.Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" $.Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    {{- if $.Values.externalAccess.service.labels }}
    {{- include "common.tplvalues.render" ( dict "value" $.Values.externalAccess.service.labels "context" $) | nindent 4 }}
    {{- end }}
  {{- if or $.Values.externalAccess.service.annotations $.Values.commonAnnotations $.Values.externalAccess.service.loadBalancerAnnotations }}
  annotations:
    {{- if and (not (empty $.Values.externalAccess.service.loadBalancerAnnotations)) (eq (len $.Values.externalAccess.service.loadBalancerAnnotations) $totalNodes) }}
    {{ include "common.tplvalues.render" ( dict "value" (index $.Values.externalAccess.service.loadBalancerAnnotations $i) "context" $) | nindent 4 }}
    {{- end }}
    {{- if $.Values.externalAccess.service.annotations }}
    {{- include "common.tplvalues.render" ( dict "value" $.Values.externalAccess.service.annotations "context" $) | nindent 4 }}
    {{- end }}
    {{- if $.Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" $.Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  type: {{ $.Values.externalAccess.service.type }}
  {{- if eq $.Values.externalAccess.service.type "LoadBalancer" }}
  {{- if and (not (empty $.Values.externalAccess.service.loadBalancerIPs)) (eq (len $.Values.externalAccess.service.loadBalancerIPs) $totalNodes) }}
  loadBalancerIP: {{ index $.Values.externalAccess.service.loadBalancerIPs $i }}
  {{- end }}
  {{- if $.Values.externalAccess.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges: {{- toYaml $.Values.externalAccess.service.loadBalancerSourceRanges | nindent 4 }}
  {{- end }}
  {{- end }}
  ports:
    - name: http
      port: {{ $.Values.externalAccess.service.ports.http }}
      targetPort: http
      {{- if not (empty $.Values.externalAccess.service.nodePorts.http) }}
      nodePort: {{ index $.Values.externalAccess.service.nodePorts.http $i }}
      {{- else }}
      nodePort: null
      {{- end }}
    {{- if $.Values.tls.enabled }}
    - name: https
      port: {{ $.Values.externalAccess.service.ports.https }}
      targetPort: https
      {{- if not (empty $.Values.externalAccess.service.nodePorts.https) }}
      nodePort: {{ index $.Values.externalAccess.service.nodePorts.https $i }}
      {{- else }}
      nodePort: null
      {{- end }}
    {{- end }}
    {{- if $.Values.metrics.enabled }}
    - name: http-metrics
      port: {{ $.Values.externalAccess.service.ports.metrics }}
      targetPort: http-metrics
      {{- if not (empty $.Values.externalAccess.service.nodePorts.metrics) }}
      nodePort: {{ index $.Values.externalAccess.service.nodePorts.metrics $i }}
      {{- else }}
      nodePort: null
      {{- end }}
    {{- end }}
    - name: tcp
      port: {{ $.Values.externalAccess.service.ports.tcp }}
      targetPort: tcp
      {{- if not (empty $.Values.externalAccess.service.nodePorts.tcp) }}
      nodePort: {{ index $.Values.externalAccess.service.nodePorts.tcp $i }}
      {{- else }}
      nodePort: null
      {{- end }}
    {{- if $.Values.tls.enabled }}
    - name: tcp-secure
      port: {{ $.Values.externalAccess.service.ports.tcpSecure }}
      targetPort: tcp-secure
      {{- if not (empty $.Values.externalAccess.service.nodePorts.tcpSecure) }}
      nodePort: {{ index $.Values.externalAccess.service.nodePorts.tcpSecure $i }}
      {{- else }}
      nodePort: null
      {{- end }}
    {{- end }}
    {{- if $.Values.keeper.enabled }}
    - name: tcp-keeper
      port: {{ $.Values.externalAccess.service.ports.keeper }}
      targetPort: tcp-keeper
      {{- if not (empty $.Values.externalAccess.service.nodePorts.keeper) }}
      nodePort: {{ index $.Values.externalAccess.service.nodePorts.keeper $i }}
      {{- else }}
      nodePort: null
      {{- end }}
    - name: tcp-keeperinter
      port: {{ $.Values.externalAccess.service.ports.keeperInter }}
      targetPort: tcp-keeperinter
      {{- if not (empty $.Values.externalAccess.service.nodePorts.keeperInter) }}
      nodePort: {{ index $.Values.externalAccess.service.nodePorts.keeperInter $i }}
      {{- else }}
      nodePort: null
      {{- end }}
    {{- if $.Values.tls.enabled }}
    - name: tcp-keepertls
      port: {{ $.Values.externalAccess.service.ports.keeperSecure }}
      targetPort: tcp-keepertls
      {{- if not (empty $.Values.externalAccess.service.nodePorts.keeperSecure) }}
      nodePort: {{ index $.Values.externalAccess.service.nodePorts.keeperSecure $i }}
      {{- else }}
      nodePort: null
      {{- end }}
    {{- end }}
    {{- end }}
    - name: tcp-mysql
      port: {{ $.Values.externalAccess.service.ports.mysql }}
      targetPort: tcp-mysql
      {{- if not (empty $.Values.externalAccess.service.nodePorts.mysql) }}
      nodePort: {{ index $.Values.externalAccess.service.nodePorts.mysql $i }}
      {{- else }}
      nodePort: null
      {{- end }}
    - name: tcp-postgresql
      port: {{ $.Values.externalAccess.service.ports.postgresql }}
      targetPort: tcp-postgresql
      {{- if not (empty $.Values.externalAccess.service.nodePorts.postgresql) }}
      nodePort: {{ index $.Values.externalAccess.service.nodePorts.postgresql $i }}
      {{- else }}
      nodePort: null
      {{- end }}
    - name: tcp-intersrv
      port: {{ $.Values.externalAccess.service.ports.interserver }}
      targetPort: tcp-intersrv
      {{- if not (empty $.Values.externalAccess.service.nodePorts.interserver) }}
      nodePort: {{ index $.Values.externalAccess.service.nodePorts.interserver $i }}
      {{- else }}
      nodePort: null
      {{- end }}
    {{- if $.Values.externalAccess.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" $.Values.externalAccess.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  selector: {{- include "common.labels.matchLabels" $ | nindent 4 }}
    app.kubernetes.io/component: clickhouse
    statefulset.kubernetes.io/pod-name: {{ $targetPod }}
---
{{- end }}
{{- end }}
{{- end }}
