{{- if .Values.minio_local }}
---
kind: Secret
apiVersion: v1
metadata:
  name: minio-auth
  namespace: {{ .Release.Namespace }}

type: Opaque
stringData:
 {{- with (first .Values.storages) }}
  MINIO_ROOT_USER: "{{ .access_key }}"
  MINIO_ROOT_PASSWORD: "{{ .secret_key }}"

  MC_HOST: "{{ $.Values.minio_scheme }}://{{ .access_key }}:{{ .secret_key }}@{{ $.Values.minio_host }}"
{{- end }}

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: minio-storage-pvc
  annotations:
    "helm.sh/resource-policy": keep
spec:
  accessModes:
    - ReadWriteOnce

  resources:
    requests:
      storage: 5Gi

  {{- if .Values.volume_storage_class }}
  storageClassName: {{ .Values.volume_storage_class }}
  {{- end }}


---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: local-minio
  namespace: {{ .Release.Namespace }}
spec:
  selector:
    matchLabels:
      app: local-minio

  replicas: 1
  template:
    metadata:
      labels:
        app: local-minio

    spec:
      {{- if .Values.main_node_type }}
      nodeSelector:
        nodeType: {{ .Values.main_node_type }}
      {{- end }}

      volumes:
        - name: data-minio
          persistentVolumeClaim:
            claimName: minio-storage-pvc

      initContainers:
        - name: init-bucket
          image: {{ .Values.minio_image }}
          imagePullPolicy: {{ .Values.minio_pull_policy }}

          command:
              - sh
              - -c
              - |
                  mkdir -p /data/{{ .Values.minio_local_bucket_name }}
                  mkdir -p /data/.minio.sys
                  echo '{"version":"1","format":"fs","id":"btrix-data-fs","fs":{"version":"2"}}' > /data/.minio.sys/format.json

          volumeMounts:
            - name: data-minio
              mountPath: /data
              subPath: minio

          resources:
            limits:
              memory: {{ .Values.minio_memory }}

            requests:
              cpu: {{ .Values.minio_cpu }}
              memory: {{ .Values.minio_memory }}

      containers:
        - name: minio
          image: {{ .Values.minio_image }}
          imagePullPolicy: {{ .Values.minio_pull_policy }}
          args: ["server", "/data", "--console-address", ":9001"]
          envFrom:
            - secretRef:
                name: minio-auth

          volumeMounts:
            - name: data-minio
              mountPath: /data
              subPath: minio

          resources:
            limits:
              memory: {{ .Values.minio_memory }}

            requests:
              cpu: {{ .Values.minio_cpu }}
              memory: {{ .Values.minio_memory }}

---
apiVersion: v1
kind: Service

metadata:
  namespace: {{ .Release.Namespace }}
  name: local-minio
  labels:
    app: local-minio

spec:
  {{- if .Values.minio_local_access_port }}
  type: NodePort
  {{- end }}
  selector:
    app: local-minio

  ports:
    - protocol: TCP
      port: 9000
      {{- if .Values.minio_local_access_port }}
      nodePort: {{ .Values.minio_local_access_port }}
      {{- end }}
      name: minio

{{- if .Values.minio_local_console_port }}
---
apiVersion: v1
kind: Service

metadata:
  namespace: {{ .Release.Namespace }}
  name: local-minio-console
  labels:
    app: local-minio-console

spec:
  type: NodePort
  selector:
    app: local-minio

  ports:
    - protocol: TCP
      port: 9001
      nodePort: {{ .Values.minio_local_console_port }}
      name: minio-console
{{- end }}

{{- end }}


