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

{{/* Adding the helper in here for better readability */}}
{{- define "dremio.dremio-conf.executor.default" -}}
{{/* Common configuration */}}
{{- include "dremio.dremio-conf.common.default" . }}
{{/* Master coordinator settings */}}
services.coordinator.enabled: false
services.coordinator.master.enabled: false
services.coordinator.master.embedded-zookeeper.enabled: false
services.executor.enabled: true
{{- end }}

{{- /*
  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 := mustMergeOverwrite $.Values.executor.common $engine.overrides }}
{{- if not $executorValues.dremioConf.existingConfigmap }}
---
apiVersion: v1
kind: ConfigMap
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 }}
data:
  {{- $extraFiles := mustMergeOverwrite $.Values.dremio.dremioConf.extraFiles $executorValues.dremioConf.extraFiles }}
  {{- if not (hasKey $extraFiles "dremio.conf") }}
  {{- /* 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.executor.default" $ | 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.configOverrides "prefix" "" "context" $) | fromYaml -}}
  {{- $configOverridesFlat := include "dremio.dremio-conf.flattenYAML" (dict "config" $executorValues.dremioConf.configOverrides "prefix" "" "context" $) | fromYaml -}}
  {{- /* Override order: default configuration <- common overrides <- specific overrides */}}
  {{- $mergedConfiguration := mustMergeOverwrite $defaultConfigurationFlat $commonConfigOverridesFlat }}
  {{- $mergedConfiguration = mustMergeOverwrite $mergedConfiguration $configOverridesFlat }}
  {{- /* Adding this engine specific configuration as we cannot put a "define" inside a "range" */}}
  {{- $mergedConfiguration = mustMergeOverwrite $mergedConfiguration (dict "services.node-tag" $engine.name) }}
  dremio.conf: |
    {{- /* Apply the helper again to avoid arrays having a HOCON incompatible format */}}
    {{- 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 }}