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

{{- if .Values.notificationController.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ printf "%s-webhook" (include "flux.notification-controller.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: flux
    app.kubernetes.io/component: notification-controller
  {{- if or .Values.notificationController.service.receiver.annotations .Values.commonAnnotations }}
  annotations:
    {{- if or .Values.notificationController.service.receiver.annotations .Values.commonAnnotations }}
    {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.notificationController.service.receiver.annotations .Values.commonAnnotations ) "context" . ) }}
    {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  type: {{ .Values.notificationController.service.webhook.type }}
  {{- if and .Values.notificationController.service.webhook.clusterIP (eq .Values.notificationController.service.webhook.type "ClusterIP") }}
  clusterIP: {{ .Values.notificationController.service.webhook.clusterIP }}
  {{- end }}
  {{- if .Values.notificationController.service.webhook.sessionAffinity }}
  sessionAffinity: {{ .Values.notificationController.service.webhook.sessionAffinity }}
  {{- end }}
  {{- if .Values.notificationController.service.webhook.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.notificationController.service.webhook.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.notificationController.service.webhook.type "LoadBalancer") (eq .Values.notificationController.service.webhook.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.notificationController.service.webhook.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.notificationController.service.webhook.type "LoadBalancer") (not (empty .Values.notificationController.service.webhook.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.notificationController.service.webhook.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.notificationController.service.webhook.type "LoadBalancer") (not (empty .Values.notificationController.service.webhook.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.notificationController.service.webhook.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http
      port: {{ .Values.notificationController.service.webhook.ports.http }}
      protocol: TCP
      {{- if and (or (eq .Values.notificationController.service.webhook.type "NodePort") (eq .Values.notificationController.service.webhook.type "LoadBalancer")) (not (empty .Values.notificationController.service.webhook.nodePorts.http)) }}
      nodePort: {{ .Values.notificationController.service.webhook.nodePorts.http }}
      {{- else if eq .Values.notificationController.service.webhook.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: http-webhook
    {{- if .Values.notificationController.service.webhook.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.notificationController.service.webhook.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.notificationController.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: flux
    app.kubernetes.io/component: notification-controller
{{- end }}
