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

{{- if and .Values.filer.enabled .Values.webdav.enabled .Values.webdav.tls.enabled .Values.webdav.tls.autoGenerated.enabled (eq .Values.webdav.tls.autoGenerated.engine "helm") -}}
{{- $ca := genCA "webdav-ca" 365 }}
{{- $releaseNamespace := include "common.names.namespace" . }}
{{- $clusterDomain := .Values.clusterDomain }}
{{- $fullname := include "seaweedfs.webdav.fullname" . -}}
{{- $altNames := list (printf "%s.%s.svc.%s" $fullname $releaseNamespace $clusterDomain) $fullname "127.0.0.1" "localhost" }}
{{- $cert := genSignedCert $fullname nil $altNames 365 $ca }}
{{- $secretName := printf "%s-crt" (include "seaweedfs.webdav.fullname" .) -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $secretName }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: webdav
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  tls.crt: {{ include "common.secrets.lookup" (dict "secret" $secretName "key" "tls.crt" "defaultValue" $cert.Cert "context" $) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $secretName "key" "tls.key" "defaultValue" $cert.Key "context" $) }}
{{- else if and .Values.filer.enabled .Values.webdav.enabled .Values.webdav.tls.enabled (not .Values.webdav.tls.autoGenerated.enabled) (empty .Values.webdav.tls.existingSecret) -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-crt" (include "seaweedfs.webdav.fullname" .) -}}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: webdav
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  tls.crt: {{ required "A valid .Values.webdav.tls.cert entry required!" .Values.webdav.tls.cert | b64enc | quote }}
  tls.key: {{ required "A valid .Values.webdav.tls.key entry required!" .Values.webdav.tls.key | b64enc | quote }}
{{- end }}

