apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "pytorch.fullname" . }}{{ if eq .Values.mode "distributed" }}-master{{ end }}
  labels: {{- include "pytorch.labels" . | nindent 4 }}
    app.kubernetes.io/component: master
spec:
  replicas: 1
  selector:
    matchLabels: {{- include "pytorch.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: master
  template:
    metadata:
      labels: {{- include "pytorch.labels" . | nindent 8 }}
        app.kubernetes.io/component: master
    spec:
{{- include "pytorch.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.affinity }}
      affinity: {{- include "pytorch.tplValue" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "pytorch.tplValue" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "pytorch.tplValue" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
        runAsUser: {{ .Values.securityContext.runAsUser }}
      {{- end }}
      {{- if or .Values.cloneFilesFromGit.enabled (and .Values.volumePermissions.enabled .Values.persistence.enabled) }}
      initContainers:
        {{- if .Values.cloneFilesFromGit.enabled }}
        - name: git-clone-repository
          image: {{ include "git.image" . }}
          imagePullPolicy: {{ .Values.git.pullPolicy | quote }}
          command:
            - /bin/sh
            - -c
            - |
              git clone {{ .Values.cloneFilesFromGit.repository }} --branch {{ .Values.cloneFilesFromGit.revision }} /app
          volumeMounts:
            - name: git-cloned-files
              mountPath: /app
        {{- end }}
        {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
        - name: volume-permissions
          image: "{{ template "mxnet.volumePermissions.image" . }}"
          imagePullPolicy: {{ default "" .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              chown -R {{ .Values.securityContext.runAsUser }}:{{ .Values.securityContext.fsGroup }} {{ .Values.persistence.mountPath }}
          securityContext:
            runAsUser: 0
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: {{ .Values.persistence.mountPath }}
        {{- end }}
      {{- end }}
      containers:
        - name: master
          image: {{ include "pytorch.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          command:
            - bash
            - -c
            - |
              {{- if .Values.entrypoint.file }}
              python {{ .Values.entrypoint.file }} {{ if .Values.entrypoint.args }}{{ .Values.entrypoint.args }}{{ end }}
              {{- end }}
              sleep infinity
          env:
            {{- if eq .Values.mode "distributed" }}
            - name: MASTER_ADDR
              value: "127.0.0.1"
            - name: MASTER_PORT
              value: {{ .Values.port | quote }}
            - name: WORLD_SIZE
              value: {{ .Values.worldSize | quote }}
            - name: RANK
              value: "0"
            {{- end }}
            {{- if .Values.extraEnvVars }}
            {{- toYaml .Values.extraEnvVars | nindent 12 }}
            {{- end }}
          ports:
            - name: pytorch
              containerPort: {{ .Values.port }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            exec:
              command:
                - python
                - -c
                - import torch; torch.__version__
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            exec:
              command:
                - python
                - -c
                - import torch; torch.__version__
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if .Values.configMap }}
            - name: ext-files
              mountPath: /app
            {{- else if .Files.Glob "files/*" }}
            - name: local-files
              mountPath: /app
            {{- else if .Values.cloneFilesFromGit.enabled }}
            - name: git-cloned-files
              mountPath: /app
            {{- end }}
            - name: data
              mountPath: {{ .Values.persistence.mountPath }}
      volumes:
        {{- if .Values.configMap }}
        - name: ext-files
          configMap:
            name: {{ .Values.configMap }}
        {{- else if .Files.Glob "files/*" }}
        - name: local-files
          configMap:
            name: {{ include "pytorch.fullname" . }}-files
        {{- else if .Values.cloneFilesFromGit.enabled }}
        - name: git-cloned-files
          emptyDir: {}
        {{- end }}
        - name: data
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ include "pytorch.fullname" . }}{{ if eq .Values.mode "distributed" }}-master{{ end }}
        {{- else }}
          emptyDir: {}
        {{- end }}
