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

{{- if and (eq .Values.architecture "replication") .Values.tls.enabled .Values.tls.autoGenerated.enabled (eq .Values.tls.autoGenerated.engine "cert-manager") }}
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: {{ printf "%s-crt" (include "keydb.replica.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: keydb
    app.kubernetes.io/component: replica
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  secretName: {{ template "keydb.tls.replica.secretName" . }}
  commonName: {{ printf "%s.%s.svc.%s" (include "keydb.replica.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 "keydb.replica.fullname" . }}'
    - '*.{{ include "keydb.replica.fullname" . }}.{{ include "common.names.namespace" . }}'
    - '*.{{ include "keydb.replica.fullname" . }}.{{ include "common.names.namespace" . }}.svc'
    - '*.{{ include "keydb.replica.fullname" . }}.{{ include "common.names.namespace" . }}.svc.{{ .Values.clusterDomain }}'
    - '*.{{ printf "%s-hl" (include "keydb.replica.fullname" .) }}'
    - '*.{{ printf "%s-hl" (include "keydb.replica.fullname" .) }}.{{ include "common.names.namespace" . }}'
    - '*.{{ printf "%s-hl" (include "keydb.replica.fullname" .) }}.{{ include "common.names.namespace" . }}.svc'
    - '*.{{ printf "%s-hl" (include "keydb.replica.fullname" .) }}.{{ include "common.names.namespace" . }}.svc.{{ .Values.clusterDomain }}'
  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 }}
