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

{{/*
Return the proper ClickHouse Keeper fullname
*/}}
{{- define "clickhouse.keeper.fullname" -}}
{{- printf "%s-keeper" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Return the proper ClickHouse image name
*/}}
{{- define "clickhouse.image" -}}
{{ include "common.images.image" (dict "imageRoot" .Values.image "global" .Values.global) }}
{{- end -}}

{{/*
Return the proper ClickHouse Keeper image name
*/}}
{{- define "clickhouse.keeper.image" -}}
{{ include "common.images.image" (dict "imageRoot" .Values.keeper.image "global" .Values.global) }}
{{- end -}}

{{/*
Return the proper image name (for the init container volume-permissions image)
*/}}
{{- define "clickhouse.volumePermissions.image" -}}
{{- include "common.images.image" ( dict "imageRoot" .Values.defaultInitContainers.volumePermissions.image "global" .Values.global ) -}}
{{- end -}}

{{/*
Return the proper Docker Image Registry Secret Names
*/}}
{{- define "clickhouse.imagePullSecrets" -}}
{{- include "common.images.pullSecrets" (dict "images" (list .Values.image .Values.keeper.image .Values.defaultInitContainers.volumePermissions.image) "global" .Values.global) -}}
{{- end -}}

{{/*
Get the ClickHouse configuration ConfigMap.
*/}}
{{- define "clickhouse.configmapName" -}}
{{- if .Values.existingConfigmap -}}
    {{- tpl .Values.existingConfigmap . -}}
{{- else }}
    {{- include "common.names.fullname" . -}}
{{- end -}}
{{- end -}}

{{/*
Get the ClickHouse configd configuration ConfigMap.
*/}}
{{- define "clickhouse.configd.configmapName" -}}
{{- if .Values.existingConfigdConfigmap -}}
    {{- tpl .Values.existingConfigdConfigmap . -}}
{{- else }}
    {{- printf "%s-configd" (include "common.names.fullname" .) -}}
{{- end -}}
{{- end -}}

{{/*
Get the ClickHouse usersd configuration ConfigMap.
*/}}
{{- define "clickhouse.usersd.configmapName" -}}
{{- if .Values.existingUsersdConfigmap -}}
    {{- tpl .Values.existingUsersdConfigmap . -}}
{{- else }}
    {{- printf "%s-usersd" (include "common.names.fullname" .) -}}
{{- end -}}
{{- end -}}

{{/*
Get the ClickHouse password secret name
*/}}
{{- define "clickhouse.secretName" -}}
{{- if .Values.auth.existingSecret -}}
    {{- tpl .Values.auth.existingSecret . -}}
{{- else }}
    {{- include "common.names.fullname" . -}}
{{- end -}}
{{- end -}}

{{/*
Get the ClickHouse password key inside the secret
*/}}
{{- define "clickhouse.secretKey" -}}
{{- if .Values.auth.existingSecret -}}
    {{- tpl .Values.auth.existingSecretKey . -}}
{{- else }}
    {{- print "admin-password" -}}
{{- end -}}
{{- end -}}

{{/*
Return the name of the secret containing the CA TLS certificate
*/}}
{{- define "clickhouse.tls.ca.secretName" -}}
{{- if or .Values.tls.autoGenerated.enabled (and (not (empty .Values.tls.ca))) -}}
    {{- printf "%s-ca-crt" (include "common.names.fullname" .) -}}
{{- else -}}
    {{- required "An existing secret name must be provided with a CA cert for ClickHouse if cert is not provided!" (tpl .Values.tls.existingCASecret .) -}}
{{- end -}}
{{- end -}}

{{/*
Return the name of the secret containing the TLS certificates for ClickHouse servers
*/}}
{{- define "clickhouse.tls.server.secretName" -}}
{{- if or .Values.tls.autoGenerated.enabled (and (not (empty .Values.tls.server.cert)) (not (empty .Values.tls.server.key))) -}}
    {{- printf "%s-crt" (include "common.names.fullname" .) -}}
{{- else -}}
    {{- required "An existing secret name must be provided with TLS certs for ClickHouse servers if cert and key are not provided!" (tpl .Values.tls.server.existingSecret .) -}}
{{- end -}}
{{- end -}}

{{/*
Return the name of the secret containing the TLS certificates for ClickHouse Keeper
*/}}
{{- define "clickhouse.tls.keeper.secretName" -}}
{{- if or .Values.tls.autoGenerated.enabled (and (not (empty .Values.tls.keeper.cert)) (not (empty .Values.tls.keeper.key))) -}}
    {{- printf "%s-keeper-crt" (include "common.names.fullname" .) -}}
{{- else -}}
    {{- required "An existing secret name must be provided with TLS certs for ClickHouse Keeper if cert and key are not provided!" (tpl .Values.tls.keeper.existingSecret .) -}}
{{- end -}}
{{- end -}}

{{/*
Get the startialization scripts Secret name.
*/}}
{{- define "clickhouse.startdbScriptsSecret" -}}
{{- if .Values.startdbScriptsSecret -}}
    {{- printf "%s" (tpl .Values.startdbScriptsSecret $) -}}
{{- else -}}
    {{- printf "%s-start-scripts" (include "common.names.fullname" .) -}}
{{- end -}}
{{- end -}}

{{/*
Get the initialization scripts Secret name.
*/}}
{{- define "clickhouse.initdbScriptsSecret" -}}
{{- if .Values.initdbScriptsSecret -}}
    {{- printf "%s" (tpl .Values.initdbScriptsSecret $) -}}
{{- else -}}
    {{- printf "%s-init-scripts" (include "common.names.fullname" .) -}}
{{- end -}}
{{- end -}}

{{/*
Return the path to the CA cert file.
*/}}
{{- define "clickhouse.headlessServiceName" -}}
{{-  printf "%s-headless" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Get the ClickHouse Keeper configuration ConfigMap.
*/}}
{{- define "clickhouse.keeper.configmapName" -}}
{{- if .Values.keeper.existingConfigmap -}}
    {{- tpl .Values.keeper.existingConfigmap . -}}
{{- else }}
    {{- include "clickhouse.keeper.fullname" . -}}
{{- end -}}
{{- end -}}

{{/*
Get the ClickHouse Keeper configd configuration ConfigMap.
*/}}
{{- define "clickhouse.keeper.configd.configmapName" -}}
{{- if .Values.keeper.existingConfigdConfigmap -}}
    {{- tpl .Values.keeper.existingConfigdConfigmap . -}}
{{- else }}
    {{- printf "%s-configd" (include "clickhouse.keeper.fullname" .) -}}
{{- end -}}
{{- end -}}

{{/*
Get the ClickHouse Keeper usersd configuration ConfigMap.
*/}}
{{- define "clickhouse.keeper.usersd.configmapName" -}}
{{- if .Values.keeper.existingUsersdConfigmap -}}
    {{- tpl .Values.keeper.existingUsersdConfigmap . -}}
{{- else }}
    {{- printf "%s-usersd" (include "clickhouse.keeper.fullname" .) -}}
{{- end -}}
{{- end -}}

{{/*
Return the ClickHouse Keeper headless service name
*/}}
{{- define "clickhouse.keeper.headlessServiceName" -}}
{{-  printf "%s-headless" (include "clickhouse.keeper.fullname" .) | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define "clickhouse.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "common.names.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}

{{/*
Compile all warnings into a single message.
*/}}
{{- define "clickhouse.validateValues" -}}
{{- $messages := list -}}
{{- $messages := append $messages (include "clickhouse.validateValues.service.perReplicaAccess" .) -}}
{{- $messages := append $messages (include "clickhouse.validateValues.keeper" .) -}}
{{- $messages := append $messages (include "clickhouse.validateValues.keeper.replicaCount" .) -}}
{{- $messages := without $messages "" -}}
{{- $message := join "\n" $messages -}}

{{- if $message -}}
{{-   printf "\nVALUES VALIDATION:\n%s" $message -}}
{{- end -}}
{{- end -}}

{{/*
Validate values of ClickHouse - service per-replica access
*/}}
{{- define "clickhouse.validateValues.service.perReplicaAccess" -}}
{{- if and .Values.service.perReplicaAccess (eq .Values.service.type "ClusterIP") -}}
service.perReplicaAccess:
    The service type ClusterIP is not compatible with enabling per-replica access.
    Please set the service type to NodePort or LoadBalancer
    (--set service.type=NodePort) or disable per-replica access
    (--set service.perReplicaAccess=false)
{{- else if and .Values.service.perReplicaAccess .Values.service.loadBalancerAnnotations (ne (len .Values.service.loadBalancerAnnotations) (mul .Values.shards .Values.replicaCount)) }}
service.loadBalancerAnnotations:
    The number of loadBalancerAnnotations must be equal to the number of shards * replicas.
    Please set proper number of load balancer annotations or
    disable per-replica access (--set service.perReplicaAccess=false)
{{- else if and .Values.service.perReplicaAccess .Values.service.loadBalancerIPs (ne (len .Values.service.loadBalancerIPs) (mul .Values.shards .Values.replicaCount)) }}
service.loadBalancerIPs:
    The number of loadBalancerIPs must be equal to the number of shards * replicas.
    Please set proper number of load balancer IPs or
    disable per-replica access (--set service.perReplicaAccess=false)
{{- end -}}
{{- end -}}

{{/*
Validate values of ClickHouse - [Zoo]keeper
*/}}
{{- define "clickhouse.validateValues.keeper" -}}
{{- if and .Values.keeper.enabled .Values.externalZookeeper.servers -}}
clickhouse: Multiple [Zoo]keeper
    You can only use one [zoo]keeper cluster
    Please choose installing ClickHouse keeper (--set keeper.enabled=true) or
    using an external instances (--set zookeeper.servers )
{{- end -}}
{{- if and (not .Values.keeper.enabled) (not .Values.externalZookeeper.servers) (ne (int .Values.shards) 1) (ne (int .Values.replicaCount) 1) -}}
clickhouse: No [Zoo]keeper
    If you are deploying more than one ClickHouse instance, you need to enable [Zoo]keeper.
    Please choose installing ClickHouse keeper (--set keeper.enabled=true) or
    using an external instances (--set zookeeper.servers )
{{- end -}}
{{- end -}}

{{/*
Validate values of ClickHouse - Keeper replicas
*/}}
{{- define "clickhouse.validateValues.keeper.replicaCount" -}}
{{- $keeperReplicaCount := int .Values.keeper.replicaCount }}
{{- if and .Values.keeper.enabled .Values.keeper.persistence.enabled .Values.keeper.persistence.existingClaim (gt $keeperReplicaCount 1) -}}
keeper.replicaCount:
    A single existing PVC cannot be shared between multiple ClickHouse Keeper replicas.
    Please set a valid number of replicas (--set keeper.replicaCount=1), disable persistence
    (--set keeper.persistence.enabled=false) or rely on dynamic provisioning via Persistent
    Volume Claims (--set master.persistence.existingClaim="").
{{- end -}}
{{- end -}}
