{{- if and ( .Capabilities.APIVersions.Has "monitoring.coreos.com/v1" ) ( .Values.serviceMonitor.enabled )  }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    app: {{ template "envoy.name" . }}
    chart: {{ template "envoy.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
{{- if .Values.serviceMonitor.additionalLabels }}
{{ toYaml .Values.serviceMonitor.additionalLabels | indent 4}}
{{- end }}
  name: {{ template "envoy.fullname" . }}
{{- if .Values.serviceMonitor.namespace }}
  namespace: {{ .Values.serviceMonitor.namespace }}
{{- end }}
spec:
  endpoints:
  - targetPort: {{ .Values.ports.admin.containerPort }}
    interval: {{ .Values.serviceMonitor.interval }}
    path: "/stats/prometheus"
  jobLabel: {{ template "envoy.fullname" . }}
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
  selector:
    matchLabels:
      app: {{ template "envoy.name" . }}
      release: {{ .Release.Name }}
  {{- with .Values.serviceMonitor.targetLabels }}
  targetLabels:
{{ toYaml . | trim | indent 4 -}}
  {{- end }}
  {{- with .Values.serviceMonitor.podTargetLabels }}
  podTargetLabels:
{{ toYaml . | trim | indent 4 -}}
  {{- end }}
{{- end }}
