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

apiVersion: v1
kind: Service
metadata:
  name: {{ template "cloudnative-pg.plugin-barman-cloud.fullname" . }}
  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.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: plugin-barman-cloud
    cnpg.io/pluginName: barman-cloud.cloudnative-pg.io
  annotations:
    cnpg.io/pluginClientSecret: {{ include "cloudnative-pg.plugin-barman-cloud.tlsClientSecretName" . }}
    cnpg.io/pluginServerSecret: {{ include "cloudnative-pg.plugin-barman-cloud.tlsServerSecretName" . }}
    cnpg.io/pluginPort: {{ .Values.pluginBarmanCloud.service.ports.grpc | quote }}
  {{- if or .Values.pluginBarmanCloud.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.pluginBarmanCloud.service.annotations .Values.commonAnnotations ) "context" . ) }}
    {{- if or .Values.pluginBarmanCloud.service.annotations .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  type: {{ .Values.pluginBarmanCloud.service.type }}
  {{- if and .Values.pluginBarmanCloud.service.clusterIP (eq .Values.pluginBarmanCloud.service.type "ClusterIP") }}
  clusterIP: {{ .Values.pluginBarmanCloud.service.clusterIP }}
  {{- end }}
  {{- if .Values.pluginBarmanCloud.service.sessionAffinity }}
  sessionAffinity: {{ .Values.pluginBarmanCloud.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.pluginBarmanCloud.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.pluginBarmanCloud.service.type "LoadBalancer") (eq .Values.pluginBarmanCloud.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.pluginBarmanCloud.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.pluginBarmanCloud.service.type "LoadBalancer") (not (empty .Values.pluginBarmanCloud.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.pluginBarmanCloud.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.pluginBarmanCloud.service.type "LoadBalancer") (not (empty .Values.pluginBarmanCloud.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.pluginBarmanCloud.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: grpc
      port: {{ .Values.pluginBarmanCloud.service.ports.grpc }}
      protocol: TCP
      {{- if and (or (eq .Values.pluginBarmanCloud.service.type "NodePort") (eq .Values.pluginBarmanCloud.service.type "LoadBalancer")) (not (empty .Values.pluginBarmanCloud.service.nodePorts.grpc)) }}
      nodePort: {{ .Values.pluginBarmanCloud.service.nodePorts.grpc }}
      {{- else if eq .Values.pluginBarmanCloud.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: grpc
    {{- if .Values.pluginBarmanCloud.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list $appNameLabels .Values.pluginBarmanCloud.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cloudnative-pg
    app.kubernetes.io/component: plugin-barman-cloud
