{{- if (include "mongodb.createSecret" .) }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "mongodb.fullname" . }}
  namespace: {{ template "mongodb.namespace" . }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: mongodb
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: Opaque
data:
  mongodb-root-password: {{ include "common.secrets.passwords.manage" (dict "secret" (include "mongodb.fullname" .) "key" "mongodb-root-password" "providedValues" (list "auth.rootPassword" ) "context" $) }}
  {{- $customUsers := include "mongodb.customUsers" . -}}
  {{- $customDatabases := include "mongodb.customDatabases" . -}}
  {{- $customPasswords := include "mongodb.customPasswords" . -}}
  {{- $passwordList := list -}}
  {{- if and (not (empty $customUsers)) (not (empty $customDatabases)) -}}
  {{- if not (empty $customPasswords) -}}
  {{- $passwordList = $customPasswords -}}
  {{- else -}}
  {{- $customUsersList := splitList "," $customUsers -}}
  {{- $customPasswordsList := list -}}
  {{- range $customUsersList -}}
  {{- $customPasswordsList = append $customPasswordsList (randAlphaNum 10) -}}
  {{- end -}}
  {{- $passwordList = (join "," $customPasswordsList) -}}
  {{- end }}
  mongodb-passwords: {{ include "common.secrets.passwords.manage" (dict "secret" (include "mongodb.fullname" .) "key" "mongodb-passwords" "providedValues" (list "mongodbPasswords") "context" (set (deepCopy $) "Values" (dict "mongodbPasswords" $passwordList))) }}
  {{- end }}
  {{- if .Values.metrics.username }}
  mongodb-metrics-password: {{ include "common.secrets.passwords.manage" (dict "secret" (include "mongodb.fullname" .) "key" "mongodb-metrics-password" "providedValues" (list "metrics.password" ) "context" $) }}
  {{- end }}
  {{- if eq .Values.architecture "replicaset" }}
  mongodb-replica-set-key: {{ include "common.secrets.passwords.manage" (dict "secret" (include "mongodb.fullname" .) "key" "mongodb-replica-set-key" "providedValues" (list "auth.replicaSetKey" ) "context" $) }}
  {{- end }}
{{- end }}
