{{- /*
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 "clickhouse-ca" 365 }}
{{- $releaseNamespace := include "common.names.namespace" . }}
{{- $clusterDomain := .Values.clusterDomain }}
{{- $caSecretName := include "clickhouse.tls.ca.secretName" . }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $caSecretName }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" . ) | nindent 4 }}
    app.kubernetes.io/part-of: clickhouse
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" . ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  tls.crt: {{ include "common.secrets.lookup" (dict "secret" $caSecretName "key" "tls.crt" "defaultValue" $ca.Cert "context" .) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $caSecretName "key" "tls.key" "defaultValue" $ca.Key "context" .) }}
---
{{- $serverFullname := include "common.names.fullname" . }}
{{- $serverHeadlessSvcName := include "clickhouse.headlessServiceName" . }}
{{- $serverAltNames := list (printf "*.%s.%s.svc.%s" $serverHeadlessSvcName $releaseNamespace $clusterDomain) (printf "%s.%s.svc.%s" $serverHeadlessSvcName $releaseNamespace $clusterDomain) $serverHeadlessSvcName (printf "%s.%s.svc.%s" $serverFullname $releaseNamespace $clusterDomain) $serverFullname "127.0.0.1" "localhost" }}
{{- $serverCert := genSignedCert $serverFullname nil $serverAltNames 365 $ca }}
{{- $serverSecretName := include "clickhouse.tls.server.secretName" . }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $serverSecretName }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" . ) | nindent 4 }}
    app.kubernetes.io/component: clickhouse
    app.kubernetes.io/part-of: clickhouse
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" . ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  tls.crt: {{ include "common.secrets.lookup" (dict "secret" $serverSecretName "key" "tls.crt" "defaultValue" $serverCert.Cert "context" .) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $serverSecretName "key" "tls.key" "defaultValue" $serverCert.Key "context" .) }}
{{- if .Values.keeper.enabled }}
---
{{- $keeperFullname := include "clickhouse.keeper.fullname" . }}
{{- $keeperHeadlessSvcName := include "clickhouse.keeper.headlessServiceName" . }}
{{- $keeperAltNames := list (printf "*.%s.%s.svc.%s" $keeperHeadlessSvcName $releaseNamespace $clusterDomain) (printf "%s.%s.svc.%s" $keeperHeadlessSvcName $releaseNamespace $clusterDomain) $keeperHeadlessSvcName (printf "%s.%s.svc.%s" $keeperFullname $releaseNamespace $clusterDomain) $keeperFullname "127.0.0.1" "localhost" }}
{{- $keeperCert := genSignedCert $keeperFullname nil $keeperAltNames 365 $ca }}
{{- $keeperSecretName := include "clickhouse.tls.keeper.secretName" . }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $keeperSecretName }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" . ) | nindent 4 }}
    app.kubernetes.io/component: keeper
    app.kubernetes.io/part-of: clickhouse
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" . ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  tls.crt: {{ include "common.secrets.lookup" (dict "secret" $keeperSecretName "key" "tls.crt" "defaultValue" $keeperCert.Cert "context" .) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $keeperSecretName "key" "tls.key" "defaultValue" $keeperCert.Key "context" .) }}
{{- end }}
{{- else if and .Values.tls.enabled (not .Values.tls.autoGenerated.enabled) (empty .Values.tls.existingCASecret) (empty .Values.tls.server.existingSecret) (empty .Values.tls.keeper.existingSecret) }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "clickhouse.tls.ca.secretName" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" . ) | nindent 4 }}
    app.kubernetes.io/part-of: clickhouse
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" . ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  tls.crt: {{ required "A valid .Values.tls.ca entry required!" .Values.tls.ca | b64enc | quote }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "clickhouse.tls.server.secretName" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" . ) | nindent 4 }}
    app.kubernetes.io/component: clickhouse
    app.kubernetes.io/part-of: clickhouse
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" . ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  tls.crt: {{ required "A valid .Values.tls.server.cert entry required!" .Values.tls.server.cert | b64enc | quote }}
  tls.key: {{ required "A valid .Values.tls.server.key entry required!" .Values.tls.server.key | b64enc | quote }}
{{- if .Values.keeper.enabled }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "clickhouse.tls.keeper.secretName" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" . ) | nindent 4 }}
    app.kubernetes.io/component: keeper
    app.kubernetes.io/part-of: clickhouse
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" . ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  tls.crt: {{ required "A valid .Values.tls.keeper.cert entry required!" .Values.tls.keeper.cert | b64enc | quote }}
  tls.key: {{ required "A valid .Values.tls.keeper.key entry required!" .Values.tls.keeper.key | b64enc | quote }}
{{- end }}
{{- end }}
