{{- if .Values.gateway.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.gateway.autoscaling.enabled }}
  replicas: {{ .Values.gateway.replicaCount }}
  {{- end }}
  {{- if .Values.gateway.updateStrategy }}
  strategy: {{- toYaml .Values.gateway.updateStrategy | nindent 4 }}
  {{- else }}
  strategy: {{- toYaml .Values.deployment.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        {{- if .Values.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
        {{- end }}
      {{- 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 "common.tplvalues.render" ( dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
      {{- include "minio.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName }}
      {{- end }}
      serviceAccountName: {{ template "minio.serviceAccountName" . }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.gateway.priorityClassName }}
      priorityClassName: {{ .Values.gateway.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: minio
          image: {{ include "minio.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          command:
            {{- if .Values.command }}
            {{- include "common.tplvalues.render" (dict "value" .Values.command "context" $) | nindent 12 }}
            {{- else }}
            - minio
            {{- end }}
          args:
            {{- if .Values.args }}
            {{- include "common.tplvalues.render" (dict "value" .Values.args "context" $) | nindent 12 }}
            {{- else }}
            - --certs-dir
            - {{ default "/opt/bitnami/minio/certs" .Values.tls.mountPath }}
            - gateway
            - {{ .Values.gateway.type }}
            - --console-address
            - :{{ .Values.containerPorts.console }}
            {{- if eq .Values.gateway.type "gcs" }}
            - {{ .Values.gateway.auth.gcs.projectID }}
            {{- else if eq .Values.gateway.type "nas" }}
            - {{ .Values.persistence.mountPath }}
            {{- else if eq .Values.gateway.type "s3" }}
            - {{ .Values.gateway.auth.s3.serviceEndpoint }}
            {{- else if and (eq .Values.gateway.type "azure") .Values.gateway.auth.azure.serviceEndpoint }}
            - {{ .Values.gateway.auth.azure.serviceEndpoint }}
            {{- end }}
            {{- end }}
          env:
            {{- if eq .Values.gateway.type "azure" }}
            - name: AZURE_STORAGE_ACCOUNT
              valueFrom:
                secretKeyRef:
                {{- if not ( or (empty .Values.gateway.auth.azure.storageAccountNameExistingSecret) (empty .Values.gateway.auth.azure.storageAccountNameExistingSecretKey)) }}
                  name: {{ .Values.gateway.auth.azure.storageAccountNameExistingSecret }}
                  key: {{ .Values.gateway.auth.azure.storageAccountNameExistingSecretKey }}
                {{- else }}
                  name: {{ include "minio.secretName" . }}
                  key: azure-storage-account-name
                {{- end }}
            - name: AZURE_STORAGE_KEY
              valueFrom:
                secretKeyRef:
                {{- if not ( or (empty .Values.gateway.auth.azure.storageAccountNameExistingSecret) (empty .Values.gateway.auth.azure.storageAccountNameExistingSecretKey)) }}
                  name: {{ .Values.gateway.auth.azure.storageAccountKeyExistingSecret }}
                  key: {{ .Values.gateway.auth.azure.storageAccountKeyExistingSecretKey }}
                {{- else }}
                  name: {{ include "minio.secretName" . }}
                  key: azure-storage-account-key
                {{- end }}
            {{- else if and (eq .Values.gateway.type "gcs") .Values.gateway.auth.gcs.keyJSON }}
            - name: GOOGLE_APPLICATION_CREDENTIALS
              value: "/opt/bitnami/minio/secrets/key.json"
            {{- end }}
            - name: MINIO_ROOT_USER
              valueFrom:
                secretKeyRef:
                  name: {{ include "minio.secretName" . }}
                  key: root-user
            - name: MINIO_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "minio.secretName" . }}
                  key: root-password
            - name: MINIO_PROMETHEUS_AUTH_TYPE
              value: {{ .Values.metrics.prometheusAuthType | quote }}
            - name: MINIO_CONSOLE_PORT_NUMBER
              value: {{ .Values.containerPorts.console | quote }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: minio-api
              containerPort: {{ .Values.containerPorts.api }}
              protocol: TCP
            - name: minio-console
              containerPort: {{ .Values.containerPorts.console }}
              protocol: TCP
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /minio/health/live
              port: minio-api
              scheme: {{ ternary "HTTPS" "HTTP" .Values.tls.enabled | quote }}
          {{- else if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: minio-api
          {{- else if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: minio-console
          {{- else if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if .Values.tls.enabled }}
            - name: minio-certs
              mountPath: {{ default "/opt/bitnami/minio/certs" .Values.tls.mountPath }}
            {{- end }}
            {{- if and (eq .Values.gateway.type "gcs") .Values.gateway.auth.gcs.keyJSON }}
            - name: minio-credentials
              mountPath: /opt/bitnami/minio/secrets/
              readOnly: true
            {{- end }}
            {{- if eq .Values.gateway.type "nas" }}
            - name: data
              mountPath: {{ .Values.persistence.mountPath }}
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if and (eq .Values.gateway.type "gcs") .Values.gateway.auth.gcs.keyJSON }}
        - name: minio-credentials
          secret:
            secretName: {{ include "minio.secretName" . }}
        {{- end }}
        {{- if eq .Values.gateway.type "nas" }}
        - name: data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ include "minio.claimName" . }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        {{- end }}
        {{- if .Values.tls.enabled }}
        - name: minio-certs
          secret:
            secretName: {{ include "minio.tlsSecretName" . }}
            items:
            - key: tls.crt
              path: public.crt
            - key: tls.key
              path: private.key
            - key: ca.crt
              path: CAs/public.crt
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
