{{- if eq .Values.mode "distributed" }}
{{ $nodeCount := .Values.replicas | int }}
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: {{ template "minio.fullname" . }}
  labels:
    app: {{ template "minio.name" . }}
    chart: {{ template "minio.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  serviceName: {{ template "minio.fullname" . }}
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ template "minio.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      name: {{ template "minio.fullname" . }}
      labels:
        app: {{ template "minio.name" . }}
        release: {{ .Release.Name }}
    spec:
      {{- if .Values.priorityClassName }}
      priorityClassName: "{{ .Values.priorityClassName }}"
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: [ "/bin/sh", 
          "-ce", 
          "cp /tmp/config.json {{ .Values.configPath }} &&
          /usr/bin/docker-entrypoint.sh minio -C {{ .Values.configPath }} server
          {{- range $i := until $nodeCount }}
          http://{{ template `minio.fullname` $ }}-{{ $i }}.{{ template `minio.fullname` $ }}.{{ $.Release.Namespace }}.svc.cluster.local{{ $.Values.mountPath }}
          {{- end }}" ]
          volumeMounts:
            - name: export
              mountPath: {{ .Values.mountPath }}
              {{- if and .Values.persistence.enabled .Values.persistence.subPath }}
              subPath: "{{ .Values.persistence.subPath }}"
              {{- end }}
            - name: minio-server-config
              mountPath: "/tmp/config.json"
              subPath: config.json
            - name: minio-config-dir
              mountPath: {{ .Values.configPath }}
          ports:
            - name: service
              containerPort: 9000
          env:
            - name: MINIO_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ template "minio.fullname" . }}
                  key: accesskey
            - name: MINIO_SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ template "minio.fullname" . }}
                  key: secretkey
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
      volumes:
        - name: minio-user
          secret:
            secretName: {{ template "minio.fullname" . }}
        - name: minio-server-config
          configMap:
            name: {{ template "minio.fullname" . }}
        - name: minio-config-dir
          emptyDir: {}
  volumeClaimTemplates:
    - metadata:
        name: export
      spec:
        accessModes: [ {{ .Values.persistence.accessMode | quote }} ]
        {{- if .Values.persistence.storageClass }}
        storageClassName: {{ .Values.persistence.storageClass }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.persistence.size }}
{{- end }}
