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

{{- if .Values.server.enabled }}
apiVersion: v1
kind: Service
metadata:
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: server
  name: {{ template "common.names.fullname" . }}-server
  namespace: {{ .Release.Namespace | quote }}
  {{- if or .Values.server.service.annotations .Values.commonAnnotations (and .Values.agent.metrics.enabled .Values.agent.metrics.annotations) }}
  {{- $annotations := dict -}}
  {{- if or .Values.server.service.annotations .Values.commonAnnotations }}
  {{- $annotations = merge $annotations .Values.server.service.annotations .Values.commonAnnotations -}}
  {{- end }}
  {{- if and .Values.agent.metrics.enabled .Values.agent.metrics.annotations }}
  {{- $annotations = merge $annotations .Values.agent.metrics.annotations }}
  {{- end }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.server.service.type }}
  {{- if and .Values.server.service.clusterIP (eq .Values.server.service.type "ClusterIP") }}
  clusterIP: {{ .Values.server.service.clusterIP }}
  {{- end }}
  {{- if (or (eq .Values.server.service.type "LoadBalancer") (eq .Values.server.service.type "NodePort")) }}
  externalTrafficPolicy: {{ .Values.server.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if eq .Values.server.service.type "LoadBalancer" }}
  loadBalancerSourceRanges: {{ .Values.server.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if (and (eq .Values.server.service.type "LoadBalancer") (not (empty .Values.server.service.loadBalancerIP))) }}
  loadBalancerIP: {{ .Values.server.service.loadBalancerIP }}
  {{- end }}
  {{- if .Values.server.service.sessionAffinity }}
  sessionAffinity: {{ .Values.server.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.server.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.server.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  ports:
    - name: grpclb
      port: {{ .Values.server.service.port }}
      targetPort: {{ .Values.server.containerPort }}
      protocol: TCP
      {{- if (and (or (eq .Values.server.service.type "NodePort") (eq .Values.server.service.type "LoadBalancer")) (not (empty .Values.server.service.nodePorts.http))) }}
      nodePort: {{ .Values.server.service.nodePorts.http }}
      {{- else if eq .Values.server.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.server.metrics.enabled }}
    - name: metrics
      port: {{ .Values.server.metrics.port }}
      targetPort: metrics
      protocol: TCP
      {{- if (and (or (eq .Values.server.service.type "NodePort") (eq .Values.server.service.type "LoadBalancer")) (not (empty .Values.server.service.nodePorts.metrics))) }}
      nodePort: {{ .Values.server.service.nodePorts.metrics }}
      {{- else if eq .Values.server.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- end }}
    {{- if .Values.server.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.server.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.server.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: server
{{- end }}
