{{- if $.Values.serviceMonitor }}
{{- if and ( .Capabilities.APIVersions.Has "monitoring.coreos.com/v1" ) ( .Values.serviceMonitor.enabled ) }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
{{- if .Values.serviceMonitor.labels }}
  labels:
{{ toYaml .Values.serviceMonitor.labels | indent 4}}
{{- end }}
  name: {{ include "aws-for-fluent-bit.fullname" . }}
  namespace: {{ include "aws-for-fluent-bit.namespace" . }}
spec:
  endpoints:
  - port: monitor-agent
    scheme: http
{{- if .Values.serviceMonitor.interval }}
    interval: {{ .Values.serviceMonitor.interval }}
{{- end }}
{{- if .Values.serviceMonitor.telemetryPath }}
    path: {{ .Values.serviceMonitor.telemetryPath }}
{{- end }}
{{- if .Values.serviceMonitor.timeout }}
    scrapeTimeout: {{ .Values.serviceMonitor.timeout }}
{{- end }}
{{- if .Values.serviceMonitor.metricRelabelings }}
    metricRelabelings:
{{ toYaml .Values.serviceMonitor.metricRelabelings | indent 4 }}
{{- end }}
{{- if .Values.serviceMonitor.relabelings }}
    relabelings:
{{ toYaml .Values.serviceMonitor.relabelings | indent 4 }}
{{- end }}
{{- with .Values.serviceMonitor.extraEndpoints }}
    {{- toYaml . | nindent 2 }}
{{- end }}
  jobLabel: {{ default "app.kubernetes.io/instance" .Values.serviceMonitor.jobLabel }}
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
  selector:
    matchLabels:
{{- include "aws-for-fluent-bit.selectorLabels" . | nindent 6 }}
{{- if .Values.serviceMonitor.targetLabels }}
  targetLabels:
{{- range .Values.serviceMonitor.targetLabels }}
    - {{ . }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
