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

apiVersion: v1
kind: Service
metadata:
  # Service name hardcoded in cloudnative-pg code
  # https://github.com/cloudnative-pg/cloudnative-pg/blob/main/internal/cmd/manager/controller/controller.go#L51
  name: cnpg-webhook-service
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels .Values.operator.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: operator
  {{- if or .Values.operator.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.operator.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations:
    {{- if or .Values.operator.service.annotations .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  type: {{ .Values.operator.service.type }}
  {{- if and .Values.operator.service.clusterIP (eq .Values.operator.service.type "ClusterIP") }}
  clusterIP: {{ .Values.operator.service.clusterIP }}
  {{- end }}
  {{- if .Values.operator.service.sessionAffinity }}
  sessionAffinity: {{ .Values.operator.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.operator.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.operator.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.operator.service.type "LoadBalancer") (eq .Values.operator.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.operator.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.operator.service.type "LoadBalancer") (not (empty .Values.operator.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.operator.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.operator.service.type "LoadBalancer") (not (empty .Values.operator.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.operator.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http-webhook
      port: {{ .Values.operator.service.ports.webhook }}
      protocol: TCP
      {{- if and (or (eq .Values.operator.service.type "NodePort") (eq .Values.operator.service.type "LoadBalancer")) (not (empty .Values.operator.service.nodePorts.webhook)) }}
      nodePort: {{ .Values.operator.service.nodePorts.webhook }}
      {{- else if eq .Values.operator.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: http-webhook
    {{- if .Values.operator.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.operator.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.operator.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: operator
