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

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