{{- if eq .Values.mode "standalone" }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "minio.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "minio.labels" . | nindent 4 }}
spec:
  {{- if .Values.deployment.updateStrategy }}
  strategy: {{ toYaml .Values.deployment.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "minio.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels: {{- include "minio.labels" . | nindent 8 }}
      {{- if or .Values.podAnnotations (include "minio.createSecret" .) }}
      annotations:
        {{- if (include "minio.createSecret" .) }}
        checksum/credentials-secret: {{ include (print $.Template.BasePath "/secrets.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.podAnnotations }}
        {{- include "minio.tplValue" ( dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName }}
      {{- end }}
      serviceAccountName: {{ template "minio.serviceAccountName" . }}
{{- include "minio.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.affinity }}
      affinity: {{- include "minio.tplValue" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "minio.tplValue" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "minio.tplValue" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
      {{- end }}
      {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
      initContainers:
        - name: volume-permissions
          image: {{ template "minio.volumePermissions.image" . }}
          imagePullPolicy: {{ default "" .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              chown -R {{ .Values.securityContext.runAsUser }}:{{ .Values.securityContext.fsGroup }} {{ .Values.persistence.mountPath }}
          securityContext:
            runAsUser: 0
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: {{ .Values.persistence.mountPath }}
      {{- end }}
      containers:
        - name: minio
          image: {{ include "minio.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.securityContext.enabled }}
          securityContext:
            runAsUser: {{ .Values.securityContext.runAsUser }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            - name: MINIO_FORCE_NEW_KEYS
              value: {{ ternary "yes" "no" .Values.forceNewKeys | quote }}
            {{- if .Values.useCredentialsFile }}
            - name: MINIO_ROOT_USER_FILE
              value: "/opt/bitnami/minio/secrets/access-key"
            {{- else }}
            - name: MINIO_ROOT_USER
              valueFrom:
                secretKeyRef:
                  name: {{ include "minio.secretName" . }}
                  key: access-key
            {{- end }}
            {{- if .Values.useCredentialsFile }}
            - name: MINIO_ROOT_PASSWORD_FILE
              value: "/opt/bitnami/minio/secrets/secret-key"
            {{- else }}
            - name: MINIO_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "minio.secretName" . }}
                  key: secret-key
            {{- end }}
            {{- if .Values.defaultBuckets }}
            - name: MINIO_DEFAULT_BUCKETS
              value: {{ .Values.defaultBuckets }}
            {{- end }}
            - name: MINIO_BROWSER
              value: {{ ternary "off" "on" .Values.disableWebUI | quote }}
            {{- if .Values.prometheusAuthType }}
            - name: MINIO_PROMETHEUS_AUTH_TYPE
              value: {{ .Values.prometheusAuthType }}
            {{- end }}
            {{- if .Values.extraEnv }}
            {{- toYaml .Values.extraEnv | nindent 12 }}
            {{- end }}
          {{ if .Values.command }}
          command: {{- toYaml .Values.command |  nindent 12 }} 
          {{- end }}
          ports:
            - name: minio
              containerPort: 9000
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
            httpGet:
              path: /minio/health/live
              port: minio
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
            tcpSocket:
              port: minio
          {{- end }}
          {{- if .Values.resources }}
          resources: {{ toYaml .Values.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if .Values.useCredentialsFile }}
            - name: minio-credentials
              mountPath: /opt/bitnami/minio/secrets/
            {{- end }}
            - name: "data"
              mountPath: {{ .Values.persistence.mountPath }}
      volumes:
        {{- if .Values.useCredentialsFile }}
        - name: minio-credentials
          secret:
            secretName: {{ include "minio.secretName" . }}
        {{- end }}
        - name: data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ if .Values.persistence.existingClaim }}{{ .Values.persistence.existingClaim }}{{- else }}{{ include "minio.fullname" . }}{{- end }}
          {{- else }}
          emptyDir: {}
          {{- end }}
 {{- end }}
