{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
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
    app.kubernetes.io/part-of: 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.containerPorts.http }}
      protocol: TCP
    - name: tcp
      targetPort: tcp
      port: {{ .Values.containerPorts.tcp }}
      protocol: TCP
    {{- if .Values.tls.enabled }}
    - name: tcp-secure
      targetPort: tcp-secure
      port: {{ .Values.containerPorts.tcpSecure }}
      protocol: TCP
    {{- end }}
    {{- if .Values.exposeMysql }}
    - name: tcp-mysql
      targetPort: tcp-mysql
      port: {{ .Values.containerPorts.mysql }}
      protocol: TCP
    {{- end }}
    {{- if .Values.exposePostgresql }}
    - name: tcp-postgresql
      targetPort: tcp-postgresql
      port: {{ .Values.containerPorts.postgresql }}
      protocol: TCP
    {{- end }}
    - name: http-intersrv
      targetPort: http-intersrv
      port: {{ .Values.containerPorts.interserver }}
      protocol: TCP
    {{- if .Values.service.headless.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.service.headless.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
    app.kubernetes.io/part-of: clickhouse
