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

{{- if and .Values.security.enabled .Values.security.mTLS.enabled .Values.security.mTLS.autoGenerated.enabled (eq .Values.security.mTLS.autoGenerated.engine "helm") -}}
{{- $ca := genCA "seaweedfs-ca" 365 }}
{{- $releaseNamespace := include "common.names.namespace" . }}
{{- $clusterDomain := .Values.clusterDomain }}
{{- $secretAdminName := printf "%s-ca-crt" (include "common.names.fullname" .) }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $secretAdminName }}
  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:
  tls.crt: {{ include "common.secrets.lookup" (dict "secret" $secretAdminName "key" "tls.crt" "defaultValue" $ca.Cert "context" $) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $secretAdminName "key" "tls.key" "defaultValue" $ca.Key "context" $) }}
---
{{- $masterFullname := include "seaweedfs.master.fullname" . -}}
{{- $masterHeadlessSvcName := printf "%s-headless" (include "seaweedfs.master.fullname" .) -}}
{{- $masterAltNames := list (printf "*.%s.%s.svc.%s" $masterHeadlessSvcName $releaseNamespace $clusterDomain) (printf "%s.%s.svc.%s" $masterHeadlessSvcName $releaseNamespace $clusterDomain) $masterHeadlessSvcName (printf "%s.%s.svc.%s" $masterFullname $releaseNamespace $clusterDomain) $masterFullname "127.0.0.1" "localhost" }}
{{- $masterCert := genSignedCert $masterFullname nil $masterAltNames 365 $ca }}
{{- $masterSecretName := printf "%s-crt" (include "seaweedfs.master.fullname" .) -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $masterSecretName }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    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:
  tls.crt: {{ include "common.secrets.lookup" (dict "secret" $masterSecretName "key" "tls.crt" "defaultValue" $masterCert.Cert "context" $) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $masterSecretName "key" "tls.key" "defaultValue" $masterCert.Key "context" $) }}
---
{{- $volumeFullname := include "seaweedfs.volume.fullname" . -}}
{{- $volumeHeadlessSvcName := printf "%s-headless" (include "seaweedfs.volume.fullname" .) -}}
{{- $volumeAltNames := list (printf "*.%s.%s.svc.%s" $volumeHeadlessSvcName $releaseNamespace $clusterDomain) (printf "%s.%s.svc.%s" $volumeHeadlessSvcName $releaseNamespace $clusterDomain) $volumeHeadlessSvcName (printf "%s.%s.svc.%s" $volumeFullname $releaseNamespace $clusterDomain) $volumeFullname "127.0.0.1" "localhost" }}
{{- $volumeCert := genSignedCert $volumeFullname nil $volumeAltNames 365 $ca }}
{{- $volumeSecretName := printf "%s-crt" (include "seaweedfs.volume.fullname" .) -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $volumeSecretName }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: volume
  {{- 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" $volumeSecretName "key" "tls.crt" "defaultValue" $volumeCert.Cert "context" $) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $volumeSecretName "key" "tls.key" "defaultValue" $volumeCert.Key "context" $) }}
---
{{- if .Values.filer.enabled }}
{{- $filerFullname := include "seaweedfs.filer.fullname" . -}}
{{- $filerHeadlessSvcName := printf "%s-headless" (include "seaweedfs.filer.fullname" .) -}}
{{- $filerAltNames := list (printf "*.%s.%s.svc.%s" $filerHeadlessSvcName $releaseNamespace $clusterDomain) (printf "%s.%s.svc.%s" $filerHeadlessSvcName $releaseNamespace $clusterDomain) $filerHeadlessSvcName (printf "%s.%s.svc.%s" $filerFullname $releaseNamespace $clusterDomain) $filerFullname "127.0.0.1" "localhost" }}
{{- $filerCert := genSignedCert $filerFullname nil $filerAltNames 365 $ca }}
{{- $filerSecretName := printf "%s-crt" (include "seaweedfs.filer.fullname" .) -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $filerSecretName }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: filer
  {{- 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" $filerSecretName "key" "tls.crt" "defaultValue" $filerCert.Cert "context" $) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $filerSecretName "key" "tls.key" "defaultValue" $filerCert.Key "context" $) }}
---
{{- end }}
{{- $clientCert := genSignedCert "client" nil nil 365 $ca }}
{{- $clientSecretName := printf "%s-client-crt" (include "common.names.fullname" .) }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $clientSecretName }}
  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:
  tls.crt: {{ include "common.secrets.lookup" (dict "secret" $clientSecretName "key" "tls.crt" "defaultValue" $clientCert.Cert "context" $) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $clientSecretName "key" "tls.key" "defaultValue" $clientCert.Key "context" $) }}
{{- else if and .Values.security.enabled .Values.security.mTLS.enabled (not .Values.security.mTLS.autoGenerated.enabled) (empty .Values.security.mTLS.existingCASecret) (empty .Values.security.mTLS.master.existingSecret) (empty .Values.security.mTLS.volume.existingSecret) (empty .Values.security.mTLS.filer.existingSecret) (empty .Values.security.mTLS.client.existingSecret) -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-ca-crt" (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:
  tls.crt: {{ required "A valid .Values.security.mTLS.ca entry required!" .Values.security.mTLS.ca | b64enc | quote }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-crt" (include "seaweedfs.master.fullname" .) -}}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    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:
  tls.crt: {{ required "A valid .Values.security.mTLS.master.cert entry required!" .Values.security.mTLS.master.cert | b64enc | quote }}
  tls.key: {{ required "A valid .Values.security.mTLS.master.key entry required!" .Values.security.mTLS.master.key | b64enc | quote }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-crt" (include "seaweedfs.volume.fullname" .) -}}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: volume
  {{- 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.security.mTLS.volume.cert entry required!" .Values.security.mTLS.volume.cert | b64enc | quote }}
  tls.key: {{ required "A valid .Values.security.mTLS.volume.key entry required!" .Values.security.mTLS.volume.key | b64enc | quote }}
---
{{- if .Values.filer.enabled }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-crt" (include "seaweedfs.filer.fullname" .) -}}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: filer
  {{- 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.security.mTLS.filer.cert entry required!" .Values.security.mTLS.filer.cert | b64enc | quote }}
  tls.key: {{ required "A valid .Values.security.mTLS.filer.key entry required!" .Values.security.mTLS.filer.key | b64enc | quote }}
---
{{- end }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-client-crt" (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:
  tls.crt: {{ required "A valid .Values.security.mTLS.client.cert entry required!" .Values.security.mTLS.client.cert | b64enc | quote }}
  tls.key: {{ required "A valid .Values.security.mTLS.client.key entry required!" .Values.security.mTLS.client.key | b64enc | quote }}
{{- end }}

