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

{{- if and .Values.controller.metrics.enabled .Values.controller.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ printf "%s-controller" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ default .Release.Namespace .Values.controller.metrics.serviceMonitor.namespace | quote }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.controller.metrics.serviceMonitor.labels .Values.commonLabels ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  jobLabel: {{ .Values.controller.metrics.serviceMonitor.jobLabel | quote }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: controller
      {{- if .Values.controller.metrics.serviceMonitor.selector }}
      {{- include "common.tplvalues.render" (dict "value" .Values.controller.metrics.serviceMonitor.selector "context" $) | nindent 6 }}
      {{- end }}
  namespaceSelector:
    matchNames:
      - {{ include "common.names.namespace" .| quote }}
  endpoints:
    - port: metrics
      {{- if .Values.controller.metrics.serviceMonitor.interval }}
      interval: {{ .Values.controller.metrics.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.controller.metrics.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ .Values.controller.metrics.serviceMonitor.scrapeTimeout }}
      {{- end }}
      {{- if .Values.controller.metrics.serviceMonitor.honorLabels }}
      honorLabels: {{ .Values.controller.metrics.serviceMonitor.honorLabels }}
      {{- end }}
      {{- if .Values.controller.metrics.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.controller.metrics.serviceMonitor.metricRelabelings "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.controller.metrics.serviceMonitor.relabelings }}
      relabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.controller.metrics.serviceMonitor.relabelings "context" $) | nindent 8 }}
      {{- end }}
{{- end -}}
