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

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