{{- if .Values.mode.standalone.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "rustfs.fullname" . }}
  labels:
    {{- include "rustfs.labels" . | nindent 4 }}
    {{- with .Values.commonLabels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  replicas: 1
  selector:
    matchLabels:
      {{- include "rustfs.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "rustfs.selectorLabels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- 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:
        {{- if .Values.affinity.nodeAffinity }}
        nodeAffinity:
        {{- toYaml .Values.affinity.nodeAffinity | nindent 10 }}
        {{- 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 }}
      {{- 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 }}
          command:
            - sh
            - -c
            - |
              mkdir -p /data /logs
              chown 10001:10001 /data /logs
          volumeMounts:
            - name: data
              mountPath: /data
            - name: logs
              mountPath: /logs
      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:
            httpGet:
              path: /health
              port: 9000
            initialDelaySeconds: 10
            periodSeconds: 5
            timeoutSeconds: 3
            successThreshold: 1
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /health
              port: 9000
            initialDelaySeconds: 30
            periodSeconds: 5
            timeoutSeconds: 3
            successThreshold: 1
            failureThreshold: 3
          volumeMounts:
            - name: logs
              mountPath: /logs
            - name: data
              mountPath: /data
            - name: tmp
              mountPath: /tmp
      volumes:
        - name: tmp
          emptyDir: {}
        - name: logs
          persistentVolumeClaim:
            claimName: {{ include "rustfs.fullname" . }}-logs
        - name: data
          persistentVolumeClaim:
            claimName: {{ include "rustfs.fullname" . }}-data
{{- end }}
