{{- if eq .Values.mode "distributed" }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ printf "%s-scheduler" (include "common.names.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: scheduler
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: 1
  {{- if .Values.scheduler.updateStrategy }}
  strategy: {{- toYaml .Values.scheduler.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  template:
    metadata:
      {{- if .Values.scheduler.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: scheduler
        {{- if .Values.scheduler.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "mxnet.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.scheduler.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.scheduler.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.scheduler.podAffinityPreset "component" "scheduler" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.scheduler.podAntiAffinityPreset "component" "scheduler" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.scheduler.nodeAffinityPreset.type "key" .Values.scheduler.nodeAffinityPreset.key "values" .Values.scheduler.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.scheduler.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.scheduler.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.scheduler.priorityClassName }}
      priorityClassName: {{ .Values.scheduler.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.scheduler.schedulerName }}
      schedulerName: {{ .Values.scheduler.schedulerName | quote }}
      {{- end }}
      {{- if .Values.scheduler.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.scheduler.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.scheduler.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.scheduler.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.scheduler.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if or .Values.cloneFilesFromGit.enabled .Values.initContainers }}
      initContainers:
        {{- if .Values.cloneFilesFromGit.enabled }}
        - name: git-clone-repository
          image: {{ include "git.image" . }}
          imagePullPolicy: {{ .Values.git.pullPolicy | quote }}
          command:
            - /bin/sh
            - -c
            - |
              [[ -f "/opt/bitnami/scripts/git/entrypoint.sh" ]] && source "/opt/bitnami/scripts/git/entrypoint.sh"
              git clone {{ .Values.cloneFilesFromGit.repository }} --branch {{ .Values.cloneFilesFromGit.revision }} /app
          volumeMounts:
            - name: git-cloned-files
              mountPath: /app
            {{- if .Values.cloneFilesFromGit.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.cloneFilesFromGit.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- end }}
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.initContainers "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.scheduler.initContainers }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.scheduler.initContainers "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
        - name: mxnet
          workingDir: {{ .Values.entrypoint.workDir }}
          image: {{ include "mxnet.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.scheduler.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.scheduler.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.scheduler.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - bash
            - -c
            - |
            {{- include "mxnet.entrypoint" . | nindent 14 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.scheduler.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.scheduler.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: DMLC_ROLE
              value: "scheduler"
            - name: DMLC_NUM_WORKER
              value: {{ .Values.worker.replicaCount | quote }}
            - name: DMLC_NUM_SERVER
              value: {{ .Values.server.replicaCount | quote }}
            - name: DMLC_PS_ROOT_URI
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: DMLC_PS_ROOT_PORT
              value: {{ .Values.scheduler.containerPorts.mxnet | quote }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
            {{- if .Values.scheduler.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
            {{- if .Values.scheduler.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.scheduler.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.scheduler.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.scheduler.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: mxnet
              containerPort: {{ .Values.scheduler.containerPorts.mxnet }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.scheduler.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.scheduler.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.scheduler.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: mxnet
          {{- end }}
          {{- if .Values.scheduler.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.scheduler.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.scheduler.readinessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: mxnet
          {{- end }}
          {{- if .Values.scheduler.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.scheduler.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.scheduler.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: mxnet
          {{- end }}
          {{- end }}
          {{- if .Values.scheduler.resources }}
          resources: {{- toYaml .Values.scheduler.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{ include "mxnet.volumeMounts" . | nindent 12 }}
            - name: data
              mountPath: {{ .Values.persistence.mountPath }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
            {{- if .Values.scheduler.extraVolumeMounts }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.scheduler.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.scheduler.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.scheduler.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{ include "mxnet.volumes" . | nindent 8 }}
        - name: data
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ default (printf "%s-scheduler" (include "common.names.fullname" .)) .Values.persistence.existingClaim }}
        {{- else }}
          emptyDir: {}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.scheduler.extraVolumes }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.scheduler.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
