{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if not .Values.dremio.coreSite.existingSecret }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-core-site" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: dremio
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
stringData:
  core-site.xml: |
    {{- /* There are values that depend on other secrets, so we make them relative to env vars. These will be rendered using an init container */}}
    <?xml version="1.0"?>
    <configuration>
      {{- if or (eq .Values.dremio.distStorageType "minio") (eq .Values.dremio.distStorageType "aws") }}
      <property>
        <name>fs.dremioS3.impl</name>
        <description>The FileSystem implementation. Must be set to com.dremio.plugins.s3.store.S3FileSystem</description>
        <value>com.dremio.plugins.s3.store.S3FileSystem</value>
      </property>
      <property>
          <name>fs.s3a.connection.ssl.enabled</name>
          <description>Value can either be true or false, set to true to use SSL with a secure Minio server.</description>
          <value>{{ ternary "true" "false" (eq (include "dremio.s3.protocol" .) "https") }}</value>
      </property>
      {{- if or (eq .Values.dremio.distStorageType "minio") (eq .Values.externalS3.authentication "accesskey") }}
      <property>
        <name>fs.s3a.aws.credentials.provider</name>
        <description>The credential provider type.</description>
        <value>org.apache.hadoop.fs.s3a.SimpleAWSCredentialsProvider</value>
      </property>
      <property>
        <name>fs.s3a.access.key</name>
        <description>AWS access key ID.</description>
        <value>{{ print "{{ DREMIO_AWS_ACCESS_KEY_ID }}" }}</value>
      </property>
      <property>
        <name>fs.s3a.secret.key</name>
        <description>AWS secret key.</description>
        <value>{{ print "{{ DREMIO_AWS_SECRET_ACCESS_KEY }}" }}</value>
      </property>
      {{- end }}
      {{- if eq .Values.dremio.distStorageType "minio" }}
      {{- /* MinIO specific settings */}}
      <property>
        <name>fs.s3a.endpoint</name>
        <value>{{ include "dremio.s3.host" . }}:{{ include "dremio.s3.port" . }}</value>
      </property>
      <property>
        <name>fs.s3a.path.style.access</name>
        <value>true</value>
      </property>
      <property>
        <name>dremio.s3.compat</name>
        <value>true</value>
      </property>
      {{- else }}
      {{- /* AWS specific settings */}}
      <property>
        <name>fs.s3a.endpoint</name>
        <value>{{ include "dremio.s3.host" . }}</value>
      </property>
      <property>
        <name>fs.s3a.endpoint.region</name>
        <value>{{ include "dremio.s3.region" . }}</value>
      </property>
      {{- end }}
      {{- end }}
      {{- if .Values.dremio.coreSite.appendConfiguration }}
      {{- include "common.tplvalues.render" (dict "value" .Values.dremio.coreSite.appendConfiguration "context" $) | nindent 6 }}
      {{- end }}
    </configuration>
{{- end }}