{{- if .Values.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "openwa.fullname" . }}
  labels:
    {{- include "openwa.labels" . | nindent 4 }}
    {{- with .Values.serviceMonitor.additionalLabels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  selector:
    # Names ONE Service. Prometheus Operator's endpoints-role discovery yields a target per address
    # for every Service matching this selector, with no cross-Service de-duplication by pod — so a
    # selector of only the shared selectorLabels matches both the ClusterIP and the headless Service
    # and produces two targets for the same pod, and two sets of series distinguished only by the
    # `service` label.
    matchLabels:
      {{- include "openwa.selectorLabels" . | nindent 6 }}
      openwa.io/scrape-target: "primary"
  endpoints:
    - port: http
      path: /api/metrics
      interval: {{ .Values.serviceMonitor.interval }}
      {{- if .Values.serviceMonitor.bearerTokenSecret.name }}
      bearerTokenSecret:
        name: {{ .Values.serviceMonitor.bearerTokenSecret.name }}
        key: {{ .Values.serviceMonitor.bearerTokenSecret.key }}
      {{- end }}
{{- end }}
