{{- if and .Values.metrics.enabled .Values.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "external-dns.fullname" . }}
  {{- with .Values.metrics.serviceMonitor.namespace }}
  namespace: {{ . }}
  {{- end }}
  labels: {{ include "external-dns.labels" . | nindent 4 }}
    {{- with .Values.metrics.serviceMonitor.selector }}
    {{ toYaml . | nindent 4 }}
    {{- end }}
spec:
  endpoints:
    - port: http
      path: /metrics
      {{- with .Values.metrics.serviceMonitor.interval }}
      interval: {{ . }}
      {{- end }}
      {{- with .Values.metrics.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ . }}
      {{- end }}
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
  selector:
    matchLabels: {{ include "external-dns.matchLabels" . | nindent 6 }}
{{- end }}
