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

{{- if include "kafka.createCertificate" . }}
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: {{ printf "%s-crt" (include "common.names.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: kafka
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  secretName: {{ include "kafka.tlsSecretName" . }}
  commonName: {{ printf "%s.%s.svc.%s" (include "common.names.fullname" .) (include "common.names.namespace" .) .Values.clusterDomain }}
  issuerRef:
    name: {{ printf "%s-ca-issuer" (include "common.names.fullname" .) }}
    kind: Issuer
  subject:
    organizations:
      - "Kafka"
  dnsNames:
    {{- $controllerSvcName := printf "%s-headless" (include "kafka.controller.fullname" .) | trunc 63 | trimSuffix "-" }}
    {{- $brokerSvcName := printf "%s-headless" (include "kafka.broker.fullname" .) | trunc 63 | trimSuffix "-" }}
    - '*.{{ include "common.names.namespace" . }}'
    - '*.{{ include "common.names.namespace" . }}.svc'
    - '*.{{ include "common.names.namespace" . }}.svc.{{ .Values.clusterDomain }}'
    - '*.{{ $controllerSvcName }}'
    - '*.{{ $controllerSvcName }}.{{ include "common.names.namespace" . }}'
    - '*.{{ $controllerSvcName }}.{{ include "common.names.namespace" . }}.svc'
    - '*.{{ $controllerSvcName }}.{{ include "common.names.namespace" . }}.svc.{{ .Values.clusterDomain }}'
    - '*.{{ $brokerSvcName }}'
    - '*.{{ $brokerSvcName }}.{{ include "common.names.namespace" . }}'
    - '*.{{ $brokerSvcName }}.{{ include "common.names.namespace" . }}.svc'
    - '*.{{ $brokerSvcName }}.{{ include "common.names.namespace" . }}.svc.{{ .Values.clusterDomain }}'
    {{- if .Values.externalAccess.enabled -}}
    {{- with .Values.externalAccess.broker.service.domain }}
    - '*.{{ . }}'
    {{- end }}
    {{- with .Values.externalAccess.controller.service.domain }}
    - '*.{{ . }}'
    {{- end }}
    {{- end }}
    {{- range .Values.tls.autoGenerated.customAltNames }}
    - '{{ . }}'
    {{- end }}
  privateKey:
    algorithm: {{ .Values.tls.autoGenerated.certManager.keyAlgorithm }}
    size: {{ int .Values.tls.autoGenerated.certManager.keySize }}
  duration: {{ .Values.tls.autoGenerated.certManager.duration }}
  renewBefore: {{ .Values.tls.autoGenerated.certManager.renewBefore }}
{{- end }}
