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

{{- if and .Values.forwarder.enabled .Values.forwarder.service.ports }}
apiVersion: v1
kind: Service
metadata:
  name: {{ printf "%s-forwarder" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: forwarder
    app: forwarder
  {{- if or .Values.commonAnnotations .Values.forwarder.service.annotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.forwarder.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.forwarder.service.type }}
  {{- if and .Values.forwarder.service.loadBalancerIP (eq .Values.forwarder.service.type "LoadBalancer") }}
  loadBalancerIP: {{ .Values.forwarder.service.loadBalancerIP }}
  {{- end }}
  {{- if (or (eq .Values.forwarder.service.type "LoadBalancer") (eq .Values.forwarder.service.type "NodePort")) }}
  externalTrafficPolicy: {{ .Values.forwarder.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and .Values.forwarder.service.loadBalancerSourceRanges (eq .Values.forwarder.service.type "LoadBalancer") }}
  loadBalancerSourceRanges: {{ .Values.forwarder.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.forwarder.service.type "ClusterIP") .Values.forwarder.service.clusterIP }}
  clusterIP: {{ .Values.forwarder.service.clusterIP }}
  {{- end }}
  {{- if .Values.forwarder.service.sessionAffinity }}
  sessionAffinity: {{ .Values.forwarder.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.forwarder.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.forwarder.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.forwarder.service.ipFamilyPolicy }}
  ipFamilyPolicy: {{ .Values.forwarder.service.ipFamilyPolicy }}
  {{- end }}
  {{- if .Values.forwarder.service.ipFamilies }}
  ipFamilies: {{ toYaml .Values.forwarder.service.ipFamilies | nindent 4 }}
  {{- end }}
  ports:
  {{- range $key, $value := .Values.forwarder.service.ports }}
    - name: {{ $key }}
      {{ toYaml $value | nindent 6 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.forwarder.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: forwarder
{{- end }}
