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

{{- if and .Values.https.enabled (not .Values.https.existingSecret) }}
{{- $secretName := printf "%s-http-certs-secret" (include "common.names.fullname" .) }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $secretName }}
  namespace: {{ include "common.names.namespace" . }}
  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: Opaque
data:
  {{- if .Values.https.autoGenerated }}
  {{- $ca := genCA "thanos-ca" 365 }}
  {{- $hostname := printf "%s" (include "common.names.fullname" .) }}
  {{- $cert := genSignedCert $hostname nil (list $hostname) 365 $ca }}
  {{ .Values.https.certFilename }}: {{ include "common.secrets.lookup" (dict "secret" $secretName "key" .Values.https.certFilename "defaultValue" $cert.Cert "context" $) }}
  {{ .Values.https.keyFilename }}: {{ include "common.secrets.lookup" (dict "secret" $secretName "key" .Values.https.keyFilename "defaultValue" $cert.Key "context" $) }}
  {{- if .Values.https.clientAuthType }}
  {{ .Values.https.caFilename }}: {{ include "common.secrets.lookup" (dict "secret" $secretName "key" .Values.https.caFilename "defaultValue" $ca.Cert "context" $) }}
  {{- end }}
  {{- else }}
  {{ .Values.https.certFilename }}: {{ required "'https.cert' is required when 'https.enabled=true'" .Values.https.cert | b64enc | quote }}
  {{ .Values.https.keyFilename }}: {{ required "'https.key' is required when 'https.enabled=true'" .Values.https.key | b64enc | quote }}
  {{- if .Values.https.clientAuthType }}
  {{ .Values.https.caFilename }}: {{ required "'https.ca' is required when 'https.clientAuthType' is provided" .Values.https.ca | b64enc | quote }}
  {{- end }}
  {{- end }}
{{- end }}
