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

{{- if .Values.operator.metrics.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ printf "%s-metrics" (include "cloudnative-pg.operator.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels .Values.operator.metrics.service.labels) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cloudnative-pg
    app.kubernetes.io/component: metrics
  {{- $defaultAnnotations := dict "prometheus.io/scrape" "true" "prometheus.io/port" (.Values.operator.metrics.service.ports.metrics | quote) "prometheus.io/path" "/metrics" }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list  $defaultAnnotations .Values.operator.metrics.service.annotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" (dict "value" $annotations "context" $) | nindent 4 }}
spec:
  type: ClusterIP
  sessionAffinity: {{ .Values.operator.metrics.service.sessionAffinity }}
  {{- if .Values.operator.metrics.service.clusterIP }}
  clusterIP: {{ .Values.operator.metrics.service.clusterIP }}
  {{- end }}
  ports:
    - name: http-metrics
      port: {{ .Values.operator.metrics.service.ports.metrics }}
      protocol: TCP
      targetPort: http-metrics
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cloudnative-pg
    app.kubernetes.io/component: operator
{{- end }}
