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

{{- if and .Values.worker.enabled .Values.worker.externalAccess.enabled }}
{{- $fullName := include "deepspeed.v0.worker.fullname" . }}
{{- $replicaCount := .Values.worker.replicaCount | int }}
{{- $root := . }}

{{- range $i, $e := until $replicaCount }}
{{- $targetPod := printf "%s-%d" (printf "%s" $fullName) $i }}
{{- $_ := set $ "targetPod" $targetPod }}
apiVersion: v1
kind: Service
metadata:
  name: {{ printf "%s-%d-external" (include "deepspeed.v0.worker.fullname" $) $i | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.namespace" $ | quote }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list $root.Values.worker.externalAccess.service.labels $root.Values.commonLabels ) "context" $ ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: deepspeed
    app.kubernetes.io/component: worker
  {{- if or $root.Values.worker.externalAccess.service.annotations $root.Values.commonAnnotations $root.Values.worker.externalAccess.service.loadBalancerAnnotations }}
  annotations:
    {{- if and (not (empty $root.Values.worker.externalAccess.service.loadBalancerAnnotations)) (eq (len $root.Values.worker.externalAccess.service.loadBalancerAnnotations) $replicaCount) }}
    {{ include "common.tplvalues.render" (dict "value" (index $root.Values.worker.externalAccess.service.loadBalancerAnnotations $i) "context" $) | nindent 4 }}
    {{- end }}
    {{- if or $root.Values.worker.externalAccess.service.annotations $root.Values.commonAnnotations }}
    {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list $root.Values.worker.externalAccess.service.annotations $root.Values.commonAnnotations ) "context" $ ) }}
    {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  type: {{ $root.Values.worker.externalAccess.service.type }}
  {{- if eq $root.Values.worker.externalAccess.service.type "LoadBalancer" }}
  {{- if and (not (empty $root.Values.worker.externalAccess.service.loadBalancerIPs)) (eq (len $root.Values.worker.externalAccess.service.loadBalancerIPs) $replicaCount) }}
  loadBalancerIP: {{ index $root.Values.worker.externalAccess.service.loadBalancerIPs $i }}
  {{- end }}
  {{- if $root.Values.worker.externalAccess.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges: {{- toYaml $root.Values.worker.externalAccess.service.loadBalancerSourceRanges | nindent 4 }}
  {{- end }}
  {{- end }}
  {{- if or (eq $root.Values.worker.externalAccess.service.type "LoadBalancer") (eq $root.Values.worker.externalAccess.service.type "NodePort") }}
  externalTrafficPolicy: {{ $root.Values.worker.externalAccess.service.externalTrafficPolicy | quote }}
  {{- end }}
  publishNotReadyAddresses: {{ $root.Values.worker.externalAccess.service.publishNotReadyAddresses }}
  {{- if $root.Values.worker.externalAccess.service.sessionAffinity }}
  sessionAffinity: {{ $root.Values.worker.externalAccess.service.sessionAffinity }}
  {{- end }}
  {{- if $root.Values.worker.externalAccess.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" $root.Values.worker.externalAccess.service.sessionAffinityConfig "context" $root) | nindent 4 }}
  {{- end }}
  ports:
    - name: tcp-ssh
      port: {{ $root.Values.worker.externalAccess.service.ports.ssh }}
      {{- if le (add $i 1) (len $root.Values.worker.externalAccess.service.nodePorts) }}
      nodePort: {{ index $root.Values.worker.externalAccess.service.nodePorts $i }}
      {{- else }}
      nodePort: null
      {{- end }}
      targetPort: tcp-ssh
    {{- if $root.Values.worker.externalAccess.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" $root.Values.worker.externalAccess.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- if and (eq $root.Values.worker.externalAccess.service.type "NodePort") (le (add $i 1) (len $root.Values.worker.externalAccess.service.externalIPs)) }}
  externalIPs: [{{ index $root.Values.worker.externalAccess.service.externalIPs $i | quote }}]
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list $root.Values.worker.podLabels $root.Values.commonLabels ) "context" $ ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: deepspeed
    app.kubernetes.io/component: worker
    statefulset.kubernetes.io/pod-name: {{ $targetPod }}
---
{{- end }}
{{- end }}
