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

{{- if .Values.keeper.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "clickhouse.keeper.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" (dict "customLabels" .Values.commonLabels "context" .) | nindent 4 }}
    app.kubernetes.io/component: keeper
    app.kubernetes.io/part-of: clickhouse
  {{- if or .Values.keeper.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.keeper.service.annotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" (dict "value" $annotations "context" .) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.keeper.service.type }}
  {{- if and .Values.keeper.service.clusterIP (eq .Values.keeper.service.type "ClusterIP") }}
  clusterIP: {{ .Values.keeper.service.clusterIP }}
  {{- end }}
  {{- if .Values.keeper.service.sessionAffinity }}
  sessionAffinity: {{ .Values.keeper.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.keeper.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.keeper.service.sessionAffinityConfig "context" .) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.keeper.service.type "LoadBalancer") (eq .Values.keeper.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.keeper.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.keeper.service.type "LoadBalancer") (not (empty .Values.keeper.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.keeper.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.keeper.service.type "LoadBalancer") (not (empty .Values.keeper.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.keeper.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: tcp
      port: {{ .Values.keeper.service.ports.tcp }}
      protocol: TCP
      targetPort: tcp
      {{- if and (or (eq .Values.keeper.service.type "NodePort") (eq .Values.keeper.service.type "LoadBalancer")) (not (empty .Values.keeper.service.nodePorts.tcp)) }}
      nodePort: {{ .Values.keeper.service.nodePorts.tcp }}
      {{- else if eq .Values.keeper.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.tls.enabled }}
    - name: tcp-secure
      targetPort: tcp-secure
      port: {{ .Values.keeper.service.ports.tcpSecure }}
      protocol: TCP
      {{- if and (or (eq .Values.keeper.service.type "NodePort") (eq .Values.keeper.service.type "LoadBalancer")) (not (empty .Values.keeper.service.nodePorts.tcpSecure)) }}
      nodePort: {{ .Values.keeper.service.nodePorts.tcpSecure }}
      {{- else if eq .Values.keeper.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- end }}
    - name: tcp-raft
      port: {{ .Values.keeper.service.ports.raft }}
      protocol: TCP
      targetPort: raft
      {{- if and (or (eq .Values.keeper.service.type "NodePort") (eq .Values.keeper.service.type "LoadBalancer")) (not (empty .Values.keeper.service.nodePorts.raft)) }}
      nodePort: {{ .Values.keeper.service.nodePorts.raft }}
      {{- else if eq .Values.keeper.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.keeper.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.keeper.service.extraPorts "context" .) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" (dict "values" (list .Values.keeper.podLabels .Values.commonLabels) "context" .) }}
  selector: {{- include "common.labels.matchLabels" (dict "customLabels" $podLabels "context" .) | nindent 4 }}
    app.kubernetes.io/component: keeper
    app.kubernetes.io/part-of: clickhouse
{{- end }}
