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

{{- if and .Values.tls.enabled (not .Values.tls.existingSecret) }}
{{- $ca := genCA "nginx-ca" 365 }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-tls" (include "common.names.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  {{- if .Values.tls.autoGenerated }}
  {{- $cert := genSignedCert (include "common.names.fullname" .) nil (list (include "common.names.fullname" .) (printf "%s.%s" (include "common.names.fullname" .) (include "common.names.namespace" .)) (printf "%s.%s.svc" (include "common.names.fullname" .) (include "common.names.namespace" .)) (printf "%s.%s.svc.%s" (include "common.names.fullname" .) (include "common.names.namespace" .) (default .Values.clusterDomain .Values.global.clusterDomain))) 365 $ca }}
  {{ .Values.tls.certFilename }}: {{ include "common.secrets.lookup" (dict "secret" (printf "%s-tls" (include "common.names.fullname" .)) "key" .Values.tls.certFilename "defaultValue" $cert.Cert "context" $) }}
  {{ .Values.tls.certKeyFilename }}: {{ include "common.secrets.lookup" (dict "secret" (printf "%s-tls" (include "common.names.fullname" .)) "key" .Values.tls.certKeyFilename "defaultValue" $cert.Key "context" $) }}
  {{ .Values.tls.certCAFilename }}: {{ include "common.secrets.lookup" (dict "secret" (printf "%s-tls" (include "common.names.fullname" .)) "key" .Values.tls.certCAFilename "defaultValue" $ca.Cert "context" $) }}
  {{- else }}
  {{- if .Values.tls.cert }}
  {{ .Values.tls.certFilename }}: {{ .Values.tls.cert | b64enc }}
  {{- end }}
  {{- if .Values.tls.key }}
  {{ .Values.tls.certKeyFilename }}: {{ .Values.tls.key | b64enc }}
  {{- end }}
  {{- if .Values.tls.ca }}
  {{ .Values.tls.certCAFilename }}: {{ .Values.tls.ca | b64enc }}
  {{- end }}
  {{- end }}
{{- end }}
