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

{{- if and .Values.tls.enabled .Values.tls.autoGenerated.enabled (eq .Values.tls.autoGenerated.engine "helm") -}}
{{- $ca := genCA "keydb-ca" 365 }}
{{- $releaseNamespace := include "common.names.namespace" . }}
{{- $clusterDomain := .Values.clusterDomain }}
{{- $masterFullname := include "keydb.master.fullname" . }}
{{- $masterHlSvcName := printf "%s-hl" (include "keydb.master.fullname" .) }}
{{- $masterAltNames := list (printf "*.%s.%s.svc.%s" $masterHlSvcName $releaseNamespace $clusterDomain) (printf "%s.%s.svc.%s" $masterHlSvcName $releaseNamespace $clusterDomain) $masterHlSvcName (printf "%s.%s.svc.%s" $masterFullname $releaseNamespace $clusterDomain) $masterFullname "127.0.0.1" "localhost" }}
{{- $masterCert := genSignedCert $masterFullname nil $masterAltNames 365 $ca }}
{{- $masterSecretName := include "keydb.tls.master.secretName" . }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $masterSecretName }}
  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: master
  {{- 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" $masterSecretName "key" "ca.crt" "defaultValue" $ca.Cert "context" $) }}
  tls.crt: {{ include "common.secrets.lookup" (dict "secret" $masterSecretName "key" "tls.crt" "defaultValue" $masterCert.Cert "context" $) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $masterSecretName "key" "tls.key" "defaultValue" $masterCert.Key "context" $) }}
---
{{- if eq .Values.architecture "replication" }}
{{- $replicaFullname := include "keydb.replica.fullname" . }}
{{- $replicaAltNames := list (printf "%s.%s.svc.%s" $replicaFullname $releaseNamespace $clusterDomain) $replicaFullname "127.0.0.1" "localhost" }}
{{- $replicaCert := genSignedCert $replicaFullname nil $replicaAltNames 365 $ca }}
{{- $replicaSecretName := include "keydb.tls.replica.secretName" . }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $replicaSecretName }}
  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 }}
type: kubernetes.io/tls
data:
  ca.crt: {{ include "common.secrets.lookup" (dict "secret" $replicaSecretName "key" "ca.crt" "defaultValue" $ca.Cert "context" $) }}
  tls.crt: {{ include "common.secrets.lookup" (dict "secret" $replicaSecretName "key" "tls.crt" "defaultValue" $replicaCert.Cert "context" $) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $replicaSecretName "key" "tls.key" "defaultValue" $replicaCert.Key "context" $) }}
{{- end }}
{{- else if and .Values.tls.enabled (not .Values.tls.autoGenerated.enabled) (empty .Values.tls.existingCASecret) (empty .Values.tls.master.existingSecret) (empty .Values.tls.replica.existingSecret) -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "keydb.tls.master.secretName" . }}
  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: master
  {{- 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.tls.ca entry required!" .Values.tls.ca | b64enc | quote }}
  tls.crt: {{ required "A valid .Values.tls.master.cert entry required!" .Values.tls.master.cert | b64enc | quote }}
  tls.key: {{ required "A valid .Values.tls.master.key entry required!" .Values.tls.master.key | b64enc | quote }}
---
{{- if eq .Values.architecture "replication" }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "keydb.tls.replica.secretName" . }}
  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 }}
type: kubernetes.io/tls
data:
  ca.crt: {{ required "A valid .Values.tls.ca entry required!" .Values.tls.ca | b64enc | quote }}
  tls.crt: {{ required "A valid .Values.tls.replica.cert entry required!" .Values.tls.replica.cert | b64enc | quote }}
  tls.key: {{ required "A valid .Values.tls.replica.key entry required!" .Values.tls.replica.key | b64enc | quote }}
{{- end }}
{{- end }}

