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

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