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

{{- if .Values.controlPlane.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "apisix.control-plane.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: apisix
    app.kubernetes.io/component: control-plane
  {{- if or .Values.controlPlane.service.annotations .Values.commonAnnotations (and .Values.controlPlane.metrics.enabled .Values.controlPlane.metrics.annotations) }}
  annotations:
    {{- if or .Values.controlPlane.service.annotations .Values.commonAnnotations }}
    {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.controlPlane.service.annotations .Values.commonAnnotations ) "context" . ) }}
    {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
    {{- end }}
    {{- if and .Values.controlPlane.metrics.enabled .Values.controlPlane.metrics.annotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.controlPlane.metrics.annotations "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  type: {{ .Values.controlPlane.service.type }}
  {{- if and .Values.controlPlane.service.clusterIP (eq .Values.controlPlane.service.type "ClusterIP") }}
  clusterIP: {{ .Values.controlPlane.service.clusterIP }}
  {{- end }}
  {{- if .Values.controlPlane.service.sessionAffinity }}
  sessionAffinity: {{ .Values.controlPlane.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.controlPlane.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.controlPlane.service.type "LoadBalancer") (eq .Values.controlPlane.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.controlPlane.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.controlPlane.service.type "LoadBalancer") (not (empty .Values.controlPlane.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ toYaml .Values.controlPlane.service.loadBalancerSourceRanges | nindent 4 }}
  {{- end }}
  {{- if and (eq .Values.controlPlane.service.type "LoadBalancer") (not (empty .Values.controlPlane.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.controlPlane.service.loadBalancerIP }}
  {{- end }}
  {{- if .Values.controlPlane.service.externalIPs }}
  externalIPs: {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.service.externalIPs "context" $) | nindent 4 }}
  {{- end }}
  ports:
    - name: {{ ternary "https" "http" .Values.controlPlane.tls.enabled }}-admin-api
      port: {{ .Values.controlPlane.service.ports.adminAPI }}
      protocol: TCP
      {{- if and (or (eq .Values.controlPlane.service.type "NodePort") (eq .Values.controlPlane.service.type "LoadBalancer")) (not (empty .Values.controlPlane.service.nodePorts.adminAPI)) }}
      nodePort: {{ .Values.controlPlane.service.nodePorts.adminAPI }}
      {{- else if eq .Values.controlPlane.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: {{ ternary "https" "http" .Values.controlPlane.tls.enabled }}-admin-api
    - name: {{ ternary "https" "http" .Values.controlPlane.tls.enabled }}-cfg-srv
      port: {{ .Values.controlPlane.service.ports.configServer }}
      protocol: TCP
      {{- if and (or (eq .Values.controlPlane.service.type "NodePort") (eq .Values.controlPlane.service.type "LoadBalancer")) (not (empty .Values.controlPlane.service.nodePorts.configServer)) }}
      nodePort: {{ .Values.controlPlane.service.nodePorts.configServer }}
      {{- else if eq .Values.controlPlane.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: {{ ternary "https" "http" .Values.controlPlane.tls.enabled }}-cfg-srv
    {{- if .Values.controlPlane.metrics.enabled }}
    - name: http-metrics
      port: {{ .Values.controlPlane.service.ports.metrics }}
      protocol: TCP
      {{- if and (or (eq .Values.controlPlane.service.type "NodePort") (eq .Values.controlPlane.service.type "LoadBalancer")) (not (empty .Values.controlPlane.service.nodePorts.metrics)) }}
      nodePort: {{ .Values.controlPlane.service.nodePorts.metrics }}
      {{- else if eq .Values.controlPlane.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: http-metrics
    {{- end }}
    {{- if .Values.controlPlane.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.controlPlane.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: control-plane
{{- end }}
