---
apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.service.name | default (include "nim-llm.fullname" .) }}
  labels:
    {{- include "nim-llm.labels" . | nindent 4 }}
    {{- if .Values.service.labels }}
    {{- toYaml .Values.service.labels | nindent 4 }}
    {{- end }}
  annotations:
    {{- if .Values.service.annotations }}
    {{- toYaml .Values.service.annotations | nindent 4 }}
    {{- end }}
spec:
  type: {{ .Values.service.type }}
  ports:
    {{- if and .Values.model.legacyCompat .Values.service.http_port }}
    - port: {{ .Values.service.http_port }}
      targetPort: http
      protocol: TCP
      name: http
    {{- end }}
    {{- if and .Values.model.legacyCompat .Values.service.grpc_port }}
    - port: {{ .Values.service.grpc_port }}
      targetPort: grpc
      protocol: TCP
      name: grpc
    {{- end }}
    {{- if and .Values.model.legacyCompat .Values.metrics.enabled .Values.service.metrics_port }}
    - port: {{ .Values.service.metrics_port }}
      targetPort: metrics
      name: metrics
    {{- end }}
    - port: {{ .Values.service.openai_port | default .Values.service.openaiPort }}
      targetPort: http-openai
      name: http-openai
    {{- if and .Values.model.legacyCompat .Values.model.nemo_port }}
    - port: {{ .Values.service.nemo_port }}
      targetPort: http-nemo
      name: http-nemo
    {{- end }}
  selector:
    {{- include "nim-llm.selectorLabels" . | nindent 4 }}
    {{- if .Values.multiNode.enabled }}
    {{- if (or (.Capabilities.APIVersions.Has "leaderworkerset.x-k8s.io/v1") .Values.multiNode.leaderWorkerSet.enabled) }}
    nim-llm-role: "leader"
    {{- else }}
    training.kubeflow.org/replica-index: "0"
    {{- end }}
    {{- end }}
{{- if (and .Values.statefulSet.enabled (not .Values.multiNode.enabled))}}
---
{{/*
If using statefulSet, ensure a headless service also exists to satisfy the API correctly
*/}}
apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.service.name | default (include "nim-llm.fullname" .) }}-sts
  labels:
    {{- include "nim-llm.labels" . | nindent 4 }}
    {{- if .Values.service.labels }}
    {{- toYaml .Values.service.labels | nindent 4 }}
    {{- end }}
  annotations:
    {{- if .Values.service.annotations }}
    {{- toYaml .Values.service.annotations | nindent 4 }}
    {{- end }}
spec:
  type: ClusterIP
  clusterIP: None
  ports:
    {{- if and .Values.model.legacyCompat .Values.service.http_port }}
    - port: {{ .Values.service.http_port }}
      targetPort: http
      protocol: TCP
      name: http
    {{- end }}
    {{- if and .Values.model.legacyCompat .Values.service.grpc_port }}
    - port: {{ .Values.service.grpc_port }}
      targetPort: grpc
      protocol: TCP
      name: grpc
    {{- end }}
    {{- if and .Values.model.legacyCompat .Values.metrics.enabled .Values.service.metrics_port }}
    - port: {{ .Values.service.metrics_port }}
      targetPort: metrics
      name: metrics
    {{- end }}
    - port: {{ .Values.service.openai_port | default .Values.service.openaiPort }}
      targetPort: http-openai
      name: http-openai
    {{- if and .Values.model.legacyCompat .Values.model.nemo_port }}
    - port: {{ .Values.service.nemo_port }}
      targetPort: http-nemo
      name: http-nemo
    {{- end }}
  selector:
    {{- include "nim-llm.selectorLabels" . | nindent 4 }}
{{- end }}
