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

{{- if and .Values.web.tls.enabled .Values.web.tls.autoGenerated.enabled (eq .Values.web.tls.autoGenerated.engine "helm") -}}
{{- $ca := genCA "airflow-web-ca" 365 }}
{{- $releaseNamespace := include "common.names.namespace" . }}
{{- $clusterDomain := .Values.clusterDomain }}
{{- $fullname := include "airflow.web.fullname" . }}
{{- $altNames := list (printf "%s.%s.svc.%s" $fullname $releaseNamespace $clusterDomain) $fullname "127.0.0.1" "localhost" }}
{{- $cert := genSignedCert $fullname nil $altNames 365 $ca }}
{{- $secretName := include "airflow.web.tls.secretName" . }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $secretName }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: web
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  ca.crt: {{ include "common.secrets.lookup" (dict "secret" $secretName "key" "ca.crt" "defaultValue" $ca.Cert "context" $) }}
  tls.crt: {{ include "common.secrets.lookup" (dict "secret" $secretName "key" "tls.crt" "defaultValue" $cert.Cert "context" $) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $secretName "key" "tls.key" "defaultValue" $cert.Key "context" $) }}
{{- else if and .Values.web.tls.enabled (not .Values.web.tls.autoGenerated.enabled) (empty .Values.web.tls.existingCASecret) (empty .Values.web.tls.existingSecret) -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "airflow.web.tls.secretName" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: web
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  ca.crt: {{ required "A valid .Values.web.tls.ca entry required!" .Values.web.tls.ca | b64enc | quote }}
  tls.crt: {{ required "A valid .Values.web.tls.cert entry required!" .Values.web.tls.cert | b64enc | quote }}
  tls.key: {{ required "A valid .Values.web.tls.key entry required!" .Values.web.tls.key | b64enc | quote }}
{{- end }}
