{{- if and .Values.metrics.enabled .Values.metrics.serviceMonitor.enabled (.Capabilities.APIVersions.Has "monitoring.coreos.com/v1") }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "velero.fullname" . }}
  {{- if .Values.metrics.serviceMonitor.namespace }}
  namespace: {{ .Values.metrics.serviceMonitor.namespace }}
  {{- end }}
  labels:
    app.kubernetes.io/name: {{ include "velero.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "velero.chart" . }}
  {{- with .Values.metrics.serviceMonitor.additionalLabels }}
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "velero.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  endpoints:
  - port: http-monitoring
    interval: {{ .Values.metrics.scrapeInterval }}
    scrapeTimeout: {{ .Values.metrics.scrapeTimeout }}
    {{- if .Values.metrics.serviceMonitor.scheme }}
    scheme: {{ .Values.metrics.serviceMonitor.scheme }}
    {{- end }}
    {{- if .Values.metrics.serviceMonitor.tlsConfig }}
    tlsConfig:
      {{- toYaml .Values.metrics.serviceMonitor.tlsConfig | nindent 6 }}
    {{- end }}
{{- end }}
