{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{/* Returns a K8s service to expose ClickHouse */}}
{{- define "clickhouse.service" -}}
{{- $name := include "common.names.fullname" .context }}
{{- if .context.Values.service.perReplicaAccess }}
  {{- $name = printf "%s-shard%d-%d" $name (int .shard) (int .replica) | trunc 63 | trimSuffix "-" }}
{{- end }}
apiVersion: v1
kind: Service
metadata:
  name: {{ $name }}
  namespace: {{ include "common.names.namespace" .context | quote }}
  labels: {{- include "common.labels.standard" (dict "customLabels" .context.Values.commonLabels "context" .context) | nindent 4 }}
    app.kubernetes.io/component: clickhouse
    app.kubernetes.io/part-of: clickhouse
    {{- if and .context.Values.metrics.enabled .context.Values.metrics.serviceMonitor.enabled }}
    {{- /* Adding extra selector for the ServiceMonitor object to avoid duplicate targets  */}}
    prometheus.io/scrape: "true"
    {{- end }}
    {{- if .context.Values.service.perReplicaAccess }}
    pod: {{ $name }}
    {{- end }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .context.Values.service.annotations .context.Values.commonAnnotations) "context" .context) }}
  {{- if .context.Values.metrics.enabled }}
    {{- $defaultMetricsAnnotations := dict "prometheus.io/scrape" "true" "prometheus.io/port" (.context.Values.service.ports.metrics | toString) "prometheus.io/path" "/metrics" }}
    {{- $annotations = include "common.tplvalues.merge" (dict "values" (list $annotations $defaultMetricsAnnotations) "context" .context) }}
  {{- end }}
  {{- if and .context.Values.service.perReplicaAccess .context.Values.service.loadBalancerAnnotations }}
    {{- /* We expect an annotation per replica. Given there are shards * replicaCount replicas, we need to calculate the offset to retrieve the proper annotation */}}
    {{- $posOffset := mul (int .shard) (.context.Values.replicaCount | int) }}
    {{- $position := add $posOffset (int .replica) }}
    {{- $annotations = include "common.tplvalues.merge" (dict "values" (list $annotations (index .context.Values.service.loadBalancerAnnotations $position)) "context" .context) }}
  {{- end }}
  annotations: {{- include "common.tplvalues.render" (dict "value" $annotations "context" .context) | nindent 4 }}
spec:
  type: {{ .context.Values.service.type }}
  {{- if and .context.Values.service.clusterIP (eq .context.Values.service.type "ClusterIP") }}
  clusterIP: {{ .context.Values.service.clusterIP }}
  {{- end }}
  {{- if .context.Values.service.sessionAffinity }}
  sessionAffinity: {{ .context.Values.service.sessionAffinity }}
  {{- end }}
  {{- if .context.Values.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .context.Values.service.sessionAffinityConfig "context" .context) | nindent 4 }}
  {{- end }}
  {{- if or (eq .context.Values.service.type "LoadBalancer") (eq .context.Values.service.type "NodePort") }}
  externalTrafficPolicy: {{ .context.Values.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .context.Values.service.type "LoadBalancer") (not (empty .context.Values.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{- toYaml .context.Values.service.loadBalancerSourceRanges | nindent 4 }}
  {{- end }}
  {{- if eq .context.Values.service.type "LoadBalancer" }}
  {{- if and .context.Values.service.perReplicaAccess (not (empty .context.Values.service.loadBalancerIPs)) }}
  {{- /* We expect an IP per replica. Given there are shards * replicaCount replicas, we need to calculate the offset to retrieve the proper IP */}}
  {{- $posOffset := mul (int .shard) (.context.Values.replicaCount | int) }}
  {{- $position := add $posOffset (int .replica) }}
  loadBalancerIP: {{ index .context.Values.service.loadBalancerIPs $position }}
  {{- else if not (empty .context.Values.service.loadBalancerIP) }}
  loadBalancerIP: {{ .context.Values.service.loadBalancerIP }}
  {{- end }}
  {{- end }}
  ports:
    - name: http
      targetPort: http
      port: {{ .context.Values.service.ports.http }}
      protocol: TCP
      {{- if and .context.Values.service.perReplicaAccess (not (empty .context.Values.service.nodePorts.http)) }}
      {{- $posOffset := mul (int .shard) (.context.Values.replicaCount | int) }}
      {{- $position := add $posOffset (int .replica) }}
      nodePort: {{ index .context.Values.service.nodePorts.http $position }}
      {{- else if and (or (eq .context.Values.service.type "NodePort") (eq .context.Values.service.type "LoadBalancer")) (not (empty .context.Values.service.nodePorts.http)) }}
      nodePort: {{ .context.Values.service.nodePorts.http }}
      {{- else if eq .context.Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .context.Values.tls.enabled }}
    - name: https
      targetPort: https
      port: {{ .context.Values.service.ports.https }}
      protocol: TCP
      {{- if and .context.Values.service.perReplicaAccess (not (empty .context.Values.service.nodePorts.https)) }}
      {{- $posOffset := mul (int .shard) (.context.Values.replicaCount | int) }}
      {{- $position := add $posOffset (int .replica) }}
      nodePort: {{ index .context.Values.service.nodePorts.https $position }}
      {{- else if and (or (eq .context.Values.service.type "NodePort") (eq .context.Values.service.type "LoadBalancer")) (not (empty .context.Values.service.nodePorts.https)) }}
      nodePort: {{ .context.Values.service.nodePorts.https }}
      {{- else if eq .context.Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- end }}
    - name: tcp
      targetPort: tcp
      port: {{ .context.Values.service.ports.tcp }}
      protocol: TCP
      {{- if and .context.Values.service.perReplicaAccess (not (empty .context.Values.service.nodePorts.tcp)) }}
      {{- $posOffset := mul (int .shard) (.context.Values.replicaCount | int) }}
      {{- $position := add $posOffset (int .replica) }}
      nodePort: {{ index .context.Values.service.nodePorts.tcp $position }}
      {{- else if and (or (eq .context.Values.service.type "NodePort") (eq .context.Values.service.type "LoadBalancer")) (not (empty .context.Values.service.nodePorts.tcp)) }}
      nodePort: {{ .context.Values.service.nodePorts.tcp }}
      {{- else if eq .context.Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .context.Values.tls.enabled }}
    - name: tcp-secure
      targetPort: tcp-secure
      port: {{ .context.Values.service.ports.tcpSecure }}
      protocol: TCP
      {{- if and .context.Values.service.perReplicaAccess (not (empty .context.Values.service.nodePorts.tcpSecure)) }}
      {{- $posOffset := mul (int .shard) (.context.Values.replicaCount | int) }}
      {{- $position := add $posOffset (int .replica) }}
      nodePort: {{ index .context.Values.service.nodePorts.tcpSecure $position }}
      {{- else if and (or (eq .context.Values.service.type "NodePort") (eq .context.Values.service.type "LoadBalancer")) (not (empty .context.Values.service.nodePorts.tcpSecure)) }}
      nodePort: {{ .context.Values.service.nodePorts.tcpSecure }}
      {{- else if eq .context.Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- end }}
    {{- if .context.Values.exposeMysql }}
    - name: tcp-mysql
      targetPort: tcp-mysql
      port: {{ .context.Values.service.ports.mysql }}
      protocol: TCP
      {{- if and .context.Values.service.perReplicaAccess (not (empty .context.Values.service.nodePorts.mysql)) }}
      {{- $posOffset := mul (int .shard) (.context.Values.replicaCount | int) }}
      {{- $position := add $posOffset (int .replica) }}
      nodePort: {{ index .context.Values.service.nodePorts.mysql $position }}
      {{- else if and (or (eq .context.Values.service.type "NodePort") (eq .context.Values.service.type "LoadBalancer")) (not (empty .context.Values.service.nodePorts.mysql)) }}
      nodePort: {{ .context.Values.service.nodePorts.mysql }}
      {{- else if eq .context.Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- end }}
    {{- if .context.Values.exposePostgresql }}
    - name: tcp-postgresql
      targetPort: tcp-postgresql
      port: {{ .context.Values.service.ports.postgresql }}
      protocol: TCP
      {{- if and .context.Values.service.perReplicaAccess (not (empty .context.Values.service.nodePorts.postgresql)) }}
      {{- $posOffset := mul (int .shard) (.context.Values.replicaCount | int) }}
      {{- $position := add $posOffset (int .replica) }}
      nodePort: {{ index .context.Values.service.nodePorts.postgresql $position }}
      {{- else if and (or (eq .context.Values.service.type "NodePort") (eq .context.Values.service.type "LoadBalancer")) (not (empty .context.Values.service.nodePorts.postgresql)) }}
      nodePort: {{ .context.Values.service.nodePorts.postgresql }}
      {{- else if eq .context.Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- end }}
    - name: http-intersrv
      targetPort: http-intersrv
      port: {{ .context.Values.service.ports.interserver }}
      protocol: TCP
      {{- if and .context.Values.service.perReplicaAccess (not (empty .context.Values.service.nodePorts.interserver)) }}
      {{- $posOffset := mul (int .shard) (.context.Values.replicaCount | int) }}
      {{- $position := add $posOffset (int .replica) }}
      nodePort: {{ index .context.Values.service.nodePorts.interserver $position }}
      {{- else if and (or (eq .context.Values.service.type "NodePort") (eq .context.Values.service.type "LoadBalancer")) (not (empty .context.Values.service.nodePorts.interserver)) }}
      nodePort: {{ .context.Values.service.nodePorts.interserver }}
      {{- else if eq .context.Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .context.Values.metrics.enabled }}
    - name: http-metrics
      targetPort: http-metrics
      port: {{ .context.Values.service.ports.metrics }}
      protocol: TCP
     {{- if and .context.Values.service.perReplicaAccess (not (empty .context.Values.service.nodePorts.metrics)) }}
      {{- $posOffset := mul (int .shard) (.context.Values.replicaCount | int) }}
      {{- $position := add $posOffset (int .replica) }}
      nodePort: {{ index .context.Values.service.nodePorts.metrics $position }}
      {{- else if and (or (eq .context.Values.service.type "NodePort") (eq .context.Values.service.type "LoadBalancer")) (not (empty .context.Values.service.nodePorts.metrics)) }}
      nodePort: {{ .context.Values.service.nodePorts.metrics }}
      {{- else if eq .context.Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- end }}
    {{- if .context.Values.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .context.Values.service.extraPorts "context" .context) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" (dict "values" ( list .context.Values.podLabels .context.Values.commonLabels ) "context" .context) }}
  selector: {{- include "common.labels.matchLabels" (dict "customLabels" $podLabels "context" .context) | nindent 4 }}
    app.kubernetes.io/component: clickhouse
    app.kubernetes.io/part-of: clickhouse
    {{- if .context.Values.service.perReplicaAccess }}
    pod: {{ $name }}
    {{- end }}
{{- end -}}

{{- if .Values.service.perReplicaAccess }}
{{- range $shard := until (.Values.shards | int) }}
{{- range $i := until ($.Values.replicaCount | int) }}
{{- include "clickhouse.service" (dict "shard" $shard "replica" $i "context" $) }}
---
{{- end }}
{{- end }}
{{- else }}
{{- include "clickhouse.service" (dict "context" .) }}
{{- end }}
