{{- /*
Copyright VMware, Inc.
SPDX-License-Identifier: APACHE-2.0
*/}}

apiVersion: v1
kind: Service
metadata:
  name: {{ include "clickhouse.headlessServiceName" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: clickhouse
  {{- if or .Values.service.headless.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.service.headless.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: ClusterIP
  clusterIP: None
  publishNotReadyAddresses: true
  ports:
    - name: http
      targetPort: http
      port: {{ .Values.service.ports.http }}
      protocol: TCP
    - name: tcp
      targetPort: tcp
      port: {{ .Values.service.ports.tcp }}
      protocol: TCP
    {{- if .Values.tls.enabled }}
    - name: tcp-secure
      targetPort: tcp-secure
      port: {{ .Values.service.ports.tcpSecure }}
      protocol: TCP
    {{- end }}
    {{- if .Values.keeper.enabled }}
    - name: tcp-keeper
      targetPort: tcp-keeper
      port: {{ .Values.service.ports.keeper }}
      protocol: TCP
    - name: tcp-keeperinter
      targetPort: tcp-keeperinter
      port: {{ .Values.service.ports.keeperInter }}
      protocol: TCP
    {{- if .Values.tls.enabled }}
    - name: tcp-keepertls
      targetPort: tcp-keepertls
      port: {{ .Values.service.ports.keeperSecure }}
      protocol: TCP
    {{- end }}
    {{- end }}
    - name: tcp-mysql
      targetPort: tcp-mysql
      port: {{ .Values.service.ports.mysql }}
      protocol: TCP
    - name: tcp-postgresql
      targetPort: tcp-postgresql
      port: {{ .Values.service.ports.postgresql }}
      protocol: TCP
    - name: http-intersrv
      targetPort: http-intersrv
      port: {{ .Values.service.ports.interserver }}
      protocol: TCP
    {{- if .Values.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: clickhouse
