{{- /*
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 "cert-manager") }}
{{- if empty .Values.webdav.tls.autoGenerated.certManager.existingIssuer }}
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  name: {{ printf "%s-issuer" (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 }}
spec:
  selfSigned: {}
---
{{- end }}
apiVersion: cert-manager.io/v1
kind: Certificate
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 }}
spec:
  secretName: {{ printf "%s-crt" (include "seaweedfs.webdav.fullname" .) }}
  commonName: {{ printf "%s.%s.svc.%s" (include "seaweedfs.webdav.fullname" .) (include "common.names.namespace" .) .Values.clusterDomain }}
  issuerRef:
    name: {{ default (printf "%s-issuer" (include "seaweedfs.webdav.fullname" .)) .Values.webdav.tls.autoGenerated.certManager.existingIssuer }}
    kind: {{ default "Issuer" .Values.webdav.tls.autoGenerated.certManager.existingIssuerKind }}
  subject:
    organizations:
      - "SeaweedFS"
  dnsNames:
    - '*.{{ include "common.names.namespace" . }}'
    - '*.{{ include "common.names.namespace" . }}.svc'
    - '*.{{ include "common.names.namespace" . }}.svc.{{ .Values.clusterDomain }}'
    - '*.{{ include "seaweedfs.webdav.fullname" . }}'
    - '*.{{ include "seaweedfs.webdav.fullname" . }}.{{ include "common.names.namespace" . }}'
    - '*.{{ include "seaweedfs.webdav.fullname" . }}.{{ include "common.names.namespace" . }}.svc'
    - '*.{{ include "seaweedfs.webdav.fullname" . }}.{{ include "common.names.namespace" . }}.svc.{{ .Values.clusterDomain }}'
  privateKey:
    algorithm: {{ .Values.webdav.tls.autoGenerated.certManager.keyAlgorithm }}
    size: {{ int .Values.webdav.tls.autoGenerated.certManager.keySize }}
  duration: {{ .Values.webdav.tls.autoGenerated.certManager.duration }}
  renewBefore: {{ .Values.webdav.tls.autoGenerated.certManager.renewBefore }}
{{- end }}
