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

{{- if and .Values.proxy.enabled .Values.proxy.metrics.enabled .Values.proxy.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "milvus.proxy.fullname" . }}
  namespace: {{ default (include "common.names.namespace" .) .Values.proxy.metrics.serviceMonitor.namespace | quote }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.proxy.metrics.serviceMonitor.labels .Values.commonLabels ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: milvus
    app.kubernetes.io/component: proxy
  {{- if or .Values.commonAnnotations .Values.proxy.metrics.serviceMonitor.annotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.proxy.metrics.serviceMonitor.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.proxy.metrics.serviceMonitor.jobLabel }}
  jobLabel: {{ .Values.proxy.metrics.serviceMonitor.jobLabel }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 6 }}
      {{- if .Values.proxy.metrics.serviceMonitor.selector }}
      {{- include "common.tplvalues.render" (dict "value" .Values.proxy.metrics.serviceMonitor.selector "context" $) | nindent 6 }}
      {{- end }}
      app.kubernetes.io/part-of: milvus
      app.kubernetes.io/component: proxy
  endpoints:
    - port: http-metrics
      {{- if .Values.proxy.metrics.serviceMonitor.interval }}
      interval: {{ .Values.proxy.metrics.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.proxy.metrics.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ .Values.proxy.metrics.serviceMonitor.scrapeTimeout }}
      {{- end }}
      {{- if .Values.proxy.metrics.serviceMonitor.honorLabels }}
      honorLabels: {{ .Values.proxy.metrics.serviceMonitor.honorLabels }}
      {{- end }}
      {{- if .Values.proxy.metrics.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{- toYaml .Values.proxy.metrics.serviceMonitor.metricRelabelings | nindent 6 }}
      {{- end }}
      {{- if .Values.proxy.metrics.serviceMonitor.relabelings }}
      relabelings: {{- toYaml .Values.proxy.metrics.serviceMonitor.relabelings | nindent 6 }}
      {{- end }}
  namespaceSelector:
    matchNames:
      - {{ include "common.names.namespace" . | quote }}
{{- end }}
