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

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