{{- if and ( include "pomerium.redis.tlsCertsGenerated" . ) (or .Release.IsInstall .Values.redis.forceGenerateTLS) }}

{{- $ca := genCA "default-ca" 3650 }}
{{- $redisSN := list (printf "%s-redis-master.%s.svc.cluster.local" (include "pomerium.fullname" .) .Release.Namespace ) (printf "%s-redis-slave.%s.svc.cluster.local" (include "pomerium.fullname" .) .Release.Namespace ) }}
{{- $clientSN := include "pomerium.authenticate.fullname" . }}
{{- $server := genSignedCert "redis" nil $redisSN 3650 $ca }}

apiVersion: v1
kind: Secret
metadata:
  labels:
    app.kubernetes.io/name: {{ template "pomerium.name" . }}
    helm.sh/chart: {{ template "pomerium.chart" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
  annotations:
    helm.sh/hook-delete-policy: before-hook-creation
{{- if .Values.redis.forceGenerateTLS }}
    helm.sh/hook: pre-upgrade
{{- else if .Values.redis.generateTLS }}
    helm.sh/hook: pre-install
{{- end }}
{{- if .Values.config.generateTLSAnnotations }}
    {{- range $key, $value := .Values.config.generateTLSAnnotations }}
    {{ $key }}: {{ $value | quote }}
    {{- end }}
{{- end }}
  name: {{ template "pomerium.redis.tlsSecret.name" . }}
type: Opaque
data:
  ca.crt: {{ $ca.Cert | b64enc }}
  tls.crt: {{ $server.Cert | b64enc }}
  tls.key: {{ $server.Key | b64enc }}
{{- end -}}
