{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if and .Values.hubble.peers.metrics.enabled .Values.hubble.peers.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ printf "%s-hubble-peers" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ default (include "common.names.namespace" .) .Values.hubble.peers.metrics.serviceMonitor.namespace | quote }}
  {{- $labels := include "common.tplvalues.merge" (dict "values" (list .Values.hubble.peers.metrics.serviceMonitor.labels .Values.commonLabels) "context" .) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cilium
    app.kubernetes.io/component: hubble-peers
  {{- if or .Values.hubble.peers.metrics.serviceMonitor.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.hubble.peers.metrics.serviceMonitor.annotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  jobLabel: {{ .Values.hubble.peers.metrics.serviceMonitor.jobLabel | quote }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: cilium
      app.kubernetes.io/component: hubble-peers
      prometheus.io/scrape: "true"
      {{- if .Values.hubble.peers.metrics.serviceMonitor.selector }}
      {{- include "common.tplvalues.render" (dict "value" .Values.hubble.peers.metrics.serviceMonitor.selector "context" $) | nindent 6 }}
      {{- end }}
  endpoints:
    - port: tcp-hubble-metrics
      path: "/metrics"
      {{- if .Values.hubble.peers.metrics.serviceMonitor.interval }}
      interval: {{ .Values.hubble.peers.metrics.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.hubble.peers.metrics.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ .Values.hubble.peers.metrics.serviceMonitor.scrapeTimeout }}
      {{- end }}
      {{- if .Values.hubble.peers.metrics.serviceMonitor.honorLabels }}
      honorLabels: {{ .Values.hubble.peers.metrics.serviceMonitor.honorLabels }}
      {{- end }}
      {{- if .Values.hubble.peers.metrics.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.hubble.peers.metrics.serviceMonitor.metricRelabelings "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.hubble.peers.metrics.serviceMonitor.relabelings }}
      relabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.hubble.peers.metrics.serviceMonitor.relabelings "context" $) | nindent 8 }}
      {{- end }}
  namespaceSelector:
    matchNames:
    - {{ include "common.names.namespace" . | quote }}
{{- end }}
