{{- 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:
  {{- if .Values.auth.rootPassword }}
  mongodb-root-password: {{ .Values.auth.rootPassword | toString | b64enc | quote }}
  {{- else }}
  mongodb-root-password: {{ randAlphaNum 10 | b64enc | quote }}
  {{- end }}
  {{- $customUsers := include "mongodb.customUsers" . -}}
  {{- $customDatabases := include "mongodb.customDatabases" . -}}
  {{- $customPasswords := include "mongodb.customPasswords" . -}}
  {{- if and (not (empty $customUsers)) (not (empty $customDatabases)) }}
  {{- if not (empty $customPasswords) }}
  mongodb-passwords: {{ $customPasswords | toString | b64enc | quote }}
  {{- else }}
  {{- $customUsersList := splitList "," $customUsers }}
  {{- $customPasswordsList := list }}
  {{- range $customUsersList }}
  {{- $customPasswordsList = append $customPasswordsList (randAlphaNum 10) }}
  {{- end }}
  mongodb-passwords: {{ (join "," $customPasswordsList) | b64enc | quote }}
  {{- end }}
  {{- end }}
  {{- if .Values.metrics.username }}
  {{- if .Values.metrics.password }}
  mongodb-metrics-password: {{ .Values.metrics.password | toString | b64enc | quote }}
  {{- else }}
  mongodb-metrics-password: {{ randAlphaNum 10 | b64enc | quote }}
  {{- end }}
  {{- end }}
  {{- if eq .Values.architecture "replicaset" }}
  {{- if .Values.auth.replicaSetKey }}
  mongodb-replica-set-key: {{ .Values.auth.replicaSetKey | toString | b64enc | quote }}
  {{- else }}
  mongodb-replica-set-key: {{ randAlphaNum 10 | b64enc | quote }}
  {{- end }}
  {{- end }}
{{- end }}
