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

{{- if and .Values.operator.prometheus.serviceMonitor.enabled .Values.operator.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  clusterIP: None
  type: ClusterIP
  ports:
    - port: 8080
      targetPort: metrics
      protocol: TCP
      name: metrics
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.operator.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ default (include "common.names.namespace" .) .Values.operator.prometheus.serviceMonitor.namespace }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.operator.prometheus.serviceMonitor.labels .Values.commonLabels ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  jobLabel: {{ .Values.operator.prometheus.serviceMonitor.jobLabel | quote }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 6 }}
      {{- if .Values.operator.prometheus.serviceMonitor.selector }}
      {{- include "common.tplvalues.render" (dict "value" .Values.operator.prometheus.serviceMonitor.selector "context" $) | nindent 6 }}
      {{- end }}
  namespaceSelector:
    matchNames:
      - {{ include "common.names.namespace" . | quote }}
  endpoints:
    - port: metrics
      {{- if .Values.operator.prometheus.serviceMonitor.interval }}
      interval: {{ .Values.operator.prometheus.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.operator.prometheus.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ .Values.operator.prometheus.serviceMonitor.scrapeTimeout }}
      {{- end }}
      {{- if .Values.operator.prometheus.serviceMonitor.honorLabels }}
      honorLabels: {{ .Values.operator.prometheus.serviceMonitor.honorLabels }}
      {{- end }}
      {{- if .Values.operator.prometheus.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.operator.prometheus.serviceMonitor.metricRelabelings "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.prometheus.serviceMonitor.relabelings }}
      relabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.operator.prometheus.serviceMonitor.relabelings "context" $) | nindent 8 }}
      {{- end }}
{{- end }}
