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

{{- if and .Values.pluginBarmanCloud.metrics.enabled .Values.pluginBarmanCloud.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "cloudnative-pg.plugin-barman-cloud.fullname" . }}
  namespace: {{ default (include "common.names.namespace" .) .Values.pluginBarmanCloud.metrics.serviceMonitor.namespace | quote }}
  {{- /* Updating app.kubernetes.io/name because to avoid "duplicate deployment" error: https://github.com/cloudnative-pg/cloudnative-pg/blob/9c2769815ff78cbfc28e2a2818f3a04add33477c/pkg/certs/operator_deployment.go#L120 */}}
  {{- $appNameLabels := dict "app.kubernetes.io/name" "plugin-barman-cloud" }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list $appNameLabels .Values.pluginBarmanCloud.metrics.serviceMonitor.labels .Values.commonLabels ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cloudnative-pg
    app.kubernetes.io/component: metrics
  {{- if or .Values.pluginBarmanCloud.metrics.serviceMonitor.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.pluginBarmanCloud.metrics.serviceMonitor.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  jobLabel: {{ .Values.pluginBarmanCloud.metrics.serviceMonitor.jobLabel | quote }}
  {{- $appNameLabels := dict "app.kubernetes.io/name" "plugin-barman-cloud" }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list $appNameLabels .Values.commonLabels) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $labels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: cloudnative-pg
      app.kubernetes.io/component: metrics
      {{- if .Values.pluginBarmanCloud.metrics.serviceMonitor.selector }}
      {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.metrics.serviceMonitor.selector "context" $) | nindent 6 }}
      {{- end }}
  endpoints:
    - port: http-metrics
      path: "/metrics"
      {{- if .Values.pluginBarmanCloud.metrics.serviceMonitor.interval }}
      interval: {{ .Values.pluginBarmanCloud.metrics.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.pluginBarmanCloud.metrics.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ .Values.pluginBarmanCloud.metrics.serviceMonitor.scrapeTimeout }}
      {{- end }}
      {{- if .Values.pluginBarmanCloud.metrics.serviceMonitor.honorLabels }}
      honorLabels: {{ .Values.pluginBarmanCloud.metrics.serviceMonitor.honorLabels }}
      {{- end }}
      {{- if .Values.pluginBarmanCloud.metrics.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.pluginBarmanCloud.metrics.serviceMonitor.metricRelabelings "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.pluginBarmanCloud.metrics.serviceMonitor.relabelings }}
      relabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.pluginBarmanCloud.metrics.serviceMonitor.relabelings "context" $) | nindent 8 }}
      {{- end }}
  namespaceSelector:
    matchNames:
    - {{ include "common.names.namespace" . | quote }}
{{- end }}
