# helm_charts_statefulset-worker.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ .Release.Name }}-worker
  labels:
    app: {{ template "distributed-tensorflow.name" . }}
    chart: {{ template "distributed-tensorflow.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    role: worker
spec:
  selector:
    matchLabels:
      app: {{ template "distributed-tensorflow.name" . }}
      release: {{ .Release.Name }}
      role: worker
  serviceName: {{ template "distributed-tensorflow.fullname" . }}-worker
  podManagementPolicy: {{ .Values.worker.podManagementPolicy }}
  replicas: {{.Values.worker.number}}
  template:
    metadata:
      labels:
        app: {{ template "distributed-tensorflow.name" . }}
        chart: {{ template "distributed-tensorflow.chart" . }}
        release: {{ .Release.Name }}
        heritage: {{ .Release.Service }}
        role: worker
    spec:
{{- if .Values.volumes }}
      volumes:
{{ toYaml .Values.volumes | indent  6 }}
{{- end }}
      containers:
      - name: worker
        image: "{{ .Values.worker.image.repository }}:{{ .Values.worker.image.tag }}"
        imagePullPolicy: {{ .Values.worker.image.pullPolicy }}
        command:
        - "python"
        - "train_distributed.py"
{{- if gt (int .Values.worker.gpuCount) 0 }}
        - --num_gpus
        - "{{ .Values.worker.gpuCount }}"
{{- end }}
{{- if .Values.hyperparams.learningrate }}
        - --learning_rate
        - "{{ .Values.hyperparams.learningrate }}"
{{- end }}
{{- if .Values.hyperparams.batchsize }}
        - --batch_size
        - "{{ .Values.hyperparams.batchsize }}"
{{- end }}
{{- if .Values.hyperparams.trainsteps }}
        - --train_steps
        - "{{ .Values.hyperparams.trainsteps }}"
{{- end }}
{{- if .Values.hyperparams.datadir }}
        - --data_dir
        - "{{ .Values.hyperparams.datadir }}"
{{- end }}
{{- if .Values.hyperparams.logdir }}
        - --log_dir
        - "{{ .Values.hyperparams.logdir }}"
{{- end }}
{{- if .Values.hyperparams.hiddenunits }}
        - --hidden_units
        - "{{ .Values.hyperparams.hiddenunits }}"
{{- end }}
        env:
        - name: WORKER_HOSTS
          valueFrom:
            configMapKeyRef:
              name: {{ template "distributed-tensorflow.fullname" . }}
              key: worker.hostList
        - name: PS_HOSTS
          valueFrom:
            configMapKeyRef:
              name: {{ template "distributed-tensorflow.fullname" . }}
              key: ps.hostList
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: JOB_NAME
          value: worker
       {{- if .Values.worker.env }}
       {{- range $key, $value := .Values.worker.env }}
        - name: "{{ $key }}"
          value: "{{ $value }}"
       {{- end }}
       {{- end }}
        ports:
        - containerPort: {{ .Values.worker.port }}
{{- if .Values.volumeMounts }}
        volumeMounts:
{{ toYaml .Values.volumeMounts | indent 10 }}
{{- end }}
{{- if gt (int .Values.worker.gpuCount) 0 }}
        resources:
          limits:
            nvidia.com/gpu: {{- .Values.worker.gpuCount }}
          request:
            nvidia.com/gpu: {{- .Values.worker.gpuCount }}
{{- end }}