{{- $shouldRenderStr := include "external-secrets.shouldRenderServiceMonitor" . | trim }}
{{- if and .Values.serviceMonitor.enabled (eq $shouldRenderStr "true") }}
apiVersion: "monitoring.coreos.com/v1"
kind: ServiceMonitor
metadata:
  labels:
    {{- include "external-secrets.labels" . | nindent 4 }}
{{- if .Values.serviceMonitor.additionalLabels }}
{{ toYaml .Values.serviceMonitor.additionalLabels | indent 4 }}
{{- end }}
  name: {{ include "external-secrets.fullname" . }}-metrics
  namespace: {{ .Values.serviceMonitor.namespace | default (include "external-secrets.namespace" .) | quote }}
spec:
  selector:
    matchLabels:
      {{- include "external-secrets.selectorLabels" . | nindent 6 }}
  namespaceSelector:
    matchNames:
    - {{ template "external-secrets.namespace" . }}
  endpoints:
  - port: metrics
    interval: {{ .Values.serviceMonitor.interval }}
    scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }}
    honorLabels: {{ .Values.serviceMonitor.honorLabels }}
    {{- with .Values.serviceMonitor.metricRelabelings }}
    metricRelabelings:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- with .Values.serviceMonitor.relabelings }}
    relabelings:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- if .Values.metrics.listen.auth.enabled }}
    authorization:
      credentials:
        name: {{ include "external-secrets.serviceAccountName" . }}-token
        key: token
      type: Bearer
    {{- end }}
---
{{- if .Values.webhook.create }}
apiVersion: "monitoring.coreos.com/v1"
kind: ServiceMonitor
metadata:
  labels:
    {{- include "external-secrets-webhook.labels" . | nindent 4 }}
{{- if .Values.serviceMonitor.additionalLabels }}
{{ toYaml .Values.serviceMonitor.additionalLabels | indent 4 }}
{{- end }}
  name: {{ include "external-secrets.fullname" . }}-webhook-metrics
  namespace: {{ .Values.serviceMonitor.namespace | default (include "external-secrets.namespace" .) | quote }}
spec:
  selector:
    matchLabels:
      {{- include "external-secrets-webhook-metrics.labels" . | nindent 6 }}
  namespaceSelector:
    matchNames:
    - {{ template "external-secrets.namespace" . }}
  endpoints:
  - port: metrics
    interval: {{ .Values.serviceMonitor.interval }}
    scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }}
    honorLabels: {{ .Values.serviceMonitor.honorLabels }}
    {{- with .Values.serviceMonitor.metricRelabelings }}
    metricRelabelings:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- with .Values.serviceMonitor.relabelings }}
    relabelings:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- if .Values.webhook.metrics.listen.auth.enabled }}
    authorization:
      credentials:
       name: {{ include "external-secrets-webhook.serviceAccountName" . }}-token
       key: token
      type: Bearer
    {{- end }}
{{- end }}
---
{{- if .Values.certController.create }}
apiVersion: "monitoring.coreos.com/v1"
kind: ServiceMonitor
metadata:
  labels:
    {{- include "external-secrets-cert-controller.labels" . | nindent 4 }}
{{- if .Values.serviceMonitor.additionalLabels }}
{{ toYaml .Values.serviceMonitor.additionalLabels | indent 4 }}
{{- end }}
  name: {{ include "external-secrets.fullname" . }}-cert-controller-metrics
  namespace: {{ .Values.serviceMonitor.namespace | default (include "external-secrets.namespace" .) | quote }}
spec:
  selector:
    matchLabels:
      {{- include "external-secrets-cert-controller-metrics.labels" . | nindent 6 }}
  namespaceSelector:
    matchNames:
    - {{ template "external-secrets.namespace" . }}
  endpoints:
  - port: metrics
    interval: {{ .Values.serviceMonitor.interval }}
    scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }}
    honorLabels: {{ .Values.serviceMonitor.honorLabels }}
    {{- with .Values.serviceMonitor.metricRelabelings }}
    metricRelabelings:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- with .Values.serviceMonitor.relabelings }}
    relabelings:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- if .Values.certController.metrics.listen.auth.enabled }}
    authorization:
      credentials:
       name: {{ include "external-secrets-cert-controller.serviceAccountName" . }}-token
       key: token
      type: Bearer
    {{- end }}
{{- end }}
{{- end }}
