{{- /*
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 "cert-manager") }}
{{- if empty .Values.web.tls.autoGenerated.certManager.existingIssuer }}
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  name: {{ printf "%s-clusterissuer" (include "airflow.web.fullname" .) }}
  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 }}
spec:
  selfSigned: {}
---
{{- end }}
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: {{ printf "%s-ca-crt" (include "airflow.web.fullname" .) }}
  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 }}
spec:
  secretName: {{ printf "%s-ca-crt" (include "airflow.web.fullname" .) }}
  commonName: {{ printf "%s-root-ca" (include "airflow.web.fullname" .) }}
  isCA: true
  issuerRef:
    name: {{ default (printf "%s-clusterissuer" (include "airflow.web.fullname" .)) .Values.web.tls.autoGenerated.certManager.existingIssuer }}
    kind: {{ default "Issuer" .Values.web.tls.autoGenerated.certManager.existingIssuerKind }}
---
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  name: {{ printf "%s-ca-issuer" (include "airflow.web.fullname" .) }}
  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 }}
spec:
  ca:
    secretName: {{ printf "%s-ca-crt" (include "airflow.web.fullname" .) }}
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: {{ printf "%s-crt" (include "airflow.web.fullname" .) }}
  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 }}
spec:
  secretName: {{ template "airflow.web.tls.secretName" . }}
  commonName: {{ printf "%s.%s.svc.%s" (include "airflow.web.fullname" .) (include "common.names.namespace" .) .Values.clusterDomain }}
  issuerRef:
    name: {{ printf "%s-ca-issuer" (include "common.names.fullname" .) }}
    kind: Issuer
  subject:
    organizations:
      - "KeyDB"
  dnsNames:
    - '*.{{ include "common.names.namespace" . }}'
    - '*.{{ include "common.names.namespace" . }}.svc'
    - '*.{{ include "common.names.namespace" . }}.svc.{{ .Values.clusterDomain }}'
    - '*.{{ include "airflow.web.fullname" . }}'
    - '*.{{ include "airflow.web.fullname" . }}.{{ include "common.names.namespace" . }}'
    - '*.{{ include "airflow.web.fullname" . }}.{{ include "common.names.namespace" . }}.svc'
    - '*.{{ include "airflow.web.fullname" . }}.{{ include "common.names.namespace" . }}.svc.{{ .Values.clusterDomain }}'
  privateKey:
    algorithm: {{ .Values.web.tls.autoGenerated.certManager.keyAlgorithm }}
    size: {{ int .Values.web.tls.autoGenerated.certManager.keySize }}
  duration: {{ .Values.web.tls.autoGenerated.certManager.duration }}
  renewBefore: {{ .Values.web.tls.autoGenerated.certManager.renewBefore }}
{{- end }}
