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

{{- if .Values.client.enabled }}
apiVersion: {{ ternary "batch/v1" "apps/v1" .Values.client.useJob }}
kind: {{ ternary "Job" "Deployment" .Values.client.useJob }}
metadata:
  name: {{ include "deepspeed.v0.client.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.client.labels .Values.commonLabels ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: deepspeed
    app.kubernetes.io/component: client
  {{- if or .Values.client.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.client.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.client.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: deepspeed
      app.kubernetes.io/component: client
  {{- if .Values.client.useJob }}
  backoffLimit: {{ .Values.client.backoffLimit }}
  {{- else }}
  {{- if .Values.client.updateStrategy }}
  strategy: {{- toYaml .Values.client.updateStrategy | nindent 4 }}
  {{- end }}
  {{- end }}
  template:
    metadata:
      annotations:
        checksum/hostfile: {{ include (print $.Template.BasePath "/client/hostfile-configmap.yaml") . | sha256sum }}
        checksum/ssh-private-key: {{ include (print $.Template.BasePath "/ssh-keys-secret.yaml") . | sha256sum }}
        checksum/ssh-client-conf: {{ include (print $.Template.BasePath "/client/ssh-client-configmap.yaml") . | sha256sum }}
        {{- if (include "deepspeed.v0.source.createConfigMap" .) }}
        checksum/source: {{ include (print $.Template.BasePath "/client/source-configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if or .Values.client.podAnnotations .Values.commonAnnotations }}
        {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.client.podAnnotations .Values.commonAnnotations ) "context" . ) }}
        {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: deepspeed
        app.kubernetes.io/component: client
    spec:
      {{- include "deepspeed.v0.imagePullSecrets" . | nindent 6 }}
      serviceAccountName: {{ template "deepspeed.v0.client.serviceAccountName" . }}
      automountServiceAccountToken: {{ .Values.client.automountServiceAccountToken }}
      {{- if .Values.client.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.client.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.client.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.client.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.client.podAffinityPreset "component" "client" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.client.podAntiAffinityPreset "component" "client" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.client.nodeAffinityPreset.type "key" .Values.client.nodeAffinityPreset.key "values" .Values.client.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.client.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.client.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.client.runtimeClassName }}
      runtimeClassName: {{ .Values.client.runtimeClassName | quote }}
      {{- end }}
      {{- if .Values.client.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.client.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.client.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.client.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.client.priorityClassName }}
      priorityClassName: {{ .Values.client.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.client.schedulerName }}
      schedulerName: {{ .Values.client.schedulerName | quote }}
      {{- end }}
      {{- if .Values.client.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.client.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.client.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.client.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        {{- if .Values.client.enableDefaultInitContainers }}
        {{- include "deepspeed.v0.ssh.clientInitContainer" . | nindent 8 }}
        {{- include "deepspeed.v0.client.waitForWorkers" . | nindent 8 }}
        {{- if eq .Values.source.type "git" }}
        {{- include "deespeed.git.cloneInitContainer" (dict "component" "client" "context" $) | nindent 8 }}
        {{- end }}
        {{- if and .Values.volumePermissions.enabled .Values.client.persistence.enabled }}
        {{- include "deepspeed.v0.volumePermissionsInitContainer" (dict "component" "client" "context" $) | nindent 8 }}
        {{- end }}
        {{- end }}
        {{- if .Values.client.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.client.initContainers "context" $) | nindent 8}}
        {{- end }}
      containers:
        - name: deepspeed
          image: {{ include "deepspeed.v0.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.client.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.client.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.client.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.client.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - bash
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.client.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.client.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - -ec
            - |
              #!/bin/bash
              [[ -f "/opt/bitnami/scripts/deepspeed/entrypoint.sh" ]] && source "/opt/bitnami/scripts/deepspeed/entrypoint.sh"
              {{- if .Values.source.launchCommand }}
              {{- include "common.tplvalues.render" (dict "value" .Values.source.launchCommand "context" $) | nindent 14 }}
              {{- end }}
              {{- if not .Values.client.useJob }}
              sleep infinity
              {{- end }}
          {{- end }}
          env:
            - name: HOME
              value: /home/deepspeed
            {{- if .Values.client.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.client.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.client.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.client.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.client.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.client.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.client.resources }}
          resources: {{- toYaml .Values.client.resources | nindent 12 }}
          {{- else if ne .Values.client.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.client.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.client.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.client.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.client.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.client.livenessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - bash
                - -c
                - |
                  [[ -f "/opt/bitnami/scripts/deepspeed/entrypoint.sh" ]] && source "/opt/bitnami/scripts/deepspeed/entrypoint.sh"
                  deepspeed --help
          {{- end }}
          {{- if .Values.client.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.client.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.client.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.client.readinessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - bash
                - -c
                - |
                  [[ -f "/opt/bitnami/scripts/deepspeed/entrypoint.sh" ]] && source "/opt/bitnami/scripts/deepspeed/entrypoint.sh"
                  python -c "import deepspeed; print(deepspeed.__version__)"
          {{- end }}
          {{- if .Values.client.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.client.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.client.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.client.startupProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - python
                - -c
                - import deepspeed; deepspeed.__version__
          {{- end }}
          {{- end }}
          {{- if .Values.client.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.client.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: source
              mountPath: /app
            - name: empty-dir
              mountPath: /etc/ssh/ssh_config
              subPath: ssh-conf-dir/ssh_config
            - name: ssh-client-config
              mountPath: /etc/ssh/ssh_config.d
            - name: hostfile
              mountPath: /job
            - name: empty-dir
              mountPath: /home/deepspeed/.local
              subPath: app-local-dir
            - name: empty-dir
              mountPath: /home/deepspeed/.ssh
              subPath: app-ssh-dir
            - name: ssh-client-private-key
              mountPath: /bitnami/ssh/client-private-key
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /home/deepspeed/.cache
              subPath: app-cache-dir
            - name: data
              mountPath: {{ .Values.client.persistence.mountPath }}
            {{- if .Values.client.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.client.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.client.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.client.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        # SSH client settings for connecting to the worker nodes
        - name: ssh-client-config
          configMap:
            name: {{ include "deepspeed.v0.ssh.clientConfigMapName" . }}
        - name: ssh-client-private-key
          secret:
            secretName: {{ include "deepspeed.v0.ssh.keySecretName" . }}
        # Source code of the application
        - name: source
          {{- if or (.Values.source.existingConfigMap) (include "deepspeed.v0.source.createConfigMap" .) }}
          configMap:
            name: {{ include "deepspeed.v0.source.configMapName" . }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        # hostfile for connecting to the worker nodes
        - name: hostfile
          configMap:
            name: {{ include "deepspeed.v0.hostfileConfigMapName" . }}
        # These folders are necessary for building and pip install operations
        # Optional data folder in case the application requires storing data
        - name: data
          {{- if .Values.client.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.client.persistence.existingClaim | default (include "deepspeed.v0.client.fullname" .) }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        # Temporary folders: /tmp and .ssh
        {{- if .Values.client.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.client.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
