{{- if and .Values.prometheus.enabled .Values.prometheus.servicemonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "cert-manager.fullname" . }}
{{- if .Values.prometheus.servicemonitor.namespace }}
  namespace: {{ .Values.prometheus.servicemonitor.namespace }}
{{- else }}
  namespace: {{ .Release.Namespace | quote }}
{{- end }}
  labels:
    app: {{ include "cert-manager.name" . }}
    app.kubernetes.io/name: {{ include "cert-manager.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/component: "controller"
    helm.sh/chart: {{ include "cert-manager.chart" . }}
    prometheus: {{ .Values.prometheus.servicemonitor.prometheusInstance }}
{{- if .Values.prometheus.servicemonitor.labels }}
{{ toYaml .Values.prometheus.servicemonitor.labels | indent 4}}
{{- end }}
spec:
  jobLabel: {{ template "cert-manager.fullname" . }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ template "cert-manager.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
      app.kubernetes.io/component: "controller"
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
  endpoints:
  - targetPort: {{ .Values.prometheus.servicemonitor.targetPort }}
    path: {{ .Values.prometheus.servicemonitor.path }}
    interval: {{ .Values.prometheus.servicemonitor.interval }}
    scrapeTimeout: {{ .Values.prometheus.servicemonitor.scrapeTimeout }}
{{- end }}
