{{- $logDir := .Values.config.rustfs.obs_log_directory }}

{{- if .Values.mode.distributed.enabled }}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "rustfs.fullname" . }}
  labels:
    {{- include "rustfs.labels" . | nindent 4 }}
    {{- with .Values.commonLabels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  serviceName: {{ include "rustfs.fullname" . }}-headless
  replicas: {{ .Values.replicaCount }}
  podManagementPolicy: Parallel
  selector:
    matchLabels:
      {{- include "rustfs.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "rustfs.selectorLabels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      enableServiceLinks: {{ .Values.enableServiceLinks }}
      {{- with include "chart.imagePullSecrets" . }}
      imagePullSecrets:
        {{- . | nindent 8 }}
      {{- end }}
      {{- if and .Values.nodeSelector (not .Values.affinity.nodeAffinity) }}
      nodeSelector:
        {{- toYaml .Values.nodeSelector | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity:
        nodeAffinity:
        {{- if .Values.affinity.nodeAffinity }}
          {{- toYaml .Values.affinity.nodeAffinity | nindent 10 }}
        {{- else }}
          {}
        {{- end }}
        {{- if .Values.affinity.podAntiAffinity.enabled }}
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: app.kubernetes.io/name
                    operator: In
                    values:
                      - {{ include "rustfs.name" . }}
              topologyKey: {{ .Values.affinity.podAntiAffinity.topologyKey }}
        {{- end }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations:
        {{- toYaml .Values.tolerations | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      {{- end }}
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml .Values.imagePullSecrets | nindent 8 }}
      {{- end }}
      initContainers:
        - name: init-step
          image: "{{ .Values.initStep.image.repository }}:{{ .Values.initStep.image.tag }}"
          imagePullPolicy: {{ .Values.initStep.image.pullPolicy }}
          securityContext:
            {{- toYaml .Values.initStep.containerSecurityContext | nindent 12 }}
          env:
            - name: REPLICA_COUNT
              value: {{ .Values.replicaCount | quote }}
          command:
            - sh
            - -c
            - |
              if [ "$REPLICA_COUNT" -eq 4 ]; then
                for i in $(seq 0 $(($REPLICA_COUNT - 1))); do
                  mkdir -p /data/rustfs$i
                done;
              elif [ "$REPLICA_COUNT" -eq 16 ]; then
                mkdir -p /data
              fi
              mkdir -p {{ $logDir }}
              chown 10001:10001 /data {{ $logDir }}
          volumeMounts:
            {{- if eq (int .Values.replicaCount) 4 }}
            {{- range $i := until (int .Values.replicaCount) }}
            - name: data-rustfs-{{ $i }}
              mountPath: /data/rustfs{{ $i }}
            {{- end }}
            {{- else if eq (int .Values.replicaCount) 16 }}
            - name: data
              mountPath: /data
            {{- end }}
            - name: logs
              mountPath: {{ $logDir }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          command: ["/usr/bin/rustfs"]
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.containerSecurityContext }}
          securityContext:
            {{- toYaml .Values.containerSecurityContext | nindent 12 }}
          {{- end }}
          ports:
            - name: endpoint
              containerPort: {{ .Values.service.endpoint.port }}
            - name: console
              containerPort: {{ .Values.service.console.port }}
          envFrom:
            - configMapRef:
                name: {{ include "rustfs.fullname" . }}-config
            - secretRef:
                name: {{ include "rustfs.secretName" . }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          livenessProbe:
            {{- toYaml .Values.livenessProbe | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.readinessProbe | nindent 12 }}
          volumeMounts:
            - name: tmp
              mountPath: /tmp
            - name: logs
              mountPath: {{ $logDir }}
            {{- if eq (int .Values.replicaCount) 4 }}
            {{- range $i := until (int .Values.replicaCount) }}
            - name: data-rustfs-{{ $i }}
              mountPath: /data/rustfs{{ $i }}
            {{- end }}
            {{- else if eq (int .Values.replicaCount) 16 }}
            - name: data
              mountPath: /data
            {{- end }}
      volumes:
        - name: tmp
          emptyDir: {}
  volumeClaimTemplates:
    - metadata:
        name: logs
        labels:
          {{- toYaml .Values.commonLabels | nindent 10 }}
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: {{ .Values.storageclass.name }}
        resources:
          requests:
            storage: {{ .Values.storageclass.logStorageSize }}
    {{- if eq (int .Values.replicaCount) 4 }}
    {{- range $i := until (int .Values.replicaCount) }}
    - metadata:
        name: data-rustfs-{{ $i }}
        labels:
          {{- toYaml $.Values.commonLabels | nindent 10 }}
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: {{ $.Values.storageclass.name }}
        resources:
          requests:
            storage: {{ $.Values.storageclass.dataStorageSize }}
    {{- end }}
    {{- else if eq (int .Values.replicaCount) 16 }}
    - metadata:
        name: data
        labels:
          {{- toYaml .Values.commonLabels | nindent 10 }}
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: {{ .Values.storageclass.name }}
        resources:
          requests:
            storage: {{ .Values.storageclass.dataStorageSize }}
    {{- end }}
{{- end }}
