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

{{- if .Values.metrics.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: clickhouse-operator
    app.kubernetes.io/component: operator
  {{- if or .Values.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.service.annotations .Values.commonAnnotations ) "context" . ) }}
  {{- $defaultMetricsAnnotations := dict "prometheus.io/scrape" "true" "prometheus.io/port" .Values.service.ports.metricsExporter "prometheus.io/path" "/metrics" }}
  {{- $annotations = include "common.tplvalues.merge" (dict "values" (list $annotations $defaultMetricsAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.service.clusterIP }}
  clusterIP: {{ .Values.service.clusterIP }}
  {{- end }}
  ports:
    - name: http-metrics
      port: {{ .Values.service.ports.metrics }}
      {{- if not (eq .Values.service.ports.metrics .Values.containerPorts.metrics) }}
      targetPort: {{ .Values.containerPorts.metrics }}
      {{- end }}
      protocol: TCP
      nodePort: null
    - name: http-metrics-exporter
      port: {{ .Values.service.ports.metricsExporter }}
      {{- if not (eq .Values.service.ports.metricsExporter .Values.metrics.containerPorts.metrics) }}
      targetPort: {{ .Values.service.ports.metricsExporter }}
      {{- end }}
      protocol: TCP
      nodePort: null
    {{- 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" .) | fromYaml }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: clickhouse-operator
    app.kubernetes.io/component: operator
{{- end }}
