{{- if eq .Values.mode "distributed" }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "common.names.fullname" . }}-server
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: server
spec:
  podManagementPolicy: {{ .Values.podManagementPolicy }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: server
  replicas: {{ .Values.server.replicaCount }}
  serviceName: {{ template "common.names.fullname" . }}-headless
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: server
    spec:
      {{- include "mxnet.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.server.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.server.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.server.podAffinityPreset "component" "server" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.server.podAntiAffinityPreset "component" "server" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.server.nodeAffinityPreset.type "key" .Values.server.nodeAffinityPreset.key "values" .Values.server.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.server.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.server.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.server.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.server.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.server.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.server.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) .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 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 }}
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.initContainers "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
        - name: mxnet
          workingDir: {{ .Values.entrypoint.workDir }}
          image: {{ include "mxnet.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - bash
            - -c
            - |
              SERVER_RANK=${POD_NAME##*-}
              ((SERVER_RANK++))
              export SERVER_RANK
              {{- include "mxnet.entrypoint" . | nindent 14 }}
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: DMLC_ROLE
              value: "server"
            - name: DMLC_NUM_WORKER
              value: {{ .Values.worker.replicaCount | quote }}
            - name: DMLC_NUM_SERVER
              value: {{ .Values.server.replicaCount | quote }}
            - name: DMLC_PS_ROOT_URI
              value: {{ include "common.names.fullname" . }}-scheduler
            - name: DMLC_PS_ROOT_PORT
              value: {{ .Values.scheduler.port | quote }}
            {{- if .Values.commonExtraEnvVars }}
            {{- include "mxnet.parseEnvVars" .Values.commonExtraEnvVars | nindent 12 }}
            {{- end }}
            {{- if .Values.server.extraEnvVars }}
            {{- include "mxnet.parseEnvVars" .Values.server.extraEnvVars | nindent 12 }}
            {{- end }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            exec:
              command:
                - python3
                - -c
                - import os; os.environ["DMLC_PS_ROOT_URI"] = "127.0.0.1"; os.environ["DMLC_ROLE"] = "worker"; import mxnet; print(mxnet.__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:
                - python3
                - -c
                - import os; os.environ["DMLC_PS_ROOT_URI"] = "127.0.0.1"; os.environ["DMLC_ROLE"] = "worker"; import mxnet; print(mxnet.__version__)
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.server.resources }}
          resources: {{- toYaml .Values.server.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 }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
            - name: data
              mountPath: {{ .Values.persistence.mountPath }}
            {{- if .Values.existingSecret }}
            - name: secret-data
              mountPath: /secrets
            {{- end }}
          {{- if .Values.sidecars }}
          {{- include "common.tplvalues.render" ( dict "value" .Values.sidecars "context" $) | nindent 8 }}
          {{- end }}
      volumes:
        {{- if .Values.existingSecret }}
        - name: secret-data
          secret:
            secretName: {{ .Values.existingSecret }}
        {{- end }}
        {{- if .Values.configMap }}
        - name: ext-files
          configMap:
            name: {{ .Values.configMap }}
        {{- else if .Files.Glob "files/*" }}
        - name: local-files
          configMap:
            name: {{ include "common.names.fullname" . }}-files
        {{- else if .Values.cloneFilesFromGit.enabled }}
        - name: git-cloned-files
          emptyDir: {}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- if not .Values.persistence.enabled }}
        - name: data
          emptyDir: {}
{{- else }}
  volumeClaimTemplates:
    - metadata:
        name: data
        labels: {{- include "common.labels.matchLabels" . | nindent 10 }}
        {{- if .Values.persistence.annotations }}
        annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.persistence.annotations "context" $) | nindent 10 }}
        {{- end }}
      spec:
        accessModes: {{- toYaml .Values.persistence.accessModes | nindent 10 }}
        {{- include "common.storage.class" (dict "persistence" .Values.persistence "global" .Values.global) | nindent 8 }}
        resources:
          requests:
            storage: {{ .Values.persistence.size | quote }}
{{- end }}
{{- end }}
