{{- if and .Values.deployment.enabled .Values.prometheus.monitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "coredns.fullname" . }}
  {{- if .Values.prometheus.monitor.namespace }}
  namespace: {{ .Values.prometheus.monitor.namespace }}
  {{- end }}
  labels: {{- include "coredns.labels" . | nindent 4 }}
    {{- if .Values.prometheus.monitor.additionalLabels }}
{{ toYaml .Values.prometheus.monitor.additionalLabels | indent 4 }}
    {{- end }}
{{- with .Values.customAnnotations }}
  annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
  {{- if ne .Values.prometheus.monitor.namespace .Release.Namespace }}
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
  {{- end }}
  selector:
    matchLabels:
      app.kubernetes.io/instance: {{ .Release.Name | quote }}
      {{- if .Values.isClusterService }}
      k8s-app: {{ template "coredns.k8sapplabel" . }}
      {{- end }}
      app.kubernetes.io/name: {{ template "coredns.name" . }}
      app.kubernetes.io/component: metrics
  endpoints:
    - port: metrics
      {{- if .Values.prometheus.monitor.interval }}
      interval: {{ .Values.prometheus.monitor.interval }}
      {{- end }}
{{- end }}
