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

{{/*
Return the Airflow common configuration.
ref: https://airflow.apache.org/docs/apache-airflow/stable/configurations-ref.html
*/}}
{{- define "airflow.configuration" -}}
{{- if .Values.configuration }}
{{- include "common.tplvalues.render" (dict "value" .Values.configuration "context" .) }}
{{- else }}
core:
  load_examples: {{ ternary "True" "False" .Values.loadExamples | squote }}
  executor: {{ .Values.executor | quote }}
  {{- if (include "airflow.isImageMajorVersion3" .) }}
  execution_api_server_url: {{ printf "http://%s:%s/execution/" (include "airflow.web.fullname" .) (.Values.service.ports.http | toString)  | quote }}
  {{- end }}
logging:
  colored_console_log: 'False'
metrics:
  statsd_on: {{ ternary "True" "False" (.Values.metrics.enabled) | squote }}
  statsd_port: {{ .Values.metrics.service.ports.ingest | quote }}
  statsd_prefix: airflow
  statsd_host: {{ include "airflow.metrics.fullname" . | quote }}
scheduler:
  standalone_dag_processor: {{ ternary "True" "False" .Values.dagProcessor.enabled | squote }}
triggerer:
  {{- if (include "airflow.isImageMajorVersion2" .) }}
  default_capacity: {{ .Values.triggerer.defaultCapacity | quote }}
  {{- else }}
  capacity: {{ .Values.triggerer.defaultCapacity | quote }}
  {{- end }}
webserver:
  base_url: {{ include "airflow.baseUrl" . | quote }}
  enable_proxy_fix: {{ ternary "True" "False" (and .Values.ingress.enabled .Values.ingress.tls) | squote }}
  {{- if .Values.web.tls.enabled }}
  web_server_ssl_cert: "/opt/bitnami/airflow/certs/tls.crt"
  web_server_ssl_key: "/opt/bitnami/airflow/certs/tls.key"
  {{- end }}
  {{- if (include "airflow.isImageMajorVersion2" .) }}
  web_server_port: {{ .Values.web.containerPorts.http | quote }}
  {{- end }}
{{- if (include "airflow.isImageMajorVersion3" .) }}
api:
  base_url: {{ include "airflow.baseUrl" . | quote }}
  port: {{ .Values.web.containerPorts.http | quote }}
{{- end }}
{{- if contains "KubernetesExecutor" .Values.executor }}
kubernetes_executor:
  namespace: {{ include "common.names.namespace" . | quote }}
  worker_container_repository: {{ printf "%s/%s" .Values.image.registry .Values.image.repository | quote }}
  worker_container_tag: {{ .Values.image.tag | quote }}
  delete_worker_pods: 'True'
  delete_worker_pods_on_failure: 'True'
  pod_template_file: "/opt/bitnami/airflow/config/pod_template.yaml"
{{- end }}
{{- end }}
{{- end }}

{{- if not .Values.existingConfigmap }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" (dict "value" .Values.commonAnnotations "context" $) | nindent 4 }}
  {{- end }}
data:
  {{- $configuration := include "airflow.configuration" . | fromYaml -}}
  {{- if .Values.overrideConfiguration }}
  {{- $overrideConfiguration := include "common.tplvalues.render" (dict "value" .Values.overrideConfiguration "context" .) | fromYaml }}
  {{- $configuration = mustMergeOverwrite $configuration $overrideConfiguration }}
  {{- end }}
  airflow.cfg: |-
    {{- range $section, $settings := $configuration }}
    [{{ $section }}]
    {{- range $key, $val := $settings }}
    {{ $key }} = {{ $val }}
    {{- end }}
    {{- end }}
  {{- if .Values.localSettings }}
  airflow_local_settings.py: |-
    {{- include "common.tplvalues.render" (dict "value" .Values.localSettings "context" .) | nindent 4 }}
  {{- end }}
{{- end }}
