{{- if .Values.prometheus.podMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: {{ template "metallb.fullname" . }}-controller
  labels:
    {{- include "metallb.labels" . | nindent 4 }}
    app.kubernetes.io/component: controller
    {{- if .Values.prometheus.podMonitor.additionalLabels }}
{{ toYaml .Values.prometheus.podMonitor.additionalLabels | indent 4 }}
    {{- end }}
  {{- if .Values.prometheus.podMonitor.annotations }}
  annotations:
{{ toYaml .Values.prometheus.podMonitor.annotations | indent 4 }}
  {{- end }}
spec:
  jobLabel: {{ .Values.prometheus.podMonitor.jobLabel | quote }}
  selector:
    matchLabels:
      {{- include "metallb.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: controller
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
  podMetricsEndpoints:
  - port: monitoring
    path: /metrics
    {{- if .Values.prometheus.podMonitor.interval }}
    interval: {{ .Values.prometheus.podMonitor.interval }}
    {{- end }}
{{- if .Values.prometheus.podMonitor.metricRelabelings }}
    metricRelabelings:
{{- toYaml .Values.prometheus.podMonitor.metricRelabelings | nindent 4 }}
{{- end }}
{{- if .Values.prometheus.podMonitor.relabelings }}
    relabelings:
{{- toYaml .Values.prometheus.podMonitor.relabelings | nindent 4 }}
{{- end }}
---
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: {{ template "metallb.fullname" . }}-speaker
  labels:
    {{- include "metallb.labels" . | nindent 4 }}
    app.kubernetes.io/component: speaker
    {{- if .Values.prometheus.podMonitor.additionalLabels }}
{{ toYaml .Values.prometheus.podMonitor.additionalLabels | indent 4 }}
    {{- end }}
  {{- if .Values.prometheus.podMonitor.annotations }}
  annotations:
{{ toYaml .Values.prometheus.podMonitor.annotations | indent 4 }}
  {{- end }}
spec:
  jobLabel: {{ .Values.prometheus.podMonitor.jobLabel | quote }}
  selector:
    matchLabels:
      {{- include "metallb.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: speaker
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
  podMetricsEndpoints:
  - port: monitoring
    path: /metrics
    {{- if .Values.prometheus.podMonitor.interval }}
    interval: {{ .Values.prometheus.podMonitor.interval }}
    {{- end }}
{{- if .Values.prometheus.podMonitor.metricRelabelings }}
    metricRelabelings:
{{- toYaml .Values.prometheus.podMonitor.metricRelabelings | nindent 4 }}
{{- end }}
{{- if .Values.prometheus.podMonitor.relabelings }}
    relabelings:
{{- toYaml .Values.prometheus.podMonitor.relabelings | nindent 4 }}
{{- end }}
---
{{- if .Values.prometheus.rbacPrometheus }}
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: {{ template "metallb.fullname" . }}-prometheus
rules:
  - apiGroups:
      - ""
    resources:
      - pods
    verbs:
      - get
      - list
      - watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ template "metallb.fullname" . }}-prometheus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ template "metallb.fullname" . }}-prometheus
subjects:
  - kind: ServiceAccount
    name: {{ required ".Values.prometheus.serviceAccount must be defined when .Values.prometheus.podMonitor.enabled == true" .Values.prometheus.serviceAccount }}
    namespace: {{ required ".Values.prometheus.namespace must be defined when .Values.prometheus.podMonitor.enabled == true" .Values.prometheus.namespace }}
{{- end }}
{{- end }}
