{{- if and .Values.controller.metrics.enabled .Values.controller.metrics.serviceMonitor.enabled -}}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "ingress-nginx.controller.fullname" . }}
  {{- if .Values.controller.metrics.serviceMonitor.namespace }}
  namespace: {{ .Values.controller.metrics.serviceMonitor.namespace }}
  {{- else }}
  namespace: {{ include "ingress-nginx.namespace" . }}
  {{- end }}
  labels:
    {{- include "ingress-nginx.labels" . | nindent 4 }}
    app.kubernetes.io/component: controller
    {{- if .Values.controller.metrics.serviceMonitor.additionalLabels }}
    {{- toYaml .Values.controller.metrics.serviceMonitor.additionalLabels | nindent 4 }}
    {{- end }}
  {{- if .Values.controller.metrics.serviceMonitor.annotations }}
  annotations: {{ toYaml .Values.controller.metrics.serviceMonitor.annotations | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.controller.metrics.serviceMonitor.namespaceSelector }}
  namespaceSelector: {{ toYaml .Values.controller.metrics.serviceMonitor.namespaceSelector | nindent 4 }}
  {{- else }}
  namespaceSelector:
    matchNames:
    - {{ include "ingress-nginx.namespace" . }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "ingress-nginx.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: controller
  endpoints:
  - port: {{ .Values.controller.metrics.portName }}
    interval: {{ .Values.controller.metrics.serviceMonitor.scrapeInterval }}
    {{- if .Values.controller.metrics.serviceMonitor.scrapeTimeout }}
    scrapeTimeout: {{ .Values.controller.metrics.serviceMonitor.scrapeTimeout }}
    {{- end }}
    {{- if .Values.controller.metrics.serviceMonitor.honorLabels }}
    honorLabels: true
    {{- end }}
    {{- if .Values.controller.metrics.serviceMonitor.relabelings }}
    relabelings: {{ toYaml .Values.controller.metrics.serviceMonitor.relabelings | nindent 4 }}
    {{- end }}
    {{- if .Values.controller.metrics.serviceMonitor.metricRelabelings }}
    metricRelabelings: {{ toYaml .Values.controller.metrics.serviceMonitor.metricRelabelings | nindent 4 }}
    {{- end }}
  {{- if .Values.controller.metrics.serviceMonitor.jobLabel }}
  jobLabel: {{ .Values.controller.metrics.serviceMonitor.jobLabel | quote }}
  {{- end }}
  {{- if .Values.controller.metrics.serviceMonitor.targetLabels }}
  targetLabels: {{ toYaml .Values.controller.metrics.serviceMonitor.targetLabels | nindent 2 }}
  {{- end }}
  {{- if .Values.controller.metrics.serviceMonitor.labelLimit }}
  labelLimit: {{ .Values.controller.metrics.serviceMonitor.labelLimit }}
  {{- end }}
  {{- if .Values.controller.metrics.serviceMonitor.labelNameLengthLimit }}
  labelNameLengthLimit: {{ .Values.controller.metrics.serviceMonitor.labelNameLengthLimit }}
  {{- end }}
  {{- if .Values.controller.metrics.serviceMonitor.labelValueLengthLimit }}
  labelValueLengthLimit: {{ .Values.controller.metrics.serviceMonitor.labelValueLengthLimit }}
  {{- end }}
  {{- if .Values.controller.metrics.serviceMonitor.sampleLimit }}
  sampleLimit: {{ .Values.controller.metrics.serviceMonitor.sampleLimit }}
  {{- end }}
  {{- if .Values.controller.metrics.serviceMonitor.targetLimit }}
  targetLimit: {{ .Values.controller.metrics.serviceMonitor.targetLimit }}
  {{- end }}
{{- end }}
