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

{{- if .Values.mode.standalone.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "rustfs.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "rustfs.labels" . | nindent 4 }}
    {{- with .Values.commonLabels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  replicas: 1
  {{- with .Values.mode.standalone.strategy }}
  {{- $type := default "RollingUpdate" .type }}
  strategy:
    type: {{ $type }}
    {{- if and (eq $type "RollingUpdate") .rollingUpdate }}
    rollingUpdate:
      {{- toYaml .rollingUpdate | nindent 6 }}
    {{- end }}
  {{- end }}
  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 }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
        - name: init-step
          image: "{{ .Values.image.initImage.repository }}:{{ .Values.image.initImage.tag }}"
          imagePullPolicy: {{ .Values.image.initImage.pullPolicy }}
          securityContext:
            {{- toYaml .Values.containerSecurityContext | nindent 12 }}
          command:
            - sh
            - -c
            - |
              mkdir -p /data /mnt/rustfs/logs
              chmod 755 /mnt/rustfs/logs
          volumeMounts:
            - name: data
              mountPath: /data
            - name: logs
              mountPath: /mnt/rustfs
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.rustfs.repository }}:{{ .Values.image.rustfs.tag | default .Chart.AppVersion }}"
          command: ["/usr/bin/rustfs"]
          imagePullPolicy: {{ .Values.image.rustfs.pullPolicy }}
          securityContext:
            {{- toYaml .Values.containerSecurityContext | nindent 12 }}
          ports:
            - name: endpoint
              containerPort: {{ .Values.service.endpoint.port }}
            - name: console
              containerPort: {{ .Values.service.console.port }}
          {{- with .Values.extraEnv }}
          env:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          envFrom:
            - configMapRef:
                name: {{ include "rustfs.fullname" . }}-config
            - secretRef:
                name: {{ include "rustfs.secretName" . }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- include "rustfs.probes" . | nindent 10 }}
          volumeMounts:
            {{- if .Values.mtls.enabled }}
            - name: server-cert
              mountPath: /opt/tls/rustfs_cert.pem
              subPath: rustfs_cert.pem
            - name: server-cert
              mountPath: /opt/tls/rustfs_key.pem
              subPath: rustfs_key.pem
            - name: server-cert
              mountPath: /opt/tls/ca.crt
              subPath: ca.crt
            - name: client-cert
              mountPath: /opt/tls/client_cert.pem
              subPath: client_cert.pem 
            - name: client-cert
              mountPath: /opt/tls/client_key.pem
              subPath: client_key.pem
            - name: client-cert
              mountPath: /opt/tls/client_ca.crt
              subPath: client_ca.crt
            {{- end }}
            - name: logs
              mountPath: {{ $logDir }}
              subPath: logs
            - name: data
              mountPath: /data
      volumes:
      {{- if .Values.mtls.enabled }}
        - name: server-cert
          secret:
            secretName: rustfs-server-tls
            items:
              - key: tls.crt
                path: rustfs_cert.pem
              - key: tls.key
                path: rustfs_key.pem
              - key: ca.crt
                path: ca.crt
        - name: client-cert
          secret:
            secretName: rustfs-client-tls
            items:
              - key: tls.crt
                path: client_cert.pem
              - key: tls.key
                path: client_key.pem
              - key: ca.crt
                path: client_ca.crt
      {{- end }}
        - name: logs
          persistentVolumeClaim:
            {{- if .Values.mode.standalone.existingClaim.logsClaim }}
            claimName: {{ .Values.mode.standalone.existingClaim.logsClaim }}
            {{- else }}
            claimName: {{ include "rustfs.fullname" $ }}-logs
            {{- end }}
        - name: data
          persistentVolumeClaim:
            {{- if .Values.mode.standalone.existingClaim.dataClaim }}
            claimName: {{ .Values.mode.standalone.existingClaim.dataClaim }}
            {{- else }}
            claimName: {{ include "rustfs.fullname" $ }}-data
            {{- end }}
{{- end }}
