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

{{- /*
  Dremio supports different executor groups called "engines".
  These are defined in executor.engines and all the executor-specific k8s objects are created per engine.
  Each engine contains an "overrides" section which allows replacing the common executor configuration
  set in executor.common. This logic is inspired in upstream Dremio chart.

  https://docs.dremio.com/cloud/sonar/workloads/engines/
 */}}
{{- range $engine := .Values.executor.engines }}
{{- $executorValues := deepCopy $.Values.executor.common }}
{{- $executorValues = mustMergeOverwrite $executorValues $engine.overrides }}
{{- $extraFiles := mustMergeOverwrite $.Values.dremio.dremioConf.extraSecretFiles $executorValues.dremioConf.extraSecretFiles }}
{{- $createDremioConf := or $.Values.dremio.dremioConf.secretConfigOverrides $executorValues.dremioConf.secretConfigOverrides $.Values.dremio.tls.enabled }}
{{- if and (not $executorValues.dremioConf.existingSecret) (or $createDremioConf $extraFiles) }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-dremio-conf" (include "dremio.executor.fullname" (dict "context" $ "engine" $engine.name)) | 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: executor
    {{- /* We need an extra label for the engine */}}
    engine: {{ $engine.name }}
  {{- 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" $executorValues.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 }}
{{- end }}