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

{{- $extraFiles := mustMergeOverwrite .Values.dremio.dremioConf.extraSecretFiles .Values.masterCoordinator.dremioConf.extraSecretFiles }}
{{- $createDremioConf := or .Values.dremio.dremioConf.secretConfigOverrides .Values.masterCoordinator.dremioConf.secretConfigOverrides .Values.dremio.tls.enabled }}
{{- if and (not .Values.masterCoordinator.dremioConf.existingSecret) (or $createDremioConf $extraFiles) }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-dremio-conf" (include "dremio.master-coordinator.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
    app.kubernetes.io/component: master-coordinator
    {{- /* We need an extra label that applies to both master and non-master coordinators. Taken from upstream dremio chart */}}
    app: dremio-coordinator
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
{{- /* We use stringData to simplify the templating of this file. It is as insecure as using base64 */}}
stringData:
  {{- if and (not (hasKey $extraFiles "dremio.conf")) $createDremioConf }}
  {{- /* dremio.conf uses HOCON, in which flattened YAML is a subset. So converting all values to flattened YAML generates a valid HOCON configuration */}}
  {{- $defaultConfiguration := include "dremio.dremio-conf.common.defaultSecret" . | fromYaml -}}
  {{- /* Obtain first the flattened version of all YAML content so the merge works as expected */}}
  {{- $defaultConfigurationFlat := include "dremio.dremio-conf.flattenYAML" (dict "config" $defaultConfiguration "prefix" "" "context" $) | fromYaml -}}
  {{- $commonConfigOverridesFlat := include "dremio.dremio-conf.flattenYAML" (dict "config" .Values.dremio.dremioConf.secretConfigOverrides "prefix" "" "context" $) | fromYaml -}}
  {{- $configOverridesFlat := include "dremio.dremio-conf.flattenYAML" (dict "config" .Values.masterCoordinator.dremioConf.secretConfigOverrides "prefix" "" "context" $) | fromYaml -}}
  {{- /* Override order: default configuration <- common overrides <- specific overrides */}}
  {{- $mergedConfiguration := mustMergeOverwrite $defaultConfigurationFlat $commonConfigOverridesFlat }}
  {{- $mergedConfiguration = mustMergeOverwrite $mergedConfiguration $configOverridesFlat }}
  {{- $result := include "dremio.dremio-conf.flattenYAML" (dict "config" $mergedConfiguration "prefix" "" "context" $) }}
  {{- /* Apply the helper again to avoid arrays having a HOCON incompatible format */}}
  dremio.conf: |
    {{- include "dremio.dremio-conf.flattenYAML" (dict "config" $mergedConfiguration "prefix" "" "context" $) | nindent 4 }}
  {{- end }}
  {{- if $extraFiles }}
  {{- include "common.tplvalues.render" (dict "value" $extraFiles "context" $) | nindent 2 }}
  {{- end }}
{{- end }}