{{- 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" . | nindent 4 }}
    app.kubernetes.io/component: forwarder
    app: forwarder
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  annotations:
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
    {{- if .Values.forwarder.service.annotations }}
    {{- include "common.tplvalues.render" (dict "value" .Values.forwarder.service.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 }}
  ports:
  {{- range $key, $value := .Values.forwarder.service.ports }}
    - name: {{ $key }}
      {{ toYaml $value | nindent 6 }}
  {{- end }}
  selector: {{ include "common.labels.matchLabels" . | nindent 4 }}
    app.kubernetes.io/component: forwarder
{{- end }}
