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

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