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

apiVersion: v1
kind: Service
metadata:
  name: {{ template "appsmith.backend.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: backend
  {{- if or .Values.backend.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.backend.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.backend.service.type }}
  {{- if and .Values.backend.service.clusterIP (eq .Values.backend.service.type "ClusterIP") }}
  clusterIP: {{ .Values.backend.service.clusterIP }}
  {{- end }}
  {{- if .Values.backend.service.sessionAffinity }}
  sessionAffinity: {{ .Values.backend.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.backend.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.backend.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.backend.service.type "LoadBalancer") (eq .Values.backend.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.backend.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.backend.service.type "LoadBalancer") (not (empty .Values.backend.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.backend.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.backend.service.type "LoadBalancer") (not (empty .Values.backend.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.backend.service.loadBalancerIP }}
  {{- end }}
  # We need this service to be published to avoid a deadlock backend <-> rts (there is a mutual dependency)
  publishNotReadyAddresses: true
  ports:
    - name: http
      port: {{ .Values.backend.service.ports.http }}
      targetPort: http
      protocol: TCP
      {{- if and (or (eq .Values.backend.service.type "NodePort") (eq .Values.backend.service.type "LoadBalancer")) (not (empty .Values.backend.service.nodePorts.http)) }}
      nodePort: {{ .Values.backend.service.nodePorts.http }}
      {{- else if eq .Values.backend.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.backend.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.backend.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.backend.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: backend
