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

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