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

{{- if and .Values.pluginBarmanCloud.enabled .Values.pluginBarmanCloud.metrics.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ printf "%s-metrics" (include "cloudnative-pg.plugin-barman-cloud.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.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.commonLabels .Values.pluginBarmanCloud.metrics.service.labels) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cloudnative-pg
    app.kubernetes.io/component: metrics
  {{- $defaultAnnotations := dict "prometheus.io/scrape" "true" "prometheus.io/port" (.Values.pluginBarmanCloud.metrics.service.ports.metrics | quote) "prometheus.io/path" "/metrics" }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list  $defaultAnnotations .Values.pluginBarmanCloud.metrics.service.annotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" (dict "value" $annotations "context" $) | nindent 4 }}
spec:
  type: ClusterIP
  sessionAffinity: {{ .Values.pluginBarmanCloud.metrics.service.sessionAffinity }}
  {{- if .Values.pluginBarmanCloud.metrics.service.clusterIP }}
  clusterIP: {{ .Values.pluginBarmanCloud.metrics.service.clusterIP }}
  {{- end }}
  ports:
    - name: http-metrics
      port: {{ .Values.pluginBarmanCloud.metrics.service.ports.metrics }}
      protocol: TCP
      targetPort: http-metrics
  {{- /* 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.commonLabels) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cloudnative-pg
    app.kubernetes.io/component: plugin-barman-cloud
{{- end }}
