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

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