{{- if and .Values.metrics.enabled .Values.metrics.servicemonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "oauth2-proxy.fullname" . }}
{{- if .Values.metrics.servicemonitor.namespace }}
  namespace: {{ .Values.metrics.servicemonitor.namespace }}
{{- else }}
  namespace: {{ template "oauth2-proxy.namespace" $ }}
{{- end }}
  labels:
    prometheus: {{ .Values.metrics.servicemonitor.prometheusInstance }}
    app: {{ template "oauth2-proxy.name" . }}
{{- include "oauth2-proxy.labels" . | indent 4 }}
{{- if .Values.metrics.servicemonitor.labels }}
{{ toYaml .Values.metrics.servicemonitor.labels | indent 4}}
{{- end }}
spec:
  jobLabel: {{ template "oauth2-proxy.fullname" . }}
  selector:
    matchLabels:
      {{- include "oauth2-proxy.selectorLabels" . | indent 6 }}
  namespaceSelector:
    matchNames:
      - {{ template "oauth2-proxy.namespace" $ }}
  endpoints:
  - port: metrics
    path: "/metrics"
    interval: {{ .Values.metrics.servicemonitor.interval }}
    scrapeTimeout: {{ .Values.metrics.servicemonitor.scrapeTimeout }}
{{- end }}
