{{- $airflowSecretName := include "airflow.secretName" . -}}
{{- $postgresqlSecretName := include "airflow.postgresql.secretName" . -}}
{{- $redisSecretName := include "airflow.redis.secretName" . -}}
{{- $baseUrl := (include "airflow.baseUrl" .) -}}
{{- if not $baseUrl -}}
###############################################################################
### ERROR: You did not provide an external URL in your 'helm install' call ###
###############################################################################

This deployment will be incomplete until you configure Airflow with a resolvable
host. To configure Airflow with the URL of your service:

{{- if .Values.ingress.enabled }}

1. Get the Airflow URL indicated on the Ingress Rule and associate it to your cluster external IP:

  export CLUSTER_IP=$(minikube ip) # On Minikube. Use: `kubectl cluster-info` on others K8s clusters
  export AIRFLOW_HOST=$(kubectl get ingress --namespace {{ .Release.Namespace }} {{ include "common.names.fullname" . }} -o jsonpath='{.spec.rules[0].host}')
  echo "Airflow URL: http://$AIRFLOW_HOST/"
  echo "$CLUSTER_IP  $AIRFLOW_HOST" | sudo tee -a /etc/hosts

{{- else }}
1. Get the Airflow URL by running:

  {{- if eq "NodePort" .Values.service.type }}

  export AIRFLOW_HOST=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
  export AIRFLOW_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "common.names.fullname" . }})

  {{- else if eq "ClusterIP" .Values.service.type }}

  export AIRFLOW_HOST=127.0.0.1
  export AIRFLOW_PORT=8080

  {{- else if eq "LoadBalancer" .Values.service.type }}

  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        Watch the status with: 'kubectl get svc --namespace {{ .Release.Namespace }} -w {{ include "common.names.fullname" . }}'

  export AIRFLOW_HOST=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "common.names.fullname" . }} --template "{{ "{{ range (index .status.loadBalancer.ingress 0) }}{{ . }}{{ end }}" }}")
  export AIRFLOW_PORT=80
  {{- end }}
{{- end }}
  {{ include "common.utils.secret.getvalue" (dict "secret" $airflowSecretName "field" "airflow-password" "context" $) }}
  {{ include "common.utils.secret.getvalue" (dict "secret" $airflowSecretName "field" "airflow-fernetKey" "context" $) }}
  {{ include "common.utils.secret.getvalue" (dict "secret" $postgresqlSecretName "field" "postgresql-password" "context" $) }}
  {{ include "common.utils.secret.getvalue" (dict "secret" $redisSecretName "field" "redis-password" "context" $) }}

2. Complete your Airflow deployment by running:

  helm upgrade {{ .Release.Name }} bitnami/{{ .Chart.Name }} \
    --set service.type={{ .Values.service.type }} \
    --set web.baseUrl=http://$AIRFLOW_HOST:$AIRFLOW_PORT \
    --set auth.password=$AIRFLOW_PASSWORD \
    --set auth.fernetKey=$AIRFLOW_FERNETKEY \
    --set postgresql.postgresqlPassword=$POSTGRESQL_PASSWORD \
    --set redis.password=$REDIS_PASSWORD

{{- else -}}

1. Get the Airflow URL by running:

{{- if .Values.ingress.enabled }}

  echo URL  : {{ $baseUrl }}

{{- else if eq .Values.service.type "ClusterIP" }}

  echo URL  : http://127.0.0.1:{{ default "8080" .Values.service.port }}
  kubectl port-forward --namespace {{ .Release.Namespace }} svc/{{ include "common.names.fullname" . }} {{ default "8080" .Values.service.port }}:8080

{{- else if eq .Values.service.type "NodePort" }}
  export AIRFLOW_HOST=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
  export AIRFLOW_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "common.names.fullname" . }})

  echo URL  : http://$AIRFLOW_HOST:$AIRFLOW_PORT

{{- else }}

  echo URL  : {{ $baseUrl }}
{{- end }}

2. Get your Airflow login credentials by running:
  {{ include "common.utils.secret.getvalue" (dict "secret" $airflowSecretName "field" "airflow-password" "context" $) }}
  echo User:     {{ .Values.auth.username }}
  echo Password: $AIRFLOW_PASSWORD

{{- end  }}

{{ include "airflow.validateValues" . }}
{{ include "airflow.checkRollingTags" . }}

{{- $passwordValidationErrors := list -}}
{{- if not .Values.auth.existingSecret -}}
    {{- $requiredAirflowPassword := dict "valueKey" "auth.password" "secret" $airflowSecretName "field" "airflow-password" "context" $ -}}
    {{- $requiredAirflowPasswordError := include "common.validations.values.single.empty" $requiredAirflowPassword -}}
    {{- $passwordValidationErrors = append $passwordValidationErrors $requiredAirflowPasswordError -}}

    {{- $requiredFernetKeyPassword := dict "valueKey" "auth.fernetKey" "secret" $airflowSecretName "field" "airflow-fernetKey" "context" $ -}}
    {{- $requiredFernetKeyPasswordError := include "common.validations.values.single.empty" $requiredFernetKeyPassword -}}
    {{- $passwordValidationErrors = append $passwordValidationErrors $requiredFernetKeyPasswordError -}}
{{- end -}}

{{- $postgresqlPasswordValidationErrors := include "common.validations.values.postgresql.passwords" (dict "secret" $postgresqlSecretName "subchart" true "context" $) -}}
{{- $passwordValidationErrors = append $passwordValidationErrors $postgresqlPasswordValidationErrors -}}

{{- $redisPasswordValidationErrors := include "common.validations.values.redis.passwords" (dict "secret" $redisSecretName "subchart" true "context" $) -}}
{{- $passwordValidationErrors = append $passwordValidationErrors $redisPasswordValidationErrors -}}

{{- include "common.errors.upgrade.passwords.empty" (dict "validationErrors" $passwordValidationErrors "context" $) -}}
