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

{{- if .Values.indexCoord.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "milvus.index-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: index-coordinator
  {{- if or .Values.commonAnnotations .Values.indexCoord.service.annotations (and .Values.indexCoord.metrics.enabled .Values.indexCoord.metrics.annotations) }}
  annotations:
    {{- if or .Values.indexCoord.service.annotations .Values.commonAnnotations }}
    {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.indexCoord.service.annotations .Values.commonAnnotations ) "context" . ) }}
    {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
    {{- end }}
    {{- if and .Values.indexCoord.metrics.enabled .Values.indexCoord.metrics.annotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.indexCoord.metrics.annotations "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  type: {{ .Values.indexCoord.service.type }}
  {{- if .Values.indexCoord.service.sessionAffinity }}
  sessionAffinity: {{ .Values.indexCoord.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.indexCoord.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.indexCoord.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.indexCoord.service.clusterIP }}
  clusterIP: {{ .Values.indexCoord.service.clusterIP }}
  {{- end }}
  {{- if (or (eq .Values.indexCoord.service.type "LoadBalancer") (eq .Values.indexCoord.service.type "NodePort")) }}
  externalTrafficPolicy: {{ .Values.indexCoord.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{ if eq .Values.indexCoord.service.type "LoadBalancer" }}
  loadBalancerSourceRanges: {{ .Values.indexCoord.service.loadBalancerSourceRanges }}
  {{ end }}
  {{- if (and (eq .Values.indexCoord.service.type "LoadBalancer") (not (empty .Values.indexCoord.service.loadBalancerIP))) }}
  loadBalancerIP: {{ .Values.indexCoord.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: grpc
      port: {{ .Values.indexCoord.service.ports.grpc }}
      targetPort: grpc
      protocol: TCP
      {{- if (and (or (eq .Values.indexCoord.service.type "NodePort") (eq .Values.indexCoord.service.type "LoadBalancer")) (not (empty .Values.indexCoord.service.nodePorts.grpc))) }}
      nodePort: {{ .Values.indexCoord.service.nodePorts.grpc }}
      {{- else if eq .Values.indexCoord.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.indexCoord.metrics.enabled }}
    - name: http-metrics
      port: {{ .Values.indexCoord.service.ports.metrics }}
      targetPort: http-metrics
      protocol: TCP
      {{- if (and (or (eq .Values.indexCoord.service.type "NodePort") (eq .Values.indexCoord.service.type "LoadBalancer")) (not (empty .Values.indexCoord.service.nodePorts.metrics))) }}
      nodePort: {{ .Values.indexCoord.service.nodePorts.metrics }}
      {{- else if eq .Values.indexCoord.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- end }}
    {{- if .Values.indexCoord.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.indexCoord.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.indexCoord.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: index-coordinator
{{- end }}
