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

apiVersion: v1
kind: Service
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels .Values.service.labels) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: envoy-gateway
    app.kubernetes.io/component: envoy-gateway
  {{- if or .Values.service.annotations .Values.metrics.enabled .Values.commonAnnotations }}
  {{- $annotations := dict }}
  {{- $annotations = include "common.tplvalues.merge" (dict "values" (list $annotations .Values.service.annotations .Values.commonAnnotations) "context" .) }}
  {{- if .Values.metrics.enabled }}
  {{- $defaultMetricsAnnotations := dict "prometheus.io/scrape" "true" "prometheus.io/port" (.Values.service.ports.metrics | toString) "prometheus.io/path" "/metrics" }}
  {{- $annotations = include "common.tplvalues.merge" (dict "values" (list $annotations $defaultMetricsAnnotations .Values.metrics.annotations) "context" .) }}
  {{- end }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.service.type }}
  {{- if and .Values.service.clusterIP (eq .Values.service.type "ClusterIP") }}
  clusterIP: {{ .Values.service.clusterIP }}
  {{- end }}
  {{- if .Values.service.sessionAffinity }}
  sessionAffinity: {{ .Values.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.service.type "LoadBalancer") (eq .Values.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if eq .Values.service.type "LoadBalancer" }}
    {{- if not (empty .Values.service.loadBalancerSourceRanges) }}
    loadBalancerSourceRanges: {{ .Values.service.loadBalancerSourceRanges }}
    {{- end }}
    {{- if not (empty .Values.service.loadBalancerIP) }}
    loadBalancerIP: {{ .Values.service.loadBalancerIP }}
    {{- end }}
    {{- if not (empty .Values.service.loadBalancerClass) }}
    loadBalancerClass: {{ .Values.service.loadBalancerClass }}
    {{- end }}
  {{- end }}
  ports:
    # All ports except admin are hardcoded and cannot be edited. Even in the case of the service, the operator
    # would configure envoy to point to the same service port. As an exception we have the metrics port which is not
    # considered in the envoy configuration
    # https://github.com/envoyproxy/gateway/blob/main/site/content/en/latest/install/install-helm.md
    - name: grpc
      port: 18000
      protocol: TCP
      {{- if and (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) (not (empty .Values.service.nodePorts.grpc)) }}
      nodePort: {{ .Values.service.nodePorts.grpc }}
      {{- else if eq .Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: grpc
    - name: tcp-ratelimit
      port: 18001
      protocol: TCP
      {{- if and (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) (not (empty .Values.service.nodePorts.ratelimit)) }}
      nodePort: {{ .Values.service.nodePorts.ratelimit }}
      {{- else if eq .Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: tcp-ratelimit
    - name: tcp-wasm
      port: 18002
      protocol: TCP
      {{- if and (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) (not (empty .Values.service.nodePorts.wasm)) }}
      nodePort: {{ .Values.service.nodePorts.wasm }}
      {{- else if eq .Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: tcp-wasm
    {{- if .Values.exposeAdmin }}
    - name: http-admin
      port: {{ .Values.service.ports.admin }}
      protocol: TCP
      {{- if and (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) (not (empty .Values.service.nodePorts.admin)) }}
      nodePort: {{ .Values.service.nodePorts.admin }}
      {{- else if eq .Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: http-admin
    {{- end }}
    {{- if .Values.metrics.enabled }}
    - name: http-metrics
      port: {{ .Values.service.ports.metrics }}
      protocol: TCP
      {{- if and (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) (not (empty .Values.service.nodePorts.metrics)) }}
      nodePort: {{ .Values.service.nodePorts.metrics }}
      {{- else if eq .Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: http-metrics
    {{- end }}
    {{- if .Values.topologyInjector.enabled }}
    - name: http-webhook
      port: 9443
      protocol: TCP
      {{- if and (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) (not (empty .Values.service.nodePorts.webhook)) }}
      nodePort: {{ .Values.service.nodePorts.webhook }}
      {{- else if eq .Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: http-webhook
    {{- end }}
    {{- if .Values.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: envoy-gateway
    app.kubernetes.io/component: envoy-gateway
