{{- if (include "elasticsearch.createTlsSecret" .) }}
{{- $ca := genCA "elasticsearch-ca" 365 }}
{{- $releaseNamespace := include "common.names.namespace" . }}
{{- $clusterDomain := .Values.clusterDomain }}

{{- if and (include "elasticsearch.master.enabled" .) (not .Values.security.tls.master.existingSecret) }}
{{- $fullname := include "elasticsearch.master.fullname" . }}
{{- $serviceName := include "elasticsearch.master.servicename" . }}
{{- $altNames := list (printf "*.%s.%s.svc.%s" $serviceName $releaseNamespace $clusterDomain) (printf "%s.%s.svc.%s" $serviceName $releaseNamespace $clusterDomain) $fullname "127.0.0.1" "localhost" }}
{{- if not (include "elasticsearch.coordinating.enabled" .) }}
{{- $altNames = append $altNames (include "elasticsearch.service.name" .) }}
{{- $altNames = append $altNames (printf "%s.%s.svc.%s" (include "elasticsearch.service.name" .) $releaseNamespace $clusterDomain) }}
{{- end }}
{{- $crt := genSignedCert $fullname nil $altNames 365 $ca }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-crt" (include "elasticsearch.master.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    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: {{ $ca.Cert | b64enc | quote }}
  tls.crt: {{ $crt.Cert | b64enc | quote }}
  tls.key: {{ $crt.Key | b64enc | quote }}
{{- end }}
{{- if and (include "elasticsearch.data.enabled" .) (not .Values.security.tls.data.existingSecret) }}
{{- $fullname := include "elasticsearch.data.fullname" . }}
{{- $serviceName := include "elasticsearch.data.servicename" . }}
{{- $altNames := list (printf "*.%s.%s.svc.%s" $serviceName $releaseNamespace $clusterDomain) (printf "%s.%s.svc.%s" $serviceName $releaseNamespace $clusterDomain) $fullname "127.0.0.1" "localhost" }}
{{- $crt := genSignedCert $fullname nil $altNames 365 $ca }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-crt" (include "elasticsearch.data.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    app.kubernetes.io/component: data
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  ca.crt: {{ $ca.Cert | b64enc | quote }}
  tls.crt: {{ $crt.Cert | b64enc | quote }}
  tls.key: {{ $crt.Key | b64enc | quote }}
{{- end }}
{{- if and (include "elasticsearch.coordinating.enabled" .) (not .Values.security.tls.coordinating.existingSecret) }}
{{- $fullname := include "elasticsearch.coordinating.fullname" . }}
{{- $serviceName := include "elasticsearch.coordinating.servicename" . }}
{{- $altNames := list (include "elasticsearch.service.name" .) (printf "%s.%s.svc.%s" (include "elasticsearch.service.name" .) $releaseNamespace $clusterDomain) (printf "*.%s.%s.svc.%s" $serviceName $releaseNamespace $clusterDomain) (printf "%s.%s.svc.%s" $serviceName $releaseNamespace $clusterDomain) $fullname "127.0.0.1" "localhost" }}
{{- $crt := genSignedCert $fullname nil $altNames 365 $ca }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-crt" (include "elasticsearch.coordinating.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    app.kubernetes.io/component: coordinating-only
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  ca.crt: {{ $ca.Cert | b64enc | quote }}
  tls.crt: {{ $crt.Cert | b64enc | quote }}
  tls.key: {{ $crt.Key | b64enc | quote }}
{{- end }}
{{- if and (include "elasticsearch.ingest.enabled" .) (not .Values.security.tls.ingest.existingSecret) }}
{{- $fullname := include "elasticsearch.ingest.fullname" . }}
{{- $serviceName := include "elasticsearch.ingest.servicename" . }}
{{- $altNames := list (printf "*.%s.%s.svc.%s" $serviceName $releaseNamespace $clusterDomain) (printf "%s.%s.svc.%s" $serviceName $releaseNamespace $clusterDomain) $fullname "127.0.0.1" "localhost" }}
{{- if .Values.ingest.service.enabled }}
{{- $altNames = append $altNames (include "elasticsearch.ingest.fullname" .) }}
{{- $altNames = append $altNames (printf "%s.%s.svc.%s" (include "elasticsearch.ingest.fullname" .) $releaseNamespace $clusterDomain) }}
{{- end }}
{{- $crt := genSignedCert $fullname nil $altNames 365 $ca }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-crt" (include "elasticsearch.ingest.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    app.kubernetes.io/component: ingest
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  ca.crt: {{ $ca.Cert | b64enc | quote }}
  tls.crt: {{ $crt.Cert | b64enc | quote }}
  tls.key: {{ $crt.Key | b64enc | quote }}
{{- end }}
{{- end }}
